1 // Copyright (c) 2011 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/memory/scoped_ptr.h"
7 #include "chrome/browser/background_mode_manager.h"
8 #include "chrome/browser/ui/browser_list.h"
9 #include "chrome/common/chrome_switches.h"
10 #include "chrome/test/testing_browser_process.h"
11 #include "chrome/test/testing_browser_process_test.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
16 using testing::InSequence;
17
18 class BackgroundModeManagerTest : public TestingBrowserProcessTest {
19 public:
BackgroundModeManagerTest()20 BackgroundModeManagerTest() {}
~BackgroundModeManagerTest()21 ~BackgroundModeManagerTest() {}
SetUp()22 void SetUp() {
23 command_line_.reset(new CommandLine(CommandLine::NO_PROGRAM));
24 }
25 scoped_ptr<CommandLine> command_line_;
26 };
27
28 class TestBackgroundModeManager : public BackgroundModeManager {
29 public:
TestBackgroundModeManager(Profile * profile,CommandLine * cl)30 TestBackgroundModeManager(Profile* profile, CommandLine* cl)
31 : BackgroundModeManager(profile, cl) {
32 }
33 MOCK_METHOD1(EnableLaunchOnStartup, void(bool));
34 MOCK_METHOD0(CreateStatusTrayIcon, void());
35 MOCK_METHOD0(RemoveStatusTrayIcon, void());
36 };
37
TEST_F(BackgroundModeManagerTest,BackgroundAppLoadUnload)38 TEST_F(BackgroundModeManagerTest, BackgroundAppLoadUnload) {
39 InSequence s;
40 TestingProfile profile;
41 TestBackgroundModeManager manager(&profile, command_line_.get());
42 EXPECT_CALL(manager, CreateStatusTrayIcon());
43 EXPECT_CALL(manager, RemoveStatusTrayIcon());
44 EXPECT_FALSE(BrowserList::WillKeepAlive());
45 // Call to AppLoaded() will cause the status tray to be created, then call to
46 // unloaded will result in call to remove the icon.
47 manager.OnBackgroundAppLoaded();
48 EXPECT_TRUE(BrowserList::WillKeepAlive());
49 manager.OnBackgroundAppUnloaded();
50 EXPECT_FALSE(BrowserList::WillKeepAlive());
51 }
52
TEST_F(BackgroundModeManagerTest,BackgroundAppInstallUninstall)53 TEST_F(BackgroundModeManagerTest, BackgroundAppInstallUninstall) {
54 InSequence s;
55 TestingProfile profile;
56 TestBackgroundModeManager manager(&profile, command_line_.get());
57 // Call to AppInstalled() will cause chrome to be set to launch on startup,
58 // and call to AppUninstalled() set chrome to not launch on startup.
59 EXPECT_CALL(manager, EnableLaunchOnStartup(true));
60 EXPECT_CALL(manager, CreateStatusTrayIcon());
61 EXPECT_CALL(manager, RemoveStatusTrayIcon());
62 EXPECT_CALL(manager, EnableLaunchOnStartup(false));
63 manager.OnBackgroundAppInstalled(NULL);
64 manager.OnBackgroundAppLoaded();
65 manager.OnBackgroundAppUnloaded();
66 manager.OnBackgroundAppUninstalled();
67 }
68