1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
|
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <memory>
#include <string>
#include "chrome/browser/media/router/mock_media_router.h"
#include "chrome/browser/ui/toolbar/component_toolbar_actions_factory.h"
#include "chrome/browser/ui/toolbar/media_router_action_controller.h"
#include "chrome/browser/ui/webui/media_router/media_router_web_ui_test.h"
#include "chrome/common/pref_names.h"
#include "testing/gmock/include/gmock/gmock.h"
using extensions::ComponentMigrationHelper;
class FakeComponentActionDelegate
: public ComponentMigrationHelper::ComponentActionDelegate {
public:
FakeComponentActionDelegate() {}
~FakeComponentActionDelegate() {}
void AddComponentAction(const std::string& action_id) override {
EXPECT_EQ(action_id, ComponentToolbarActionsFactory::kMediaRouterActionId);
EXPECT_FALSE(HasComponentAction(
ComponentToolbarActionsFactory::kMediaRouterActionId));
has_media_router_action_ = true;
}
void RemoveComponentAction(const std::string& action_id) override {
EXPECT_EQ(action_id, ComponentToolbarActionsFactory::kMediaRouterActionId);
EXPECT_TRUE(HasComponentAction(
ComponentToolbarActionsFactory::kMediaRouterActionId));
has_media_router_action_ = false;
}
bool HasComponentAction(const std::string& action_id) const override {
EXPECT_EQ(action_id, ComponentToolbarActionsFactory::kMediaRouterActionId);
return has_media_router_action_;
}
private:
bool has_media_router_action_ = false;
};
class MediaRouterActionControllerUnitTest : public MediaRouterWebUITest {
public:
MediaRouterActionControllerUnitTest()
: issue_(media_router::IssueInfo(
"title notification",
media_router::IssueInfo::Action::DISMISS,
media_router::IssueInfo::Severity::NOTIFICATION)),
source1_("fakeSource1"),
source2_("fakeSource2") {}
~MediaRouterActionControllerUnitTest() override {}
// MediaRouterWebUITest:
void SetUp() override {
MediaRouterWebUITest::SetUp();
router_.reset(new media_router::MockMediaRouter());
component_action_delegate_.reset(new FakeComponentActionDelegate());
component_migration_helper_.reset(new ComponentMigrationHelper(
profile(), component_action_delegate_.get()));
controller_.reset(new MediaRouterActionController(
profile(), router_.get(), component_action_delegate_.get(),
component_migration_helper_.get()));
SetAlwaysShowActionPref(false);
local_display_route_list_.push_back(
media_router::MediaRoute("routeId1", source1_, "sinkId1", "description",
true, std::string(), true));
non_local_display_route_list_.push_back(
media_router::MediaRoute("routeId2", source1_, "sinkId2", "description",
false, std::string(), true));
non_local_display_route_list_.push_back(
media_router::MediaRoute("routeId3", source2_, "sinkId3", "description",
true, std::string(), false));
}
void TearDown() override {
controller_.reset();
component_migration_helper_.reset();
component_action_delegate_.reset();
router_.reset();
MediaRouterWebUITest::TearDown();
}
bool ActionExists() {
return component_action_delegate_->HasComponentAction(
ComponentToolbarActionsFactory::kMediaRouterActionId);
}
void SetAlwaysShowActionPref(bool always_show) {
component_migration_helper_->SetComponentActionPref(
ComponentToolbarActionsFactory::kMediaRouterActionId, always_show);
}
MediaRouterActionController* controller() { return controller_.get(); }
const media_router::Issue& issue() { return issue_; }
const std::vector<media_router::MediaRoute>& local_display_route_list()
const {
return local_display_route_list_;
}
const std::vector<media_router::MediaRoute>& non_local_display_route_list()
const {
return non_local_display_route_list_;
}
const std::vector<media_router::MediaRoute::Id>& empty_route_id_list() const {
return empty_route_id_list_;
}
private:
std::unique_ptr<MediaRouterActionController> controller_;
std::unique_ptr<media_router::MockMediaRouter> router_;
std::unique_ptr<FakeComponentActionDelegate> component_action_delegate_;
std::unique_ptr<ComponentMigrationHelper> component_migration_helper_;
const media_router::Issue issue_;
// Fake Sources, used for the Routes.
const media_router::MediaSource source1_;
const media_router::MediaSource source2_;
std::vector<media_router::MediaRoute> local_display_route_list_;
std::vector<media_router::MediaRoute> non_local_display_route_list_;
std::vector<media_router::MediaRoute::Id> empty_route_id_list_;
DISALLOW_COPY_AND_ASSIGN(MediaRouterActionControllerUnitTest);
};
TEST_F(MediaRouterActionControllerUnitTest, EphemeralIconForRoutesAndIssues) {
EXPECT_FALSE(ActionExists());
// Creating a local route should show the action icon.
controller()->OnRoutesUpdated(local_display_route_list(),
empty_route_id_list());
EXPECT_TRUE(controller()->has_local_display_route_);
EXPECT_TRUE(ActionExists());
// Removing the local route should hide the icon.
controller()->OnRoutesUpdated(non_local_display_route_list(),
empty_route_id_list());
EXPECT_FALSE(controller()->has_local_display_route_);
EXPECT_FALSE(ActionExists());
// Creating an issue should show the action icon.
controller()->OnIssue(issue());
EXPECT_TRUE(controller()->has_issue_);
EXPECT_TRUE(ActionExists());
// Removing the issue should hide the icon.
controller()->OnIssuesCleared();
EXPECT_FALSE(controller()->has_issue_);
EXPECT_FALSE(ActionExists());
controller()->OnIssue(issue());
controller()->OnRoutesUpdated(local_display_route_list(),
empty_route_id_list());
controller()->OnIssuesCleared();
// When the issue disappears, the icon should remain visible if there's
// a local route.
EXPECT_TRUE(ActionExists());
controller()->OnRoutesUpdated(std::vector<media_router::MediaRoute>(),
empty_route_id_list());
EXPECT_FALSE(ActionExists());
}
TEST_F(MediaRouterActionControllerUnitTest, EphemeralIconForDialog) {
EXPECT_FALSE(ActionExists());
// Showing a dialog should show the icon.
controller()->OnDialogShown();
EXPECT_TRUE(ActionExists());
// Showing and hiding a dialog shouldn't hide the icon as long as we have a
// positive number of dialogs.
controller()->OnDialogShown();
EXPECT_TRUE(ActionExists());
controller()->OnDialogHidden();
EXPECT_TRUE(ActionExists());
// When we have zero dialogs, the icon should be hidden.
controller()->OnDialogHidden();
EXPECT_FALSE(ActionExists());
controller()->OnDialogShown();
EXPECT_TRUE(ActionExists());
controller()->OnRoutesUpdated(local_display_route_list(),
empty_route_id_list());
// Hiding the dialog while there are local routes shouldn't hide the icon.
controller()->OnDialogHidden();
EXPECT_TRUE(ActionExists());
controller()->OnRoutesUpdated(non_local_display_route_list(),
empty_route_id_list());
EXPECT_FALSE(ActionExists());
controller()->OnDialogShown();
EXPECT_TRUE(ActionExists());
controller()->OnIssue(issue());
// Hiding the dialog while there is an issue shouldn't hide the icon.
controller()->OnDialogHidden();
EXPECT_TRUE(ActionExists());
controller()->OnIssuesCleared();
EXPECT_FALSE(ActionExists());
}
TEST_F(MediaRouterActionControllerUnitTest, ObserveAlwaysShowPrefChange) {
EXPECT_FALSE(ActionExists());
SetAlwaysShowActionPref(true);
EXPECT_TRUE(ActionExists());
controller()->OnRoutesUpdated(local_display_route_list(),
empty_route_id_list());
SetAlwaysShowActionPref(false);
// Unchecking the option while having a local route shouldn't hide the icon.
EXPECT_TRUE(ActionExists());
SetAlwaysShowActionPref(true);
controller()->OnRoutesUpdated(non_local_display_route_list(),
empty_route_id_list());
// Removing the local route should not hide the icon.
EXPECT_TRUE(ActionExists());
SetAlwaysShowActionPref(false);
EXPECT_FALSE(ActionExists());
}
|