| OLD | NEW |
| 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_base.h" | 5 #include "ash/mus/test/wm_test_base.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "ash/mus/root_window_controller.h" | 10 #include "ash/mus/root_window_controller.h" |
| 11 #include "ash/mus/test/wm_test_helper.h" | 11 #include "ash/mus/test/wm_test_helper.h" |
| 12 #include "ash/mus/test/wm_test_screen.h" | |
| 13 #include "ash/mus/window_manager.h" | 12 #include "ash/mus/window_manager.h" |
| 14 #include "ash/mus/window_manager_application.h" | 13 #include "ash/mus/window_manager_application.h" |
| 15 #include "base/strings/string_number_conversions.h" | |
| 16 #include "base/strings/string_split.h" | |
| 17 #include "services/ui/public/cpp/property_type_converters.h" | 14 #include "services/ui/public/cpp/property_type_converters.h" |
| 18 #include "services/ui/public/cpp/window_tree_client.h" | 15 #include "services/ui/public/cpp/window_tree_client.h" |
| 19 #include "ui/display/display.h" | 16 #include "ui/display/display.h" |
| 20 | 17 |
| 21 namespace ash { | 18 namespace ash { |
| 22 namespace mus { | 19 namespace mus { |
| 23 namespace { | 20 namespace { |
| 24 | 21 |
| 25 ui::mojom::WindowType MusWindowTypeFromWmWindowType( | 22 ui::mojom::WindowType MusWindowTypeFromWmWindowType( |
| 26 ui::wm::WindowType wm_window_type) { | 23 ui::wm::WindowType wm_window_type) { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 44 return ui::mojom::WindowType::MENU; | 41 return ui::mojom::WindowType::MENU; |
| 45 | 42 |
| 46 case ui::wm::WINDOW_TYPE_TOOLTIP: | 43 case ui::wm::WINDOW_TYPE_TOOLTIP: |
| 47 return ui::mojom::WindowType::TOOLTIP; | 44 return ui::mojom::WindowType::TOOLTIP; |
| 48 } | 45 } |
| 49 | 46 |
| 50 NOTREACHED(); | 47 NOTREACHED(); |
| 51 return ui::mojom::WindowType::CONTROL; | 48 return ui::mojom::WindowType::CONTROL; |
| 52 } | 49 } |
| 53 | 50 |
| 54 bool CompareByDisplayId(const RootWindowController* root1, | |
| 55 const RootWindowController* root2) { | |
| 56 return root1->display().id() < root2->display().id(); | |
| 57 } | |
| 58 | |
| 59 // TODO(sky): at some point this needs to support everything in DisplayInfo, | |
| 60 // for now just the bare minimum, which is [x+y-]wxh. | |
| 61 gfx::Rect ParseDisplayBounds(const std::string& spec) { | |
| 62 gfx::Rect bounds; | |
| 63 const std::vector<std::string> parts = | |
| 64 base::SplitString(spec, "-", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | |
| 65 std::string size_spec; | |
| 66 if (parts.size() == 2u) { | |
| 67 size_spec = parts[1]; | |
| 68 const std::vector<std::string> origin_parts = base::SplitString( | |
| 69 parts[0], "+", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | |
| 70 CHECK_EQ(2u, origin_parts.size()); | |
| 71 int x, y; | |
| 72 CHECK(base::StringToInt(origin_parts[0], &x)); | |
| 73 CHECK(base::StringToInt(origin_parts[1], &y)); | |
| 74 bounds.set_origin(gfx::Point(x, y)); | |
| 75 } else { | |
| 76 CHECK_EQ(1u, parts.size()); | |
| 77 size_spec = spec; | |
| 78 } | |
| 79 const std::vector<std::string> size_parts = base::SplitString( | |
| 80 size_spec, "x", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | |
| 81 CHECK_EQ(2u, size_parts.size()); | |
| 82 int w, h; | |
| 83 CHECK(base::StringToInt(size_parts[0], &w)); | |
| 84 CHECK(base::StringToInt(size_parts[1], &h)); | |
| 85 bounds.set_size(gfx::Size(w, h)); | |
| 86 return bounds; | |
| 87 } | |
| 88 | |
| 89 } // namespace | 51 } // namespace |
| 90 | 52 |
| 91 WmTestBase::WmTestBase() {} | 53 WmTestBase::WmTestBase() {} |
| 92 | 54 |
| 93 WmTestBase::~WmTestBase() { | 55 WmTestBase::~WmTestBase() { |
| 94 CHECK(setup_called_) | 56 CHECK(setup_called_) |
| 95 << "You have overridden SetUp but never called WmTestBase::SetUp"; | 57 << "You have overridden SetUp but never called WmTestBase::SetUp"; |
| 96 CHECK(teardown_called_) | 58 CHECK(teardown_called_) |
| 97 << "You have overridden TearDown but never called WmTestBase::TearDown"; | 59 << "You have overridden TearDown but never called WmTestBase::TearDown"; |
| 98 } | 60 } |
| 99 | 61 |
| 100 bool WmTestBase::SupportsMultipleDisplays() const { | 62 bool WmTestBase::SupportsMultipleDisplays() const { |
| 101 return false; | 63 return true; |
| 102 } | 64 } |
| 103 | 65 |
| 104 void WmTestBase::UpdateDisplay(const std::string& display_spec) { | 66 void WmTestBase::UpdateDisplay(const std::string& display_spec) { |
| 105 const std::vector<std::string> parts = base::SplitString( | 67 test_helper_->UpdateDisplay(display_spec); |
| 106 display_spec, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | |
| 107 // TODO(sky): allow > 1 once http://crbug.com/611563 is fixed. | |
| 108 DCHECK_EQ(1u, parts.size()); | |
| 109 gfx::Rect bounds = ParseDisplayBounds(parts[0]); | |
| 110 std::vector<RootWindowController*> roots = | |
| 111 WmTestBase::GetRootsOrderedByDisplayId(); | |
| 112 roots[0]->display_.set_bounds(bounds); | |
| 113 gfx::Rect work_area(bounds); | |
| 114 // Offset the height slightly to give a different work area. -2 is arbitrary, | |
| 115 // it could be anything. | |
| 116 work_area.set_height(std::max(0, work_area.height() - 2)); | |
| 117 roots[0]->display_.set_work_area(work_area); | |
| 118 roots[0]->root()->SetBounds(gfx::Rect(bounds.size())); | |
| 119 test_helper_->screen()->display_list()->UpdateDisplay( | |
| 120 roots[0]->display(), display::DisplayList::Type::PRIMARY); | |
| 121 } | 68 } |
| 122 | 69 |
| 123 ui::Window* WmTestBase::GetPrimaryRootWindow() { | 70 ui::Window* WmTestBase::GetPrimaryRootWindow() { |
| 124 std::vector<RootWindowController*> roots = | 71 std::vector<RootWindowController*> roots = |
| 125 WmTestBase::GetRootsOrderedByDisplayId(); | 72 test_helper_->GetRootsOrderedByDisplayId(); |
| 126 DCHECK(!roots.empty()); | 73 DCHECK(!roots.empty()); |
| 127 return roots[0]->root(); | 74 return roots[0]->root(); |
| 128 } | 75 } |
| 129 | 76 |
| 130 ui::Window* WmTestBase::GetSecondaryRootWindow() { | 77 ui::Window* WmTestBase::GetSecondaryRootWindow() { |
| 131 std::vector<RootWindowController*> roots = | 78 std::vector<RootWindowController*> roots = |
| 132 WmTestBase::GetRootsOrderedByDisplayId(); | 79 test_helper_->GetRootsOrderedByDisplayId(); |
| 133 return roots.size() < 2 ? nullptr : roots[1]->root(); | 80 return roots.size() < 2 ? nullptr : roots[1]->root(); |
| 134 } | 81 } |
| 135 | 82 |
| 136 display::Display WmTestBase::GetPrimaryDisplay() { | 83 display::Display WmTestBase::GetPrimaryDisplay() { |
| 137 std::vector<RootWindowController*> roots = | 84 std::vector<RootWindowController*> roots = |
| 138 WmTestBase::GetRootsOrderedByDisplayId(); | 85 test_helper_->GetRootsOrderedByDisplayId(); |
| 139 DCHECK(!roots.empty()); | 86 DCHECK(!roots.empty()); |
| 140 return roots[0]->display(); | 87 return roots[0]->display(); |
| 141 } | 88 } |
| 142 | 89 |
| 143 display::Display WmTestBase::GetSecondaryDisplay() { | 90 display::Display WmTestBase::GetSecondaryDisplay() { |
| 144 std::vector<RootWindowController*> roots = | 91 std::vector<RootWindowController*> roots = |
| 145 WmTestBase::GetRootsOrderedByDisplayId(); | 92 test_helper_->GetRootsOrderedByDisplayId(); |
| 146 return roots.size() < 2 ? display::Display() : roots[1]->display(); | 93 return roots.size() < 2 ? display::Display() : roots[1]->display(); |
| 147 } | 94 } |
| 148 | 95 |
| 149 ui::Window* WmTestBase::CreateTestWindow(const gfx::Rect& bounds) { | 96 ui::Window* WmTestBase::CreateTestWindow(const gfx::Rect& bounds) { |
| 150 return CreateTestWindow(bounds, ui::wm::WINDOW_TYPE_NORMAL); | 97 return CreateTestWindow(bounds, ui::wm::WINDOW_TYPE_NORMAL); |
| 151 } | 98 } |
| 152 | 99 |
| 153 ui::Window* WmTestBase::CreateTestWindow(const gfx::Rect& bounds, | 100 ui::Window* WmTestBase::CreateTestWindow(const gfx::Rect& bounds, |
| 154 ui::wm::WindowType window_type) { | 101 ui::wm::WindowType window_type) { |
| 155 std::map<std::string, std::vector<uint8_t>> properties; | 102 std::map<std::string, std::vector<uint8_t>> properties; |
| 156 properties[ui::mojom::WindowManager::kWindowType_Property] = | 103 properties[ui::mojom::WindowManager::kWindowType_Property] = |
| 157 mojo::ConvertTo<std::vector<uint8_t>>( | 104 mojo::ConvertTo<std::vector<uint8_t>>( |
| 158 static_cast<int32_t>(MusWindowTypeFromWmWindowType(window_type))); | 105 static_cast<int32_t>(MusWindowTypeFromWmWindowType(window_type))); |
| 159 if (!bounds.IsEmpty()) { | 106 if (!bounds.IsEmpty()) { |
| 160 properties[ui::mojom::WindowManager::kInitialBounds_Property] = | 107 properties[ui::mojom::WindowManager::kInitialBounds_Property] = |
| 161 mojo::ConvertTo<std::vector<uint8_t>>(bounds); | 108 mojo::ConvertTo<std::vector<uint8_t>>(bounds); |
| 162 } | 109 } |
| 163 properties[ui::mojom::WindowManager::kResizeBehavior_Property] = | 110 properties[ui::mojom::WindowManager::kResizeBehavior_Property] = |
| 164 mojo::ConvertTo<std::vector<uint8_t>>( | 111 mojo::ConvertTo<std::vector<uint8_t>>( |
| 165 ui::mojom::kResizeBehaviorCanResize | | 112 ui::mojom::kResizeBehaviorCanResize | |
| 166 ui::mojom::kResizeBehaviorCanMaximize | | 113 ui::mojom::kResizeBehaviorCanMaximize | |
| 167 ui::mojom::kResizeBehaviorCanMinimize); | 114 ui::mojom::kResizeBehaviorCanMinimize); |
| 168 | 115 |
| 169 ui::Window* window = | 116 ui::Window* window = test_helper_->GetRootsOrderedByDisplayId()[0] |
| 170 GetRootsOrderedByDisplayId()[0]->window_manager()->NewTopLevelWindow( | 117 ->window_manager() |
| 171 &properties); | 118 ->NewTopLevelWindow(&properties); |
| 172 window->SetVisible(true); | 119 window->SetVisible(true); |
| 173 return window; | 120 return window; |
| 174 } | 121 } |
| 175 | 122 |
| 176 ui::Window* WmTestBase::CreateFullscreenTestWindow() { | 123 ui::Window* WmTestBase::CreateFullscreenTestWindow() { |
| 177 std::map<std::string, std::vector<uint8_t>> properties; | 124 std::map<std::string, std::vector<uint8_t>> properties; |
| 178 properties[ui::mojom::WindowManager::kShowState_Property] = | 125 properties[ui::mojom::WindowManager::kShowState_Property] = |
| 179 mojo::ConvertTo<std::vector<uint8_t>>( | 126 mojo::ConvertTo<std::vector<uint8_t>>( |
| 180 static_cast<int32_t>(ui::mojom::ShowState::FULLSCREEN)); | 127 static_cast<int32_t>(ui::mojom::ShowState::FULLSCREEN)); |
| 181 ui::Window* window = | 128 ui::Window* window = test_helper_->GetRootsOrderedByDisplayId()[0] |
| 182 GetRootsOrderedByDisplayId()[0]->window_manager()->NewTopLevelWindow( | 129 ->window_manager() |
| 183 &properties); | 130 ->NewTopLevelWindow(&properties); |
| 184 window->SetVisible(true); | 131 window->SetVisible(true); |
| 185 return window; | 132 return window; |
| 186 } | 133 } |
| 187 | 134 |
| 188 ui::Window* WmTestBase::CreateChildTestWindow(ui::Window* parent, | 135 ui::Window* WmTestBase::CreateChildTestWindow(ui::Window* parent, |
| 189 const gfx::Rect& bounds) { | 136 const gfx::Rect& bounds) { |
| 190 std::map<std::string, std::vector<uint8_t>> properties; | 137 std::map<std::string, std::vector<uint8_t>> properties; |
| 191 properties[ui::mojom::WindowManager::kWindowType_Property] = | 138 properties[ui::mojom::WindowManager::kWindowType_Property] = |
| 192 mojo::ConvertTo<std::vector<uint8_t>>(static_cast<int32_t>( | 139 mojo::ConvertTo<std::vector<uint8_t>>(static_cast<int32_t>( |
| 193 MusWindowTypeFromWmWindowType(ui::wm::WINDOW_TYPE_NORMAL))); | 140 MusWindowTypeFromWmWindowType(ui::wm::WINDOW_TYPE_NORMAL))); |
| 194 ui::Window* window = | 141 ui::Window* window = test_helper_->GetRootsOrderedByDisplayId()[0] |
| 195 GetRootsOrderedByDisplayId()[0]->root()->window_tree()->NewWindow( | 142 ->root() |
| 196 &properties); | 143 ->window_tree() |
| 144 ->NewWindow(&properties); |
| 197 window->SetBounds(bounds); | 145 window->SetBounds(bounds); |
| 198 window->SetVisible(true); | 146 window->SetVisible(true); |
| 199 parent->AddChild(window); | 147 parent->AddChild(window); |
| 200 return window; | 148 return window; |
| 201 } | 149 } |
| 202 | 150 |
| 203 void WmTestBase::SetUp() { | 151 void WmTestBase::SetUp() { |
| 204 setup_called_ = true; | 152 setup_called_ = true; |
| 205 test_helper_.reset(new WmTestHelper); | 153 test_helper_.reset(new WmTestHelper); |
| 206 test_helper_->Init(); | 154 test_helper_->Init(); |
| 207 } | 155 } |
| 208 | 156 |
| 209 void WmTestBase::TearDown() { | 157 void WmTestBase::TearDown() { |
| 210 teardown_called_ = true; | 158 teardown_called_ = true; |
| 211 test_helper_.reset(); | 159 test_helper_.reset(); |
| 212 } | 160 } |
| 213 | 161 |
| 214 std::vector<RootWindowController*> WmTestBase::GetRootsOrderedByDisplayId() { | |
| 215 std::set<RootWindowController*> roots = test_helper_->window_manager_app() | |
| 216 ->window_manager() | |
| 217 ->GetRootWindowControllers(); | |
| 218 std::vector<RootWindowController*> ordered_roots; | |
| 219 ordered_roots.insert(ordered_roots.begin(), roots.begin(), roots.end()); | |
| 220 std::sort(ordered_roots.begin(), ordered_roots.end(), &CompareByDisplayId); | |
| 221 return ordered_roots; | |
| 222 } | |
| 223 | |
| 224 } // namespace mus | 162 } // namespace mus |
| 225 } // namespace ash | 163 } // namespace ash |
| OLD | NEW |