OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/views/location_bar/icon_label_bubble_view.h" | 5 #include "chrome/browser/ui/views/location_bar/icon_label_bubble_view.h" |
6 | 6 |
7 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
8 #include "chrome/browser/ui/layout_constants.h" | 8 #include "chrome/browser/ui/layout_constants.h" |
9 #include "chrome/browser/ui/views/location_bar/location_bar_view.h" | 9 #include "chrome/browser/ui/views/location_bar/location_bar_view.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
11 #include "ui/events/base_event_utils.h" | |
12 #include "ui/events/gesture_detection/gesture_configuration.h" | |
13 #include "ui/events/test/event_generator.h" | |
14 #include "ui/views/animation/test/ink_drop_host_view_test_api.h" | |
15 #include "ui/views/animation/test/test_ink_drop.h" | |
11 #include "ui/views/controls/image_view.h" | 16 #include "ui/views/controls/image_view.h" |
12 #include "ui/views/test/views_test_base.h" | 17 #include "ui/views/test/views_test_base.h" |
13 | 18 |
14 #if defined(USE_ASH) | 19 #if defined(USE_ASH) |
15 #include "ui/aura/window.h" | 20 #include "ui/aura/window.h" |
16 #endif | 21 #endif |
17 | 22 |
23 using views::test::InkDropHostViewTestApi; | |
24 using views::test::TestInkDrop; | |
25 | |
18 namespace { | 26 namespace { |
19 | 27 |
20 const int kStayOpenTimeMS = 100; | 28 const int kStayOpenTimeMS = 100; |
21 const int kOpenTimeMS = 100; | 29 const int kOpenTimeMS = 100; |
22 const int kAnimationDurationMS = (kOpenTimeMS * 2) + kStayOpenTimeMS; | 30 const int kAnimationDurationMS = (kOpenTimeMS * 2) + kStayOpenTimeMS; |
23 const int kImageSize = 15; | 31 const int kImageSize = 15; |
24 const SkColor kTestColor = SkColorSetRGB(64, 64, 64); | 32 const SkColor kTestColor = SkColorSetRGB(64, 64, 64); |
25 const int kNumberOfSteps = 300; | 33 const int kNumberOfSteps = 300; |
26 | 34 |
27 class TestIconLabelBubbleView : public IconLabelBubbleView { | 35 class TestIconLabelBubbleView : public IconLabelBubbleView { |
28 public: | 36 public: |
29 enum State { | 37 enum State { |
30 GROWING, | 38 GROWING, |
31 STEADY, | 39 STEADY, |
32 SHRINKING, | 40 SHRINKING, |
33 }; | 41 }; |
34 | 42 |
35 explicit TestIconLabelBubbleView(const gfx::FontList& font_list) | 43 explicit TestIconLabelBubbleView(const gfx::FontList& font_list) |
36 : IconLabelBubbleView(font_list, false), value_(0) { | 44 : IconLabelBubbleView(font_list, false), |
45 value_(0), | |
46 is_bubble_shown_(false) { | |
37 GetImageView()->SetImageSize(gfx::Size(kImageSize, kImageSize)); | 47 GetImageView()->SetImageSize(gfx::Size(kImageSize, kImageSize)); |
38 SetLabel(base::ASCIIToUTF16("Label")); | 48 SetLabel(base::ASCIIToUTF16("Label")); |
39 } | 49 } |
40 | 50 |
41 void SetCurrentAnimationValue(int value) { | 51 void SetCurrentAnimationValue(int value) { |
42 value_ = value; | 52 value_ = value; |
43 SizeToPreferredSize(); | 53 SizeToPreferredSize(); |
44 } | 54 } |
45 | 55 |
56 void SetInkDrop(std::unique_ptr<views::InkDrop> ink_drop) { | |
bruthig
2017/05/01 22:42:18
Is this used?
spqchan
2017/05/02 22:50:05
Removed
| |
57 InkDropHostViewTestApi(this).SetInkDrop(std::move(ink_drop)); | |
58 } | |
59 | |
46 int width() const { return bounds().width(); } | 60 int width() const { return bounds().width(); } |
47 bool IsLabelVisible() const { return label()->visible(); } | 61 bool IsLabelVisible() const { return label()->visible(); } |
48 void SetLabelVisible(bool visible) { label()->SetVisible(visible); } | 62 void SetLabelVisible(bool visible) { label()->SetVisible(visible); } |
49 const gfx::Rect& GetLabelBounds() const { return label()->bounds(); } | 63 const gfx::Rect& GetLabelBounds() const { return label()->bounds(); } |
50 | 64 |
51 State state() const { | 65 State state() const { |
52 const double kOpenFraction = | 66 const double kOpenFraction = |
53 static_cast<double>(kOpenTimeMS) / kAnimationDurationMS; | 67 static_cast<double>(kOpenTimeMS) / kAnimationDurationMS; |
54 double state = value_ / (double)kNumberOfSteps; | 68 double state = value_ / (double)kNumberOfSteps; |
55 if (state < kOpenFraction) | 69 if (state < kOpenFraction) |
56 return GROWING; | 70 return GROWING; |
57 if (state > (1.0 - kOpenFraction)) | 71 if (state > (1.0 - kOpenFraction)) |
58 return SHRINKING; | 72 return SHRINKING; |
59 return STEADY; | 73 return STEADY; |
60 } | 74 } |
61 | 75 |
76 void HideBubble() { OnWidgetVisibilityChanged(nullptr, false); } | |
77 | |
78 void set_is_bubble_shown(bool is_bubble_shown) { | |
79 is_bubble_shown_ = is_bubble_shown; | |
80 } | |
81 | |
62 protected: | 82 protected: |
63 // IconLabelBubbleView: | 83 // IconLabelBubbleView: |
64 SkColor GetTextColor() const override { return kTestColor; } | 84 SkColor GetTextColor() const override { return kTestColor; } |
65 | 85 |
66 bool ShouldShowLabel() const override { | 86 bool ShouldShowLabel() const override { |
67 return !IsShrinking() || | 87 return !IsShrinking() || |
68 (width() > (image()->GetPreferredSize().width() + | 88 (width() > (image()->GetPreferredSize().width() + |
69 2 * LocationBarView::kIconInteriorPadding + | 89 2 * LocationBarView::kIconInteriorPadding + |
70 2 * GetLayoutConstant(LOCATION_BAR_ELEMENT_PADDING))); | 90 2 * GetLayoutConstant(LOCATION_BAR_ELEMENT_PADDING))); |
71 } | 91 } |
72 | 92 |
73 double WidthMultiplier() const override { | 93 double WidthMultiplier() const override { |
74 const double kOpenFraction = | 94 const double kOpenFraction = |
75 static_cast<double>(kOpenTimeMS) / kAnimationDurationMS; | 95 static_cast<double>(kOpenTimeMS) / kAnimationDurationMS; |
76 double fraction = value_ / (double)kNumberOfSteps; | 96 double fraction = value_ / (double)kNumberOfSteps; |
77 switch (state()) { | 97 switch (state()) { |
78 case GROWING: | 98 case GROWING: |
79 return fraction / kOpenFraction; | 99 return fraction / kOpenFraction; |
80 case STEADY: | 100 case STEADY: |
81 return 1.0; | 101 return 1.0; |
82 case SHRINKING: | 102 case SHRINKING: |
83 return (1.0 - fraction) / kOpenFraction; | 103 return (1.0 - fraction) / kOpenFraction; |
84 } | 104 } |
85 NOTREACHED(); | 105 NOTREACHED(); |
86 return 1.0; | 106 return 1.0; |
87 } | 107 } |
88 | 108 |
89 bool IsShrinking() const override { return state() == SHRINKING; } | 109 bool IsShrinking() const override { return state() == SHRINKING; } |
90 | 110 |
111 bool IsBubbleShown() const override { return is_bubble_shown_; } | |
112 | |
113 bool ShowBubble(const ui::Event& event) override { | |
114 return !is_bubble_shown_; | |
115 }; | |
116 | |
91 private: | 117 private: |
92 int value_; | 118 int value_; |
119 bool is_bubble_shown_; | |
93 DISALLOW_COPY_AND_ASSIGN(TestIconLabelBubbleView); | 120 DISALLOW_COPY_AND_ASSIGN(TestIconLabelBubbleView); |
94 }; | 121 }; |
95 | 122 |
96 } // namespace | 123 } // namespace |
97 | 124 |
98 class IconLabelBubbleViewTest : public views::ViewsTestBase { | 125 class IconLabelBubbleViewTest : public views::ViewsTestBase { |
99 public: | 126 public: |
100 IconLabelBubbleViewTest() | 127 IconLabelBubbleViewTest() |
101 : views::ViewsTestBase(), | 128 : views::ViewsTestBase(), |
129 widget_(nullptr), | |
130 view_(nullptr), | |
131 ink_drop_(nullptr), | |
102 steady_reached_(false), | 132 steady_reached_(false), |
103 shrinking_reached_(false), | 133 shrinking_reached_(false), |
104 minimum_size_reached_(false), | 134 minimum_size_reached_(false), |
105 previous_width_(0), | 135 previous_width_(0), |
106 initial_image_x_(0) {} | 136 initial_image_x_(0) {} |
107 ~IconLabelBubbleViewTest() override {} | 137 ~IconLabelBubbleViewTest() override {} |
108 | 138 |
109 protected: | 139 protected: |
110 // views::ViewsTestBase: | 140 // views::ViewsTestBase: |
111 void SetUp() override { | 141 void SetUp() override { |
112 views::ViewsTestBase::SetUp(); | 142 views::ViewsTestBase::SetUp(); |
113 gfx::FontList font_list; | 143 gfx::FontList font_list; |
114 view_.reset(new TestIconLabelBubbleView(font_list)); | 144 |
145 CreateWidget(); | |
146 generator_.reset(new ui::test::EventGenerator(widget_->GetNativeWindow())); | |
147 view_ = new TestIconLabelBubbleView(font_list); | |
148 view_->SetBoundsRect(gfx::Rect(0, 0, 24, 24)); | |
149 widget_->SetContentsView(view_); | |
150 | |
151 widget_->Show(); | |
152 } | |
153 | |
154 void TearDown() override { | |
155 generator_.reset(); | |
156 if (widget_ && !widget_->IsClosed()) | |
157 widget_->Close(); | |
158 | |
159 ViewsTestBase::TearDown(); | |
115 } | 160 } |
116 | 161 |
117 void VerifyWithAnimationStep(int step) { | 162 void VerifyWithAnimationStep(int step) { |
118 Reset(); | 163 Reset(); |
119 for (int value = 0; value < kNumberOfSteps; value += step) { | 164 for (int value = 0; value < kNumberOfSteps; value += step) { |
120 SetValue(value); | 165 SetValue(value); |
121 VerifyAnimationStep(); | 166 VerifyAnimationStep(); |
122 } | 167 } |
123 view_->SetLabelVisible(false); | 168 view_->SetLabelVisible(false); |
124 } | 169 } |
125 | 170 |
171 TestInkDrop* ink_drop() { return ink_drop_; } | |
172 | |
173 TestIconLabelBubbleView* view() { return view_; } | |
174 | |
175 ui::test::EventGenerator* generator() { return generator_.get(); } | |
176 | |
177 void AttachInkDrop() { | |
178 ink_drop_ = new TestInkDrop(); | |
179 InkDropHostViewTestApi(view_).SetInkDrop(base::WrapUnique(ink_drop_)); | |
180 } | |
181 | |
126 private: | 182 private: |
183 void CreateWidget() { | |
184 DCHECK(!widget_); | |
185 | |
186 widget_ = new views::Widget; | |
187 views::Widget::InitParams params = | |
188 CreateParams(views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); | |
189 params.bounds = gfx::Rect(0, 0, 200, 200); | |
190 widget_->Init(params); | |
191 } | |
192 | |
127 void Reset() { | 193 void Reset() { |
128 view_->SetLabelVisible(true); | 194 view_->SetLabelVisible(true); |
129 SetValue(0); | 195 SetValue(0); |
130 steady_reached_ = false; | 196 steady_reached_ = false; |
131 shrinking_reached_ = false; | 197 shrinking_reached_ = false; |
132 minimum_size_reached_ = false; | 198 minimum_size_reached_ = false; |
133 previous_width_ = 0; | 199 previous_width_ = 0; |
134 initial_image_x_ = GetImageBounds().x(); | 200 initial_image_x_ = GetImageBounds().x(); |
135 EXPECT_EQ(0, initial_image_x_); | 201 EXPECT_EQ(0, initial_image_x_); |
136 } | 202 } |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
195 int width() { return view_->width(); } | 261 int width() { return view_->width(); } |
196 | 262 |
197 bool IsLabelVisible() { return view_->IsLabelVisible(); } | 263 bool IsLabelVisible() { return view_->IsLabelVisible(); } |
198 | 264 |
199 const gfx::Rect& GetLabelBounds() const { return view_->GetLabelBounds(); } | 265 const gfx::Rect& GetLabelBounds() const { return view_->GetLabelBounds(); } |
200 | 266 |
201 const gfx::Rect& GetImageBounds() const { | 267 const gfx::Rect& GetImageBounds() const { |
202 return view_->GetImageView()->bounds(); | 268 return view_->GetImageView()->bounds(); |
203 } | 269 } |
204 | 270 |
205 std::unique_ptr<TestIconLabelBubbleView> view_; | 271 views::Widget* widget_; |
272 TestIconLabelBubbleView* view_; | |
273 TestInkDrop* ink_drop_; | |
274 std::unique_ptr<ui::test::EventGenerator> generator_; | |
206 | 275 |
207 bool steady_reached_; | 276 bool steady_reached_; |
208 bool shrinking_reached_; | 277 bool shrinking_reached_; |
209 bool minimum_size_reached_; | 278 bool minimum_size_reached_; |
210 int previous_width_; | 279 int previous_width_; |
211 int initial_image_x_; | 280 int initial_image_x_; |
212 }; | 281 }; |
213 | 282 |
214 // Tests layout rules for IconLabelBubbleView while simulating animation. | 283 // Tests layout rules for IconLabelBubbleView while simulating animation. |
215 // The animation is first growing the bubble from zero, then keeping its size | 284 // The animation is first growing the bubble from zero, then keeping its size |
216 // constant and finally shrinking it down to its minimum size which is the image | 285 // constant and finally shrinking it down to its minimum size which is the image |
217 // size. | 286 // size. |
218 // Various step sizes during animation simulate different possible timing. | 287 // Various step sizes during animation simulate different possible timing. |
219 TEST_F(IconLabelBubbleViewTest, AnimateLayout) { | 288 TEST_F(IconLabelBubbleViewTest, AnimateLayout) { |
220 VerifyWithAnimationStep(1); | 289 VerifyWithAnimationStep(1); |
221 VerifyWithAnimationStep(5); | 290 VerifyWithAnimationStep(5); |
222 VerifyWithAnimationStep(10); | 291 VerifyWithAnimationStep(10); |
223 VerifyWithAnimationStep(25); | 292 VerifyWithAnimationStep(25); |
224 } | 293 } |
225 | 294 |
295 TEST_F(IconLabelBubbleViewTest, InkDropState) { | |
296 AttachInkDrop(); | |
297 generator()->PressLeftButton(); | |
298 EXPECT_EQ(views::InkDropState::ACTION_PENDING, | |
299 ink_drop()->GetTargetInkDropState()); | |
300 generator()->ReleaseLeftButton(); | |
301 view()->HideBubble(); | |
302 EXPECT_EQ(views::InkDropState::HIDDEN, ink_drop()->GetTargetInkDropState()); | |
303 | |
304 // If the bubble is shown, the InkDropState should not change to | |
305 // ACTION_PENDING. | |
306 view()->set_is_bubble_shown(true); | |
bruthig
2017/05/01 22:42:18
Can the |ink_drop_| be put in the ACTIVATED state
spqchan
2017/05/02 22:50:05
Done.
| |
307 generator()->PressLeftButton(); | |
308 EXPECT_EQ(views::InkDropState::HIDDEN, ink_drop()->GetTargetInkDropState()); | |
309 | |
310 #if !defined(OS_MACOSX) | |
311 generator()->GestureTapAt(gfx::Point()); | |
bruthig
2017/05/01 22:42:18
I think this '#if defined' block would be better a
spqchan
2017/05/02 22:50:05
Done.
| |
312 EXPECT_EQ(views::InkDropState::HIDDEN, ink_drop()->GetTargetInkDropState()); | |
313 view()->set_is_bubble_shown(false); | |
314 generator()->GestureTapAt(gfx::Point()); | |
315 EXPECT_EQ(views::InkDropState::ACTIVATED, | |
316 ink_drop()->GetTargetInkDropState()); | |
317 #endif | |
318 } | |
319 | |
226 #if defined(USE_ASH) | 320 #if defined(USE_ASH) |
227 // Verifies IconLabelBubbleView::GetPreferredSize() doesn't crash when there is | 321 // Verifies IconLabelBubbleView::GetPreferredSize() doesn't crash when there is |
228 // a widget but no compositor. | 322 // a widget but no compositor. |
229 using IconLabelBubbleViewCrashTest = views::ViewsTestBase; | 323 using IconLabelBubbleViewCrashTest = views::ViewsTestBase; |
230 | 324 |
231 TEST_F(IconLabelBubbleViewCrashTest, | 325 TEST_F(IconLabelBubbleViewCrashTest, |
232 GetPreferredSizeDoesntCrashWhenNoCompositor) { | 326 GetPreferredSizeDoesntCrashWhenNoCompositor) { |
233 gfx::FontList font_list; | 327 gfx::FontList font_list; |
234 views::Widget::InitParams params = | 328 views::Widget::InitParams params = |
235 CreateParams(views::Widget::InitParams::TYPE_WINDOW); | 329 CreateParams(views::Widget::InitParams::TYPE_WINDOW); |
236 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | 330 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
237 views::Widget widget; | 331 views::Widget widget; |
238 widget.Init(params); | 332 widget.Init(params); |
239 IconLabelBubbleView* icon_label_bubble_view = | 333 IconLabelBubbleView* icon_label_bubble_view = |
240 new TestIconLabelBubbleView(font_list); | 334 new TestIconLabelBubbleView(font_list); |
241 icon_label_bubble_view->SetLabel(base::ASCIIToUTF16("x")); | 335 icon_label_bubble_view->SetLabel(base::ASCIIToUTF16("x")); |
242 widget.GetContentsView()->AddChildView(icon_label_bubble_view); | 336 widget.GetContentsView()->AddChildView(icon_label_bubble_view); |
243 aura::Window* widget_native_view = widget.GetNativeView(); | 337 aura::Window* widget_native_view = widget.GetNativeView(); |
244 // Remove the window from its parent. This means GetWidget() in | 338 // Remove the window from its parent. This means GetWidget() in |
245 // IconLabelBubbleView will return non-null, but GetWidget()->GetCompositor() | 339 // IconLabelBubbleView will return non-null, but GetWidget()->GetCompositor() |
246 // will return null. | 340 // will return null. |
247 ASSERT_TRUE(widget_native_view->parent()); | 341 ASSERT_TRUE(widget_native_view->parent()); |
248 widget_native_view->parent()->RemoveChild(widget_native_view); | 342 widget_native_view->parent()->RemoveChild(widget_native_view); |
249 static_cast<views::View*>(icon_label_bubble_view)->GetPreferredSize(); | 343 static_cast<views::View*>(icon_label_bubble_view)->GetPreferredSize(); |
250 } | 344 } |
251 #endif | 345 #endif |
OLD | NEW |