OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/extensions/extension_context_menu_model.h" | 5 #include "chrome/browser/extensions/extension_context_menu_model.h" |
6 | 6 |
7 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
8 #include "chrome/app/chrome_command_ids.h" | 8 #include "chrome/app/chrome_command_ids.h" |
9 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h" | 9 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h" |
| 10 #include "chrome/browser/extensions/extension_action_test_util.h" |
10 #include "chrome/browser/extensions/extension_service.h" | 11 #include "chrome/browser/extensions/extension_service.h" |
11 #include "chrome/browser/extensions/extension_service_test_base.h" | 12 #include "chrome/browser/extensions/extension_service_test_base.h" |
12 #include "chrome/browser/extensions/menu_manager.h" | 13 #include "chrome/browser/extensions/menu_manager.h" |
13 #include "chrome/browser/extensions/menu_manager_factory.h" | 14 #include "chrome/browser/extensions/menu_manager_factory.h" |
14 #include "chrome/browser/ui/browser.h" | 15 #include "chrome/browser/ui/browser.h" |
15 #include "chrome/browser/ui/host_desktop.h" | 16 #include "chrome/browser/ui/host_desktop.h" |
16 #include "chrome/common/extensions/api/context_menus.h" | 17 #include "chrome/common/extensions/api/context_menus.h" |
17 #include "chrome/grit/chromium_strings.h" | 18 #include "chrome/grit/chromium_strings.h" |
18 #include "chrome/grit/generated_resources.h" | 19 #include "chrome/grit/generated_resources.h" |
19 #include "chrome/test/base/test_browser_window.h" | 20 #include "chrome/test/base/test_browser_window.h" |
(...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
290 // We start at 1, so this should try to add the limit + 1. | 291 // We start at 1, so this should try to add the limit + 1. |
291 for (int i = 0; i < api::context_menus::ACTION_MENU_TOP_LEVEL_LIMIT; ++i) | 292 for (int i = 0; i < api::context_menus::ACTION_MENU_TOP_LEVEL_LIMIT; ++i) |
292 builder.AddContextItem(MenuItem::PAGE_ACTION); | 293 builder.AddContextItem(MenuItem::PAGE_ACTION); |
293 | 294 |
294 // We shouldn't go above the limit of top-level items. | 295 // We shouldn't go above the limit of top-level items. |
295 EXPECT_EQ(api::context_menus::ACTION_MENU_TOP_LEVEL_LIMIT, | 296 EXPECT_EQ(api::context_menus::ACTION_MENU_TOP_LEVEL_LIMIT, |
296 CountExtensionItems(*builder.BuildMenu())); | 297 CountExtensionItems(*builder.BuildMenu())); |
297 } | 298 } |
298 | 299 |
299 // Test that the "show" and "hide" menu items appear correctly in the extension | 300 // Test that the "show" and "hide" menu items appear correctly in the extension |
300 // context menu. | 301 // context menu without the toolbar redesign. |
301 TEST_F(ExtensionContextMenuModelTest, ExtensionContextMenuShowAndHide) { | 302 TEST_F(ExtensionContextMenuModelTest, ExtensionContextMenuShowAndHideLegacy) { |
| 303 // Start with the toolbar redesign disabled. |
| 304 scoped_ptr<FeatureSwitch::ScopedOverride> toolbar_redesign_override( |
| 305 new FeatureSwitch::ScopedOverride( |
| 306 FeatureSwitch::extension_action_redesign(), false)); |
| 307 |
302 InitializeEmptyExtensionService(); | 308 InitializeEmptyExtensionService(); |
| 309 Browser* browser = GetBrowser(); |
| 310 extension_action_test_util::CreateToolbarModelForProfile(profile()); |
303 scoped_refptr<const Extension> page_action = | 311 scoped_refptr<const Extension> page_action = |
304 BuildExtension("page_action_extension", | 312 BuildExtension("page_action_extension", |
305 manifest_keys::kPageAction, | 313 manifest_keys::kPageAction, |
306 Manifest::INTERNAL); | 314 Manifest::INTERNAL); |
307 ASSERT_TRUE(page_action.get()); | 315 ASSERT_TRUE(page_action.get()); |
308 scoped_refptr<const Extension> browser_action = | 316 scoped_refptr<const Extension> browser_action = |
309 BuildExtension("browser_action_extension", | 317 BuildExtension("browser_action_extension", |
310 manifest_keys::kBrowserAction, | 318 manifest_keys::kBrowserAction, |
311 Manifest::INTERNAL); | 319 Manifest::INTERNAL); |
312 ASSERT_TRUE(browser_action.get()); | 320 ASSERT_TRUE(browser_action.get()); |
313 | 321 |
314 service()->AddExtension(page_action.get()); | 322 service()->AddExtension(page_action.get()); |
315 service()->AddExtension(browser_action.get()); | 323 service()->AddExtension(browser_action.get()); |
316 | 324 |
317 // For laziness. | 325 // For laziness. |
318 const ExtensionContextMenuModel::MenuEntries visibility_command = | 326 const ExtensionContextMenuModel::MenuEntries visibility_command = |
319 ExtensionContextMenuModel::TOGGLE_VISIBILITY; | 327 ExtensionContextMenuModel::TOGGLE_VISIBILITY; |
320 base::string16 hide_string = | 328 base::string16 hide_string = |
321 l10n_util::GetStringUTF16(IDS_EXTENSIONS_HIDE_BUTTON); | 329 l10n_util::GetStringUTF16(IDS_EXTENSIONS_HIDE_BUTTON); |
322 base::string16 redesign_hide_string = | |
323 l10n_util::GetStringUTF16(IDS_EXTENSIONS_HIDE_BUTTON_IN_MENU); | |
324 base::string16 redesign_show_string = | |
325 l10n_util::GetStringUTF16(IDS_EXTENSIONS_SHOW_BUTTON_IN_TOOLBAR); | |
326 base::string16 redesign_keep_string = | |
327 l10n_util::GetStringUTF16(IDS_EXTENSIONS_KEEP_BUTTON_IN_TOOLBAR); | |
328 | 330 |
329 { | 331 { |
330 ExtensionContextMenuModel menu(page_action.get(), GetBrowser(), | 332 ExtensionContextMenuModel menu(page_action.get(), browser, |
331 ExtensionContextMenuModel::VISIBLE, nullptr); | 333 ExtensionContextMenuModel::VISIBLE, nullptr); |
332 | 334 |
333 int index = GetCommandIndex(menu, visibility_command); | 335 int index = GetCommandIndex(menu, visibility_command); |
334 // Without the toolbar redesign switch, page action menus shouldn't have a | 336 // Without the toolbar redesign switch, page action menus shouldn't have a |
335 // visibility option. | 337 // visibility option. |
336 EXPECT_EQ(-1, index); | 338 EXPECT_EQ(-1, index); |
337 } | 339 } |
338 | 340 |
339 { | 341 { |
340 ExtensionContextMenuModel menu(browser_action.get(), GetBrowser(), | 342 ExtensionContextMenuModel menu(browser_action.get(), browser, |
341 ExtensionContextMenuModel::VISIBLE, nullptr); | 343 ExtensionContextMenuModel::VISIBLE, nullptr); |
342 int index = GetCommandIndex(menu, visibility_command); | 344 int index = GetCommandIndex(menu, visibility_command); |
343 // Browser actions should have the visibility option. | 345 // Browser actions should have the visibility option. |
344 EXPECT_NE(-1, index); | 346 EXPECT_NE(-1, index); |
345 // Since the action is currently visible, it should have the option to hide | 347 // Since the action is currently visible, it should have the option to hide |
346 // it. | 348 // it. |
347 EXPECT_EQ(hide_string, menu.GetLabelAt(index)); | 349 EXPECT_EQ(hide_string, menu.GetLabelAt(index)); |
348 } | 350 } |
349 | 351 |
350 // Enabling the toolbar redesign switch should give page actions the button. | |
351 FeatureSwitch::ScopedOverride enable_toolbar_redesign( | |
352 FeatureSwitch::extension_action_redesign(), true); | |
353 { | 352 { |
354 ExtensionContextMenuModel menu(page_action.get(), GetBrowser(), | 353 ExtensionContextMenuModel menu(browser_action.get(), browser, |
| 354 ExtensionContextMenuModel::OVERFLOWED, |
| 355 nullptr); |
| 356 int index = GetCommandIndex(menu, visibility_command); |
| 357 EXPECT_NE(-1, index); |
| 358 // Without the redesign, 'hiding' refers to removing the action from the |
| 359 // toolbar entirely, so even with the action overflowed, the string should |
| 360 // be 'Hide action'. |
| 361 EXPECT_EQ(hide_string, menu.GetLabelAt(index)); |
| 362 |
| 363 ExtensionActionAPI* action_api = ExtensionActionAPI::Get(profile()); |
| 364 // At the start, the action should be visible. |
| 365 EXPECT_TRUE(action_api->GetBrowserActionVisibility(browser_action->id())); |
| 366 menu.ExecuteCommand(visibility_command, 0); |
| 367 // After execution, it should be hidden. |
| 368 EXPECT_FALSE(action_api->GetBrowserActionVisibility(browser_action->id())); |
| 369 |
| 370 // Cleanup - make the action visible again. |
| 371 action_api->SetBrowserActionVisibility(browser_action->id(), true); |
| 372 } |
| 373 } |
| 374 |
| 375 // Test that the "show" and "hide" menu items appear correctly in the extension |
| 376 // context menu with the toolbar redesign. |
| 377 TEST_F(ExtensionContextMenuModelTest, ExtensionContextMenuShowAndHideRedesign) { |
| 378 // Start with the toolbar redesign disabled. |
| 379 scoped_ptr<FeatureSwitch::ScopedOverride> toolbar_redesign_override( |
| 380 new FeatureSwitch::ScopedOverride( |
| 381 FeatureSwitch::extension_action_redesign(), true)); |
| 382 |
| 383 InitializeEmptyExtensionService(); |
| 384 Browser* browser = GetBrowser(); |
| 385 extension_action_test_util::CreateToolbarModelForProfile(profile()); |
| 386 scoped_refptr<const Extension> page_action = |
| 387 BuildExtension("page_action_extension", |
| 388 manifest_keys::kPageAction, |
| 389 Manifest::INTERNAL); |
| 390 ASSERT_TRUE(page_action.get()); |
| 391 scoped_refptr<const Extension> browser_action = |
| 392 BuildExtension("browser_action_extension", |
| 393 manifest_keys::kBrowserAction, |
| 394 Manifest::INTERNAL); |
| 395 ASSERT_TRUE(browser_action.get()); |
| 396 |
| 397 service()->AddExtension(page_action.get()); |
| 398 service()->AddExtension(browser_action.get()); |
| 399 |
| 400 // For laziness. |
| 401 const ExtensionContextMenuModel::MenuEntries visibility_command = |
| 402 ExtensionContextMenuModel::TOGGLE_VISIBILITY; |
| 403 base::string16 redesign_hide_string = |
| 404 l10n_util::GetStringUTF16(IDS_EXTENSIONS_HIDE_BUTTON_IN_MENU); |
| 405 base::string16 redesign_show_string = |
| 406 l10n_util::GetStringUTF16(IDS_EXTENSIONS_SHOW_BUTTON_IN_TOOLBAR); |
| 407 base::string16 redesign_keep_string = |
| 408 l10n_util::GetStringUTF16(IDS_EXTENSIONS_KEEP_BUTTON_IN_TOOLBAR); |
| 409 |
| 410 { |
| 411 // Even page actions should have a visibility option with the redesign on. |
| 412 ExtensionContextMenuModel menu(page_action.get(), browser, |
355 ExtensionContextMenuModel::VISIBLE, nullptr); | 413 ExtensionContextMenuModel::VISIBLE, nullptr); |
356 int index = GetCommandIndex(menu, visibility_command); | 414 int index = GetCommandIndex(menu, visibility_command); |
357 EXPECT_NE(-1, index); | 415 EXPECT_NE(-1, index); |
358 EXPECT_EQ(redesign_hide_string, menu.GetLabelAt(index)); | 416 EXPECT_EQ(redesign_hide_string, menu.GetLabelAt(index)); |
359 } | 417 } |
360 | 418 |
361 { | 419 { |
| 420 ExtensionContextMenuModel menu(browser_action.get(), browser, |
| 421 ExtensionContextMenuModel::VISIBLE, nullptr); |
| 422 int index = GetCommandIndex(menu, visibility_command); |
| 423 EXPECT_NE(-1, index); |
| 424 EXPECT_EQ(redesign_hide_string, menu.GetLabelAt(index)); |
| 425 |
| 426 ExtensionActionAPI* action_api = ExtensionActionAPI::Get(profile()); |
| 427 EXPECT_TRUE(action_api->GetBrowserActionVisibility(browser_action->id())); |
| 428 // Executing the 'hide' command shouldn't modify the prefs with the redesign |
| 429 // turned on (the ordering behavior is tested in ToolbarActionsModel tests). |
| 430 menu.ExecuteCommand(visibility_command, 0); |
| 431 EXPECT_TRUE(action_api->GetBrowserActionVisibility(browser_action->id())); |
| 432 } |
| 433 |
| 434 { |
362 // If the action is overflowed, it should have the "Show button in toolbar" | 435 // If the action is overflowed, it should have the "Show button in toolbar" |
363 // string. | 436 // string. |
364 ExtensionContextMenuModel menu(browser_action.get(), GetBrowser(), | 437 ExtensionContextMenuModel menu(browser_action.get(), browser, |
365 ExtensionContextMenuModel::OVERFLOWED, | 438 ExtensionContextMenuModel::OVERFLOWED, |
366 nullptr); | 439 nullptr); |
367 int index = GetCommandIndex(menu, visibility_command); | 440 int index = GetCommandIndex(menu, visibility_command); |
368 EXPECT_NE(-1, index); | 441 EXPECT_NE(-1, index); |
369 EXPECT_EQ(redesign_show_string, menu.GetLabelAt(index)); | 442 EXPECT_EQ(redesign_show_string, menu.GetLabelAt(index)); |
370 } | 443 } |
371 | 444 |
372 { | 445 { |
373 // If the action is transitively visible, as happens when it is showing a | 446 // If the action is transitively visible, as happens when it is showing a |
374 // popup, we should use a "Keep button in toolbar" string. | 447 // popup, we should use a "Keep button in toolbar" string. |
375 ExtensionContextMenuModel menu( | 448 ExtensionContextMenuModel menu( |
376 browser_action.get(), GetBrowser(), | 449 browser_action.get(), browser, |
377 ExtensionContextMenuModel::TRANSITIVELY_VISIBLE, nullptr); | 450 ExtensionContextMenuModel::TRANSITIVELY_VISIBLE, nullptr); |
378 int index = GetCommandIndex(menu, visibility_command); | 451 int index = GetCommandIndex(menu, visibility_command); |
379 EXPECT_NE(-1, index); | 452 EXPECT_NE(-1, index); |
380 EXPECT_EQ(redesign_keep_string, menu.GetLabelAt(index)); | 453 EXPECT_EQ(redesign_keep_string, menu.GetLabelAt(index)); |
381 } | 454 } |
382 } | 455 } |
383 | 456 |
384 TEST_F(ExtensionContextMenuModelTest, ExtensionContextUninstall) { | 457 TEST_F(ExtensionContextMenuModelTest, ExtensionContextUninstall) { |
385 InitializeEmptyExtensionService(); | 458 InitializeEmptyExtensionService(); |
386 | 459 |
(...skipping 13 matching lines...) Expand all Loading... |
400 ExtensionContextMenuModel menu(extension.get(), GetBrowser(), | 473 ExtensionContextMenuModel menu(extension.get(), GetBrowser(), |
401 ExtensionContextMenuModel::VISIBLE, nullptr); | 474 ExtensionContextMenuModel::VISIBLE, nullptr); |
402 menu.ExecuteCommand(ExtensionContextMenuModel::UNINSTALL, 0); | 475 menu.ExecuteCommand(ExtensionContextMenuModel::UNINSTALL, 0); |
403 } | 476 } |
404 uninstalled_observer.WaitForExtensionUninstalled(); | 477 uninstalled_observer.WaitForExtensionUninstalled(); |
405 EXPECT_FALSE(registry()->GetExtensionById(extension_id, | 478 EXPECT_FALSE(registry()->GetExtensionById(extension_id, |
406 ExtensionRegistry::EVERYTHING)); | 479 ExtensionRegistry::EVERYTHING)); |
407 } | 480 } |
408 | 481 |
409 } // namespace extensions | 482 } // namespace extensions |
OLD | NEW |