| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "ash/test/display_manager_test_api.h" | |
| 6 | |
| 7 #include <cstdarg> | |
| 8 #include <vector> | |
| 9 | |
| 10 #include "ash/display/display_manager.h" | |
| 11 #include "base/command_line.h" | |
| 12 #include "base/strings/string_split.h" | |
| 13 #include "ui/display/display.h" | |
| 14 #include "ui/display/manager/display_layout_builder.h" | |
| 15 #include "ui/display/manager/display_manager_utilities.h" | |
| 16 #include "ui/display/manager/managed_display_info.h" | |
| 17 #include "ui/display/screen.h" | |
| 18 #include "ui/events/test/event_generator.h" | |
| 19 | |
| 20 namespace ash { | |
| 21 namespace test { | |
| 22 namespace { | |
| 23 | |
| 24 DisplayInfoList CreateDisplayInfoListFromString( | |
| 25 const std::string specs, | |
| 26 DisplayManager* display_manager) { | |
| 27 DisplayInfoList display_info_list; | |
| 28 std::vector<std::string> parts = base::SplitString( | |
| 29 specs, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | |
| 30 size_t index = 0; | |
| 31 | |
| 32 display::Displays list = | |
| 33 display_manager->IsInUnifiedMode() | |
| 34 ? display_manager->software_mirroring_display_list() | |
| 35 : display_manager->active_display_list(); | |
| 36 | |
| 37 for (std::vector<std::string>::const_iterator iter = parts.begin(); | |
| 38 iter != parts.end(); ++iter, ++index) { | |
| 39 int64_t id = (index < list.size()) ? list[index].id() | |
| 40 : display::Display::kInvalidDisplayID; | |
| 41 display_info_list.push_back( | |
| 42 display::ManagedDisplayInfo::CreateFromSpecWithID(*iter, id)); | |
| 43 } | |
| 44 return display_info_list; | |
| 45 } | |
| 46 | |
| 47 scoped_refptr<display::ManagedDisplayMode> GetDisplayModeForUIScale( | |
| 48 const display::ManagedDisplayInfo& info, | |
| 49 float ui_scale) { | |
| 50 const display::ManagedDisplayInfo::ManagedDisplayModeList& modes = | |
| 51 info.display_modes(); | |
| 52 auto iter = std::find_if( | |
| 53 modes.begin(), modes.end(), | |
| 54 [ui_scale](const scoped_refptr<display::ManagedDisplayMode>& mode) { | |
| 55 return mode->ui_scale() == ui_scale; | |
| 56 }); | |
| 57 if (iter == modes.end()) | |
| 58 return scoped_refptr<display::ManagedDisplayMode>(); | |
| 59 return *iter; | |
| 60 } | |
| 61 | |
| 62 } // namespace | |
| 63 | |
| 64 DisplayManagerTestApi::DisplayManagerTestApi(DisplayManager* display_manager) | |
| 65 : display_manager_(display_manager) {} | |
| 66 | |
| 67 DisplayManagerTestApi::~DisplayManagerTestApi() {} | |
| 68 | |
| 69 void DisplayManagerTestApi::UpdateDisplay(const std::string& display_specs) { | |
| 70 DisplayInfoList display_info_list = | |
| 71 CreateDisplayInfoListFromString(display_specs, display_manager_); | |
| 72 bool is_host_origin_set = false; | |
| 73 for (size_t i = 0; i < display_info_list.size(); ++i) { | |
| 74 const display::ManagedDisplayInfo& display_info = display_info_list[i]; | |
| 75 if (display_info.bounds_in_native().origin() != gfx::Point(0, 0)) { | |
| 76 is_host_origin_set = true; | |
| 77 break; | |
| 78 } | |
| 79 } | |
| 80 | |
| 81 // On non-testing environment, when a secondary display is connected, a new | |
| 82 // native (i.e. X) window for the display is always created below the | |
| 83 // previous one for GPU performance reasons. Try to emulate the behavior | |
| 84 // unless host origins are explicitly set. | |
| 85 if (!is_host_origin_set) { | |
| 86 // Start from (1,1) so that windows won't overlap with native mouse cursor. | |
| 87 // See |AshTestBase::SetUp()|. | |
| 88 int next_y = 1; | |
| 89 for (DisplayInfoList::iterator iter = display_info_list.begin(); | |
| 90 iter != display_info_list.end(); ++iter) { | |
| 91 gfx::Rect bounds(iter->bounds_in_native().size()); | |
| 92 bounds.set_x(1); | |
| 93 bounds.set_y(next_y); | |
| 94 next_y += bounds.height(); | |
| 95 iter->SetBounds(bounds); | |
| 96 } | |
| 97 } | |
| 98 | |
| 99 display_manager_->OnNativeDisplaysChanged(display_info_list); | |
| 100 display_manager_->UpdateInternalManagedDisplayModeListForTest(); | |
| 101 display_manager_->RunPendingTasksForTest(); | |
| 102 } | |
| 103 | |
| 104 int64_t DisplayManagerTestApi::SetFirstDisplayAsInternalDisplay() { | |
| 105 const display::Display& internal = display_manager_->active_display_list_[0]; | |
| 106 SetInternalDisplayId(internal.id()); | |
| 107 return display::Display::InternalDisplayId(); | |
| 108 } | |
| 109 | |
| 110 void DisplayManagerTestApi::SetInternalDisplayId(int64_t id) { | |
| 111 display::Display::SetInternalDisplayId(id); | |
| 112 display_manager_->UpdateInternalManagedDisplayModeListForTest(); | |
| 113 } | |
| 114 | |
| 115 void DisplayManagerTestApi::DisableChangeDisplayUponHostResize() { | |
| 116 display_manager_->set_change_display_upon_host_resize(false); | |
| 117 } | |
| 118 | |
| 119 void DisplayManagerTestApi::SetAvailableColorProfiles( | |
| 120 int64_t display_id, | |
| 121 const std::vector<ui::ColorCalibrationProfile>& profiles) { | |
| 122 display_manager_->display_info_[display_id].set_available_color_profiles( | |
| 123 profiles); | |
| 124 } | |
| 125 | |
| 126 const display::ManagedDisplayInfo& | |
| 127 DisplayManagerTestApi::GetInternalManagedDisplayInfo(int64_t display_id) { | |
| 128 return display_manager_->display_info_[display_id]; | |
| 129 } | |
| 130 | |
| 131 bool DisplayManagerTestApi::SetDisplayUIScale(int64_t id, float ui_scale) { | |
| 132 if (!display_manager_->IsActiveDisplayId(id) || | |
| 133 !display::Display::IsInternalDisplayId(id)) { | |
| 134 return false; | |
| 135 } | |
| 136 const display::ManagedDisplayInfo& info = | |
| 137 display_manager_->GetDisplayInfo(id); | |
| 138 | |
| 139 scoped_refptr<display::ManagedDisplayMode> mode = | |
| 140 GetDisplayModeForUIScale(info, ui_scale); | |
| 141 if (!mode) | |
| 142 return false; | |
| 143 return display_manager_->SetDisplayMode(id, mode); | |
| 144 } | |
| 145 | |
| 146 ScopedDisable125DSFForUIScaling::ScopedDisable125DSFForUIScaling() { | |
| 147 display::ManagedDisplayInfo::SetUse125DSFForUIScalingForTest(false); | |
| 148 } | |
| 149 | |
| 150 ScopedDisable125DSFForUIScaling::~ScopedDisable125DSFForUIScaling() { | |
| 151 display::ManagedDisplayInfo::SetUse125DSFForUIScalingForTest(true); | |
| 152 } | |
| 153 | |
| 154 ScopedSetInternalDisplayId::ScopedSetInternalDisplayId( | |
| 155 DisplayManager* display_manager, | |
| 156 int64_t id) { | |
| 157 DisplayManagerTestApi(display_manager).SetInternalDisplayId(id); | |
| 158 } | |
| 159 | |
| 160 ScopedSetInternalDisplayId::~ScopedSetInternalDisplayId() { | |
| 161 display::Display::SetInternalDisplayId(display::Display::kInvalidDisplayID); | |
| 162 } | |
| 163 | |
| 164 bool SetDisplayResolution(DisplayManager* display_manager, | |
| 165 int64_t display_id, | |
| 166 const gfx::Size& resolution) { | |
| 167 const display::ManagedDisplayInfo& info = | |
| 168 display_manager->GetDisplayInfo(display_id); | |
| 169 scoped_refptr<display::ManagedDisplayMode> mode = | |
| 170 GetDisplayModeForResolution(info, resolution); | |
| 171 if (!mode) | |
| 172 return false; | |
| 173 return display_manager->SetDisplayMode(display_id, mode); | |
| 174 } | |
| 175 | |
| 176 std::unique_ptr<display::DisplayLayout> CreateDisplayLayout( | |
| 177 DisplayManager* display_manager, | |
| 178 display::DisplayPlacement::Position position, | |
| 179 int offset) { | |
| 180 display::DisplayLayoutBuilder builder( | |
| 181 display::Screen::GetScreen()->GetPrimaryDisplay().id()); | |
| 182 builder.SetSecondaryPlacement(display_manager->GetSecondaryDisplay().id(), | |
| 183 position, offset); | |
| 184 return builder.Build(); | |
| 185 } | |
| 186 | |
| 187 display::DisplayIdList CreateDisplayIdList2(int64_t id1, int64_t id2) { | |
| 188 display::DisplayIdList list; | |
| 189 list.push_back(id1); | |
| 190 list.push_back(id2); | |
| 191 display::SortDisplayIdList(&list); | |
| 192 return list; | |
| 193 } | |
| 194 | |
| 195 display::DisplayIdList CreateDisplayIdListN(size_t count, ...) { | |
| 196 display::DisplayIdList list; | |
| 197 va_list args; | |
| 198 va_start(args, count); | |
| 199 for (size_t i = 0; i < count; i++) { | |
| 200 int64_t id = va_arg(args, int64_t); | |
| 201 list.push_back(id); | |
| 202 } | |
| 203 display::SortDisplayIdList(&list); | |
| 204 return list; | |
| 205 } | |
| 206 | |
| 207 } // namespace test | |
| 208 } // namespace ash | |
| OLD | NEW |