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