| 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 "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 9 #include "ui/views/controls/image_view.h" | 10 #include "ui/views/controls/image_view.h" |
| 10 #include "ui/views/test/views_test_base.h" | 11 #include "ui/views/test/views_test_base.h" |
| 11 | 12 |
| 12 namespace { | 13 namespace { |
| 13 | 14 |
| 14 const int kStayOpenTimeMS = 100; | 15 const int kStayOpenTimeMS = 100; |
| 15 const int kOpenTimeMS = 100; | 16 const int kOpenTimeMS = 100; |
| 16 const int kAnimationDurationMS = (kOpenTimeMS * 2) + kStayOpenTimeMS; | 17 const int kAnimationDurationMS = (kOpenTimeMS * 2) + kStayOpenTimeMS; |
| 17 const int kImageSize = 15; | 18 const int kImageSize = 15; |
| 18 const SkColor kTestColor = SkColorSetRGB(64, 64, 64); | 19 const SkColor kTestColor = SkColorSetRGB(64, 64, 64); |
| 19 const int kNumberOfSteps = 300; | 20 const int kNumberOfSteps = 300; |
| 20 | 21 |
| 21 class TestIconLabelBubbleView : public IconLabelBubbleView { | 22 class TestIconLabelBubbleView : public IconLabelBubbleView { |
| 22 public: | 23 public: |
| 23 enum State { | 24 enum State { |
| 24 GROWING, | 25 GROWING, |
| 25 STEADY, | 26 STEADY, |
| 26 SHRINKING, | 27 SHRINKING, |
| 27 }; | 28 }; |
| 28 | 29 |
| 29 TestIconLabelBubbleView(const gfx::FontList& font_list, SkColor color) | 30 explicit TestIconLabelBubbleView(const gfx::FontList& font_list) |
| 30 : IconLabelBubbleView(0, font_list, color, false), value_(0) { | 31 : IconLabelBubbleView(font_list, false), value_(0) { |
| 31 GetImageView()->SetImageSize(gfx::Size(kImageSize, kImageSize)); | 32 GetImageView()->SetImageSize(gfx::Size(kImageSize, kImageSize)); |
| 32 SetLabel(base::ASCIIToUTF16("Label")); | 33 SetLabel(base::ASCIIToUTF16("Label")); |
| 33 } | 34 } |
| 34 | 35 |
| 35 void SetCurrentAnimationValue(int value) { | 36 void SetCurrentAnimationValue(int value) { |
| 36 value_ = value; | 37 value_ = value; |
| 37 SizeToPreferredSize(); | 38 SizeToPreferredSize(); |
| 38 } | 39 } |
| 39 | 40 |
| 40 int width() const { return bounds().width(); } | 41 int width() const { return bounds().width(); } |
| 41 bool IsLabelVisible() const { return label()->visible(); } | 42 bool IsLabelVisible() const { return label()->visible(); } |
| 42 void SetLabelVisible(bool visible) { label()->SetVisible(visible); } | 43 void SetLabelVisible(bool visible) { label()->SetVisible(visible); } |
| 43 const gfx::Rect& GetLabelBounds() const { return label()->bounds(); } | 44 const gfx::Rect& GetLabelBounds() const { return label()->bounds(); } |
| 44 | 45 |
| 45 State state() const { | 46 State state() const { |
| 46 const double kOpenFraction = | 47 const double kOpenFraction = |
| 47 static_cast<double>(kOpenTimeMS) / kAnimationDurationMS; | 48 static_cast<double>(kOpenTimeMS) / kAnimationDurationMS; |
| 48 double state = value_ / (double)kNumberOfSteps; | 49 double state = value_ / (double)kNumberOfSteps; |
| 49 if (state < kOpenFraction) | 50 if (state < kOpenFraction) |
| 50 return GROWING; | 51 return GROWING; |
| 51 if (state > (1.0 - kOpenFraction)) | 52 if (state > (1.0 - kOpenFraction)) |
| 52 return SHRINKING; | 53 return SHRINKING; |
| 53 return STEADY; | 54 return STEADY; |
| 54 } | 55 } |
| 55 | 56 |
| 56 protected: | 57 protected: |
| 57 // IconLabelBubbleView: | 58 // IconLabelBubbleView: |
| 58 SkColor GetTextColor() const override { return kTestColor; } | 59 SkColor GetTextColor() const override { return kTestColor; } |
| 59 SkColor GetBorderColor() const override { return kTestColor; } | |
| 60 | 60 |
| 61 bool ShouldShowBackground() const override { | 61 bool ShouldShowLabel() const override { |
| 62 return !IsShrinking() || | 62 return !IsShrinking() || |
| 63 (width() >= MinimumWidthForImageWithBackgroundShown()); | 63 (width() > (image()->GetPreferredSize().width() + |
| 64 2 * GetLayoutConstant(LOCATION_BAR_HORIZONTAL_PADDING))); |
| 64 } | 65 } |
| 65 | 66 |
| 66 double WidthMultiplier() const override { | 67 double WidthMultiplier() const override { |
| 67 const double kOpenFraction = | 68 const double kOpenFraction = |
| 68 static_cast<double>(kOpenTimeMS) / kAnimationDurationMS; | 69 static_cast<double>(kOpenTimeMS) / kAnimationDurationMS; |
| 69 double fraction = value_ / (double)kNumberOfSteps; | 70 double fraction = value_ / (double)kNumberOfSteps; |
| 70 switch (state()) { | 71 switch (state()) { |
| 71 case GROWING: | 72 case GROWING: |
| 72 return fraction / kOpenFraction; | 73 return fraction / kOpenFraction; |
| 73 case STEADY: | 74 case STEADY: |
| (...skipping 23 matching lines...) Expand all Loading... |
| 97 minimum_size_reached_(false), | 98 minimum_size_reached_(false), |
| 98 previous_width_(0), | 99 previous_width_(0), |
| 99 initial_image_x_(0) {} | 100 initial_image_x_(0) {} |
| 100 ~IconLabelBubbleViewTest() override {} | 101 ~IconLabelBubbleViewTest() override {} |
| 101 | 102 |
| 102 protected: | 103 protected: |
| 103 // views::ViewsTestBase: | 104 // views::ViewsTestBase: |
| 104 void SetUp() override { | 105 void SetUp() override { |
| 105 views::ViewsTestBase::SetUp(); | 106 views::ViewsTestBase::SetUp(); |
| 106 gfx::FontList font_list; | 107 gfx::FontList font_list; |
| 107 view_.reset(new TestIconLabelBubbleView(font_list, kTestColor)); | 108 view_.reset(new TestIconLabelBubbleView(font_list)); |
| 108 } | 109 } |
| 109 | 110 |
| 110 void VerifyWithAnimationStep(int step) { | 111 void VerifyWithAnimationStep(int step) { |
| 111 Reset(); | 112 Reset(); |
| 112 for (int value = 0; value < kNumberOfSteps; value += step) { | 113 for (int value = 0; value < kNumberOfSteps; value += step) { |
| 113 SetValue(value); | 114 SetValue(value); |
| 114 VerifyAnimationStep(); | 115 VerifyAnimationStep(); |
| 115 } | 116 } |
| 116 view_->SetLabelVisible(false); | 117 view_->SetLabelVisible(false); |
| 117 } | 118 } |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 // The animation is first growing the bubble from zero, then keeping its size | 210 // The animation is first growing the bubble from zero, then keeping its size |
| 210 // constant and finally shrinking it down to its minimum size which is the image | 211 // constant and finally shrinking it down to its minimum size which is the image |
| 211 // size. | 212 // size. |
| 212 // Various step sizes during animation simulate different possible timing. | 213 // Various step sizes during animation simulate different possible timing. |
| 213 TEST_F(IconLabelBubbleViewTest, AnimateLayout) { | 214 TEST_F(IconLabelBubbleViewTest, AnimateLayout) { |
| 214 VerifyWithAnimationStep(1); | 215 VerifyWithAnimationStep(1); |
| 215 VerifyWithAnimationStep(5); | 216 VerifyWithAnimationStep(5); |
| 216 VerifyWithAnimationStep(10); | 217 VerifyWithAnimationStep(10); |
| 217 VerifyWithAnimationStep(25); | 218 VerifyWithAnimationStep(25); |
| 218 } | 219 } |
| OLD | NEW |