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