summaryrefslogtreecommitdiffstats
path: root/ui/views/view_unittest.cc
blob: 202b2238ff1fc57a9d76d1c4948eb698b57c0aac (plain)
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
193
194
195
196
197
198
199
200
201
202
203
204
// Copyright (c) 2011 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 <algorithm>

#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/canvas.h"
#include "ui/views/view.h"
#include "ui/views/widget/widget.h"

namespace ui {

class ViewTest : public testing::Test {
 public:
  ViewTest() {}
  virtual ~ViewTest() {}

 private:
  DISALLOW_COPY_AND_ASSIGN(ViewTest);
};

TEST_F(ViewTest, SizeAndDisposition) {
  View v;
  EXPECT_TRUE(v.bounds().IsEmpty());
  EXPECT_TRUE(v.visible());

  v.SetBounds(gfx::Rect(10, 10, 200, 200));
  EXPECT_EQ(200, v.width());

  EXPECT_TRUE(v.GetPreferredSize().IsEmpty());
}

TEST_F(ViewTest, TreeOperations) {
  View v;
  EXPECT_EQ(NULL, v.GetWidget());
  EXPECT_TRUE(v.children_empty());

  View child1;
  v.AddChildView(&child1);
  EXPECT_EQ(1, v.children_size());
  EXPECT_EQ(&v, child1.parent());

  View child2;
  v.AddChildViewAt(&child2, 0);
  EXPECT_EQ(2, v.children_size());
  EXPECT_EQ(child1.parent(), child2.parent());

  v.RemoveChildView(&child2, false);
  EXPECT_EQ(1, v.children_size());
  EXPECT_EQ(NULL, child2.parent());

  //v.RemoveAllChildViews(false);
  //EXPECT_TRUE(v.children_empty());
}

class ObserverView : public View {
 public:
  ObserverView()
      : view_added_(false),
        view_removed_(false),
        subject_view_(NULL) {}
  virtual ~ObserverView() {}

  void ResetTestState() {
    view_added_ = false;
    view_removed_ = false;
    subject_view_ = NULL;
  }

  // Overridden from View:
  virtual void OnViewAdded(View* parent, View* child) {
    subject_view_ = child;
    view_added_ = true;
    view_removed_ = false;
  }
  virtual void OnViewRemoved(View* parent, View* child) {
    subject_view_ = child;
    view_removed_ = true;
    view_added_ = false;
  }

  bool view_added() const { return view_added_; }
  bool view_removed() const { return view_removed_; }
  View* subject_view() const { return subject_view_; }

 private:
  bool view_added_;
  bool view_removed_;
  View* subject_view_;

  DISALLOW_COPY_AND_ASSIGN(ObserverView);
};

class WidgetObserverView : public View {
 public:
  WidgetObserverView() : in_widget_(false) {}
  virtual ~WidgetObserverView() {}

  // Overridden from View:
  virtual void OnViewAddedToWidget() {
    in_widget_ = true;
  }
  virtual void OnViewRemovedFromWidget() {
    in_widget_ = false;
  }

  bool in_widget() const { return in_widget_; }

 private:
  bool in_widget_;

  DISALLOW_COPY_AND_ASSIGN(WidgetObserverView);
};

/*
TEST_F(ViewTest, HierarchyObserver) {
  ObserverView ov;
  Widget widget(&ov);
  widget.InitWithNativeViewParent(NULL, gfx::Rect(20, 20, 400, 400));

  // |ov|'s addition to the RootView's hierarchy should have caused these values
  // to be set.
  EXPECT_TRUE(ov.view_added());
  EXPECT_FALSE(ov.view_removed());
  EXPECT_EQ(&ov, ov.subject_view());

  ov.ResetTestState();

  // Direct descendants.
  View v2;
  ov.AddChildView(&v2);
  EXPECT_TRUE(ov.view_added());
  EXPECT_FALSE(ov.view_removed());
  EXPECT_EQ(&v2, ov.subject_view());

  ov.ResetTestState();

  // Nested Views and Widget addition.
  WidgetObserverView v3;
  EXPECT_FALSE(v3.in_widget());
  v2.AddChildView(&v3);
  EXPECT_TRUE(v3.in_widget());
  EXPECT_EQ(&widget, v3.GetWidget());
  EXPECT_TRUE(ov.view_added());
  EXPECT_FALSE(ov.view_removed());
  EXPECT_EQ(&v3, ov.subject_view());

  ov.ResetTestState();

  // Removal and Widget removal.
  ov.RemoveChildView(&v2);
  EXPECT_FALSE(ov.view_added());
  EXPECT_TRUE(ov.view_removed());
  EXPECT_EQ(&v2, ov.subject_view());

  EXPECT_FALSE(v3.in_widget());
  EXPECT_EQ(NULL, v3.GetWidget());
}
*/

TEST_F(ViewTest, IDs) {
  const int kV1ID = 1;
  const int kV2ID = 2;
  const int kV3ID = 3;
  const int kV4ID = 4;
  const int kV5ID = 5;
  const int kGroupID = 1;
  View v1;
  v1.set_id(kV1ID);
  View v2;
  v2.set_id(kV2ID);
  View v3;
  v3.set_id(kV3ID);
  v3.set_group(kGroupID);
  View v4;
  v4.set_id(kV4ID);
  v4.set_group(kGroupID);
  v1.AddChildView(&v2);
  v2.AddChildView(&v3);
  v2.AddChildView(&v4);

  EXPECT_EQ(&v4, v1.GetViewByID(kV4ID));
  EXPECT_EQ(&v1, v1.GetViewByID(kV1ID));
  EXPECT_EQ(NULL, v1.GetViewByID(kV5ID)); // No V5 exists.

  View::Views views;
  v1.GetViewsInGroup(kGroupID, &views);
  EXPECT_EQ(2, views.size());
  View::Views::const_iterator it = std::find(views.begin(), views.end(), &v3);
  EXPECT_NE(views.end(), it);
  it = std::find(views.begin(), views.end(), &v4);
  EXPECT_NE(views.end(), it);
}

TEST_F(ViewTest, EventHandlers) {

}

TEST_F(ViewTest, Painting) {

}

}  // namespace ui