1 // Copyright (c) 2013 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.
5 #include "chrome/browser/ui/ash/launcher/launcher_context_menu.h"
7 #include "ash/shelf/shelf.h"
8 #include "ash/shelf/shelf_item_types.h"
9 #include "ash/shelf/shelf_model.h"
10 #include "ash/shell.h"
11 #include "ash/test/ash_test_base.h"
12 #include "base/prefs/pref_service.h"
13 #include "chrome/app/chrome_command_ids.h"
14 #include "chrome/browser/prefs/incognito_mode_prefs.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/ui/ash/launcher/chrome_launcher_controller.h"
17 #include "chrome/test/base/testing_profile.h"
18 #include "ui/aura/window_event_dispatcher.h"
20 class TestChromeLauncherController
: public ChromeLauncherController
{
22 TestChromeLauncherController(Profile
* profile
, ash::ShelfModel
* model
)
23 : ChromeLauncherController(profile
, model
) {}
24 bool IsLoggedInAsGuest() override
{ return false; }
26 DISALLOW_COPY_AND_ASSIGN(TestChromeLauncherController
);
29 class LauncherContextMenuTest
: public ash::test::AshTestBase
{
31 static bool IsItemPresentInMenu(LauncherContextMenu
* menu
, int command_id
) {
33 return menu
->GetIndexOfCommandId(command_id
) != -1;
36 LauncherContextMenuTest()
37 : profile_(new TestingProfile()) {}
39 void SetUp() override
{
40 ash::test::AshTestBase::SetUp();
42 new TestChromeLauncherController(profile(), &shelf_model_
));
45 void TearDown() override
{
46 controller_
.reset(NULL
);
47 ash::test::AshTestBase::TearDown();
50 LauncherContextMenu
* CreateLauncherContextMenu(
51 ash::ShelfItemType shelf_item_type
) {
53 item
.id
= 1; // dummy id
54 item
.type
= shelf_item_type
;
55 return new LauncherContextMenu(controller_
.get(), &item
, CurrentContext());
58 Profile
* profile() { return profile_
.get(); }
61 scoped_ptr
<TestingProfile
> profile_
;
62 ash::ShelfModel shelf_model_
;
63 scoped_ptr
<ChromeLauncherController
> controller_
;
65 DISALLOW_COPY_AND_ASSIGN(LauncherContextMenuTest
);
68 // Verifies that "New Incognito window" menu item in the launcher context
69 // menu is disabled when Incognito mode is switched off (by a policy).
70 TEST_F(LauncherContextMenuTest
,
71 NewIncognitoWindowMenuIsDisabledWhenIncognitoModeOff
) {
72 // Initially, "New Incognito window" should be enabled.
73 scoped_ptr
<LauncherContextMenu
> menu(
74 CreateLauncherContextMenu(ash::TYPE_BROWSER_SHORTCUT
));
75 ASSERT_TRUE(IsItemPresentInMenu(
76 menu
.get(), LauncherContextMenu::MENU_NEW_INCOGNITO_WINDOW
));
77 EXPECT_TRUE(menu
->IsCommandIdEnabled(
78 LauncherContextMenu::MENU_NEW_INCOGNITO_WINDOW
));
80 // Disable Incognito mode.
81 IncognitoModePrefs::SetAvailability(profile()->GetPrefs(),
82 IncognitoModePrefs::DISABLED
);
83 menu
.reset(CreateLauncherContextMenu(ash::TYPE_BROWSER_SHORTCUT
));
84 // The item should be disabled.
85 ASSERT_TRUE(IsItemPresentInMenu(
86 menu
.get(), LauncherContextMenu::MENU_NEW_INCOGNITO_WINDOW
));
87 EXPECT_FALSE(menu
->IsCommandIdEnabled(
88 LauncherContextMenu::MENU_NEW_INCOGNITO_WINDOW
));
91 // Verifies that "New window" menu item in the launcher context
92 // menu is disabled when Incognito mode is forced (by a policy).
93 TEST_F(LauncherContextMenuTest
,
94 NewWindowMenuIsDisabledWhenIncognitoModeForced
) {
95 // Initially, "New window" should be enabled.
96 scoped_ptr
<LauncherContextMenu
> menu(
97 CreateLauncherContextMenu(ash::TYPE_BROWSER_SHORTCUT
));
98 ASSERT_TRUE(IsItemPresentInMenu(
99 menu
.get(), LauncherContextMenu::MENU_NEW_WINDOW
));
100 EXPECT_TRUE(menu
->IsCommandIdEnabled(LauncherContextMenu::MENU_NEW_WINDOW
));
102 // Disable Incognito mode.
103 IncognitoModePrefs::SetAvailability(profile()->GetPrefs(),
104 IncognitoModePrefs::FORCED
);
105 menu
.reset(CreateLauncherContextMenu(ash::TYPE_BROWSER_SHORTCUT
));
106 ASSERT_TRUE(IsItemPresentInMenu(
107 menu
.get(), LauncherContextMenu::MENU_NEW_WINDOW
));
108 EXPECT_FALSE(menu
->IsCommandIdEnabled(LauncherContextMenu::MENU_NEW_WINDOW
));