Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(547)

Side by Side Diff: ash/mus/test/wm_test_helper.cc

Issue 2322613002: Adds support for multiple displays to WmTestBase (Closed)
Patch Set: feedback Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ash/mus/test/wm_test_helper.h" 5 #include "ash/mus/test/wm_test_helper.h"
6 6
7 #include "ash/common/material_design/material_design_controller.h" 7 #include "ash/common/material_design/material_design_controller.h"
8 #include "ash/common/test/material_design_controller_test_api.h" 8 #include "ash/common/test/material_design_controller_test_api.h"
9 #include "ash/mus/root_window_controller.h" 9 #include "ash/mus/root_window_controller.h"
10 #include "ash/mus/test/wm_test_screen.h"
11 #include "ash/mus/window_manager.h" 10 #include "ash/mus/window_manager.h"
12 #include "ash/mus/window_manager_application.h" 11 #include "ash/mus/window_manager_application.h"
13 #include "base/memory/ptr_util.h" 12 #include "base/memory/ptr_util.h"
14 #include "base/message_loop/message_loop.h" 13 #include "base/message_loop/message_loop.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/string_split.h"
15 #include "base/test/sequenced_worker_pool_owner.h" 16 #include "base/test/sequenced_worker_pool_owner.h"
16 #include "services/ui/public/cpp/property_type_converters.h" 17 #include "services/ui/public/cpp/property_type_converters.h"
17 #include "services/ui/public/cpp/tests/window_tree_client_private.h" 18 #include "services/ui/public/cpp/tests/window_tree_client_private.h"
18 #include "services/ui/public/cpp/window_tree_client.h" 19 #include "services/ui/public/cpp/window_tree_client.h"
19 #include "ui/base/material_design/material_design_controller.h" 20 #include "ui/base/material_design/material_design_controller.h"
20 #include "ui/base/test/material_design_controller_test_api.h" 21 #include "ui/base/test/material_design_controller_test_api.h"
21 #include "ui/display/display.h" 22 #include "ui/display/display.h"
23 #include "ui/display/display_list.h"
24 #include "ui/display/screen_base.h"
22 25
23 namespace ash { 26 namespace ash {
24 namespace mus { 27 namespace mus {
28 namespace {
29
30 bool CompareByDisplayId(const RootWindowController* root1,
31 const RootWindowController* root2) {
32 return root1->display().id() < root2->display().id();
33 }
34
35 // TODO(sky): at some point this needs to support everything in DisplayInfo,
36 // for now just the bare minimum, which is [x+y-]wxh.
37 gfx::Rect ParseDisplayBounds(const std::string& spec) {
38 gfx::Rect bounds;
39 const std::vector<std::string> parts =
40 base::SplitString(spec, "-", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
41 std::string size_spec;
42 if (parts.size() == 2u) {
43 size_spec = parts[1];
44 const std::vector<std::string> origin_parts = base::SplitString(
45 parts[0], "+", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
46 CHECK_EQ(2u, origin_parts.size());
47 int x, y;
48 CHECK(base::StringToInt(origin_parts[0], &x));
49 CHECK(base::StringToInt(origin_parts[1], &y));
50 bounds.set_origin(gfx::Point(x, y));
51 } else {
52 CHECK_EQ(1u, parts.size());
53 size_spec = spec;
54 }
55 const std::vector<std::string> size_parts = base::SplitString(
56 size_spec, "x", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
57 CHECK_EQ(2u, size_parts.size());
58 int w = 0, h = 0;
59 CHECK(base::StringToInt(size_parts[0], &w));
60 CHECK(base::StringToInt(size_parts[1], &h));
61 bounds.set_size(gfx::Size(w, h));
62 return bounds;
63 }
64
65 } // namespace
25 66
26 WmTestHelper::WmTestHelper() {} 67 WmTestHelper::WmTestHelper() {}
27 68
28 WmTestHelper::~WmTestHelper() { 69 WmTestHelper::~WmTestHelper() {
29 // Needs to be destroyed before material design. 70 // Needs to be destroyed before material design.
30 window_manager_app_.reset(); 71 window_manager_app_.reset();
31 72
32 base::RunLoop().RunUntilIdle(); 73 base::RunLoop().RunUntilIdle();
33 blocking_pool_owner_.reset(); 74 blocking_pool_owner_.reset();
34 base::RunLoop().RunUntilIdle(); 75 base::RunLoop().RunUntilIdle();
35 76
36 ash::test::MaterialDesignControllerTestAPI::Uninitialize(); 77 ash::test::MaterialDesignControllerTestAPI::Uninitialize();
37 ui::test::MaterialDesignControllerTestAPI::Uninitialize(); 78 ui::test::MaterialDesignControllerTestAPI::Uninitialize();
38 } 79 }
39 80
40 void WmTestHelper::Init() { 81 void WmTestHelper::Init() {
41 ui::MaterialDesignController::Initialize(); 82 ui::MaterialDesignController::Initialize();
42 ash::MaterialDesignController::Initialize(); 83 ash::MaterialDesignController::Initialize();
43 84
44 window_manager_app_ = base::MakeUnique<WindowManagerApplication>(); 85 window_manager_app_ = base::MakeUnique<WindowManagerApplication>();
45 86
46 message_loop_.reset(new base::MessageLoopForUI()); 87 message_loop_.reset(new base::MessageLoopForUI());
47 88
48 const size_t kMaxNumberThreads = 3u; // Matches that of content. 89 const size_t kMaxNumberThreads = 3u; // Matches that of content.
49 const char kThreadNamePrefix[] = "MashBlockingForTesting"; 90 const char kThreadNamePrefix[] = "MashBlockingForTesting";
50 blocking_pool_owner_ = base::MakeUnique<base::SequencedWorkerPoolOwner>( 91 blocking_pool_owner_ = base::MakeUnique<base::SequencedWorkerPoolOwner>(
51 kMaxNumberThreads, kThreadNamePrefix); 92 kMaxNumberThreads, kThreadNamePrefix);
52 93
53 window_manager_app_->window_manager_.reset(new WindowManager(nullptr)); 94 window_manager_app_->window_manager_.reset(new WindowManager(nullptr));
54 screen_ = new WmTestScreen;
55 window_manager_app_->window_manager_->screen_.reset(screen_);
56 95
57 // Need an id other than kInvalidDisplayID so the Display is considered valid.
58 display::Display display(1);
59 const gfx::Rect display_bounds(0, 0, 800, 600);
60 display.set_bounds(display_bounds);
61 // Offset the height slightly to give a different work area. -20 is arbitrary,
62 // it could be anything.
63 const gfx::Rect work_area(0, 0, display_bounds.width(),
64 display_bounds.height() - 20);
65 display.set_work_area(work_area);
66 window_tree_client_setup_.InitForWindowManager( 96 window_tree_client_setup_.InitForWindowManager(
67 window_manager_app_->window_manager_.get(), 97 window_manager_app_->window_manager_.get(),
68 window_manager_app_->window_manager_.get(), display); 98 window_manager_app_->window_manager_.get());
69
70 window_manager_app_->InitWindowManager( 99 window_manager_app_->InitWindowManager(
71 window_tree_client_setup_.OwnWindowTreeClient(), 100 window_tree_client_setup_.OwnWindowTreeClient(),
72 blocking_pool_owner_->pool()); 101 blocking_pool_owner_->pool());
102
73 ui::WindowTreeClient* window_tree_client = 103 ui::WindowTreeClient* window_tree_client =
74 window_manager_app_->window_manager()->window_tree_client(); 104 window_manager_app_->window_manager()->window_tree_client();
105 window_tree_client_private_ =
106 base::MakeUnique<ui::WindowTreeClientPrivate>(window_tree_client);
107 CreateRootWindowController("800x600");
108 }
75 109
76 screen_->display_list()->AddDisplay(display, 110 std::vector<RootWindowController*> WmTestHelper::GetRootsOrderedByDisplayId() {
77 display::DisplayList::Type::PRIMARY); 111 std::set<RootWindowController*> roots =
112 window_manager_app_->window_manager()->GetRootWindowControllers();
113 std::vector<RootWindowController*> ordered_roots;
114 ordered_roots.insert(ordered_roots.begin(), roots.begin(), roots.end());
115 std::sort(ordered_roots.begin(), ordered_roots.end(), &CompareByDisplayId);
116 return ordered_roots;
117 }
78 118
79 ui::WindowTreeClientPrivate(window_tree_client) 119 void WmTestHelper::UpdateDisplay(const std::string& display_spec) {
80 .CallWmNewDisplayAdded(display); 120 const std::vector<std::string> parts = base::SplitString(
121 display_spec, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
122 std::vector<RootWindowController*> root_window_controllers =
123 GetRootsOrderedByDisplayId();
124 for (size_t i = 0,
125 end = std::min(parts.size(), root_window_controllers.size());
126 i < end; ++i) {
127 UpdateDisplay(root_window_controllers[i], parts[i]);
128 }
129 for (size_t i = root_window_controllers.size(); i < parts.size(); ++i)
130 root_window_controllers.push_back(CreateRootWindowController(parts[i]));
131 while (root_window_controllers.size() > parts.size()) {
132 window_manager_app_->window_manager()->DestroyRootWindowController(
133 root_window_controllers.back());
134 root_window_controllers.pop_back();
135 }
136 }
137
138 RootWindowController* WmTestHelper::CreateRootWindowController(
139 const std::string& display_spec) {
140 display::Display display(next_display_id_++,
141 ParseDisplayBounds(display_spec));
142 gfx::Rect work_area(display.bounds());
143 // Offset the height slightly to give a different work area. -20 is arbitrary,
144 // it could be anything.
145 work_area.set_height(std::max(0, work_area.height() - 20));
146 display.set_work_area(work_area);
147 window_tree_client_private_->CallWmNewDisplayAdded(display);
148 return GetRootsOrderedByDisplayId().back();
149 }
150
151 void WmTestHelper::UpdateDisplay(RootWindowController* root_window_controller,
152 const std::string& display_spec) {
153 gfx::Rect bounds = ParseDisplayBounds(display_spec);
154 root_window_controller->display_.set_bounds(bounds);
155 gfx::Rect work_area(bounds);
156 // Offset the height slightly to give a different work area. -2 is arbitrary,
msw 2016/09/12 23:19:48 nit: -2 versus -20 above.
sky 2016/09/13 00:10:26 Done.
157 // it could be anything.
158 work_area.set_height(std::max(0, work_area.height() - 2));
159 root_window_controller->display_.set_work_area(work_area);
160 root_window_controller->root()->SetBounds(gfx::Rect(bounds.size()));
161 display::ScreenBase* screen =
162 window_manager_app_->window_manager()->screen_.get();
163 const bool is_primary = screen->display_list()->FindDisplayById(
164 root_window_controller->display().id()) ==
165 screen->display_list()->GetPrimaryDisplayIterator();
166 screen->display_list()->UpdateDisplay(
167 root_window_controller->display(),
168 is_primary ? display::DisplayList::Type::PRIMARY
169 : display::DisplayList::Type::NOT_PRIMARY);
81 } 170 }
82 171
83 } // namespace mus 172 } // namespace mus
84 } // namespace ash 173 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698