| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 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/mus/test/wm_test_helper.h" | |
| 6 | |
| 7 #include "ash/mus/screen_mus.h" | |
| 8 #include "ash/mus/window_manager.h" | |
| 9 #include "ash/mus/window_manager_application.h" | |
| 10 #include "ash/public/cpp/config.h" | |
| 11 #include "ash/shell.h" | |
| 12 #include "ash/test/test_shell_delegate.h" | |
| 13 #include "ash/wm_window.h" | |
| 14 #include "base/memory/ptr_util.h" | |
| 15 #include "base/message_loop/message_loop.h" | |
| 16 #include "base/strings/string_number_conversions.h" | |
| 17 #include "base/strings/string_split.h" | |
| 18 #include "base/test/sequenced_worker_pool_owner.h" | |
| 19 #include "services/ui/public/cpp/input_devices/input_device_client.h" | |
| 20 #include "services/ui/public/cpp/property_type_converters.h" | |
| 21 #include "ui/aura/mus/window_tree_client.h" | |
| 22 #include "ui/aura/test/env_test_helper.h" | |
| 23 #include "ui/aura/test/mus/window_tree_client_private.h" | |
| 24 #include "ui/aura/window.h" | |
| 25 #include "ui/base/material_design/material_design_controller.h" | |
| 26 #include "ui/base/test/material_design_controller_test_api.h" | |
| 27 #include "ui/display/display.h" | |
| 28 #include "ui/display/display_list.h" | |
| 29 #include "ui/display/screen_base.h" | |
| 30 #include "ui/display/test/display_manager_test_api.h" | |
| 31 #include "ui/views/test/test_views_delegate.h" | |
| 32 | |
| 33 namespace ash { | |
| 34 namespace mus { | |
| 35 namespace { | |
| 36 | |
| 37 bool CompareByDisplayId(RootWindowController* root1, | |
| 38 RootWindowController* root2) { | |
| 39 return root1->GetWindow()->GetDisplayNearestWindow().id() < | |
| 40 root2->GetWindow()->GetDisplayNearestWindow().id(); | |
| 41 } | |
| 42 | |
| 43 // TODO(sky): at some point this needs to support everything in DisplayInfo, | |
| 44 // for now just the bare minimum, which is [x+y-]wxh. | |
| 45 gfx::Rect ParseDisplayBounds(const std::string& spec) { | |
| 46 gfx::Rect bounds; | |
| 47 const std::vector<std::string> parts = | |
| 48 base::SplitString(spec, "-", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | |
| 49 std::string size_spec; | |
| 50 if (parts.size() == 2u) { | |
| 51 size_spec = parts[1]; | |
| 52 const std::vector<std::string> origin_parts = base::SplitString( | |
| 53 parts[0], "+", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | |
| 54 CHECK_EQ(2u, origin_parts.size()); | |
| 55 int x, y; | |
| 56 CHECK(base::StringToInt(origin_parts[0], &x)); | |
| 57 CHECK(base::StringToInt(origin_parts[1], &y)); | |
| 58 bounds.set_origin(gfx::Point(x, y)); | |
| 59 } else { | |
| 60 CHECK_EQ(1u, parts.size()); | |
| 61 size_spec = spec; | |
| 62 } | |
| 63 const std::vector<std::string> size_parts = base::SplitString( | |
| 64 size_spec, "x", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | |
| 65 CHECK_EQ(2u, size_parts.size()); | |
| 66 int w = 0, h = 0; | |
| 67 CHECK(base::StringToInt(size_parts[0], &w)); | |
| 68 CHECK(base::StringToInt(size_parts[1], &h)); | |
| 69 bounds.set_size(gfx::Size(w, h)); | |
| 70 return bounds; | |
| 71 } | |
| 72 | |
| 73 } // namespace | |
| 74 | |
| 75 WmTestHelper::WmTestHelper() {} | |
| 76 | |
| 77 WmTestHelper::~WmTestHelper() { | |
| 78 // Flush the message loop so that any pending tasks are run. This ensures | |
| 79 // any delayed tasks, such as deleting RootWindowControllers, are processed | |
| 80 // before continuing. | |
| 81 base::RunLoop().RunUntilIdle(); | |
| 82 | |
| 83 // Needs to be destroyed before material design. | |
| 84 window_manager_app_.reset(); | |
| 85 | |
| 86 base::RunLoop().RunUntilIdle(); | |
| 87 blocking_pool_owner_.reset(); | |
| 88 base::RunLoop().RunUntilIdle(); | |
| 89 | |
| 90 ui::test::MaterialDesignControllerTestAPI::Uninitialize(); | |
| 91 | |
| 92 input_device_client_.reset(); | |
| 93 } | |
| 94 | |
| 95 void WmTestHelper::Init() { | |
| 96 const Config config = base::CommandLine::ForCurrentProcess()->HasSwitch("mus") | |
| 97 ? Config::MUS | |
| 98 : Config::MASH; | |
| 99 if (config == Config::MUS) | |
| 100 input_device_client_ = base::MakeUnique<ui::InputDeviceClient>(); | |
| 101 | |
| 102 // MaterialDesignController may have already been initialized. To cover that | |
| 103 // case explicitly uninitialize before initializing. | |
| 104 ui::test::MaterialDesignControllerTestAPI::Uninitialize(); | |
| 105 ui::MaterialDesignController::Initialize(); | |
| 106 | |
| 107 views_delegate_ = base::MakeUnique<views::TestViewsDelegate>(); | |
| 108 | |
| 109 const bool show_primary_host_on_connect = false; | |
| 110 window_manager_app_ = base::MakeUnique<WindowManagerApplication>( | |
| 111 show_primary_host_on_connect, config); | |
| 112 | |
| 113 message_loop_.reset(new base::MessageLoopForUI()); | |
| 114 | |
| 115 const size_t kMaxNumberThreads = 3u; // Matches that of content. | |
| 116 const char kThreadNamePrefix[] = "MashBlockingForTesting"; | |
| 117 blocking_pool_owner_ = base::MakeUnique<base::SequencedWorkerPoolOwner>( | |
| 118 kMaxNumberThreads, kThreadNamePrefix); | |
| 119 | |
| 120 window_manager_app_->window_manager_ = base::MakeUnique<WindowManager>( | |
| 121 nullptr, config, show_primary_host_on_connect); | |
| 122 window_manager_app_->window_manager()->shell_delegate_ = | |
| 123 base::MakeUnique<test::TestShellDelegate>(); | |
| 124 | |
| 125 window_tree_client_setup_.InitForWindowManager( | |
| 126 window_manager_app_->window_manager_.get(), | |
| 127 window_manager_app_->window_manager_.get()); | |
| 128 aura::test::EnvTestHelper().SetWindowTreeClient( | |
| 129 window_tree_client_setup_.window_tree_client()); | |
| 130 // See comment in AshTestHelper for details on why NetworkHandler is not | |
| 131 // initialized. | |
| 132 const bool init_network_handler = false; | |
| 133 window_manager_app_->InitWindowManager( | |
| 134 window_tree_client_setup_.OwnWindowTreeClient(), | |
| 135 blocking_pool_owner_->pool(), init_network_handler); | |
| 136 | |
| 137 aura::WindowTreeClient* window_tree_client = | |
| 138 window_manager_app_->window_manager()->window_tree_client(); | |
| 139 window_tree_client_private_ = | |
| 140 base::MakeUnique<aura::WindowTreeClientPrivate>(window_tree_client); | |
| 141 if (config == Config::MUS) { | |
| 142 window_tree_client_private_->CallOnConnect(); | |
| 143 } else { | |
| 144 int next_x = 0; | |
| 145 CreateRootWindowController("800x600", &next_x); | |
| 146 } | |
| 147 } | |
| 148 | |
| 149 std::vector<RootWindowController*> WmTestHelper::GetRootsOrderedByDisplayId() { | |
| 150 std::set<RootWindowController*> roots = | |
| 151 window_manager_app_->window_manager()->GetRootWindowControllers(); | |
| 152 std::vector<RootWindowController*> ordered_roots; | |
| 153 ordered_roots.insert(ordered_roots.begin(), roots.begin(), roots.end()); | |
| 154 std::sort(ordered_roots.begin(), ordered_roots.end(), &CompareByDisplayId); | |
| 155 return ordered_roots; | |
| 156 } | |
| 157 | |
| 158 void WmTestHelper::UpdateDisplay(const std::string& display_spec) { | |
| 159 if (Shell::GetAshConfig() == Config::MUS) { | |
| 160 display::test::DisplayManagerTestApi(Shell::Get()->display_manager()) | |
| 161 .UpdateDisplay(display_spec); | |
| 162 return; | |
| 163 } | |
| 164 const std::vector<std::string> parts = base::SplitString( | |
| 165 display_spec, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | |
| 166 std::vector<RootWindowController*> root_window_controllers = | |
| 167 GetRootsOrderedByDisplayId(); | |
| 168 int next_x = 0; | |
| 169 for (size_t i = 0, | |
| 170 end = std::min(parts.size(), root_window_controllers.size()); | |
| 171 i < end; ++i) { | |
| 172 UpdateDisplay(root_window_controllers[i], parts[i], &next_x); | |
| 173 } | |
| 174 for (size_t i = root_window_controllers.size(); i < parts.size(); ++i) { | |
| 175 root_window_controllers.push_back( | |
| 176 CreateRootWindowController(parts[i], &next_x)); | |
| 177 } | |
| 178 const bool in_shutdown = false; | |
| 179 while (root_window_controllers.size() > parts.size()) { | |
| 180 window_manager_app_->window_manager()->DestroyRootWindowController( | |
| 181 root_window_controllers.back(), in_shutdown); | |
| 182 root_window_controllers.pop_back(); | |
| 183 } | |
| 184 } | |
| 185 | |
| 186 RootWindowController* WmTestHelper::CreateRootWindowController( | |
| 187 const std::string& display_spec, | |
| 188 int* next_x) { | |
| 189 gfx::Rect bounds = ParseDisplayBounds(display_spec); | |
| 190 bounds.set_x(*next_x); | |
| 191 *next_x += bounds.size().width(); | |
| 192 display::Display display(next_display_id_++, bounds); | |
| 193 gfx::Rect work_area(display.bounds()); | |
| 194 // Offset the height slightly to give a different work area. -20 is arbitrary, | |
| 195 // it could be anything. | |
| 196 work_area.set_height(std::max(0, work_area.height() - 20)); | |
| 197 display.set_work_area(work_area); | |
| 198 window_tree_client_private_->CallWmNewDisplayAdded(display); | |
| 199 return GetRootsOrderedByDisplayId().back(); | |
| 200 } | |
| 201 | |
| 202 void WmTestHelper::UpdateDisplay(RootWindowController* root_window_controller, | |
| 203 const std::string& display_spec, | |
| 204 int* next_x) { | |
| 205 gfx::Rect bounds = ParseDisplayBounds(display_spec); | |
| 206 bounds.set_x(*next_x); | |
| 207 *next_x += bounds.size().width(); | |
| 208 display::Display updated_display = | |
| 209 root_window_controller->GetWindow()->GetDisplayNearestWindow(); | |
| 210 gfx::Insets work_area_insets = updated_display.GetWorkAreaInsets(); | |
| 211 updated_display.set_bounds(bounds); | |
| 212 updated_display.UpdateWorkAreaFromInsets(work_area_insets); | |
| 213 root_window_controller->GetWindow()->SetBounds(gfx::Rect(bounds.size())); | |
| 214 ScreenMus* screen = window_manager_app_->window_manager()->screen_.get(); | |
| 215 const bool is_primary = | |
| 216 screen->display_list().FindDisplayById(updated_display.id()) == | |
| 217 screen->display_list().GetPrimaryDisplayIterator(); | |
| 218 screen->display_list().UpdateDisplay( | |
| 219 updated_display, is_primary ? display::DisplayList::Type::PRIMARY | |
| 220 : display::DisplayList::Type::NOT_PRIMARY); | |
| 221 } | |
| 222 | |
| 223 } // namespace mus | |
| 224 } // namespace ash | |
| OLD | NEW |