Chromium Code Reviews| Index: mash/wm/test/wm_test_base.cc |
| diff --git a/mash/wm/test/wm_test_base.cc b/mash/wm/test/wm_test_base.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..92de29a99337f6701c310b52b6d2ca6c6d2cba1a |
| --- /dev/null |
| +++ b/mash/wm/test/wm_test_base.cc |
| @@ -0,0 +1,208 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "mash/wm/test/wm_test_base.h" |
| + |
| +#include <algorithm> |
| +#include <vector> |
| + |
| +#include "base/strings/string_number_conversions.h" |
| +#include "base/strings/string_split.h" |
| +#include "components/mus/public/cpp/property_type_converters.h" |
| +#include "components/mus/public/cpp/window_tree_connection.h" |
| +#include "mash/wm/root_window_controller.h" |
| +#include "mash/wm/test/wm_test_helper.h" |
| +#include "mash/wm/test/wm_test_screen.h" |
| +#include "mash/wm/window_manager.h" |
| +#include "mash/wm/window_manager_application.h" |
| +#include "ui/display/display.h" |
| + |
| +namespace mash { |
| +namespace wm { |
| +namespace { |
| + |
| +mus::mojom::WindowType MusWindowTypeFromWmWindowType( |
| + ui::wm::WindowType wm_window_type) { |
| + switch (wm_window_type) { |
| + case ui::wm::WINDOW_TYPE_UNKNOWN: |
| + break; |
| + |
| + case ui::wm::WINDOW_TYPE_NORMAL: |
| + return mus::mojom::WindowType::WINDOW; |
| + |
| + case ui::wm::WINDOW_TYPE_POPUP: |
| + return mus::mojom::WindowType::POPUP; |
| + |
| + case ui::wm::WINDOW_TYPE_CONTROL: |
| + return mus::mojom::WindowType::CONTROL; |
| + |
| + case ui::wm::WINDOW_TYPE_PANEL: |
| + return mus::mojom::WindowType::PANEL; |
| + |
| + case ui::wm::WINDOW_TYPE_MENU: |
| + return mus::mojom::WindowType::MENU; |
| + |
| + case ui::wm::WINDOW_TYPE_TOOLTIP: |
| + return mus::mojom::WindowType::TOOLTIP; |
| + } |
| + |
| + NOTREACHED(); |
| + return mus::mojom::WindowType::CONTROL; |
| +} |
| + |
| +bool CompareByDisplayId(const RootWindowController* root1, |
| + const RootWindowController* root2) { |
| + return root1->display().id() < root2->display().id(); |
| +} |
| + |
| +// TODO(sky): at some point this needs to support everything in DisplayInfo, |
| +// for now just the bare minimum, which is [x+y-]wxh. |
| +gfx::Rect ParseDisplayBounds(const std::string& spec) { |
| + 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.
|
| + const std::vector<std::string> parts = |
| + base::SplitString(spec, "-", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
| + std::string size_spec; |
| + if (parts.size() == 2u) { |
| + size_spec = parts[1]; |
| + const std::vector<std::string> origin_parts = base::SplitString( |
| + 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]
|
| + 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.
|
| + int x, y; |
| + CHECK(base::StringToInt(origin_parts[0], &x)); |
| + CHECK(base::StringToInt(origin_parts[1], &y)); |
| + bounds.set_origin(gfx::Point(x, y)); |
| + } else { |
| + DCHECK_EQ(1u, parts.size()); |
| + size_spec = spec; |
| + } |
| + const std::vector<std::string> size_parts = base::SplitString( |
| + size_spec, "x", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
| + DCHECK_EQ(2u, size_parts.size()); |
|
James Cook
2016/05/19 05:08:22
ditto
sky
2016/05/19 15:14:24
Done.
|
| + int w, h; |
| + CHECK(base::StringToInt(size_parts[0], &w)); |
| + CHECK(base::StringToInt(size_parts[1], &h)); |
| + bounds.set_size(gfx::Size(w, h)); |
| + return bounds; |
| +} |
| + |
| +} // namespace |
| + |
| +WmTestBase::WmTestBase() {} |
| + |
| +WmTestBase::~WmTestBase() { |
| + CHECK(setup_called_) |
| + << "You have overridden SetUp but never called WmTestBase::SetUp"; |
| + CHECK(teardown_called_) |
| + << "You have overridden TearDown but never called WmTestBase::TearDown"; |
| +} |
| + |
| +bool WmTestBase::SupportsMultipleDisplays() const { |
| + return false; |
| +} |
| + |
| +void WmTestBase::UpdateDisplay(const std::string& display_spec) { |
| + const std::vector<std::string> parts = base::SplitString( |
| + display_spec, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
| + // TODO(sky): allow > 1 once http://crbug.com/611563 is fixed. |
| + DCHECK_EQ(1u, parts.size()); |
| + gfx::Rect bounds = ParseDisplayBounds(parts[0]); |
| + std::vector<RootWindowController*> roots = |
| + WmTestBase::GetRootsOrderedByDisplayId(); |
| + roots[0]->display_.set_bounds(bounds); |
| + gfx::Rect work_area(bounds); |
| + 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.
|
| + roots[0]->display_.set_work_area(work_area); |
| + roots[0]->root()->SetBounds(gfx::Rect(bounds.size())); |
| + test_helper_->screen()->display_list()->UpdateDisplay( |
| + roots[0]->display(), views::DisplayList::Type::PRIMARY); |
| +} |
| + |
| +mus::Window* WmTestBase::GetPrimaryRootWindow() { |
| + std::vector<RootWindowController*> roots = |
| + WmTestBase::GetRootsOrderedByDisplayId(); |
| + 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
|
| +} |
| + |
| +mus::Window* WmTestBase::GetSecondaryRootWindow() { |
| + std::vector<RootWindowController*> roots = |
| + WmTestBase::GetRootsOrderedByDisplayId(); |
| + return roots.size() < 2 ? nullptr : roots[1]->root(); |
| +} |
| + |
| +display::Display WmTestBase::GetPrimaryDisplay() { |
| + std::vector<RootWindowController*> roots = |
| + WmTestBase::GetRootsOrderedByDisplayId(); |
| + return roots.empty() ? display::Display() : roots[0]->display(); |
| +} |
| + |
| +display::Display WmTestBase::GetSecondaryDisplay() { |
| + std::vector<RootWindowController*> roots = |
| + WmTestBase::GetRootsOrderedByDisplayId(); |
| + return roots.size() < 2 ? display::Display() : roots[1]->display(); |
| +} |
| + |
| +mus::Window* WmTestBase::CreateTestWindow(const gfx::Rect& bounds) { |
| + return CreateTestWindow(bounds, ui::wm::WINDOW_TYPE_NORMAL); |
| +} |
| + |
| +mus::Window* WmTestBase::CreateTestWindow(const gfx::Rect& bounds, |
| + ui::wm::WindowType window_type) { |
| + std::map<std::string, std::vector<uint8_t>> properties; |
| + properties[mus::mojom::WindowManager::kWindowType_Property] = |
| + mojo::ConvertTo<std::vector<uint8_t>>( |
| + static_cast<int32_t>(MusWindowTypeFromWmWindowType(window_type))); |
| + if (!bounds.IsEmpty()) { |
| + properties[mus::mojom::WindowManager::kInitialBounds_Property] = |
| + mojo::ConvertTo<std::vector<uint8_t>>(bounds); |
| + } |
| + properties[mus::mojom::WindowManager::kResizeBehavior_Property] = |
| + mojo::ConvertTo<std::vector<uint8_t>>( |
| + mus::mojom::kResizeBehaviorCanResize | |
| + mus::mojom::kResizeBehaviorCanMaximize | |
| + mus::mojom::kResizeBehaviorCanMinimize); |
| + |
| + mus::Window* window = |
| + GetRootsOrderedByDisplayId()[0]->window_manager()->NewTopLevelWindow( |
| + &properties); |
| + window->SetVisible(true); |
| + return window; |
| +} |
| + |
| +mus::Window* WmTestBase::CreateChildTestWindow(mus::Window* parent, |
| + const gfx::Rect& bounds) { |
| + std::map<std::string, std::vector<uint8_t>> properties; |
| + properties[mus::mojom::WindowManager::kWindowType_Property] = |
| + mojo::ConvertTo<std::vector<uint8_t>>(static_cast<int32_t>( |
| + MusWindowTypeFromWmWindowType(ui::wm::WINDOW_TYPE_NORMAL))); |
| + mus::Window* window = |
| + GetRootsOrderedByDisplayId()[0]->root()->connection()->NewWindow( |
| + &properties); |
| + 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
|
| + window->SetVisible(true); |
| + parent->AddChild(window); |
| + return window; |
| +} |
| + |
| +void WmTestBase::SetUp() { |
| + setup_called_ = true; |
| + test_helper_.reset(new WmTestHelper); |
| + test_helper_->Init(); |
| +} |
| + |
| +void WmTestBase::TearDown() { |
| + teardown_called_ = true; |
| + test_helper_.reset(); |
| +} |
| + |
| +std::vector<RootWindowController*> WmTestBase::GetRootsOrderedByDisplayId() { |
| + std::set<RootWindowController*> roots = |
| + test_helper_->window_manager_app()->GetRootControllers(); |
| + std::vector<RootWindowController*> ordered_roots; |
| + ordered_roots.insert(ordered_roots.begin(), roots.begin(), roots.end()); |
| + std::sort(ordered_roots.begin(), ordered_roots.end(), &CompareByDisplayId); |
| + return ordered_roots; |
| +} |
| + |
| +} // namespace wm |
| +} // namespace mash |