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 <memory> |
| 6 #include <string> |
| 7 |
| 8 #include "chrome/browser/media/router/mock_media_router.h" |
| 9 #include "chrome/browser/ui/toolbar/component_toolbar_actions_factory.h" |
| 10 #include "chrome/browser/ui/toolbar/media_router_action_controller.h" |
| 11 #include "chrome/browser/ui/webui/media_router/media_router_test.h" |
| 12 #include "chrome/common/pref_names.h" |
| 13 #include "testing/gmock/include/gmock/gmock.h" |
| 14 |
| 15 using extensions::ComponentMigrationHelper; |
| 16 |
| 17 class FakeComponentActionDelegate |
| 18 : public ComponentMigrationHelper::ComponentActionDelegate { |
| 19 public: |
| 20 FakeComponentActionDelegate() {} |
| 21 ~FakeComponentActionDelegate() {} |
| 22 |
| 23 void AddComponentAction(const std::string& action_id) override { |
| 24 EXPECT_EQ(action_id, ComponentToolbarActionsFactory::kMediaRouterActionId); |
| 25 EXPECT_FALSE(HasComponentAction( |
| 26 ComponentToolbarActionsFactory::kMediaRouterActionId)); |
| 27 has_media_router_action_ = true; |
| 28 } |
| 29 |
| 30 void RemoveComponentAction(const std::string& action_id) override { |
| 31 EXPECT_EQ(action_id, ComponentToolbarActionsFactory::kMediaRouterActionId); |
| 32 EXPECT_TRUE(HasComponentAction( |
| 33 ComponentToolbarActionsFactory::kMediaRouterActionId)); |
| 34 has_media_router_action_ = false; |
| 35 } |
| 36 |
| 37 bool HasComponentAction(const std::string& action_id) const override { |
| 38 EXPECT_EQ(action_id, ComponentToolbarActionsFactory::kMediaRouterActionId); |
| 39 return has_media_router_action_; |
| 40 } |
| 41 |
| 42 private: |
| 43 bool has_media_router_action_ = false; |
| 44 }; |
| 45 |
| 46 class MediaRouterActionControllerUnitTest : public MediaRouterTest { |
| 47 public: |
| 48 MediaRouterActionControllerUnitTest() |
| 49 : issue_(media_router::Issue( |
| 50 "title notification", |
| 51 "message notification", |
| 52 media_router::IssueAction(media_router::IssueAction::TYPE_DISMISS), |
| 53 std::vector<media_router::IssueAction>(), |
| 54 "route_id", |
| 55 media_router::Issue::NOTIFICATION, |
| 56 false, |
| 57 -1)), |
| 58 source1_("fakeSource1"), |
| 59 source2_("fakeSource2") {} |
| 60 |
| 61 ~MediaRouterActionControllerUnitTest() override {} |
| 62 |
| 63 // MediaRouterTest: |
| 64 void SetUp() override { |
| 65 MediaRouterTest::SetUp(); |
| 66 |
| 67 router_.reset(new media_router::MockMediaRouter()); |
| 68 component_action_delegate_.reset(new FakeComponentActionDelegate()); |
| 69 component_migration_helper_.reset(new ComponentMigrationHelper( |
| 70 profile(), component_action_delegate_.get())); |
| 71 controller_.reset(new MediaRouterActionController( |
| 72 profile(), router_.get(), component_action_delegate_.get(), |
| 73 component_migration_helper_.get())); |
| 74 |
| 75 SetAlwaysShowActionPref(false); |
| 76 |
| 77 local_display_route_list_.push_back( |
| 78 media_router::MediaRoute("routeId1", source1_, "sinkId1", "description", |
| 79 true, std::string(), true)); |
| 80 non_local_display_route_list_.push_back( |
| 81 media_router::MediaRoute("routeId2", source1_, "sinkId2", "description", |
| 82 false, std::string(), true)); |
| 83 non_local_display_route_list_.push_back( |
| 84 media_router::MediaRoute("routeId3", source2_, "sinkId3", "description", |
| 85 true, std::string(), false)); |
| 86 } |
| 87 |
| 88 void TearDown() override { |
| 89 controller_.reset(); |
| 90 component_migration_helper_.reset(); |
| 91 component_action_delegate_.reset(); |
| 92 router_.reset(); |
| 93 MediaRouterTest::TearDown(); |
| 94 } |
| 95 |
| 96 bool ActionExists() { |
| 97 return component_action_delegate_->HasComponentAction( |
| 98 ComponentToolbarActionsFactory::kMediaRouterActionId); |
| 99 } |
| 100 |
| 101 void SetAlwaysShowActionPref(bool always_show) { |
| 102 component_migration_helper_->SetComponentActionPref( |
| 103 ComponentToolbarActionsFactory::kMediaRouterActionId, always_show); |
| 104 } |
| 105 |
| 106 MediaRouterActionController* controller() { return controller_.get(); } |
| 107 |
| 108 const media_router::Issue* issue() { return &issue_; } |
| 109 const std::vector<media_router::MediaRoute>& local_display_route_list() |
| 110 const { |
| 111 return local_display_route_list_; |
| 112 } |
| 113 const std::vector<media_router::MediaRoute>& non_local_display_route_list() |
| 114 const { |
| 115 return non_local_display_route_list_; |
| 116 } |
| 117 const std::vector<media_router::MediaRoute::Id>& empty_route_id_list() const { |
| 118 return empty_route_id_list_; |
| 119 } |
| 120 |
| 121 private: |
| 122 std::unique_ptr<MediaRouterActionController> controller_; |
| 123 std::unique_ptr<media_router::MockMediaRouter> router_; |
| 124 std::unique_ptr<FakeComponentActionDelegate> component_action_delegate_; |
| 125 std::unique_ptr<ComponentMigrationHelper> component_migration_helper_; |
| 126 |
| 127 const media_router::Issue issue_; |
| 128 |
| 129 // Fake Sources, used for the Routes. |
| 130 const media_router::MediaSource source1_; |
| 131 const media_router::MediaSource source2_; |
| 132 |
| 133 std::vector<media_router::MediaRoute> local_display_route_list_; |
| 134 std::vector<media_router::MediaRoute> non_local_display_route_list_; |
| 135 std::vector<media_router::MediaRoute::Id> empty_route_id_list_; |
| 136 |
| 137 DISALLOW_COPY_AND_ASSIGN(MediaRouterActionControllerUnitTest); |
| 138 }; |
| 139 |
| 140 TEST_F(MediaRouterActionControllerUnitTest, EphemeralIcon) { |
| 141 EXPECT_FALSE(ActionExists()); |
| 142 |
| 143 // Creating a local route should show the action icon. |
| 144 controller()->OnRoutesUpdated(local_display_route_list(), |
| 145 empty_route_id_list()); |
| 146 EXPECT_TRUE(controller()->has_local_display_route_); |
| 147 EXPECT_TRUE(ActionExists()); |
| 148 // Removing the local route should hide the icon. |
| 149 controller()->OnRoutesUpdated(non_local_display_route_list(), |
| 150 empty_route_id_list()); |
| 151 EXPECT_FALSE(controller()->has_local_display_route_); |
| 152 EXPECT_FALSE(ActionExists()); |
| 153 |
| 154 // Creating an issue should show the action icon. |
| 155 controller()->OnIssueUpdated(issue()); |
| 156 EXPECT_TRUE(controller()->has_issue_); |
| 157 EXPECT_TRUE(ActionExists()); |
| 158 // Removing the issue should hide the icon. |
| 159 controller()->OnIssueUpdated(nullptr); |
| 160 EXPECT_FALSE(controller()->has_issue_); |
| 161 EXPECT_FALSE(ActionExists()); |
| 162 |
| 163 controller()->OnIssueUpdated(issue()); |
| 164 controller()->OnRoutesUpdated(local_display_route_list(), |
| 165 empty_route_id_list()); |
| 166 controller()->OnIssueUpdated(nullptr); |
| 167 // When the issue disappears, the icon should remain visible if there's |
| 168 // a local route. |
| 169 EXPECT_TRUE(ActionExists()); |
| 170 controller()->OnRoutesUpdated(std::vector<media_router::MediaRoute>(), |
| 171 empty_route_id_list()); |
| 172 EXPECT_FALSE(ActionExists()); |
| 173 } |
| 174 |
| 175 TEST_F(MediaRouterActionControllerUnitTest, ObserveAlwaysShowPrefChange) { |
| 176 EXPECT_FALSE(ActionExists()); |
| 177 |
| 178 SetAlwaysShowActionPref(true); |
| 179 EXPECT_TRUE(ActionExists()); |
| 180 |
| 181 controller()->OnRoutesUpdated(local_display_route_list(), |
| 182 empty_route_id_list()); |
| 183 SetAlwaysShowActionPref(false); |
| 184 // Unchecking the option while having a local route shouldn't hide the icon. |
| 185 EXPECT_TRUE(ActionExists()); |
| 186 |
| 187 SetAlwaysShowActionPref(true); |
| 188 controller()->OnRoutesUpdated(non_local_display_route_list(), |
| 189 empty_route_id_list()); |
| 190 // Removing the local route should not hide the icon. |
| 191 EXPECT_TRUE(ActionExists()); |
| 192 |
| 193 SetAlwaysShowActionPref(false); |
| 194 EXPECT_FALSE(ActionExists()); |
| 195 } |
OLD | NEW |