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/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"
19
20class TestChromeLauncherController : public ChromeLauncherController {
21 public:
22  TestChromeLauncherController(Profile* profile, ash::ShelfModel* model)
23      : ChromeLauncherController(profile, model) {}
24  virtual bool IsLoggedInAsGuest() OVERRIDE {
25    return false;
26  }
27 private:
28  DISALLOW_COPY_AND_ASSIGN(TestChromeLauncherController);
29};
30
31class LauncherContextMenuTest : public ash::test::AshTestBase {
32 protected:
33  static bool IsItemPresentInMenu(LauncherContextMenu* menu, int command_id) {
34    DCHECK(menu);
35    return menu->GetIndexOfCommandId(command_id) != -1;
36  }
37
38  LauncherContextMenuTest()
39      : profile_(new TestingProfile()) {}
40
41  virtual void SetUp() OVERRIDE {
42    ash::test::AshTestBase::SetUp();
43    controller_.reset(
44        new TestChromeLauncherController(profile(), &shelf_model_));
45  }
46
47  virtual void TearDown() OVERRIDE {
48    controller_.reset(NULL);
49    ash::test::AshTestBase::TearDown();
50  }
51
52  LauncherContextMenu* CreateLauncherContextMenu(
53      ash::ShelfItemType shelf_item_type) {
54    ash::ShelfItem item;
55    item.id = 1;  // dummy id
56    item.type = shelf_item_type;
57    return new LauncherContextMenu(controller_.get(), &item, CurrentContext());
58  }
59
60  Profile* profile() { return profile_.get(); }
61
62 private:
63  scoped_ptr<TestingProfile> profile_;
64  ash::ShelfModel shelf_model_;
65  scoped_ptr<ChromeLauncherController> controller_;
66
67  DISALLOW_COPY_AND_ASSIGN(LauncherContextMenuTest);
68};
69
70// Verifies that "New Incognito window" menu item in the launcher context
71// menu is disabled when Incognito mode is switched off (by a policy).
72TEST_F(LauncherContextMenuTest,
73       NewIncognitoWindowMenuIsDisabledWhenIncognitoModeOff) {
74  // Initially, "New Incognito window" should be enabled.
75  scoped_ptr<LauncherContextMenu> menu(
76      CreateLauncherContextMenu(ash::TYPE_BROWSER_SHORTCUT));
77  ASSERT_TRUE(IsItemPresentInMenu(
78      menu.get(), LauncherContextMenu::MENU_NEW_INCOGNITO_WINDOW));
79  EXPECT_TRUE(menu->IsCommandIdEnabled(
80      LauncherContextMenu::MENU_NEW_INCOGNITO_WINDOW));
81
82  // Disable Incognito mode.
83  IncognitoModePrefs::SetAvailability(profile()->GetPrefs(),
84                                      IncognitoModePrefs::DISABLED);
85  menu.reset(CreateLauncherContextMenu(ash::TYPE_BROWSER_SHORTCUT));
86  // The item should be disabled.
87  ASSERT_TRUE(IsItemPresentInMenu(
88      menu.get(), LauncherContextMenu::MENU_NEW_INCOGNITO_WINDOW));
89  EXPECT_FALSE(menu->IsCommandIdEnabled(
90      LauncherContextMenu::MENU_NEW_INCOGNITO_WINDOW));
91}
92
93// Verifies that "New window" menu item in the launcher context
94// menu is disabled when Incognito mode is forced (by a policy).
95TEST_F(LauncherContextMenuTest,
96       NewWindowMenuIsDisabledWhenIncognitoModeForced) {
97  // Initially, "New window" should be enabled.
98  scoped_ptr<LauncherContextMenu> menu(
99      CreateLauncherContextMenu(ash::TYPE_BROWSER_SHORTCUT));
100  ASSERT_TRUE(IsItemPresentInMenu(
101      menu.get(), LauncherContextMenu::MENU_NEW_WINDOW));
102  EXPECT_TRUE(menu->IsCommandIdEnabled(LauncherContextMenu::MENU_NEW_WINDOW));
103
104  // Disable Incognito mode.
105  IncognitoModePrefs::SetAvailability(profile()->GetPrefs(),
106                                      IncognitoModePrefs::FORCED);
107  menu.reset(CreateLauncherContextMenu(ash::TYPE_BROWSER_SHORTCUT));
108  ASSERT_TRUE(IsItemPresentInMenu(
109      menu.get(), LauncherContextMenu::MENU_NEW_WINDOW));
110  EXPECT_FALSE(menu->IsCommandIdEnabled(LauncherContextMenu::MENU_NEW_WINDOW));
111}
112