Chromium Code Reviews| 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 "mash/wm/test/wm_test_base.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 #include <vector> | |
| 9 | |
| 10 #include "base/strings/string_number_conversions.h" | |
| 11 #include "base/strings/string_split.h" | |
| 12 #include "components/mus/public/cpp/property_type_converters.h" | |
| 13 #include "components/mus/public/cpp/window_tree_connection.h" | |
| 14 #include "mash/wm/root_window_controller.h" | |
| 15 #include "mash/wm/test/wm_test_helper.h" | |
| 16 #include "mash/wm/test/wm_test_screen.h" | |
| 17 #include "mash/wm/window_manager.h" | |
| 18 #include "mash/wm/window_manager_application.h" | |
| 19 #include "ui/display/display.h" | |
| 20 | |
| 21 namespace mash { | |
| 22 namespace wm { | |
| 23 namespace { | |
| 24 | |
| 25 mus::mojom::WindowType MusWindowTypeFromWmWindowType( | |
| 26 ui::wm::WindowType wm_window_type) { | |
| 27 switch (wm_window_type) { | |
| 28 case ui::wm::WINDOW_TYPE_UNKNOWN: | |
| 29 break; | |
| 30 | |
| 31 case ui::wm::WINDOW_TYPE_NORMAL: | |
| 32 return mus::mojom::WindowType::WINDOW; | |
| 33 | |
| 34 case ui::wm::WINDOW_TYPE_POPUP: | |
| 35 return mus::mojom::WindowType::POPUP; | |
| 36 | |
| 37 case ui::wm::WINDOW_TYPE_CONTROL: | |
| 38 return mus::mojom::WindowType::CONTROL; | |
| 39 | |
| 40 case ui::wm::WINDOW_TYPE_PANEL: | |
| 41 return mus::mojom::WindowType::PANEL; | |
| 42 | |
| 43 case ui::wm::WINDOW_TYPE_MENU: | |
| 44 return mus::mojom::WindowType::MENU; | |
| 45 | |
| 46 case ui::wm::WINDOW_TYPE_TOOLTIP: | |
| 47 return mus::mojom::WindowType::TOOLTIP; | |
| 48 } | |
| 49 | |
| 50 NOTREACHED(); | |
| 51 return mus::mojom::WindowType::CONTROL; | |
| 52 } | |
| 53 | |
| 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(0, 0, 1366, 768); | |
|
James Cook
2016/05/19 15:17:07
Maybe start with just gfx::Rect since the size is
sky
2016/05/19 16:12:38
Done.
| |
| 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[1], "+", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | |
|
James Cook
2016/05/19 05:08:22
parts[0]?
sky
2016/05/19 15:14:24
origin is first. Format supported is on line 60, w
James Cook
2016/05/19 15:27:46
Huh? It looks to me like 1+2-800x600 will split in
sky
2016/05/19 16:12:38
D'OH! You are clearly right. It should be parts[0]
| |
| 70 DCHECK_EQ(2u, origin_parts.size()); | |
|
James Cook
2016/05/19 05:08:22
maybe CHECK_EQ, since you're going to access origi
sky
2016/05/19 15:14:24
Done.
| |
| 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 DCHECK_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 DCHECK_EQ(2u, size_parts.size()); | |
|
James Cook
2016/05/19 05:08:22
ditto
sky
2016/05/19 15:14:24
Done.
| |
| 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 | |
| 90 | |
| 91 WmTestBase::WmTestBase() {} | |
| 92 | |
| 93 WmTestBase::~WmTestBase() { | |
| 94 CHECK(setup_called_) | |
| 95 << "You have overridden SetUp but never called WmTestBase::SetUp"; | |
| 96 CHECK(teardown_called_) | |
| 97 << "You have overridden TearDown but never called WmTestBase::TearDown"; | |
| 98 } | |
| 99 | |
| 100 bool WmTestBase::SupportsMultipleDisplays() const { | |
| 101 return false; | |
| 102 } | |
| 103 | |
| 104 void WmTestBase::UpdateDisplay(const std::string& display_spec) { | |
| 105 const std::vector<std::string> parts = base::SplitString( | |
| 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 work_area.set_height(std::max(0, work_area.height() - 2)); | |
|
James Cook
2016/05/19 05:08:22
Comment where the -2 comes from, or use a constant
sky
2016/05/19 15:14:24
Done.
| |
| 115 roots[0]->display_.set_work_area(work_area); | |
| 116 roots[0]->root()->SetBounds(gfx::Rect(bounds.size())); | |
| 117 test_helper_->screen()->display_list()->UpdateDisplay( | |
| 118 roots[0]->display(), views::DisplayList::Type::PRIMARY); | |
| 119 } | |
| 120 | |
| 121 mus::Window* WmTestBase::GetPrimaryRootWindow() { | |
| 122 std::vector<RootWindowController*> roots = | |
| 123 WmTestBase::GetRootsOrderedByDisplayId(); | |
| 124 return roots.empty() ? nullptr : roots[0]->root(); | |
|
James Cook
2016/05/19 05:08:22
Does this have to support the case where there are
sky
2016/05/19 15:14:24
In theory tests that shutdown all the roots could
| |
| 125 } | |
| 126 | |
| 127 mus::Window* WmTestBase::GetSecondaryRootWindow() { | |
| 128 std::vector<RootWindowController*> roots = | |
| 129 WmTestBase::GetRootsOrderedByDisplayId(); | |
| 130 return roots.size() < 2 ? nullptr : roots[1]->root(); | |
| 131 } | |
| 132 | |
| 133 display::Display WmTestBase::GetPrimaryDisplay() { | |
| 134 std::vector<RootWindowController*> roots = | |
| 135 WmTestBase::GetRootsOrderedByDisplayId(); | |
| 136 return roots.empty() ? display::Display() : roots[0]->display(); | |
| 137 } | |
| 138 | |
| 139 display::Display WmTestBase::GetSecondaryDisplay() { | |
| 140 std::vector<RootWindowController*> roots = | |
| 141 WmTestBase::GetRootsOrderedByDisplayId(); | |
| 142 return roots.size() < 2 ? display::Display() : roots[1]->display(); | |
| 143 } | |
| 144 | |
| 145 mus::Window* WmTestBase::CreateTestWindow(const gfx::Rect& bounds) { | |
| 146 return CreateTestWindow(bounds, ui::wm::WINDOW_TYPE_NORMAL); | |
| 147 } | |
| 148 | |
| 149 mus::Window* WmTestBase::CreateTestWindow(const gfx::Rect& bounds, | |
| 150 ui::wm::WindowType window_type) { | |
| 151 std::map<std::string, std::vector<uint8_t>> properties; | |
| 152 properties[mus::mojom::WindowManager::kWindowType_Property] = | |
| 153 mojo::ConvertTo<std::vector<uint8_t>>( | |
| 154 static_cast<int32_t>(MusWindowTypeFromWmWindowType(window_type))); | |
| 155 if (!bounds.IsEmpty()) { | |
| 156 properties[mus::mojom::WindowManager::kInitialBounds_Property] = | |
| 157 mojo::ConvertTo<std::vector<uint8_t>>(bounds); | |
| 158 } | |
| 159 properties[mus::mojom::WindowManager::kResizeBehavior_Property] = | |
| 160 mojo::ConvertTo<std::vector<uint8_t>>( | |
| 161 mus::mojom::kResizeBehaviorCanResize | | |
| 162 mus::mojom::kResizeBehaviorCanMaximize | | |
| 163 mus::mojom::kResizeBehaviorCanMinimize); | |
| 164 | |
| 165 mus::Window* window = | |
| 166 GetRootsOrderedByDisplayId()[0]->window_manager()->NewTopLevelWindow( | |
| 167 &properties); | |
| 168 window->SetVisible(true); | |
| 169 return window; | |
| 170 } | |
| 171 | |
| 172 mus::Window* WmTestBase::CreateChildTestWindow(mus::Window* parent, | |
| 173 const gfx::Rect& bounds) { | |
| 174 std::map<std::string, std::vector<uint8_t>> properties; | |
| 175 properties[mus::mojom::WindowManager::kWindowType_Property] = | |
| 176 mojo::ConvertTo<std::vector<uint8_t>>(static_cast<int32_t>( | |
| 177 MusWindowTypeFromWmWindowType(ui::wm::WINDOW_TYPE_NORMAL))); | |
| 178 mus::Window* window = | |
| 179 GetRootsOrderedByDisplayId()[0]->root()->connection()->NewWindow( | |
| 180 &properties); | |
| 181 window->SetBounds(bounds); | |
|
James Cook
2016/05/19 05:08:22
This one doesn't need kInitialBounds_Property?
sky
2016/05/19 15:14:24
Top level windows are routed through the windowman
| |
| 182 window->SetVisible(true); | |
| 183 parent->AddChild(window); | |
| 184 return window; | |
| 185 } | |
| 186 | |
| 187 void WmTestBase::SetUp() { | |
| 188 setup_called_ = true; | |
| 189 test_helper_.reset(new WmTestHelper); | |
| 190 test_helper_->Init(); | |
| 191 } | |
| 192 | |
| 193 void WmTestBase::TearDown() { | |
| 194 teardown_called_ = true; | |
| 195 test_helper_.reset(); | |
| 196 } | |
| 197 | |
| 198 std::vector<RootWindowController*> WmTestBase::GetRootsOrderedByDisplayId() { | |
| 199 std::set<RootWindowController*> roots = | |
| 200 test_helper_->window_manager_app()->GetRootControllers(); | |
| 201 std::vector<RootWindowController*> ordered_roots; | |
| 202 ordered_roots.insert(ordered_roots.begin(), roots.begin(), roots.end()); | |
| 203 std::sort(ordered_roots.begin(), ordered_roots.end(), &CompareByDisplayId); | |
| 204 return ordered_roots; | |
| 205 } | |
| 206 | |
| 207 } // namespace wm | |
| 208 } // namespace mash | |
| OLD | NEW |