1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/ui_devtools/views/window_element.h"
#include <memory>
#include "components/ui_devtools/protocol.h"
#include "components/ui_devtools/ui_devtools_unittest_utils.h"
#include "ui/aura/window.h"
#include "ui/views/test/views_test_base.h"
#include "ui/wm/core/window_util.h"
namespace ui_devtools {
using ::testing::_;
class WindowElementTest : public views::ViewsTestBase {
public:
WindowElementTest() = default;
WindowElementTest(const WindowElementTest&) = delete;
WindowElementTest& operator=(const WindowElementTest&) = delete;
~WindowElementTest() override = default;
void SetUp() override {
views::ViewsTestBase::SetUp();
window_ = std::make_unique<aura::Window>(nullptr,
aura::client::WINDOW_TYPE_NORMAL);
window_->Init(ui::LAYER_NOT_DRAWN);
aura::Window* root_window = GetContext();
DCHECK(root_window);
root_window->AddChild(window_.get());
delegate_ = std::make_unique<testing::NiceMock<MockUIElementDelegate>>();
// |OnUIElementAdded| is called on element creation.
EXPECT_CALL(*delegate_, OnUIElementAdded(_, _)).Times(1);
element_ = std::make_unique<WindowElement>(window_.get(), delegate_.get(),
nullptr);
}
void TearDown() override {
// The root window needs to be empty by teardown.
element_.reset();
delegate_.reset();
window_.reset();
views::ViewsTestBase::TearDown();
}
protected:
aura::Window* window() { return window_.get(); }
WindowElement* element() { return element_.get(); }
MockUIElementDelegate* delegate() { return delegate_.get(); }
private:
std::unique_ptr<aura::Window> window_;
std::unique_ptr<WindowElement> element_;
std::unique_ptr<MockUIElementDelegate> delegate_;
};
TEST_F(WindowElementTest, SettingsBoundsOnWindowCallsDelegate) {
EXPECT_CALL(*delegate(), OnUIElementBoundsChanged(element())).Times(1);
window()->SetBounds(gfx::Rect(10, 20, 300, 400));
}
TEST_F(WindowElementTest, SettingsBoundsOnElementSetsOnWindow) {
gfx::Rect old_bounds = window()->bounds();
gfx::Rect new_bounds = gfx::Rect(10, 20, 300, 400);
DCHECK(old_bounds != new_bounds);
element()->SetBounds(new_bounds);
EXPECT_EQ(window()->bounds(), new_bounds);
}
TEST_F(WindowElementTest, SettingVisibleOnElementSetsOnWindow) {
DCHECK(!window()->IsVisible());
element()->SetVisible(true);
EXPECT_TRUE(window()->IsVisible());
element()->SetVisible(false);
EXPECT_FALSE(window()->IsVisible());
}
TEST_F(WindowElementTest, GetVisible) {
bool visible;
window()->Hide();
element()->GetVisible(&visible);
EXPECT_FALSE(visible);
window()->Show();
element()->GetVisible(&visible);
EXPECT_TRUE(visible);
}
TEST_F(WindowElementTest, GetBounds) {
gfx::Rect actual_bounds;
gfx::Rect new_bounds = gfx::Rect(10, 20, 300, 400);
window()->SetBounds(new_bounds);
element()->GetBounds(&actual_bounds);
EXPECT_EQ(actual_bounds, new_bounds);
}
TEST_F(WindowElementTest, GetAttributes) {
std::string window_name("A window name");
window()->SetName(window_name);
std::vector<std::string> attrs = element()->GetAttributes();
ASSERT_FALSE(wm::IsActiveWindow(window()));
EXPECT_THAT(attrs,
testing::ElementsAre("name", window_name, "active", "false"));
wm::ActivateWindow(window());
attrs = element()->GetAttributes();
EXPECT_THAT(attrs,
testing::ElementsAre("name", window_name, "active", "true"));
}
} // namespace ui_devtools
|