background_mode_manager_unittest.cc revision 3345a6884c488ff3a535c2c9acdd33d74b37e311
1// Copyright (c) 2010 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 "base/command_line.h"
6#include "base/scoped_ptr.h"
7#include "chrome/browser/background_mode_manager.h"
8#include "chrome/browser/browser_list.h"
9#include "chrome/browser/prefs/pref_service.h"
10#include "chrome/common/chrome_switches.h"
11#include "chrome/common/pref_names.h"
12#include "chrome/test/testing_profile.h"
13#include "testing/gmock/include/gmock/gmock.h"
14#include "testing/gtest/include/gtest/gtest.h"
15
16using testing::InSequence;
17
18class BackgroundModeManagerTest : public testing::Test {
19 public:
20  BackgroundModeManagerTest() {}
21  ~BackgroundModeManagerTest() {}
22  void SetUp() {
23    command_line_.reset(new CommandLine(CommandLine::ARGUMENTS_ONLY));
24    command_line_->AppendSwitch(switches::kEnableBackgroundMode);
25  }
26  scoped_ptr<CommandLine> command_line_;
27};
28
29class TestBackgroundModeManager : public BackgroundModeManager {
30 public:
31  TestBackgroundModeManager(Profile* profile, CommandLine* cl)
32      : BackgroundModeManager(profile, cl) {
33  }
34  MOCK_METHOD1(EnableLaunchOnStartup, void(bool));
35  MOCK_METHOD0(CreateStatusTrayIcon, void());
36  MOCK_METHOD0(RemoveStatusTrayIcon, void());
37};
38
39TEST_F(BackgroundModeManagerTest, BackgroundAppLoadUnload) {
40  InSequence s;
41  TestingProfile profile;
42  TestBackgroundModeManager manager(&profile, command_line_.get());
43  EXPECT_CALL(manager, CreateStatusTrayIcon());
44  EXPECT_CALL(manager, RemoveStatusTrayIcon());
45  EXPECT_FALSE(BrowserList::WillKeepAlive());
46  // Call to AppLoaded() will cause the status tray to be created, then call to
47  // unloaded will result in call to remove the icon.
48  manager.OnBackgroundAppLoaded();
49  EXPECT_TRUE(BrowserList::WillKeepAlive());
50  manager.OnBackgroundAppUnloaded();
51  EXPECT_FALSE(BrowserList::WillKeepAlive());
52}
53
54TEST_F(BackgroundModeManagerTest, BackgroundAppInstallUninstall) {
55  InSequence s;
56  TestingProfile profile;
57  TestBackgroundModeManager manager(&profile, command_line_.get());
58  // Call to AppInstalled() will cause chrome to be set to launch on startup,
59  // and call to AppUninstalling() set chrome to not launch on startup.
60  EXPECT_CALL(manager, EnableLaunchOnStartup(true));
61  EXPECT_CALL(manager, CreateStatusTrayIcon());
62  EXPECT_CALL(manager, EnableLaunchOnStartup(false));
63  EXPECT_CALL(manager, RemoveStatusTrayIcon());
64  manager.OnBackgroundAppInstalled();
65  manager.OnBackgroundAppLoaded();
66  manager.OnBackgroundAppUninstalled();
67  manager.OnBackgroundAppUnloaded();
68}
69
70TEST_F(BackgroundModeManagerTest, BackgroundPrefDisabled) {
71  InSequence s;
72  TestingProfile profile;
73  profile.GetPrefs()->SetBoolean(prefs::kBackgroundModeEnabled, false);
74  TestBackgroundModeManager manager(&profile, command_line_.get());
75  // Should not change launch on startup status when installing/uninstalling
76  // if background mode is disabled.
77  EXPECT_CALL(manager, EnableLaunchOnStartup(true)).Times(0);
78  EXPECT_CALL(manager, CreateStatusTrayIcon()).Times(0);
79  manager.OnBackgroundAppInstalled();
80  manager.OnBackgroundAppLoaded();
81  EXPECT_FALSE(BrowserList::WillKeepAlive());
82  manager.OnBackgroundAppUninstalled();
83  manager.OnBackgroundAppUnloaded();
84}
85
86TEST_F(BackgroundModeManagerTest, BackgroundPrefDynamicDisable) {
87  InSequence s;
88  TestingProfile profile;
89  TestBackgroundModeManager manager(&profile, command_line_.get());
90  EXPECT_CALL(manager, EnableLaunchOnStartup(true));
91  EXPECT_CALL(manager, CreateStatusTrayIcon());
92  EXPECT_CALL(manager, EnableLaunchOnStartup(false));
93  EXPECT_CALL(manager, RemoveStatusTrayIcon());
94  manager.OnBackgroundAppInstalled();
95  manager.OnBackgroundAppLoaded();
96  EXPECT_TRUE(BrowserList::WillKeepAlive());
97  // Disable status on the fly.
98  profile.GetPrefs()->SetBoolean(prefs::kBackgroundModeEnabled, false);
99  // Manually notify background mode manager that pref has changed
100  manager.OnBackgroundModePrefChanged();
101  EXPECT_FALSE(BrowserList::WillKeepAlive());
102}
103
104TEST_F(BackgroundModeManagerTest, BackgroundPrefDynamicEnable) {
105  InSequence s;
106  TestingProfile profile;
107  TestBackgroundModeManager manager(&profile, command_line_.get());
108  profile.GetPrefs()->SetBoolean(prefs::kBackgroundModeEnabled, false);
109  EXPECT_CALL(manager, EnableLaunchOnStartup(true));
110  EXPECT_CALL(manager, CreateStatusTrayIcon());
111  manager.OnBackgroundAppInstalled();
112  manager.OnBackgroundAppLoaded();
113  EXPECT_FALSE(BrowserList::WillKeepAlive());
114  // Enable status on the fly.
115  profile.GetPrefs()->SetBoolean(prefs::kBackgroundModeEnabled, true);
116  EXPECT_TRUE(BrowserList::WillKeepAlive());
117}
118