Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(473)

Side by Side Diff: chrome/browser/ui/views/location_bar/icon_label_bubble_view_unittest.cc

Issue 2720183002: [Views] Update ink drop for omnibox icons (Closed)
Patch Set: Nits Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/test/event_generator.h"
12 #include "ui/views/animation/test/ink_drop_host_view_test_api.h"
13 #include "ui/views/animation/test/test_ink_drop.h"
11 #include "ui/views/controls/image_view.h" 14 #include "ui/views/controls/image_view.h"
12 #include "ui/views/test/views_test_base.h" 15 #include "ui/views/test/views_test_base.h"
13 16
14 #if defined(USE_ASH) 17 #if defined(USE_ASH)
15 #include "ui/aura/window.h" 18 #include "ui/aura/window.h"
16 #endif 19 #endif
17 20
18 namespace { 21 namespace {
19 22
23 using views::test::InkDropHostViewTestApi;
24 using views::test::TestInkDrop;
25
20 const int kStayOpenTimeMS = 100; 26 const int kStayOpenTimeMS = 100;
21 const int kOpenTimeMS = 100; 27 const int kOpenTimeMS = 100;
22 const int kAnimationDurationMS = (kOpenTimeMS * 2) + kStayOpenTimeMS; 28 const int kAnimationDurationMS = (kOpenTimeMS * 2) + kStayOpenTimeMS;
23 const int kImageSize = 15; 29 const int kImageSize = 15;
24 const SkColor kTestColor = SkColorSetRGB(64, 64, 64); 30 const SkColor kTestColor = SkColorSetRGB(64, 64, 64);
25 const int kNumberOfSteps = 300; 31 const int kNumberOfSteps = 300;
26 32
27 class TestIconLabelBubbleView : public IconLabelBubbleView { 33 class TestIconLabelBubbleView : public IconLabelBubbleView {
28 public: 34 public:
29 enum State { 35 enum State {
30 GROWING, 36 GROWING,
31 STEADY, 37 STEADY,
32 SHRINKING, 38 SHRINKING,
33 }; 39 };
34 40
35 explicit TestIconLabelBubbleView(const gfx::FontList& font_list) 41 explicit TestIconLabelBubbleView(const gfx::FontList& font_list)
36 : IconLabelBubbleView(font_list, false), value_(0) { 42 : IconLabelBubbleView(font_list, false), value_(0) {
37 GetImageView()->SetImageSize(gfx::Size(kImageSize, kImageSize)); 43 GetImageView()->SetImageSize(gfx::Size(kImageSize, kImageSize));
38 SetLabel(base::ASCIIToUTF16("Label")); 44 SetLabel(base::ASCIIToUTF16("Label"));
39 } 45 }
40 46
41 void SetCurrentAnimationValue(int value) { 47 void SetCurrentAnimationValue(int value) {
42 value_ = value; 48 value_ = value;
43 SizeToPreferredSize(); 49 SizeToPreferredSize();
44 } 50 }
45 51
52 void SetInkDrop(std::unique_ptr<views::InkDrop> ink_drop) {
53 InkDropHostViewTestApi(this).SetInkDrop(std::move(ink_drop));
54 }
55
46 int width() const { return bounds().width(); } 56 int width() const { return bounds().width(); }
47 bool IsLabelVisible() const { return label()->visible(); } 57 bool IsLabelVisible() const { return label()->visible(); }
48 void SetLabelVisible(bool visible) { label()->SetVisible(visible); } 58 void SetLabelVisible(bool visible) { label()->SetVisible(visible); }
49 const gfx::Rect& GetLabelBounds() const { return label()->bounds(); } 59 const gfx::Rect& GetLabelBounds() const { return label()->bounds(); }
50 60
51 State state() const { 61 State state() const {
52 const double kOpenFraction = 62 const double kOpenFraction =
53 static_cast<double>(kOpenTimeMS) / kAnimationDurationMS; 63 static_cast<double>(kOpenTimeMS) / kAnimationDurationMS;
54 double state = value_ / (double)kNumberOfSteps; 64 double state = value_ / (double)kNumberOfSteps;
55 if (state < kOpenFraction) 65 if (state < kOpenFraction)
56 return GROWING; 66 return GROWING;
57 if (state > (1.0 - kOpenFraction)) 67 if (state > (1.0 - kOpenFraction))
58 return SHRINKING; 68 return SHRINKING;
59 return STEADY; 69 return STEADY;
60 } 70 }
61 71
72 void OnWidgetVisibilityChanged(views::Widget* widget, bool visible) override {
bruthig 2017/03/24 20:48:03 Is this needed?
spqchan 2017/03/27 23:41:51 Removed
73 IconLabelBubbleView::OnWidgetVisibilityChanged(widget, visible);
74 };
75
62 protected: 76 protected:
63 // IconLabelBubbleView: 77 // IconLabelBubbleView:
64 SkColor GetTextColor() const override { return kTestColor; } 78 SkColor GetTextColor() const override { return kTestColor; }
65 79
66 bool ShouldShowLabel() const override { 80 bool ShouldShowLabel() const override {
67 return !IsShrinking() || 81 return !IsShrinking() ||
68 (width() > (image()->GetPreferredSize().width() + 82 (width() > (image()->GetPreferredSize().width() +
69 2 * LocationBarView::kIconInteriorPadding + 83 2 * LocationBarView::kIconInteriorPadding +
70 2 * GetLayoutConstant(LOCATION_BAR_ELEMENT_PADDING))); 84 2 * GetLayoutConstant(LOCATION_BAR_ELEMENT_PADDING)));
71 } 85 }
(...skipping 20 matching lines...) Expand all
92 int value_; 106 int value_;
93 DISALLOW_COPY_AND_ASSIGN(TestIconLabelBubbleView); 107 DISALLOW_COPY_AND_ASSIGN(TestIconLabelBubbleView);
94 }; 108 };
95 109
96 } // namespace 110 } // namespace
97 111
98 class IconLabelBubbleViewTest : public views::ViewsTestBase { 112 class IconLabelBubbleViewTest : public views::ViewsTestBase {
99 public: 113 public:
100 IconLabelBubbleViewTest() 114 IconLabelBubbleViewTest()
101 : views::ViewsTestBase(), 115 : views::ViewsTestBase(),
116 widget_(nullptr),
117 view_(nullptr),
118 ink_drop_(nullptr),
102 steady_reached_(false), 119 steady_reached_(false),
103 shrinking_reached_(false), 120 shrinking_reached_(false),
104 minimum_size_reached_(false), 121 minimum_size_reached_(false),
105 previous_width_(0), 122 previous_width_(0),
106 initial_image_x_(0) {} 123 initial_image_x_(0) {}
107 ~IconLabelBubbleViewTest() override {} 124 ~IconLabelBubbleViewTest() override {}
108 125
109 protected: 126 protected:
110 // views::ViewsTestBase: 127 // views::ViewsTestBase:
111 void SetUp() override { 128 void SetUp() override {
112 views::ViewsTestBase::SetUp(); 129 views::ViewsTestBase::SetUp();
113 gfx::FontList font_list; 130 gfx::FontList font_list;
114 view_.reset(new TestIconLabelBubbleView(font_list)); 131
132 CreateWidget();
133 generator_.reset(new ui::test::EventGenerator(widget_->GetNativeWindow()));
134 view_ = new TestIconLabelBubbleView(font_list);
135 view_->SetBoundsRect(gfx::Rect(0, 0, 24, 24));
136 widget_->SetContentsView(view_);
137
138 widget_->Show();
139 }
140
141 void TearDown() override {
142 generator_.reset();
143 if (widget_ && !widget_->IsClosed())
144 widget_->Close();
145
146 ViewsTestBase::TearDown();
115 } 147 }
116 148
117 void VerifyWithAnimationStep(int step) { 149 void VerifyWithAnimationStep(int step) {
118 Reset(); 150 Reset();
119 for (int value = 0; value < kNumberOfSteps; value += step) { 151 for (int value = 0; value < kNumberOfSteps; value += step) {
120 SetValue(value); 152 SetValue(value);
121 VerifyAnimationStep(); 153 VerifyAnimationStep();
122 } 154 }
123 view_->SetLabelVisible(false); 155 view_->SetLabelVisible(false);
124 } 156 }
125 157
158 TestInkDrop* ink_drop() { return ink_drop_; }
159
160 TestIconLabelBubbleView* view() { return view_; }
161
162 ui::test::EventGenerator* generator() { return generator_.get(); }
163
164 void AttachInkDrop() {
165 ink_drop_ = new TestInkDrop();
166 InkDropHostViewTestApi(view_).SetInkDrop(base::WrapUnique(ink_drop_));
167 }
168
126 private: 169 private:
170 void CreateWidget() {
171 DCHECK(!widget_);
172
173 widget_ = new views::Widget;
174 views::Widget::InitParams params =
175 CreateParams(views::Widget::InitParams::TYPE_WINDOW_FRAMELESS);
176 params.bounds = gfx::Rect(0, 0, 200, 200);
177 widget_->Init(params);
178 }
179
127 void Reset() { 180 void Reset() {
128 view_->SetLabelVisible(true); 181 view_->SetLabelVisible(true);
129 SetValue(0); 182 SetValue(0);
130 steady_reached_ = false; 183 steady_reached_ = false;
131 shrinking_reached_ = false; 184 shrinking_reached_ = false;
132 minimum_size_reached_ = false; 185 minimum_size_reached_ = false;
133 previous_width_ = 0; 186 previous_width_ = 0;
134 initial_image_x_ = GetImageBounds().x(); 187 initial_image_x_ = GetImageBounds().x();
135 EXPECT_EQ(0, initial_image_x_); 188 EXPECT_EQ(0, initial_image_x_);
136 } 189 }
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
195 int width() { return view_->width(); } 248 int width() { return view_->width(); }
196 249
197 bool IsLabelVisible() { return view_->IsLabelVisible(); } 250 bool IsLabelVisible() { return view_->IsLabelVisible(); }
198 251
199 const gfx::Rect& GetLabelBounds() const { return view_->GetLabelBounds(); } 252 const gfx::Rect& GetLabelBounds() const { return view_->GetLabelBounds(); }
200 253
201 const gfx::Rect& GetImageBounds() const { 254 const gfx::Rect& GetImageBounds() const {
202 return view_->GetImageView()->bounds(); 255 return view_->GetImageView()->bounds();
203 } 256 }
204 257
205 std::unique_ptr<TestIconLabelBubbleView> view_; 258 views::Widget* widget_;
259 TestIconLabelBubbleView* view_;
260 TestInkDrop* ink_drop_;
261 std::unique_ptr<ui::test::EventGenerator> generator_;
206 262
207 bool steady_reached_; 263 bool steady_reached_;
208 bool shrinking_reached_; 264 bool shrinking_reached_;
209 bool minimum_size_reached_; 265 bool minimum_size_reached_;
210 int previous_width_; 266 int previous_width_;
211 int initial_image_x_; 267 int initial_image_x_;
212 }; 268 };
213 269
214 // Tests layout rules for IconLabelBubbleView while simulating animation. 270 // Tests layout rules for IconLabelBubbleView while simulating animation.
215 // The animation is first growing the bubble from zero, then keeping its size 271 // 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 272 // constant and finally shrinking it down to its minimum size which is the image
217 // size. 273 // size.
218 // Various step sizes during animation simulate different possible timing. 274 // Various step sizes during animation simulate different possible timing.
219 TEST_F(IconLabelBubbleViewTest, AnimateLayout) { 275 TEST_F(IconLabelBubbleViewTest, AnimateLayout) {
220 VerifyWithAnimationStep(1); 276 VerifyWithAnimationStep(1);
221 VerifyWithAnimationStep(5); 277 VerifyWithAnimationStep(5);
222 VerifyWithAnimationStep(10); 278 VerifyWithAnimationStep(10);
223 VerifyWithAnimationStep(25); 279 VerifyWithAnimationStep(25);
224 } 280 }
225 281
282 TEST_F(IconLabelBubbleViewTest, InkDropState) {
283 AttachInkDrop();
284 generator()->ClickLeftButton();
285 EXPECT_EQ(views::InkDropState::ACTION_PENDING,
286 ink_drop()->GetTargetInkDropState());
287 view()->OnWidgetVisibilityChanged(nullptr, false);
288 EXPECT_EQ(views::InkDropState::HIDDEN, ink_drop()->GetTargetInkDropState());
289 }
290
226 #if defined(USE_ASH) 291 #if defined(USE_ASH)
227 // Verifies IconLabelBubbleView::GetPreferredSize() doesn't crash when there is 292 // Verifies IconLabelBubbleView::GetPreferredSize() doesn't crash when there is
228 // a widget but no compositor. 293 // a widget but no compositor.
229 using IconLabelBubbleViewCrashTest = views::ViewsTestBase; 294 using IconLabelBubbleViewCrashTest = views::ViewsTestBase;
230 295
231 TEST_F(IconLabelBubbleViewCrashTest, 296 TEST_F(IconLabelBubbleViewCrashTest,
232 GetPreferredSizeDoesntCrashWhenNoCompositor) { 297 GetPreferredSizeDoesntCrashWhenNoCompositor) {
233 gfx::FontList font_list; 298 gfx::FontList font_list;
234 views::Widget::InitParams params = 299 views::Widget::InitParams params =
235 CreateParams(views::Widget::InitParams::TYPE_WINDOW); 300 CreateParams(views::Widget::InitParams::TYPE_WINDOW);
236 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; 301 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
237 views::Widget widget; 302 views::Widget widget;
238 widget.Init(params); 303 widget.Init(params);
239 IconLabelBubbleView* icon_label_bubble_view = 304 IconLabelBubbleView* icon_label_bubble_view =
240 new TestIconLabelBubbleView(font_list); 305 new TestIconLabelBubbleView(font_list);
241 icon_label_bubble_view->SetLabel(base::ASCIIToUTF16("x")); 306 icon_label_bubble_view->SetLabel(base::ASCIIToUTF16("x"));
242 widget.GetContentsView()->AddChildView(icon_label_bubble_view); 307 widget.GetContentsView()->AddChildView(icon_label_bubble_view);
243 aura::Window* widget_native_view = widget.GetNativeView(); 308 aura::Window* widget_native_view = widget.GetNativeView();
244 // Remove the window from its parent. This means GetWidget() in 309 // Remove the window from its parent. This means GetWidget() in
245 // IconLabelBubbleView will return non-null, but GetWidget()->GetCompositor() 310 // IconLabelBubbleView will return non-null, but GetWidget()->GetCompositor()
246 // will return null. 311 // will return null.
247 ASSERT_TRUE(widget_native_view->parent()); 312 ASSERT_TRUE(widget_native_view->parent());
248 widget_native_view->parent()->RemoveChild(widget_native_view); 313 widget_native_view->parent()->RemoveChild(widget_native_view);
249 static_cast<views::View*>(icon_label_bubble_view)->GetPreferredSize(); 314 static_cast<views::View*>(icon_label_bubble_view)->GetPreferredSize();
250 } 315 }
251 #endif 316 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698