File: widget_element_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (192 lines) | stat: -rw-r--r-- 6,057 bytes parent folder | download | duplicates (6)
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// 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/widget_element.h"

#include <memory>

#include "base/memory/raw_ptr.h"
#include "components/ui_devtools/protocol.h"
#include "components/ui_devtools/root_element.h"
#include "components/ui_devtools/ui_devtools_unittest_utils.h"
#include "components/ui_devtools/views/view_element.h"
#include "ui/views/test/views_test_base.h"

namespace ui_devtools {

using ::testing::_;

namespace {
const std::string kWidgetName = "A test widget";
}

class MockWidgetElementDelegate : public MockUIElementDelegate {
 public:
  MockWidgetElementDelegate() = default;
  MockWidgetElementDelegate(const MockWidgetElementDelegate&) = delete;
  MockWidgetElementDelegate& operator=(const MockWidgetElementDelegate&) =
      delete;
  ~MockWidgetElementDelegate() override = default;

  UIElement* root_element() { return &root_element_; }

 private:
  RootElement root_element_{this};
};

class WidgetElementTest : public views::ViewsTestBase {
 public:
  WidgetElementTest() = default;
  WidgetElementTest(const WidgetElementTest&) = delete;
  WidgetElementTest& operator=(const WidgetElementTest&) = delete;
  ~WidgetElementTest() override = default;

  void SetUp() override {
    views::ViewsTestBase::SetUp();

    widget_ = new views::Widget;
    views::Widget::InitParams params =
        CreateParams(views::Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET,
                     views::Widget::InitParams::TYPE_WINDOW);
    params.name = kWidgetName;
    widget_->Init(std::move(params));

    delegate_ =
        std::make_unique<testing::NiceMock<MockWidgetElementDelegate>>();
    element_ =
        std::make_unique<WidgetElement>(widget_, delegate_.get(), nullptr);
    // The widget element will delete the ViewElement in |OnWillRemoveView|
    // TODO(lgrey): I think probably WidgetElement should do this itself
    // rather than making the DOMAgent (or test)  put the tree together.
    element()->AddChild(
        new ViewElement(widget_->GetRootView(), delegate_.get(), element()));
  }

  void TearDown() override {
    if (widget())
      CloseWidget();
    views::ViewsTestBase::TearDown();
  }

 protected:
  views::Widget* widget() { return widget_; }
  void CloseWidget() {
    widget_->CloseNow();
    widget_ = nullptr;
  }
  WidgetElement* element() { return element_.get(); }
  MockWidgetElementDelegate* delegate() { return delegate_.get(); }
  void AddWidgetElementToTree() {
    element_->set_parent(delegate_->root_element());
    delegate_->root_element()->AddChild(element_.get());
  }

 private:
  raw_ptr<views::Widget, DanglingUntriaged> widget_ = nullptr;
  std::unique_ptr<WidgetElement> element_;
  std::unique_ptr<MockWidgetElementDelegate> delegate_;
};

TEST_F(WidgetElementTest, SettingsBoundsOnWidgetCallsDelegate) {
  // Once for the root view, and once for the widget.
  EXPECT_CALL(*delegate(), OnUIElementBoundsChanged(_))
      .Times(::testing::AtLeast(1));
  EXPECT_CALL(*delegate(), OnUIElementBoundsChanged(element()))
      .Times(::testing::AtLeast(1));
  widget()->SetBounds(gfx::Rect(10, 20, 300, 400));
}

TEST_F(WidgetElementTest, SettingsBoundsOnElementSetsOnWidget) {
  gfx::Rect old_bounds = widget()->GetRestoredBounds();
  gfx::Rect new_bounds = gfx::Rect(10, 20, 300, 400);
  DCHECK(old_bounds != new_bounds);

  element()->SetBounds(new_bounds);
  EXPECT_EQ(widget()->GetRestoredBounds(), new_bounds);
}

TEST_F(WidgetElementTest, SettingVisibleOnElementSetsOnWidget) {
  DCHECK(!widget()->IsVisible());

  element()->SetVisible(true);
  EXPECT_TRUE(widget()->IsVisible());

  element()->SetVisible(false);
  EXPECT_FALSE(widget()->IsVisible());
}

TEST_F(WidgetElementTest, GetBounds) {
  gfx::Rect actual_bounds;

  gfx::Rect new_bounds = gfx::Rect(10, 20, 300, 400);
  widget()->SetBounds(new_bounds);
  element()->GetBounds(&actual_bounds);
  EXPECT_EQ(actual_bounds, new_bounds);
}

TEST_F(WidgetElementTest, GetVisible) {
  DCHECK(!widget()->IsVisible());
  bool visible;

  element()->GetVisible(&visible);
  EXPECT_FALSE(visible);

  widget()->Show();
  element()->GetVisible(&visible);
  EXPECT_TRUE(visible);

  widget()->Hide();
  element()->GetVisible(&visible);
  EXPECT_FALSE(visible);
}

TEST_F(WidgetElementTest, GetAttributes) {
  std::vector<std::string> attrs = element()->GetAttributes();

  ASSERT_FALSE(widget()->IsActive());
  EXPECT_THAT(attrs,
              testing::ElementsAre("name", kWidgetName, "active", "false"));

  widget()->Activate();
  attrs = element()->GetAttributes();
  EXPECT_THAT(attrs,
              testing::ElementsAre("name", kWidgetName, "active", "true"));
}

TEST_F(WidgetElementTest, GetNodeWindowAndScreenBounds) {
  std::pair<gfx::NativeWindow, gfx::Rect> window_and_bounds =
      element()->GetNodeWindowAndScreenBounds();
  EXPECT_EQ(widget()->GetNativeWindow(), window_and_bounds.first);
  EXPECT_EQ(widget()->GetWindowBoundsInScreen(), window_and_bounds.second);
}

TEST_F(WidgetElementTest, TrackNonParentedElementLifetime) {
  ASSERT_TRUE(widget());
  EXPECT_FALSE(element()->parent());
  // While closing the widget, the widget element and its children should be
  // removed as well.
  // Remove the root view element.
  EXPECT_CALL(*delegate(), OnUIElementRemoved(_));
  // Remove the widget element.
  EXPECT_CALL(*delegate(), OnUIElementRemoved(element()));
  CloseWidget();
}

TEST_F(WidgetElementTest, TrackParentedElementLifetime) {
  ASSERT_TRUE(widget());
  AddWidgetElementToTree();
  UIElement* root = delegate()->root_element();
  EXPECT_EQ(root, element()->parent());
  EXPECT_EQ(1u, root->children().size());

  // Remove the root view element.
  EXPECT_CALL(*delegate(), OnUIElementRemoved(_));
  // Remove the widget element.
  EXPECT_CALL(*delegate(), OnUIElementRemoved(element()));
  CloseWidget();
  // After closing the widget, the element tree should only has the root.
  EXPECT_EQ(0u, root->children().size());
}

}  // namespace ui_devtools