launcher_context_menu_unittest.cc revision c2e0dbddbe15c98d52c4786dac06cb8952a8ae6d
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.
4
5#include "chrome/browser/ui/ash/launcher/launcher_context_menu.h"
6
7#include "ash/launcher/launcher.h"
8#include "ash/launcher/launcher_model.h"
9#include "ash/launcher/launcher_types.h"
10#include "ash/test/ash_test_base.h"
11#include "base/prefs/pref_service.h"
12#include "chrome/app/chrome_command_ids.h"
13#include "chrome/browser/prefs/incognito_mode_prefs.h"
14#include "chrome/browser/profiles/profile.h"
15#include "chrome/browser/ui/ash/launcher/chrome_launcher_controller.h"
16#include "chrome/browser/ui/ash/launcher/chrome_launcher_controller_per_browser.h"
17#include "chrome/test/base/testing_profile.h"
18#include "content/public/test/test_browser_thread.h"
19#include "ui/aura/root_window.h"
20
21class TestChromeLauncherControllerPerBrowser :
22    public ChromeLauncherControllerPerBrowser {
23 public:
24  TestChromeLauncherControllerPerBrowser(
25      Profile* profile, ash::LauncherModel* model)
26      : ChromeLauncherControllerPerBrowser(profile, model) {}
27  virtual bool IsLoggedInAsGuest() OVERRIDE {
28    return false;
29  }
30 private:
31  DISALLOW_COPY_AND_ASSIGN(TestChromeLauncherControllerPerBrowser);
32};
33
34class LauncherContextMenuTest : public ash::test::AshTestBase {
35 protected:
36  static bool IsItemPresentInMenu(LauncherContextMenu* menu, int command_id) {
37    DCHECK(menu);
38    return menu->GetIndexOfCommandId(command_id) != -1;
39  }
40
41  LauncherContextMenuTest()
42      : ash::test::AshTestBase(),
43        profile_(new TestingProfile()),
44        browser_thread_(content::BrowserThread::UI, message_loop()) {}
45
46  virtual void SetUp() OVERRIDE {
47    ash::test::AshTestBase::SetUp();
48    controller_.reset(
49        new TestChromeLauncherControllerPerBrowser(profile(),
50                                                   &launcher_model_));
51  }
52
53  virtual void TearDown() OVERRIDE {
54    controller_.reset(NULL);
55    ash::test::AshTestBase::TearDown();
56  }
57
58  LauncherContextMenu* CreateLauncherContextMenu(
59      ash::LauncherItemType launcher_item_type) {
60    ash::LauncherItem item;
61    item.id = 1;  // dummy id
62    item.type = launcher_item_type;
63    return new LauncherContextMenu(controller_.get(), &item, CurrentContext());
64  }
65
66  Profile* profile() { return profile_.get(); }
67
68 private:
69  scoped_ptr<TestingProfile> profile_;
70  content::TestBrowserThread browser_thread_;
71  ash::LauncherModel launcher_model_;
72  scoped_ptr<ChromeLauncherController> controller_;
73
74  DISALLOW_COPY_AND_ASSIGN(LauncherContextMenuTest);
75};
76
77// Verifies that "New Incognito window" menu item in the launcher context
78// menu is disabled when Incognito mode is switched off (by a policy).
79TEST_F(LauncherContextMenuTest,
80       NewIncognitoWindowMenuIsDisabledWhenIncognitoModeOff) {
81  // Initially, "New Incognito window" should be enabled.
82  scoped_ptr<LauncherContextMenu> menu(
83      CreateLauncherContextMenu(ash::TYPE_BROWSER_SHORTCUT));
84  ASSERT_TRUE(IsItemPresentInMenu(
85      menu.get(), LauncherContextMenu::MENU_NEW_INCOGNITO_WINDOW));
86  EXPECT_TRUE(menu->IsCommandIdEnabled(
87      LauncherContextMenu::MENU_NEW_INCOGNITO_WINDOW));
88
89  // Disable Incognito mode.
90  IncognitoModePrefs::SetAvailability(profile()->GetPrefs(),
91                                      IncognitoModePrefs::DISABLED);
92  menu.reset(CreateLauncherContextMenu(ash::TYPE_BROWSER_SHORTCUT));
93  // The item should be disabled.
94  ASSERT_TRUE(IsItemPresentInMenu(
95      menu.get(), LauncherContextMenu::MENU_NEW_INCOGNITO_WINDOW));
96  EXPECT_FALSE(menu->IsCommandIdEnabled(
97      LauncherContextMenu::MENU_NEW_INCOGNITO_WINDOW));
98}
99
100// Verifies that "New window" menu item in the launcher context
101// menu is disabled when Incognito mode is forced (by a policy).
102TEST_F(LauncherContextMenuTest,
103       NewWindowMenuIsDisabledWhenIncognitoModeForced) {
104  // Initially, "New window" should be enabled.
105  scoped_ptr<LauncherContextMenu> menu(
106      CreateLauncherContextMenu(ash::TYPE_BROWSER_SHORTCUT));
107  ASSERT_TRUE(IsItemPresentInMenu(
108      menu.get(), LauncherContextMenu::MENU_NEW_WINDOW));
109  EXPECT_TRUE(menu->IsCommandIdEnabled(LauncherContextMenu::MENU_NEW_WINDOW));
110
111  // Disable Incognito mode.
112  IncognitoModePrefs::SetAvailability(profile()->GetPrefs(),
113                                      IncognitoModePrefs::FORCED);
114  menu.reset(CreateLauncherContextMenu(ash::TYPE_BROWSER_SHORTCUT));
115  ASSERT_TRUE(IsItemPresentInMenu(
116      menu.get(), LauncherContextMenu::MENU_NEW_WINDOW));
117  EXPECT_FALSE(menu->IsCommandIdEnabled(LauncherContextMenu::MENU_NEW_WINDOW));
118}
119