OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/command_line.h" | 5 #include "base/command_line.h" |
6 #include "base/memory/scoped_ptr.h" | 6 #include "base/memory/scoped_ptr.h" |
7 #include "chrome/browser/background_mode_manager.h" | 7 #include "chrome/browser/background_mode_manager.h" |
8 #include "chrome/browser/ui/browser_list.h" | 8 #include "chrome/browser/ui/browser_list.h" |
9 #include "chrome/common/chrome_switches.h" | 9 #include "chrome/common/chrome_switches.h" |
10 #include "chrome/test/testing_browser_process.h" | 10 #include "chrome/test/testing_browser_process.h" |
11 #include "chrome/test/testing_browser_process_test.h" | 11 #include "chrome/test/testing_browser_process_test.h" |
12 #include "chrome/test/testing_profile.h" | 12 #include "chrome/test/testing_profile.h" |
13 #include "testing/gmock/include/gmock/gmock.h" | 13 #include "testing/gmock/include/gmock/gmock.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
15 | 15 |
16 using testing::_; | |
16 using testing::AtLeast; | 17 using testing::AtLeast; |
17 using testing::InSequence; | 18 using testing::InSequence; |
18 using testing::Return; | 19 using testing::Return; |
19 | 20 |
20 class BackgroundModeManagerTest : public TestingBrowserProcessTest { | 21 class BackgroundModeManagerTest : public TestingBrowserProcessTest { |
21 public: | 22 public: |
22 BackgroundModeManagerTest() {} | 23 BackgroundModeManagerTest() {} |
23 ~BackgroundModeManagerTest() {} | 24 ~BackgroundModeManagerTest() {} |
24 void SetUp() { | 25 void SetUp() { |
25 command_line_.reset(new CommandLine(CommandLine::NO_PROGRAM)); | 26 command_line_.reset(new CommandLine(CommandLine::NO_PROGRAM)); |
26 } | 27 } |
27 scoped_ptr<CommandLine> command_line_; | 28 scoped_ptr<CommandLine> command_line_; |
28 }; | 29 }; |
29 | 30 |
30 class TestBackgroundModeManager : public BackgroundModeManager { | 31 class TestBackgroundModeManager : public BackgroundModeManager { |
31 public: | 32 public: |
32 TestBackgroundModeManager(Profile* profile, CommandLine* cl) | 33 explicit TestBackgroundModeManager(CommandLine* command_line) |
33 : BackgroundModeManager(profile, cl), | 34 : BackgroundModeManager(command_line), |
34 enabled_(true) {} | 35 enabled_(true) {} |
35 MOCK_METHOD1(EnableLaunchOnStartup, void(bool)); | 36 MOCK_METHOD1(EnableLaunchOnStartup, void(bool)); |
36 MOCK_METHOD0(CreateStatusTrayIcon, void()); | 37 MOCK_METHOD1(CreateStatusTrayIcon, void(Profile*)); // NOLINT |
37 MOCK_METHOD0(RemoveStatusTrayIcon, void()); | 38 MOCK_METHOD1(RemoveStatusTrayIcon, void(Profile*)); // NOLINT |
38 virtual bool IsBackgroundModePrefEnabled() { return enabled_; } | 39 virtual bool IsBackgroundModePrefEnabled() { return enabled_; } |
39 void SetEnabled(bool enabled) { enabled_ = enabled; } | 40 void SetEnabled(bool enabled) { enabled_ = enabled; } |
40 private: | 41 private: |
41 bool enabled_; | 42 bool enabled_; |
42 }; | 43 }; |
43 | 44 |
44 TEST_F(BackgroundModeManagerTest, BackgroundAppLoadUnload) { | 45 TEST_F(BackgroundModeManagerTest, BackgroundAppLoadUnload) { |
45 InSequence s; | 46 InSequence s; |
46 TestingProfile profile; | 47 TestingProfile profile; |
47 TestBackgroundModeManager manager(&profile, command_line_.get()); | 48 TestBackgroundModeManager manager(command_line_.get()); |
48 EXPECT_CALL(manager, CreateStatusTrayIcon()); | 49 manager.RegisterProfile(&profile); |
49 EXPECT_CALL(manager, RemoveStatusTrayIcon()); | 50 EXPECT_CALL(manager, RemoveStatusTrayIcon(_)); |
50 EXPECT_FALSE(BrowserList::WillKeepAlive()); | 51 EXPECT_FALSE(BrowserList::WillKeepAlive()); |
51 // Call to AppLoaded() will cause the status tray to be created, then call to | 52 // Call to AppLoaded() will not cause the status tray to be created, |
52 // unloaded will result in call to remove the icon. | 53 // because no apps have been installed. However the call to AppUnloaded() |
54 // will result in a call RemoveStatusTrayIcon since it will try to unload | |
55 // all icons now that there are no apps | |
Andrew T Wilson (Slow)
2011/05/23 20:35:40
nit: period at end of sentence.
rpetterson
2011/05/24 02:18:01
Done.
| |
53 manager.OnBackgroundAppLoaded(); | 56 manager.OnBackgroundAppLoaded(); |
54 EXPECT_TRUE(BrowserList::WillKeepAlive()); | 57 EXPECT_TRUE(BrowserList::WillKeepAlive()); |
55 manager.OnBackgroundAppUnloaded(); | 58 manager.OnBackgroundAppUnloaded(); |
56 EXPECT_FALSE(BrowserList::WillKeepAlive()); | 59 EXPECT_FALSE(BrowserList::WillKeepAlive()); |
57 } | 60 } |
58 | 61 |
59 TEST_F(BackgroundModeManagerTest, BackgroundAppInstallUninstall) { | 62 TEST_F(BackgroundModeManagerTest, BackgroundAppInstallUninstall) { |
60 InSequence s; | 63 InSequence s; |
61 TestingProfile profile; | 64 TestingProfile profile; |
62 TestBackgroundModeManager manager(&profile, command_line_.get()); | 65 TestBackgroundModeManager manager(command_line_.get()); |
66 manager.RegisterProfile(&profile); | |
63 // Call to AppInstalled() will cause chrome to be set to launch on startup, | 67 // Call to AppInstalled() will cause chrome to be set to launch on startup, |
64 // and call to AppUninstalled() set chrome to not launch on startup. | 68 // and call to AppUninstalled() set chrome to not launch on startup. |
65 EXPECT_CALL(manager, EnableLaunchOnStartup(true)); | 69 EXPECT_CALL(manager, EnableLaunchOnStartup(true)); |
66 EXPECT_CALL(manager, CreateStatusTrayIcon()); | 70 EXPECT_CALL(manager, CreateStatusTrayIcon(_)); |
67 EXPECT_CALL(manager, RemoveStatusTrayIcon()); | 71 EXPECT_CALL(manager, RemoveStatusTrayIcon(_)).Times(2); |
68 EXPECT_CALL(manager, EnableLaunchOnStartup(false)); | 72 EXPECT_CALL(manager, EnableLaunchOnStartup(false)); |
69 manager.OnBackgroundAppInstalled(NULL); | 73 manager.OnBackgroundAppInstalled(NULL, &profile); |
70 manager.OnBackgroundAppLoaded(); | 74 manager.OnBackgroundAppLoaded(); |
71 manager.OnBackgroundAppUnloaded(); | 75 manager.OnBackgroundAppUnloaded(); |
72 manager.OnBackgroundAppUninstalled(); | 76 manager.OnBackgroundAppUninstalled(&profile);} |
73 } | |
74 | 77 |
75 // App installs while disabled should do nothing. | 78 // App installs while disabled should do nothing. |
76 TEST_F(BackgroundModeManagerTest, BackgroundAppInstallUninstallWhileDisabled) { | 79 TEST_F(BackgroundModeManagerTest, BackgroundAppInstallUninstallWhileDisabled) { |
77 InSequence s; | 80 InSequence s; |
78 TestingProfile profile; | 81 TestingProfile profile; |
79 TestBackgroundModeManager manager(&profile, command_line_.get()); | 82 TestBackgroundModeManager manager(command_line_.get()); |
83 manager.RegisterProfile(&profile); | |
80 // Turn off background mode. | 84 // Turn off background mode. |
85 EXPECT_CALL(manager, RemoveStatusTrayIcon(_)); | |
81 manager.SetEnabled(false); | 86 manager.SetEnabled(false); |
82 manager.DisableBackgroundMode(); | 87 manager.DisableBackgroundMode(); |
83 | 88 |
84 // Status tray icons will not be created, launch on startup status will be set | 89 // Status tray icons will not be created, launch on startup status will be set |
85 // to "do not launch on startup". | 90 // to "do not launch on startup". |
86 EXPECT_CALL(manager, EnableLaunchOnStartup(false)); | 91 EXPECT_CALL(manager, EnableLaunchOnStartup(false)); |
87 manager.OnBackgroundAppInstalled(NULL); | 92 manager.OnBackgroundAppInstalled(NULL, &profile); |
88 manager.OnBackgroundAppLoaded(); | 93 manager.OnBackgroundAppLoaded(); |
89 manager.OnBackgroundAppUnloaded(); | 94 manager.OnBackgroundAppUnloaded(); |
90 manager.OnBackgroundAppUninstalled(); | 95 manager.OnBackgroundAppUninstalled(&profile); |
91 | 96 |
92 // Re-enable background mode. | 97 // Re-enable background mode. |
93 manager.SetEnabled(true); | 98 manager.SetEnabled(true); |
94 manager.EnableBackgroundMode(); | 99 manager.EnableBackgroundMode(); |
95 } | 100 } |
96 | 101 |
97 | 102 |
98 // App installs while disabled should do nothing. | 103 // App installs while disabled should do nothing. |
99 TEST_F(BackgroundModeManagerTest, EnableAfterBackgroundAppInstall) { | 104 TEST_F(BackgroundModeManagerTest, EnableAfterBackgroundAppInstall) { |
100 InSequence s; | 105 InSequence s; |
101 TestingProfile profile; | 106 TestingProfile profile; |
102 TestBackgroundModeManager manager(&profile, command_line_.get()); | 107 TestBackgroundModeManager manager(command_line_.get()); |
108 manager.RegisterProfile(&profile); | |
103 EXPECT_CALL(manager, EnableLaunchOnStartup(true)); | 109 EXPECT_CALL(manager, EnableLaunchOnStartup(true)); |
104 EXPECT_CALL(manager, CreateStatusTrayIcon()); | 110 EXPECT_CALL(manager, CreateStatusTrayIcon(_)); |
105 EXPECT_CALL(manager, RemoveStatusTrayIcon()); | 111 EXPECT_CALL(manager, RemoveStatusTrayIcon(_)); |
106 EXPECT_CALL(manager, EnableLaunchOnStartup(false)); | 112 EXPECT_CALL(manager, EnableLaunchOnStartup(false)); |
107 EXPECT_CALL(manager, CreateStatusTrayIcon()); | 113 //EXPECT_CALL(manager, CreateStatusTrayIcon(_)); |
108 EXPECT_CALL(manager, EnableLaunchOnStartup(true)); | 114 EXPECT_CALL(manager, EnableLaunchOnStartup(true)); |
109 EXPECT_CALL(manager, RemoveStatusTrayIcon()); | 115 EXPECT_CALL(manager, RemoveStatusTrayIcon(_)).Times(2); |
110 EXPECT_CALL(manager, EnableLaunchOnStartup(false)); | 116 EXPECT_CALL(manager, EnableLaunchOnStartup(false)); |
111 | 117 |
112 // Install app, should show status tray icon. | 118 // Install app, should show status tray icon. |
113 manager.OnBackgroundAppInstalled(NULL); | 119 manager.OnBackgroundAppInstalled(NULL, &profile); |
120 // OnBackgroundAppInstalled does not actually add an app to the | |
121 // BackgroundApplicationListModel which would result in another | |
122 // call to CreateStatusTray | |
Andrew T Wilson (Slow)
2011/05/23 20:35:40
nit: period at end of sentence.
rpetterson
2011/05/24 02:18:01
Done.
| |
114 manager.OnBackgroundAppLoaded(); | 123 manager.OnBackgroundAppLoaded(); |
115 | 124 |
116 // Turn off background mode - should hide status tray icon. | 125 // Turn off background mode - should hide status tray icon. |
117 manager.SetEnabled(false); | 126 manager.SetEnabled(false); |
118 manager.DisableBackgroundMode(); | 127 manager.DisableBackgroundMode(); |
119 | 128 |
120 // Turn back on background mode - should show status tray icon. | 129 // Turn back on background mode - again, no status tray icon |
130 // will show up since we didn't actually add anything to the list. | |
121 manager.SetEnabled(true); | 131 manager.SetEnabled(true); |
122 manager.EnableBackgroundMode(); | 132 manager.EnableBackgroundMode(); |
123 | 133 |
124 // Uninstall app, should hide status tray icon again. | 134 // Uninstall app, should hide status tray icon again. |
125 manager.OnBackgroundAppUnloaded(); | 135 manager.OnBackgroundAppUnloaded(); |
126 manager.OnBackgroundAppUninstalled(); | 136 manager.OnBackgroundAppUninstalled(&profile); |
127 } | 137 } |
138 | |
139 TEST_F(BackgroundModeManagerTest, MultiProfile) { | |
140 InSequence s; | |
141 TestingProfile profile1; | |
142 TestingProfile profile2; | |
143 TestBackgroundModeManager manager(command_line_.get()); | |
144 manager.RegisterProfile(&profile1); | |
145 manager.RegisterProfile(&profile2); | |
146 EXPECT_CALL(manager, EnableLaunchOnStartup(true)); | |
147 EXPECT_CALL(manager, CreateStatusTrayIcon(_)).Times(2); | |
148 EXPECT_CALL(manager, RemoveStatusTrayIcon(_)).Times(2); | |
149 EXPECT_CALL(manager, EnableLaunchOnStartup(false)); | |
150 EXPECT_CALL(manager, EnableLaunchOnStartup(true)); | |
151 EXPECT_CALL(manager, RemoveStatusTrayIcon(_)).Times(4); | |
152 EXPECT_CALL(manager, EnableLaunchOnStartup(false)); | |
153 EXPECT_FALSE(BrowserList::WillKeepAlive()); | |
154 | |
155 // Install app, should show status tray icon. | |
156 manager.OnBackgroundAppInstalled(NULL, &profile1); | |
157 // OnBackgroundAppInstalled does not actually add an app to the | |
158 // BackgroundApplicationListModel which would result in another | |
159 // call to CreateStatusTray | |
Andrew T Wilson (Slow)
2011/05/23 20:35:40
nit: period at end.
rpetterson
2011/05/24 02:18:01
Done.
| |
160 manager.OnBackgroundAppLoaded(); | |
161 | |
162 // Install app for other profile, hsould show other status tray icon. | |
163 manager.OnBackgroundAppInstalled(NULL, &profile2); | |
164 manager.OnBackgroundAppLoaded(); | |
165 | |
166 // Should hide both status tray icons. | |
167 manager.SetEnabled(false); | |
168 manager.DisableBackgroundMode(); | |
169 | |
170 // Turn back on background mode - should show both status tray icons. | |
171 manager.SetEnabled(true); | |
172 manager.EnableBackgroundMode(); | |
173 | |
174 manager.OnBackgroundAppUnloaded(); | |
175 manager.OnBackgroundAppUninstalled(&profile1); | |
176 // There is still one background app alive | |
177 EXPECT_TRUE(BrowserList::WillKeepAlive()); | |
178 manager.OnBackgroundAppUnloaded(); | |
179 manager.OnBackgroundAppUninstalled(&profile2); | |
180 EXPECT_FALSE(BrowserList::WillKeepAlive()); | |
181 } | |
OLD | NEW |