File: display_manager_test_api.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (301 lines) | stat: -rw-r--r-- 10,918 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/display/test/display_manager_test_api.h"

#include <algorithm>
#include <cstdarg>
#include <iterator>
#include <vector>

#include "base/logging.h"
#include "base/strings/string_split.h"
#include "ui/display/display_layout_builder.h"
#include "ui/display/manager/display_manager.h"
#include "ui/display/manager/managed_display_info.h"
#include "ui/display/manager/util/display_manager_test_util.h"
#include "ui/display/screen.h"
#include "ui/display/test/display_test_util.h"
#include "ui/display/util/display_util.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/size.h"

namespace display {
namespace test {
namespace {

// Indicates the default maximum of displays that chrome device can support.
constexpr size_t kDefaultMaxSupportDisplayTest = 10;

DisplayInfoList CreateDisplayInfoListFromString(const std::string& specs,
                                                DisplayManager* display_manager,
                                                bool generate_new_ids) {
  Displays list = display_manager->IsInUnifiedMode()
                      ? display_manager->software_mirroring_display_list()
                      : display_manager->active_display_list();

  return CreateDisplayInfoListFromSpecs(specs, list, generate_new_ids);
}

// Gets the display |mode| for |resolution|. Returns false if no display
// mode matches the resolution, or the display is an internal display.
bool GetDisplayModeForResolution(const ManagedDisplayInfo& info,
                                 const gfx::Size& resolution,
                                 ManagedDisplayMode* mode) {
  if (IsInternalDisplayId(info.id()))
    return false;

  const ManagedDisplayInfo::ManagedDisplayModeList& modes =
      info.display_modes();
  DCHECK_NE(0u, modes.size());
  auto iter = std::ranges::find(modes, resolution, &ManagedDisplayMode::size);
  if (iter == modes.end()) {
    DLOG(WARNING) << "Unsupported resolution was requested:"
                  << resolution.ToString();
    return false;
  }
  *mode = *iter;
  return true;
}

}  // namespace

size_t DisplayManagerTestApi::maximum_support_display_ =
    kDefaultMaxSupportDisplayTest;

DisplayManagerTestApi::DisplayManagerTestApi(DisplayManager* display_manager)
    : display_manager_(display_manager) {
  DCHECK(display_manager);
}

DisplayManagerTestApi::~DisplayManagerTestApi() = default;

void DisplayManagerTestApi::ResetMaximumDisplay() {
  maximum_support_display_ = kDefaultMaxSupportDisplayTest;
}

int64_t DisplayManagerTestApi::AddDisplay(const DisplayParams& display_params) {
  const Displays& current_displays = display_manager_->active_display_list();
  if (current_displays.size() >= maximum_support_display_) {
    LOG(ERROR) << "Display limit exceeded.";
    return kInvalidDisplayId;
  }
  int64_t new_display_id = GetASynthesizedDisplayId();
  std::vector<ManagedDisplayInfo> current_display_infos;
  for (const Display& display : current_displays) {
    ManagedDisplayInfo display_info =
        GetInternalManagedDisplayInfo(display.id());
    gfx::Rect bounds = display_info.bounds_in_native();
    // Reset the bounds so that UpdateDisplayWithDisplayInfoList automatically
    // arranges them.
    bounds.set_origin(gfx::Point());
    display_info.SetBounds(bounds);
    current_display_infos.push_back(display_info);
  }
  ManagedDisplayInfo new_display;
  new_display.set_display_id(new_display_id);
  new_display.SetBounds(gfx::Rect(display_params.resolution));
  ManagedDisplayInfo::ManagedDisplayModeList display_modes;
  display_modes.emplace_back(display_params.resolution, /*refresh_rate=*/60,
                             /*is_interlaced=*/false, /*native=*/true,
                             /*device_scale_factor=*/1);
  new_display.SetManagedDisplayModes(display_modes);
  current_display_infos.push_back(new_display);
  UpdateDisplayWithDisplayInfoList(current_display_infos,
                                   /*from_native_platform=*/false);
  return new_display.id();
}

void DisplayManagerTestApi::RemoveDisplay(int64_t display_id) {
  const Displays& active_displays = display_manager_->active_display_list();
  std::vector<ManagedDisplayInfo> desired_display_infos;
  for (const Display& display : active_displays) {
    if (display.id() == display_id) {
      continue;
    }
    desired_display_infos.push_back(
        GetInternalManagedDisplayInfo(display.id()));
  }
  if (desired_display_infos.size() == active_displays.size()) {
    LOG(ERROR) << "Display with ID " << display_id << " not found.";
    return;
  }

  UpdateDisplayWithDisplayInfoList(desired_display_infos,
                                   /*from_native_platform=*/false);
}

void DisplayManagerTestApi::ResetDisplays() {
  display_manager_->InitDefaultDisplay();
}

void DisplayManagerTestApi::UpdateDisplay(const std::string& display_specs,
                                          bool from_native_platform,
                                          bool generate_new_ids) {
  DisplayInfoList display_info_list = CreateDisplayInfoListFromString(
      display_specs, display_manager_, generate_new_ids);
  UpdateDisplayWithDisplayInfoList(display_info_list, from_native_platform);
}

void DisplayManagerTestApi::UpdateDisplayWithDisplayInfoList(
    const std::vector<ManagedDisplayInfo>& display_info_list,
    bool from_native_platform) {
  std::vector<ManagedDisplayInfo> display_list_copy = display_info_list;
  if (display_list_copy.size() > maximum_support_display_) {
    display_manager_->configurator()->has_unassociated_display_ = true;
    while (display_list_copy.size() > maximum_support_display_) {
      display_list_copy.pop_back();
    }
  } else {
    display_manager_->configurator()->has_unassociated_display_ = false;
  }
  bool is_host_origin_set = false;
  for (const ManagedDisplayInfo& display_info : display_list_copy) {
    if (display_info.bounds_in_native().origin() != gfx::Point(0, 0)) {
      is_host_origin_set = true;
      break;
    }
  }

  // Start from (1,1) so that windows won't overlap with native mouse cursor.
  // See |AshTestBase::SetUp()|.
  int next_y = 1;
  for (auto& info : display_list_copy) {
    // On non-testing environment, when a secondary display is connected, a new
    // native (i.e. X) window for the display is always created below the
    // previous one for GPU performance reasons. Try to emulate the behavior
    // unless host origins are explicitly set.
    if (!is_host_origin_set) {
      gfx::Rect bounds(info.bounds_in_native().size());
      bounds.set_x(1);
      bounds.set_y(next_y);
      next_y += bounds.height();
      info.SetBounds(bounds);
    }
    info.set_from_native_platform(from_native_platform);

    // Overscan and native resolution are excluded for now as they require
    // special handing (has_overscan flag. resolution change makes sense
    // only on external).
    if (!from_native_platform) {
      display_manager_->RegisterDisplayProperty(
          info.id(), info.GetRotation(Display::RotationSource::USER),
          /*overscan_insets=*/nullptr,
          /*resolution_in_pixels=*/gfx::Size(), info.device_scale_factor(),
          info.zoom_factor(), info.zoom_factor_map(), info.refresh_rate(),
          info.is_interlaced(), info.variable_refresh_rate_state(),
          info.vsync_rate_min());
    }
  }

  bool tasks = display_manager_->OnNativeDisplaysChanged(display_list_copy);
  display_manager_->UpdateInternalManagedDisplayModeListForTest();
  if (tasks) {
    display_manager_->RunPendingTasksForTest();
  }
}

int64_t DisplayManagerTestApi::SetFirstDisplayAsInternalDisplay() {
  const Display& internal = display_manager_->active_display_list_[0];
  SetInternalDisplayIds({internal.id()});
  return Display::InternalDisplayId();
}

void DisplayManagerTestApi::SetInternalDisplayId(int64_t id) {
  SetInternalDisplayIds({id});
  display_manager_->UpdateInternalManagedDisplayModeListForTest();
}

void DisplayManagerTestApi::DisableChangeDisplayUponHostResize() {
  display_manager_->set_change_display_upon_host_resize(false);
}

const ManagedDisplayInfo& DisplayManagerTestApi::GetInternalManagedDisplayInfo(
    int64_t display_id) {
  return display_manager_->display_info_[display_id];
}

void DisplayManagerTestApi::SetTouchSupport(
    int64_t display_id,
    Display::TouchSupport touch_support) {
  display_manager_->FindDisplayForId(display_id)
      ->set_touch_support(touch_support);
}

const Display& DisplayManagerTestApi::GetSecondaryDisplay() const {
  CHECK_GE(display_manager_->GetNumDisplays(), 2U);

  const int64_t primary_display_id =
      Screen::GetScreen()->GetPrimaryDisplay().id();

  auto primary_display_iter = std::ranges::find(
      display_manager_->active_display_list_, primary_display_id, &Display::id);

  CHECK(primary_display_iter != display_manager_->active_display_list_.end());

  ++primary_display_iter;

  // If we've reach the end of |active_display_list_|, wrap back around to the
  // front.
  if (primary_display_iter == display_manager_->active_display_list_.end())
    return *display_manager_->active_display_list_.begin();

  return *primary_display_iter;
}

ScopedSetInternalDisplayId::ScopedSetInternalDisplayId(
    DisplayManager* display_manager,
    int64_t id) {
  DisplayManagerTestApi(display_manager).SetInternalDisplayId(id);
}

ScopedSetInternalDisplayId::~ScopedSetInternalDisplayId() {
  SetInternalDisplayIds({});
}

bool SetDisplayResolution(DisplayManager* display_manager,
                          int64_t display_id,
                          const gfx::Size& resolution) {
  const ManagedDisplayInfo& info = display_manager->GetDisplayInfo(display_id);
  ManagedDisplayMode mode;
  if (!GetDisplayModeForResolution(info, resolution, &mode))
    return false;
  return display_manager->SetDisplayMode(display_id, mode);
}

std::unique_ptr<DisplayLayout> CreateDisplayLayout(
    DisplayManager* display_manager,
    DisplayPlacement::Position position,
    int offset) {
  DisplayLayoutBuilder builder(Screen::GetScreen()->GetPrimaryDisplay().id());
  builder.SetSecondaryPlacement(
      DisplayManagerTestApi(display_manager).GetSecondaryDisplay().id(),
      position, offset);
  return builder.Build();
}

DisplayIdList CreateDisplayIdList2(int64_t id1, int64_t id2) {
  DisplayIdList list;
  list.push_back(id1);
  list.push_back(id2);
  SortDisplayIdList(&list);
  return list;
}

DisplayIdList CreateDisplayIdListN(int64_t start_id, size_t count) {
  DisplayIdList list;
  list.push_back(start_id);
  int64_t id = start_id;
  size_t N = count;
  while (count-- > 1) {
    id = display::SynthesizeDisplayIdFromSeed(id);
    list.push_back(id);
  }
  SortDisplayIdList(&list);
  DCHECK_EQ(N, list.size());
  return list;
}

}  // namespace test
}  // namespace display