| 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/intent_picker_bubble_view.h" | 5 #include "chrome/browser/ui/views/intent_picker_bubble_view.h" |
| 6 | 6 |
| 7 #include <string> |
| 8 |
| 7 #include "base/bind.h" | 9 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| 9 #include "base/callback.h" | 11 #include "base/callback.h" |
| 10 #include "base/macros.h" | 12 #include "base/macros.h" |
| 11 #include "chrome/browser/chromeos/arc/arc_navigation_throttle.h" | 13 #include "chrome/browser/chromeos/arc/arc_navigation_throttle.h" |
| 12 #include "chrome/test/base/browser_with_test_window_test.h" | 14 #include "chrome/test/base/browser_with_test_window_test.h" |
| 13 #include "content/public/browser/web_contents.h" | 15 #include "content/public/browser/web_contents.h" |
| 14 #include "ui/base/resource/resource_bundle.h" | 16 #include "ui/base/resource/resource_bundle.h" |
| 15 #include "ui/gfx/image/image.h" | 17 #include "ui/gfx/image/image.h" |
| 16 #include "ui/views/controls/button/button.h" | 18 #include "ui/views/controls/button/button.h" |
| 17 #include "ui/views/controls/button/label_button.h" | |
| 18 #include "ui/views/controls/scroll_view.h" | 19 #include "ui/views/controls/scroll_view.h" |
| 19 #include "ui/views/resources/grit/views_resources.h" | 20 #include "ui/views/resources/grit/views_resources.h" |
| 20 #include "url/gurl.h" | 21 #include "url/gurl.h" |
| 21 | 22 |
| 22 using NameAndIcon = arc::ArcNavigationThrottle::NameAndIcon; | 23 using AppInfo = arc::ArcNavigationThrottle::AppInfo; |
| 23 using content::WebContents; | 24 using content::WebContents; |
| 24 using content::OpenURLParams; | 25 using content::OpenURLParams; |
| 25 using content::Referrer; | 26 using content::Referrer; |
| 26 | 27 |
| 28 class MousePressedEvent : public ui::Event { |
| 29 public: |
| 30 MousePressedEvent() : Event(ui::ET_MOUSE_PRESSED, base::TimeTicks(), 0) {} |
| 31 ~MousePressedEvent() override {} |
| 32 }; |
| 33 |
| 27 class IntentPickerBubbleViewTest : public BrowserWithTestWindowTest { | 34 class IntentPickerBubbleViewTest : public BrowserWithTestWindowTest { |
| 28 public: | 35 public: |
| 29 IntentPickerBubbleViewTest() = default; | 36 IntentPickerBubbleViewTest() = default; |
| 30 | 37 |
| 31 void TearDown() override { | 38 void TearDown() override { |
| 32 // Make sure the bubble is destroyed before the profile to avoid a crash. | 39 // Make sure the bubble is destroyed before the profile to avoid a crash. |
| 33 bubble_.reset(); | 40 bubble_.reset(); |
| 34 | 41 |
| 35 BrowserWithTestWindowTest::TearDown(); | 42 BrowserWithTestWindowTest::TearDown(); |
| 36 } | 43 } |
| 37 | 44 |
| 38 protected: | 45 protected: |
| 39 void CreateBubbleView(bool use_icons) { | 46 void CreateBubbleView(bool use_icons) { |
| 40 // Pushing a couple of fake apps just to check they are created on the UI. | 47 // Pushing a couple of fake apps just to check they are created on the UI. |
| 41 app_info_.emplace_back("dank app 1", gfx::Image()); | 48 app_info_.emplace_back(AppInfo(gfx::Image(), "package_1", "dank app 1")); |
| 42 app_info_.emplace_back("dank app 2", gfx::Image()); | 49 app_info_.emplace_back(AppInfo(gfx::Image(), "package_2", "dank_app_2")); |
| 43 | 50 |
| 44 if (use_icons) | 51 if (use_icons) |
| 45 FillAppListWithDummyIcons(); | 52 FillAppListWithDummyIcons(); |
| 46 | 53 |
| 47 // We create |web_contents| since the Bubble UI has an Observer that | 54 // We create |web_contents| since the Bubble UI has an Observer that |
| 48 // depends on this, otherwise it wouldn't work. | 55 // depends on this, otherwise it wouldn't work. |
| 49 GURL url("http://www.google.com"); | 56 GURL url("http://www.google.com"); |
| 50 WebContents* web_contents = browser()->OpenURL( | 57 WebContents* web_contents = browser()->OpenURL( |
| 51 OpenURLParams(url, Referrer(), WindowOpenDisposition::CURRENT_TAB, | 58 OpenURLParams(url, Referrer(), WindowOpenDisposition::CURRENT_TAB, |
| 52 ui::PAGE_TRANSITION_TYPED, false)); | 59 ui::PAGE_TRANSITION_TYPED, false)); |
| 53 | 60 |
| 54 bubble_ = IntentPickerBubbleView::CreateBubbleView( | 61 bubble_ = IntentPickerBubbleView::CreateBubbleView( |
| 55 app_info_, base::Bind(&IntentPickerBubbleViewTest::OnBubbleClosed, | 62 app_info_, base::Bind(&IntentPickerBubbleViewTest::OnBubbleClosed, |
| 56 base::Unretained(this)), | 63 base::Unretained(this)), |
| 57 web_contents); | 64 web_contents); |
| 58 } | 65 } |
| 59 | 66 |
| 60 void FillAppListWithDummyIcons() { | 67 void FillAppListWithDummyIcons() { |
| 61 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 68 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| 62 gfx::Image dummy_icon = rb.GetImageNamed(IDR_CLOSE); | 69 gfx::Image dummy_icon = rb.GetImageNamed(IDR_CLOSE); |
| 63 for (auto& app : app_info_) | 70 for (auto& app : app_info_) |
| 64 app.second = dummy_icon; | 71 app.icon = dummy_icon; |
| 65 } | 72 } |
| 66 | 73 |
| 67 // Dummy method to be called upon bubble closing. | 74 // Dummy method to be called upon bubble closing. |
| 68 void OnBubbleClosed(size_t selected_app_tag, | 75 void OnBubbleClosed(std::string selected_app_package, |
| 69 arc::ArcNavigationThrottle::CloseReason close_reason) {} | 76 arc::ArcNavigationThrottle::CloseReason close_reason) {} |
| 70 | 77 |
| 71 std::unique_ptr<IntentPickerBubbleView> bubble_; | 78 std::unique_ptr<IntentPickerBubbleView> bubble_; |
| 72 std::vector<NameAndIcon> app_info_; | 79 std::vector<AppInfo> app_info_; |
| 73 | 80 |
| 74 private: | 81 private: |
| 75 DISALLOW_COPY_AND_ASSIGN(IntentPickerBubbleViewTest); | 82 DISALLOW_COPY_AND_ASSIGN(IntentPickerBubbleViewTest); |
| 76 }; | 83 }; |
| 77 | 84 |
| 78 // Verifies that we didn't set up an image for any LabelButton. | 85 // Verifies that we didn't set up an image for any LabelButton. |
| 79 TEST_F(IntentPickerBubbleViewTest, NullIcons) { | 86 TEST_F(IntentPickerBubbleViewTest, NullIcons) { |
| 80 CreateBubbleView(false); | 87 CreateBubbleView(false); |
| 81 size_t size = bubble_->app_info_.size(); | 88 size_t size = bubble_->app_info_.size(); |
| 82 for (size_t i = 0; i < size; ++i) { | 89 for (size_t i = 0; i < size; ++i) { |
| 83 views::LabelButton* app = bubble_->GetLabelButtonAt(i); | 90 gfx::ImageSkia image = bubble_->GetAppImageForTesting(i); |
| 84 EXPECT_TRUE( | 91 EXPECT_TRUE(image.isNull()) << i; |
| 85 app->GetImage(views::Button::ButtonState::STATE_NORMAL).isNull()) << i; | |
| 86 } | 92 } |
| 87 } | 93 } |
| 88 | 94 |
| 89 // Verifies that all the icons contain a non-null icon. | 95 // Verifies that all the icons contain a non-null icon. |
| 90 TEST_F(IntentPickerBubbleViewTest, NonNullIcons) { | 96 TEST_F(IntentPickerBubbleViewTest, NonNullIcons) { |
| 91 CreateBubbleView(true); | 97 CreateBubbleView(true); |
| 92 size_t size = bubble_->app_info_.size(); | 98 size_t size = bubble_->app_info_.size(); |
| 93 for (size_t i = 0; i < size; ++i) { | 99 for (size_t i = 0; i < size; ++i) { |
| 94 views::LabelButton* app = bubble_->GetLabelButtonAt(i); | 100 gfx::ImageSkia image = bubble_->GetAppImageForTesting(i); |
| 95 EXPECT_FALSE( | 101 EXPECT_FALSE(image.isNull()) << i; |
| 96 app->GetImage(views::Button::ButtonState::STATE_NORMAL).isNull()) << i; | |
| 97 } | 102 } |
| 98 } | 103 } |
| 99 | 104 |
| 100 // Verifies that the bubble contains as many rows as the input. Populated the | 105 // Verifies that the bubble contains as many rows as the input. Populated the |
| 101 // bubble with an arbitrary image in every row. | 106 // bubble with an arbitrary image in every row. |
| 102 TEST_F(IntentPickerBubbleViewTest, LabelsPtrVectorSize) { | 107 TEST_F(IntentPickerBubbleViewTest, LabelsPtrVectorSize) { |
| 103 CreateBubbleView(true); | 108 CreateBubbleView(true); |
| 104 EXPECT_EQ(app_info_.size(), bubble_->app_info_.size()); | 109 EXPECT_EQ(app_info_.size(), bubble_->app_info_.size()); |
| 105 } | 110 } |
| 111 |
| 112 // Verifies the InkDrop state when creating a new bubble. |
| 113 TEST_F(IntentPickerBubbleViewTest, VerifyStartingInkDrop) { |
| 114 CreateBubbleView(true); |
| 115 size_t size = bubble_->app_info_.size(); |
| 116 for (size_t i = 0; i < size; ++i) { |
| 117 EXPECT_EQ(bubble_->GetInkDropStateForTesting(i), |
| 118 views::InkDropState::HIDDEN); |
| 119 } |
| 120 } |
| 121 |
| 122 // Press each button at a time and make sure it goes to ACTIVATED state, |
| 123 // followed by HIDDEN state after selecting other button. |
| 124 TEST_F(IntentPickerBubbleViewTest, InkDropStateTransition) { |
| 125 CreateBubbleView(true); |
| 126 size_t size = bubble_->app_info_.size(); |
| 127 for (size_t i = 0; i < size; ++i) { |
| 128 bubble_->PressButtonForTesting((i + 1) % size, MousePressedEvent()); |
| 129 EXPECT_EQ(bubble_->GetInkDropStateForTesting(i), |
| 130 views::InkDropState::HIDDEN); |
| 131 EXPECT_EQ(bubble_->GetInkDropStateForTesting((i + 1) % size), |
| 132 views::InkDropState::ACTIVATED); |
| 133 } |
| 134 } |
| 135 |
| 136 // Arbitrary press the first button twice, check that the InkDropState remains |
| 137 // the same. |
| 138 TEST_F(IntentPickerBubbleViewTest, PressButtonTwice) { |
| 139 CreateBubbleView(true); |
| 140 EXPECT_EQ(bubble_->GetInkDropStateForTesting(0), views::InkDropState::HIDDEN); |
| 141 bubble_->PressButtonForTesting(0, MousePressedEvent()); |
| 142 EXPECT_EQ(bubble_->GetInkDropStateForTesting(0), |
| 143 views::InkDropState::ACTIVATED); |
| 144 bubble_->PressButtonForTesting(0, MousePressedEvent()); |
| 145 EXPECT_EQ(bubble_->GetInkDropStateForTesting(0), |
| 146 views::InkDropState::ACTIVATED); |
| 147 } |
| OLD | NEW |