Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <memory> | 5 #include <memory> |
| 6 | 6 |
| 7 #include "base/macros.h" | 7 #include "base/macros.h" |
| 8 #include "chrome/app/chrome_command_ids.h" | 8 #include "chrome/app/chrome_command_ids.h" |
| 9 #include "chrome/browser/extensions/browser_action_test_util.h" | 9 #include "chrome/browser/extensions/browser_action_test_util.h" |
| 10 #include "chrome/browser/extensions/extension_action_test_util.h" | 10 #include "chrome/browser/extensions/extension_action_test_util.h" |
| 11 #include "chrome/browser/signin/fake_signin_manager_builder.h" | 11 #include "chrome/browser/signin/fake_signin_manager_builder.h" |
| 12 #include "chrome/browser/signin/signin_manager_factory.h" | 12 #include "chrome/browser/signin/signin_manager_factory.h" |
| 13 #include "chrome/browser/ui/toolbar/component_toolbar_actions_factory.h" | 13 #include "chrome/browser/ui/toolbar/component_toolbar_actions_factory.h" |
| 14 #include "chrome/browser/ui/toolbar/media_router_action.h" | 14 #include "chrome/browser/ui/toolbar/media_router_action.h" |
| 15 #include "chrome/browser/ui/toolbar/media_router_action_controller.h" | 15 #include "chrome/browser/ui/toolbar/media_router_action_controller.h" |
| 16 #include "chrome/browser/ui/toolbar/media_router_contextual_menu.h" | 16 #include "chrome/browser/ui/toolbar/media_router_contextual_menu.h" |
| 17 #include "chrome/grit/chromium_strings.h" | |
| 18 #include "chrome/grit/generated_resources.h" | |
| 17 #include "chrome/test/base/browser_with_test_window_test.h" | 19 #include "chrome/test/base/browser_with_test_window_test.h" |
| 20 #include "ui/base/l10n/l10n_util.h" | |
| 18 | 21 |
| 19 namespace { | 22 namespace { |
| 20 | 23 |
| 21 bool HasCommandId(ui::MenuModel* menu_model, int command_id) { | 24 bool HasCommandId(ui::MenuModel* menu_model, int command_id) { |
| 22 for (int i = 0; i < menu_model->GetItemCount(); i++) { | 25 for (int i = 0; i < menu_model->GetItemCount(); i++) { |
| 23 if (menu_model->GetCommandIdAt(i) == command_id) | 26 if (menu_model->GetCommandIdAt(i) == command_id) |
| 24 return true; | 27 return true; |
| 25 } | 28 } |
| 26 return false; | 29 return false; |
| 27 } | 30 } |
| 28 | 31 |
| 29 } // namespace | 32 } // namespace |
| 30 | 33 |
| 31 class MediaRouterContextualMenuUnitTest : public BrowserWithTestWindowTest { | 34 class MediaRouterContextualMenuUnitTest : public BrowserWithTestWindowTest { |
| 32 public: | 35 public: |
| 33 MediaRouterContextualMenuUnitTest() {} | 36 MediaRouterContextualMenuUnitTest() {} |
| 34 ~MediaRouterContextualMenuUnitTest() override {} | 37 ~MediaRouterContextualMenuUnitTest() override {} |
| 35 | 38 |
| 36 void SetUp() override { | 39 void SetUp() override { |
| 37 BrowserWithTestWindowTest::SetUp(); | 40 BrowserWithTestWindowTest::SetUp(); |
| 38 | 41 |
| 39 toolbar_actions_model_ = | 42 toolbar_actions_model_ = |
| 40 extensions::extension_action_test_util::CreateToolbarModelForProfile( | 43 extensions::extension_action_test_util::CreateToolbarModelForProfile( |
| 41 profile()); | 44 profile()); |
| 42 | 45 |
| 43 signin_manager_ = | 46 signin_manager_ = |
| 44 SigninManagerFactory::GetInstance()->GetForProfile(profile()); | 47 SigninManagerFactory::GetInstance()->GetForProfile(profile()); |
| 45 browser_action_test_util_.reset( | 48 browser_action_test_util_ = |
| 46 new BrowserActionTestUtil(browser(), false)); | 49 base::MakeUnique<BrowserActionTestUtil>(browser(), false); |
| 47 action_.reset(new MediaRouterAction(browser(), | 50 action_ = base::MakeUnique<MediaRouterAction>( |
| 48 browser_action_test_util_->GetToolbarActionsBar())); | 51 browser(), browser_action_test_util_->GetToolbarActionsBar()); |
| 52 | |
| 53 // Pin the Media Router action to the toolbar. | |
| 54 MediaRouterActionController::SetAlwaysShowActionPref(profile(), true); | |
| 49 model_ = static_cast<ui::SimpleMenuModel*>(action_->GetContextMenu()); | 55 model_ = static_cast<ui::SimpleMenuModel*>(action_->GetContextMenu()); |
| 50 } | 56 } |
| 51 | 57 |
| 52 void TearDown() override { | 58 void TearDown() override { |
| 53 action_.reset(); | 59 action_.reset(); |
| 54 browser_action_test_util_.reset(); | 60 browser_action_test_util_.reset(); |
| 55 BrowserWithTestWindowTest::TearDown(); | 61 BrowserWithTestWindowTest::TearDown(); |
| 56 } | 62 } |
| 57 | 63 |
| 58 SigninManagerBase* signin_manager() { return signin_manager_; } | 64 protected: |
| 59 ui::SimpleMenuModel* model() { return model_; } | 65 // These constants are used to specify the state of the Media Router action |
| 60 ToolbarActionsModel* toolbar_actions_model() { | 66 // that is inferred in the production code. |
| 61 return toolbar_actions_model_; | 67 static const bool kInToolbar = true; |
| 62 } | 68 static const bool kInOverflowMenu = false; |
| 69 static const bool kShownByPolicy = true; | |
| 70 static const bool kShownByUser = false; | |
| 71 | |
| 72 std::unique_ptr<BrowserActionTestUtil> browser_action_test_util_; | |
| 73 std::unique_ptr<MediaRouterAction> action_; | |
| 74 SigninManagerBase* signin_manager_ = nullptr; | |
| 75 ui::SimpleMenuModel* model_ = nullptr; | |
| 76 ToolbarActionsModel* toolbar_actions_model_ = nullptr; | |
| 63 | 77 |
| 64 private: | 78 private: |
| 65 std::unique_ptr<BrowserActionTestUtil> browser_action_test_util_; | |
| 66 std::unique_ptr<MediaRouterAction> action_; | |
| 67 SigninManagerBase* signin_manager_; | |
| 68 ui::SimpleMenuModel* model_; | |
| 69 ToolbarActionsModel* toolbar_actions_model_; | |
| 70 | |
| 71 DISALLOW_COPY_AND_ASSIGN(MediaRouterContextualMenuUnitTest); | 79 DISALLOW_COPY_AND_ASSIGN(MediaRouterContextualMenuUnitTest); |
| 72 }; | 80 }; |
| 73 | 81 |
| 74 // Tests the basic state of the contextual menu. | 82 // Tests the basic state of the contextual menu. |
| 75 TEST_F(MediaRouterContextualMenuUnitTest, Basic) { | 83 TEST_F(MediaRouterContextualMenuUnitTest, Basic) { |
| 76 // About | 84 // About |
| 77 // ----- | 85 // ----- |
| 78 // Learn more | 86 // Learn more |
| 79 // Help | 87 // Help |
| 80 // Always show icon (checkbox) | 88 // Always show icon (checkbox) |
| 89 // Hide in Chrome menu / Show in toolbar | |
| 81 // ----- | 90 // ----- |
| 82 // Enable cloud services (checkbox) | 91 // Enable cloud services (checkbox) |
| 83 // Report an issue | 92 // Report an issue |
| 84 int expected_number_items = 8; | 93 int expected_number_items = 9; |
| 85 | 94 |
| 86 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_CHROMEOS) | 95 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_CHROMEOS) |
| 87 // On all platforms except Linux, there's an additional menu item to access | 96 // On all platforms except Linux, there's an additional menu item to access |
| 88 // Cast device management. | 97 // Cast device management. |
| 89 expected_number_items++; | 98 expected_number_items++; |
| 90 #endif // defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_CHROMEOS) | 99 #endif // defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_CHROMEOS) |
| 91 | 100 |
| 92 // Verify the number of menu items, including separators. | 101 // Verify the number of menu items, including separators. |
| 93 EXPECT_EQ(model()->GetItemCount(), expected_number_items); | 102 EXPECT_EQ(model_->GetItemCount(), expected_number_items); |
| 94 | 103 |
| 95 for (int i = 0; i < expected_number_items; i++) { | 104 for (int i = 0; i < expected_number_items; i++) { |
| 96 EXPECT_TRUE(model()->IsEnabledAt(i)); | 105 EXPECT_TRUE(model_->IsEnabledAt(i)); |
| 97 | 106 |
| 98 // The cloud services toggle exists and is enabled, but not visible until | 107 // The cloud services toggle exists and is enabled, but not visible until |
| 99 // the user has authenticated their account. | 108 // the user has authenticated their account. |
| 100 const bool expected_visibility = | 109 const bool expected_visibility = |
| 101 model()->GetCommandIdAt(i) != IDC_MEDIA_ROUTER_CLOUD_SERVICES_TOGGLE; | 110 model_->GetCommandIdAt(i) != IDC_MEDIA_ROUTER_CLOUD_SERVICES_TOGGLE; |
| 102 EXPECT_EQ(expected_visibility, model()->IsVisibleAt(i)); | 111 EXPECT_EQ(expected_visibility, model_->IsVisibleAt(i)); |
| 103 } | 112 } |
| 104 | 113 |
| 105 // Set up an authenticated account. | 114 // Set up an authenticated account. |
| 106 signin_manager()->SetAuthenticatedAccountInfo("foo@bar.com", "password"); | 115 signin_manager_->SetAuthenticatedAccountInfo("foo@bar.com", "password"); |
| 107 | 116 |
| 108 // Run the same checks as before. All existing menu items should be now | 117 // Run the same checks as before. All existing menu items should be now |
| 109 // enabled and visible. | 118 // enabled and visible. |
| 110 EXPECT_EQ(model()->GetItemCount(), expected_number_items); | 119 EXPECT_EQ(model_->GetItemCount(), expected_number_items); |
| 111 for (int i = 0; i < expected_number_items; i++) { | 120 for (int i = 0; i < expected_number_items; i++) { |
| 112 EXPECT_TRUE(model()->IsEnabledAt(i)); | 121 EXPECT_TRUE(model_->IsEnabledAt(i)); |
| 113 EXPECT_TRUE(model()->IsVisibleAt(i)); | 122 EXPECT_TRUE(model_->IsVisibleAt(i)); |
| 114 } | 123 } |
| 115 } | 124 } |
| 116 | 125 |
| 117 // Tests whether the cloud services item is correctly toggled. This menu item | 126 // Tests whether the cloud services item is correctly toggled. This menu item |
| 118 // is only availble on official Chrome builds. | 127 // is only availble on official Chrome builds. |
| 119 TEST_F(MediaRouterContextualMenuUnitTest, ToggleCloudServicesItem) { | 128 TEST_F(MediaRouterContextualMenuUnitTest, ToggleCloudServicesItem) { |
| 120 // The Media Router Action has a getter for the model, but not the delegate. | 129 // The Media Router Action has a getter for the model, but not the delegate. |
| 121 // Create the MediaRouterContextualMenu ui::SimpleMenuModel::Delegate here. | 130 // Create the MediaRouterContextualMenu ui::SimpleMenuModel::Delegate here. |
| 122 MediaRouterContextualMenu menu(browser()); | 131 MediaRouterContextualMenu menu(browser(), kInToolbar, kShownByPolicy); |
| 123 | 132 |
| 124 // Set up an authenticated account such that the cloud services menu item is | 133 // Set up an authenticated account such that the cloud services menu item is |
| 125 // surfaced. Whether or not it is surfaced is tested in the "Basic" test. | 134 // surfaced. Whether or not it is surfaced is tested in the "Basic" test. |
| 126 signin_manager()->SetAuthenticatedAccountInfo("foo@bar.com", "password"); | 135 signin_manager_->SetAuthenticatedAccountInfo("foo@bar.com", "password"); |
| 127 | 136 |
| 128 // By default, the command is not checked. | 137 // By default, the command is not checked. |
| 129 EXPECT_FALSE(menu.IsCommandIdChecked( | 138 EXPECT_FALSE(menu.IsCommandIdChecked( |
| 130 IDC_MEDIA_ROUTER_CLOUD_SERVICES_TOGGLE)); | 139 IDC_MEDIA_ROUTER_CLOUD_SERVICES_TOGGLE)); |
| 131 | 140 |
| 132 menu.ExecuteCommand(IDC_MEDIA_ROUTER_CLOUD_SERVICES_TOGGLE, 0); | 141 menu.ExecuteCommand(IDC_MEDIA_ROUTER_CLOUD_SERVICES_TOGGLE, 0); |
| 133 EXPECT_TRUE(menu.IsCommandIdChecked( | 142 EXPECT_TRUE(menu.IsCommandIdChecked( |
| 134 IDC_MEDIA_ROUTER_CLOUD_SERVICES_TOGGLE)); | 143 IDC_MEDIA_ROUTER_CLOUD_SERVICES_TOGGLE)); |
| 135 | 144 |
| 136 menu.ExecuteCommand(IDC_MEDIA_ROUTER_CLOUD_SERVICES_TOGGLE, 0); | 145 menu.ExecuteCommand(IDC_MEDIA_ROUTER_CLOUD_SERVICES_TOGGLE, 0); |
| 137 EXPECT_FALSE(menu.IsCommandIdChecked( | 146 EXPECT_FALSE(menu.IsCommandIdChecked( |
| 138 IDC_MEDIA_ROUTER_CLOUD_SERVICES_TOGGLE)); | 147 IDC_MEDIA_ROUTER_CLOUD_SERVICES_TOGGLE)); |
| 139 } | 148 } |
| 140 | 149 |
| 141 TEST_F(MediaRouterContextualMenuUnitTest, ToggleAlwaysShowIconItem) { | 150 TEST_F(MediaRouterContextualMenuUnitTest, ToggleAlwaysShowIconItem) { |
| 142 MediaRouterContextualMenu menu(browser()); | 151 MediaRouterContextualMenu menu(browser(), kInToolbar, kShownByUser); |
| 143 | 152 |
| 144 // Whether the option is checked should reflect the pref. | 153 // Whether the option is checked should reflect the pref. |
| 145 MediaRouterActionController::SetAlwaysShowActionPref(profile(), true); | 154 MediaRouterActionController::SetAlwaysShowActionPref(profile(), true); |
| 146 EXPECT_TRUE( | 155 EXPECT_TRUE( |
| 147 menu.IsCommandIdChecked(IDC_MEDIA_ROUTER_ALWAYS_SHOW_TOOLBAR_ACTION)); | 156 menu.IsCommandIdChecked(IDC_MEDIA_ROUTER_ALWAYS_SHOW_TOOLBAR_ACTION)); |
| 148 | 157 |
| 149 MediaRouterActionController::SetAlwaysShowActionPref(profile(), false); | 158 MediaRouterActionController::SetAlwaysShowActionPref(profile(), false); |
| 150 EXPECT_FALSE( | 159 EXPECT_FALSE( |
| 151 menu.IsCommandIdChecked(IDC_MEDIA_ROUTER_ALWAYS_SHOW_TOOLBAR_ACTION)); | 160 menu.IsCommandIdChecked(IDC_MEDIA_ROUTER_ALWAYS_SHOW_TOOLBAR_ACTION)); |
| 152 | 161 |
| 153 // Executing the option should toggle the pref. | 162 // Executing the option should toggle the pref. |
| 154 menu.ExecuteCommand(IDC_MEDIA_ROUTER_ALWAYS_SHOW_TOOLBAR_ACTION, 0); | 163 menu.ExecuteCommand(IDC_MEDIA_ROUTER_ALWAYS_SHOW_TOOLBAR_ACTION, 0); |
| 155 EXPECT_TRUE(MediaRouterActionController::GetAlwaysShowActionPref(profile())); | 164 EXPECT_TRUE(MediaRouterActionController::GetAlwaysShowActionPref(profile())); |
| 156 | 165 |
| 157 menu.ExecuteCommand(IDC_MEDIA_ROUTER_ALWAYS_SHOW_TOOLBAR_ACTION, 0); | 166 menu.ExecuteCommand(IDC_MEDIA_ROUTER_ALWAYS_SHOW_TOOLBAR_ACTION, 0); |
| 158 EXPECT_FALSE(MediaRouterActionController::GetAlwaysShowActionPref(profile())); | 167 EXPECT_FALSE(MediaRouterActionController::GetAlwaysShowActionPref(profile())); |
| 159 } | 168 } |
| 160 | 169 |
| 161 TEST_F(MediaRouterContextualMenuUnitTest, ActionShownByPolicy) { | 170 TEST_F(MediaRouterContextualMenuUnitTest, ActionShownByPolicy) { |
| 162 // Create a contextual menu for an icon shown by administrator policy. | 171 // Create a contextual menu for an icon shown by administrator policy. |
| 163 MediaRouterContextualMenu menu(browser(), true); | 172 MediaRouterContextualMenu menu(browser(), kInToolbar, kShownByPolicy); |
| 164 | 173 |
| 165 // The item "Added by your administrator" should be shown disabled. | 174 // The item "Added by your administrator" should be shown disabled. |
| 166 EXPECT_TRUE(menu.IsCommandIdVisible(IDC_MEDIA_ROUTER_SHOWN_BY_POLICY)); | 175 EXPECT_TRUE(menu.IsCommandIdVisible(IDC_MEDIA_ROUTER_SHOWN_BY_POLICY)); |
| 167 EXPECT_FALSE(menu.IsCommandIdEnabled(IDC_MEDIA_ROUTER_SHOWN_BY_POLICY)); | 176 EXPECT_FALSE(menu.IsCommandIdEnabled(IDC_MEDIA_ROUTER_SHOWN_BY_POLICY)); |
| 168 | 177 |
| 169 // The checkbox item "Always show icon" should not be shown. | 178 // The checkbox item "Always show icon" should not be shown. |
| 170 EXPECT_FALSE(HasCommandId(menu.menu_model(), | 179 EXPECT_FALSE(HasCommandId(menu.menu_model(), |
| 171 IDC_MEDIA_ROUTER_ALWAYS_SHOW_TOOLBAR_ACTION)); | 180 IDC_MEDIA_ROUTER_ALWAYS_SHOW_TOOLBAR_ACTION)); |
| 172 } | 181 } |
| 182 | |
| 183 TEST_F(MediaRouterContextualMenuUnitTest, HideActionInOverflowItem) { | |
| 184 MediaRouterContextualMenu menu(browser(), kInToolbar, kShownByUser); | |
| 185 | |
| 186 // When the action icon is in the toolbar, this menu item should say "Hide | |
| 187 // in Chrome menu." | |
|
apacible
2017/03/02 02:02:57
nit here and below: period outside quotes since it
takumif
2017/03/03 00:10:48
Done.
| |
| 188 const base::string16& menu_item_label = menu.menu_model()->GetLabelAt( | |
| 189 menu.menu_model()->GetIndexOfCommandId(IDC_MEDIA_ROUTER_SHOW_IN_TOOLBAR)); | |
| 190 EXPECT_EQ(menu_item_label, | |
| 191 l10n_util::GetStringUTF16(IDS_EXTENSIONS_HIDE_BUTTON_IN_MENU)); | |
| 192 } | |
| 193 | |
| 194 TEST_F(MediaRouterContextualMenuUnitTest, ShowActionInToolbarItem) { | |
| 195 MediaRouterContextualMenu menu(browser(), kInOverflowMenu, kShownByUser); | |
| 196 | |
| 197 // When the action icon is in the overflow menu, this menu item should say | |
| 198 // "Show in toolbar." | |
| 199 const base::string16& menu_item_label = menu.menu_model()->GetLabelAt( | |
| 200 menu.menu_model()->GetIndexOfCommandId(IDC_MEDIA_ROUTER_SHOW_IN_TOOLBAR)); | |
| 201 EXPECT_EQ(menu_item_label, | |
| 202 l10n_util::GetStringUTF16(IDS_EXTENSIONS_SHOW_BUTTON_IN_TOOLBAR)); | |
| 203 } | |
| OLD | NEW |