OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/media/router/mock_media_router.h" |
| 6 #include "chrome/browser/ui/toolbar/component_toolbar_actions_factory.h" |
| 7 #include "chrome/browser/ui/toolbar/media_router_action_controller.h" |
| 8 #include "chrome/browser/ui/webui/media_router/media_router_test.h" |
| 9 #include "chrome/common/pref_names.h" |
| 10 #include "testing/gmock/include/gmock/gmock.h" |
| 11 |
| 12 using testing::_; |
| 13 |
| 14 class TestComponentActionDelegate |
| 15 : public extensions::ComponentMigrationHelper::ComponentActionDelegate { |
| 16 public: |
| 17 TestComponentActionDelegate() : has_media_router_action_(false) {} |
| 18 ~TestComponentActionDelegate() {} |
| 19 |
| 20 void AddComponentAction(const std::string& action_id) override { |
| 21 if (action_id == ComponentToolbarActionsFactory::kMediaRouterActionId) { |
| 22 EXPECT_FALSE(HasComponentAction( |
| 23 ComponentToolbarActionsFactory::kMediaRouterActionId)); |
| 24 has_media_router_action_ = true; |
| 25 } |
| 26 } |
| 27 |
| 28 void RemoveComponentAction(const std::string& action_id) override { |
| 29 if (action_id == ComponentToolbarActionsFactory::kMediaRouterActionId) { |
| 30 EXPECT_TRUE(HasComponentAction( |
| 31 ComponentToolbarActionsFactory::kMediaRouterActionId)); |
| 32 has_media_router_action_ = false; |
| 33 } |
| 34 } |
| 35 |
| 36 bool HasComponentAction(const std::string& action_id) const override { |
| 37 if (action_id == ComponentToolbarActionsFactory::kMediaRouterActionId) |
| 38 return has_media_router_action_; |
| 39 return false; |
| 40 } |
| 41 |
| 42 private: |
| 43 bool has_media_router_action_; |
| 44 }; |
| 45 |
| 46 class MockMediaRouterActionObserver |
| 47 : public MediaRouterActionController::Observer { |
| 48 public: |
| 49 MockMediaRouterActionObserver() {} |
| 50 ~MockMediaRouterActionObserver() {} |
| 51 MOCK_METHOD1(OnIssueUpdated, void(const media_router::Issue* issue)); |
| 52 MOCK_METHOD1(OnLocalRouteUpdated, void(bool has_local_route)); |
| 53 }; |
| 54 |
| 55 class MediaRouterActionControllerUnitTest : public MediaRouterTest { |
| 56 public: |
| 57 MediaRouterActionControllerUnitTest() |
| 58 : fake_issue_(media_router::Issue( |
| 59 "title notification", |
| 60 "message notification", |
| 61 media_router::IssueAction(media_router::IssueAction::TYPE_DISMISS), |
| 62 std::vector<media_router::IssueAction>(), |
| 63 "route_id", |
| 64 media_router::Issue::NOTIFICATION, |
| 65 false, |
| 66 -1)), |
| 67 fake_source1_("fakeSource1"), |
| 68 fake_source2_("fakeSource2") {} |
| 69 |
| 70 ~MediaRouterActionControllerUnitTest() override {} |
| 71 |
| 72 // MediaRouterTest: |
| 73 void SetUp() override { |
| 74 MediaRouterTest::SetUp(); |
| 75 |
| 76 router_.reset(new media_router::MockMediaRouter()); |
| 77 component_action_delegate_.reset(new TestComponentActionDelegate()); |
| 78 controller_.reset( |
| 79 new MediaRouterActionController( |
| 80 profile(), router_.get(), component_action_delegate_.get())); |
| 81 |
| 82 observer_.reset(new MockMediaRouterActionObserver()); |
| 83 controller_->AddObserver(observer_.get()); |
| 84 |
| 85 local_display_route_list_.push_back( |
| 86 media_router::MediaRoute("routeId1", fake_source1_, "sinkId1", |
| 87 "description", true, std::string(), true)); |
| 88 non_local_display_route_list_.push_back( |
| 89 media_router::MediaRoute("routeId2", fake_source1_, "sinkId2", |
| 90 "description", false, std::string(), true)); |
| 91 non_local_display_route_list_.push_back( |
| 92 media_router::MediaRoute("routeId3", fake_source2_, "sinkId3", |
| 93 "description", true, std::string(), false)); |
| 94 } |
| 95 |
| 96 void TearDown() override { |
| 97 controller_->RemoveObserver(observer_.get()); |
| 98 controller_.reset(); |
| 99 component_action_delegate_.reset(); |
| 100 router_.reset(); |
| 101 observer_.reset(); |
| 102 MediaRouterTest::TearDown(); |
| 103 } |
| 104 |
| 105 bool ActionExists() { |
| 106 return component_action_delegate_->HasComponentAction( |
| 107 ComponentToolbarActionsFactory::kMediaRouterActionId); |
| 108 } |
| 109 |
| 110 MediaRouterActionController* controller() { return controller_.get(); } |
| 111 MockMediaRouterActionObserver* observer() { return observer_.get(); } |
| 112 |
| 113 const media_router::Issue* fake_issue() { |
| 114 return &fake_issue_; |
| 115 } |
| 116 const std::vector<media_router::MediaRoute>& local_display_route_list() |
| 117 const { |
| 118 return local_display_route_list_; |
| 119 } |
| 120 const std::vector<media_router::MediaRoute>& non_local_display_route_list() |
| 121 const { |
| 122 return non_local_display_route_list_; |
| 123 } |
| 124 const std::vector<media_router::MediaRoute::Id>& empty_route_id_list() const { |
| 125 return empty_route_id_list_; |
| 126 } |
| 127 |
| 128 private: |
| 129 std::unique_ptr<MediaRouterActionController> controller_; |
| 130 std::unique_ptr<media_router::MockMediaRouter> router_; |
| 131 std::unique_ptr<TestComponentActionDelegate> component_action_delegate_; |
| 132 std::unique_ptr<MockMediaRouterActionObserver> observer_; |
| 133 |
| 134 const media_router::Issue fake_issue_; |
| 135 |
| 136 // Fake Sources, used for the Routes. |
| 137 const media_router::MediaSource fake_source1_; |
| 138 const media_router::MediaSource fake_source2_; |
| 139 |
| 140 std::vector<media_router::MediaRoute> local_display_route_list_; |
| 141 std::vector<media_router::MediaRoute> non_local_display_route_list_; |
| 142 std::vector<media_router::MediaRoute::Id> empty_route_id_list_; |
| 143 |
| 144 DISALLOW_COPY_AND_ASSIGN(MediaRouterActionControllerUnitTest); |
| 145 }; |
| 146 |
| 147 TEST_F(MediaRouterActionControllerUnitTest, EphemeralIcon) { |
| 148 EXPECT_FALSE(ActionExists()); |
| 149 |
| 150 // Creating a local route should show the action icon. |
| 151 controller()->OnRoutesUpdated( |
| 152 local_display_route_list(), empty_route_id_list()); |
| 153 EXPECT_TRUE(controller()->HasLocalRoute()); |
| 154 EXPECT_TRUE(ActionExists()); |
| 155 // Removing the local route should hide the icon. |
| 156 controller()->OnRoutesUpdated( |
| 157 non_local_display_route_list(), empty_route_id_list()); |
| 158 EXPECT_FALSE(controller()->HasLocalRoute()); |
| 159 EXPECT_FALSE(ActionExists()); |
| 160 |
| 161 // Creating an issue should show the action icon. |
| 162 controller()->OnIssueUpdated(fake_issue()); |
| 163 EXPECT_TRUE(controller()->GetIssue()->Equals(*fake_issue())); |
| 164 EXPECT_TRUE(ActionExists()); |
| 165 // Removing the issue should hide the icon. |
| 166 controller()->OnIssueUpdated(nullptr); |
| 167 EXPECT_EQ(controller()->GetIssue(), nullptr); |
| 168 EXPECT_FALSE(ActionExists()); |
| 169 |
| 170 controller()->OnIssueUpdated(fake_issue()); |
| 171 controller()->OnRoutesUpdated( |
| 172 local_display_route_list(), empty_route_id_list()); |
| 173 controller()->OnIssueUpdated(nullptr); |
| 174 // When the issue disappears, the icon should remain visible if there's |
| 175 // a local route. |
| 176 EXPECT_TRUE(ActionExists()); |
| 177 controller()->OnRoutesUpdated( |
| 178 non_local_display_route_list(), empty_route_id_list()); |
| 179 EXPECT_FALSE(ActionExists()); |
| 180 } |
| 181 |
| 182 TEST_F(MediaRouterActionControllerUnitTest, ToggleAlwaysShowActionPref) { |
| 183 EXPECT_FALSE(ActionExists()); |
| 184 |
| 185 // Turn on the "always show icon" setting. |
| 186 controller()->SetAlwaysShowActionPref(true); |
| 187 EXPECT_TRUE(ActionExists()); |
| 188 controller()->OnRoutesUpdated( |
| 189 local_display_route_list(), empty_route_id_list()); |
| 190 controller()->OnRoutesUpdated( |
| 191 non_local_display_route_list(), empty_route_id_list()); |
| 192 // Removing the local route should not hide the icon. |
| 193 EXPECT_TRUE(ActionExists()); |
| 194 |
| 195 controller()->OnIssueUpdated(fake_issue()); |
| 196 controller()->OnIssueUpdated(nullptr); |
| 197 // Removing the issue should not hide the icon. |
| 198 EXPECT_TRUE(ActionExists()); |
| 199 |
| 200 controller()->SetAlwaysShowActionPref(false); |
| 201 EXPECT_FALSE(ActionExists()); |
| 202 } |
| 203 |
| 204 TEST_F(MediaRouterActionControllerUnitTest, NotifyObservers) { |
| 205 EXPECT_CALL(*observer(), OnIssueUpdated(_)).Times(1); |
| 206 controller()->OnIssueUpdated(fake_issue()); |
| 207 |
| 208 EXPECT_CALL(*observer(), OnIssueUpdated(nullptr)).Times(1); |
| 209 controller()->OnIssueUpdated(nullptr); |
| 210 |
| 211 EXPECT_CALL(*observer(), OnLocalRouteUpdated(true)).Times(1); |
| 212 controller()->OnRoutesUpdated( |
| 213 local_display_route_list(), empty_route_id_list()); |
| 214 |
| 215 EXPECT_CALL(*observer(), OnLocalRouteUpdated(false)).Times(1); |
| 216 controller()->OnRoutesUpdated( |
| 217 non_local_display_route_list(), empty_route_id_list()); |
| 218 } |
| 219 |
| 220 TEST_F(MediaRouterActionControllerUnitTest, ObserveAlwaysShowPrefChange) { |
| 221 EXPECT_FALSE(ActionExists()); |
| 222 |
| 223 profile()->GetPrefs()->SetBoolean( |
| 224 prefs::kMediaRouterAlwaysShowActionIcon, true); |
| 225 EXPECT_TRUE(ActionExists()); |
| 226 |
| 227 profile()->GetPrefs()->SetBoolean( |
| 228 prefs::kMediaRouterAlwaysShowActionIcon, false); |
| 229 EXPECT_FALSE(ActionExists()); |
| 230 } |
OLD | NEW |