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
|
// 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 "base/macros.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/extensions/browser_action_test_util.h"
#include "chrome/browser/extensions/extension_action_test_util.h"
#include "chrome/browser/signin/fake_signin_manager_builder.h"
#include "chrome/browser/signin/signin_manager_factory.h"
#include "chrome/browser/ui/toolbar/component_toolbar_actions_factory.h"
#include "chrome/browser/ui/toolbar/media_router_action.h"
#include "chrome/browser/ui/toolbar/media_router_contextual_menu.h"
#include "chrome/test/base/browser_with_test_window_test.h"
class MediaRouterContextualMenuUnitTest : public BrowserWithTestWindowTest {
public:
MediaRouterContextualMenuUnitTest() {}
~MediaRouterContextualMenuUnitTest() override {}
void SetUp() override {
BrowserWithTestWindowTest::SetUp();
toolbar_actions_model_ =
extensions::extension_action_test_util::CreateToolbarModelForProfile(
profile());
signin_manager_ =
SigninManagerFactory::GetInstance()->GetForProfile(profile());
browser_action_test_util_.reset(
new BrowserActionTestUtil(browser(), false));
action_.reset(new MediaRouterAction(browser(),
browser_action_test_util_->GetToolbarActionsBar()));
model_ = static_cast<ui::SimpleMenuModel*>(action_->GetContextMenu());
}
void TearDown() override {
action_.reset();
browser_action_test_util_.reset();
BrowserWithTestWindowTest::TearDown();
}
SigninManagerBase* signin_manager() { return signin_manager_; }
ui::SimpleMenuModel* model() { return model_; }
ToolbarActionsModel* toolbar_actions_model() {
return toolbar_actions_model_;
}
private:
std::unique_ptr<BrowserActionTestUtil> browser_action_test_util_;
std::unique_ptr<MediaRouterAction> action_;
SigninManagerBase* signin_manager_;
ui::SimpleMenuModel* model_;
ToolbarActionsModel* toolbar_actions_model_;
DISALLOW_COPY_AND_ASSIGN(MediaRouterContextualMenuUnitTest);
};
// Tests the basic state of the contextual menu.
TEST_F(MediaRouterContextualMenuUnitTest, Basic) {
// About
// -----
// Learn more
// Help
// Always show icon (checkbox)
// -----
// Enable cloud services (checkbox)
// Report an issue
int expected_number_items = 8;
#if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_CHROMEOS)
// On all platforms except Linux, there's an additional menu item to access
// Cast device management.
expected_number_items++;
#endif // defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_CHROMEOS)
// Verify the number of menu items, including separators.
EXPECT_EQ(model()->GetItemCount(), expected_number_items);
for (int i = 0; i < expected_number_items; i++) {
EXPECT_TRUE(model()->IsEnabledAt(i));
// The cloud services toggle exists and is enabled, but not visible until
// the user has authenticated their account.
const bool expected_visibility =
model()->GetCommandIdAt(i) != IDC_MEDIA_ROUTER_CLOUD_SERVICES_TOGGLE;
EXPECT_EQ(expected_visibility, model()->IsVisibleAt(i));
}
// Set up an authenticated account.
signin_manager()->SetAuthenticatedAccountInfo("foo@bar.com", "password");
// Run the same checks as before. All existing menu items should be now
// enabled and visible.
EXPECT_EQ(model()->GetItemCount(), expected_number_items);
for (int i = 0; i < expected_number_items; i++) {
EXPECT_TRUE(model()->IsEnabledAt(i));
EXPECT_TRUE(model()->IsVisibleAt(i));
}
}
// Tests whether the cloud services item is correctly toggled. This menu item
// is only availble on official Chrome builds.
TEST_F(MediaRouterContextualMenuUnitTest, ToggleCloudServicesItem) {
// The Media Router Action has a getter for the model, but not the delegate.
// Create the MediaRouterContextualMenu ui::SimpleMenuModel::Delegate here.
MediaRouterContextualMenu menu(browser());
// Set up an authenticated account such that the cloud services menu item is
// surfaced. Whether or not it is surfaced is tested in the "Basic" test.
signin_manager()->SetAuthenticatedAccountInfo("foo@bar.com", "password");
// By default, the command is not checked.
EXPECT_FALSE(menu.IsCommandIdChecked(
IDC_MEDIA_ROUTER_CLOUD_SERVICES_TOGGLE));
menu.ExecuteCommand(IDC_MEDIA_ROUTER_CLOUD_SERVICES_TOGGLE, 0);
EXPECT_TRUE(menu.IsCommandIdChecked(
IDC_MEDIA_ROUTER_CLOUD_SERVICES_TOGGLE));
menu.ExecuteCommand(IDC_MEDIA_ROUTER_CLOUD_SERVICES_TOGGLE, 0);
EXPECT_FALSE(menu.IsCommandIdChecked(
IDC_MEDIA_ROUTER_CLOUD_SERVICES_TOGGLE));
}
TEST_F(MediaRouterContextualMenuUnitTest, ToggleAlwaysShowIconItem) {
MediaRouterContextualMenu menu(browser());
extensions::ComponentMigrationHelper* const component_migration_helper =
toolbar_actions_model()->component_migration_helper();
// Whether the option is checked should reflect the pref.
component_migration_helper->SetComponentActionPref(
ComponentToolbarActionsFactory::kMediaRouterActionId, true);
EXPECT_TRUE(
menu.IsCommandIdChecked(IDC_MEDIA_ROUTER_ALWAYS_SHOW_TOOLBAR_ACTION));
component_migration_helper->SetComponentActionPref(
ComponentToolbarActionsFactory::kMediaRouterActionId, false);
EXPECT_FALSE(
menu.IsCommandIdChecked(IDC_MEDIA_ROUTER_ALWAYS_SHOW_TOOLBAR_ACTION));
// Executing the option should toggle the pref.
menu.ExecuteCommand(IDC_MEDIA_ROUTER_ALWAYS_SHOW_TOOLBAR_ACTION, 0);
EXPECT_TRUE(component_migration_helper->GetComponentActionPref(
ComponentToolbarActionsFactory::kMediaRouterActionId));
menu.ExecuteCommand(IDC_MEDIA_ROUTER_ALWAYS_SHOW_TOOLBAR_ACTION, 0);
EXPECT_FALSE(component_migration_helper->GetComponentActionPref(
ComponentToolbarActionsFactory::kMediaRouterActionId));
}
|