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 "base/utf_string_conversions.h" | 7 #include "base/utf_string_conversions.h" |
8 #include "chrome/browser/background/background_mode_manager.h" | 8 #include "chrome/browser/background/background_mode_manager.h" |
9 #include "chrome/browser/lifetime/application_lifetime.h" | 9 #include "chrome/browser/lifetime/application_lifetime.h" |
10 #include "chrome/browser/profiles/profile_info_cache.h" | 10 #include "chrome/browser/profiles/profile_info_cache.h" |
(...skipping 14 matching lines...) Expand all Loading... |
25 void SetUp() { | 25 void SetUp() { |
26 command_line_.reset(new CommandLine(CommandLine::NO_PROGRAM)); | 26 command_line_.reset(new CommandLine(CommandLine::NO_PROGRAM)); |
27 ASSERT_TRUE(profile_manager_.SetUp()); | 27 ASSERT_TRUE(profile_manager_.SetUp()); |
28 } | 28 } |
29 scoped_ptr<CommandLine> command_line_; | 29 scoped_ptr<CommandLine> command_line_; |
30 TestingProfileManager profile_manager_; | 30 TestingProfileManager profile_manager_; |
31 }; | 31 }; |
32 | 32 |
33 class TestBackgroundModeManager : public BackgroundModeManager { | 33 class TestBackgroundModeManager : public BackgroundModeManager { |
34 public: | 34 public: |
35 explicit TestBackgroundModeManager( | 35 TestBackgroundModeManager( |
36 CommandLine* command_line, ProfileInfoCache* cache) | 36 CommandLine* command_line, ProfileInfoCache* cache, bool enabled) |
37 : BackgroundModeManager(command_line, cache), | 37 : BackgroundModeManager(command_line, cache), |
38 enabled_(true), | 38 enabled_(enabled), |
39 app_count_(0), | 39 app_count_(0), |
40 profile_app_count_(0), | 40 profile_app_count_(0), |
41 have_status_tray_(false), | 41 have_status_tray_(false), |
42 launch_on_startup_(false) {} | 42 launch_on_startup_(false) {} |
43 virtual void EnableLaunchOnStartup(bool launch) OVERRIDE { | 43 virtual void EnableLaunchOnStartup(bool launch) OVERRIDE { |
44 launch_on_startup_ = launch; | 44 launch_on_startup_ = launch; |
45 } | 45 } |
46 virtual void CreateStatusTrayIcon() OVERRIDE { have_status_tray_ = true; } | 46 virtual void CreateStatusTrayIcon() OVERRIDE { have_status_tray_ = true; } |
47 virtual void RemoveStatusTrayIcon() OVERRIDE { have_status_tray_ = false; } | 47 virtual void RemoveStatusTrayIcon() OVERRIDE { have_status_tray_ = false; } |
48 virtual int GetBackgroundAppCount() const OVERRIDE { return app_count_; } | 48 virtual int GetBackgroundAppCount() const OVERRIDE { return app_count_; } |
49 virtual int GetBackgroundAppCountForProfile( | 49 virtual int GetBackgroundAppCountForProfile( |
50 Profile* const profile) const OVERRIDE { | 50 Profile* const profile) const OVERRIDE { |
51 return profile_app_count_; | 51 return profile_app_count_; |
52 } | 52 } |
53 virtual bool IsBackgroundModePrefEnabled() const OVERRIDE { return enabled_; } | 53 virtual bool IsBackgroundModePrefEnabled() const OVERRIDE { return enabled_; } |
54 void SetBackgroundAppCount(int count) { app_count_ = count; } | 54 void SetBackgroundAppCount(int count) { app_count_ = count; } |
55 void SetBackgroundAppCountForProfile(int count) { | 55 void SetBackgroundAppCountForProfile(int count) { |
56 profile_app_count_ = count; | 56 profile_app_count_ = count; |
57 } | 57 } |
58 void SetEnabled(bool enabled) { enabled_ = enabled; } | 58 void SetEnabled(bool enabled) { |
| 59 enabled_ = enabled; |
| 60 OnBackgroundModeEnabledPrefChanged(); |
| 61 } |
59 bool HaveStatusTray() const { return have_status_tray_; } | 62 bool HaveStatusTray() const { return have_status_tray_; } |
60 bool IsLaunchOnStartup() const { return launch_on_startup_; } | 63 bool IsLaunchOnStartup() const { return launch_on_startup_; } |
61 private: | 64 private: |
62 bool enabled_; | 65 bool enabled_; |
63 int app_count_; | 66 int app_count_; |
64 int profile_app_count_; | 67 int profile_app_count_; |
65 | 68 |
66 // Flags to track whether we are launching on startup/have a status tray. | 69 // Flags to track whether we are launching on startup/have a status tray. |
67 bool have_status_tray_; | 70 bool have_status_tray_; |
68 bool launch_on_startup_; | 71 bool launch_on_startup_; |
69 }; | 72 }; |
70 | 73 |
71 static void AssertBackgroundModeActive( | 74 static void AssertBackgroundModeActive( |
72 const TestBackgroundModeManager& manager) { | 75 const TestBackgroundModeManager& manager) { |
73 EXPECT_TRUE(browser::WillKeepAlive()); | 76 EXPECT_TRUE(browser::WillKeepAlive()); |
74 EXPECT_TRUE(manager.HaveStatusTray()); | 77 EXPECT_TRUE(manager.HaveStatusTray()); |
75 EXPECT_TRUE(manager.IsLaunchOnStartup()); | 78 EXPECT_TRUE(manager.IsLaunchOnStartup()); |
76 } | 79 } |
77 | 80 |
78 static void AssertBackgroundModeInactive( | 81 static void AssertBackgroundModeInactive( |
79 const TestBackgroundModeManager& manager) { | 82 const TestBackgroundModeManager& manager) { |
80 EXPECT_FALSE(browser::WillKeepAlive()); | 83 EXPECT_FALSE(browser::WillKeepAlive()); |
81 EXPECT_FALSE(manager.HaveStatusTray()); | 84 EXPECT_FALSE(manager.HaveStatusTray()); |
82 EXPECT_FALSE(manager.IsLaunchOnStartup()); | 85 EXPECT_FALSE(manager.IsLaunchOnStartup()); |
83 } | 86 } |
84 | 87 |
85 TEST_F(BackgroundModeManagerTest, BackgroundAppLoadUnload) { | 88 TEST_F(BackgroundModeManagerTest, BackgroundAppLoadUnload) { |
86 TestingProfile* profile = profile_manager_.CreateTestingProfile("p1"); | 89 TestingProfile* profile = profile_manager_.CreateTestingProfile("p1"); |
87 TestBackgroundModeManager manager( | 90 TestBackgroundModeManager manager( |
88 command_line_.get(), profile_manager_.profile_info_cache()); | 91 command_line_.get(), profile_manager_.profile_info_cache(), true); |
89 manager.RegisterProfile(profile); | 92 manager.RegisterProfile(profile); |
90 EXPECT_FALSE(browser::WillKeepAlive()); | 93 EXPECT_FALSE(browser::WillKeepAlive()); |
91 | 94 |
92 // Mimic app load. | 95 // Mimic app load. |
93 manager.OnBackgroundAppInstalled(NULL); | 96 manager.OnBackgroundAppInstalled(NULL); |
94 manager.SetBackgroundAppCount(1); | 97 manager.SetBackgroundAppCount(1); |
95 manager.OnApplicationListChanged(profile); | 98 manager.OnApplicationListChanged(profile); |
96 AssertBackgroundModeActive(manager); | 99 AssertBackgroundModeActive(manager); |
97 | 100 |
98 // Mimic app unload. | 101 // Mimic app unload. |
99 manager.SetBackgroundAppCount(0); | 102 manager.SetBackgroundAppCount(0); |
100 manager.OnApplicationListChanged(profile); | 103 manager.OnApplicationListChanged(profile); |
101 AssertBackgroundModeInactive(manager); | 104 AssertBackgroundModeInactive(manager); |
102 } | 105 } |
103 | 106 |
104 // App installs while background mode is disabled should do nothing. | 107 // App installs while background mode is disabled should do nothing. |
105 TEST_F(BackgroundModeManagerTest, BackgroundAppInstallUninstallWhileDisabled) { | 108 TEST_F(BackgroundModeManagerTest, BackgroundAppInstallUninstallWhileDisabled) { |
106 TestingProfile* profile = profile_manager_.CreateTestingProfile("p1"); | 109 TestingProfile* profile = profile_manager_.CreateTestingProfile("p1"); |
107 TestBackgroundModeManager manager( | 110 TestBackgroundModeManager manager( |
108 command_line_.get(), profile_manager_.profile_info_cache()); | 111 command_line_.get(), profile_manager_.profile_info_cache(), true); |
109 manager.RegisterProfile(profile); | 112 manager.RegisterProfile(profile); |
110 // Turn off background mode. | 113 // Turn off background mode. |
111 manager.SetEnabled(false); | 114 manager.SetEnabled(false); |
112 manager.DisableBackgroundMode(); | 115 manager.DisableBackgroundMode(); |
113 AssertBackgroundModeInactive(manager); | 116 AssertBackgroundModeInactive(manager); |
114 | 117 |
115 // Status tray icons will not be created, launch on startup status will not | 118 // Status tray icons will not be created, launch on startup status will not |
116 // be modified. | 119 // be modified. |
117 manager.OnBackgroundAppInstalled(NULL); | 120 manager.OnBackgroundAppInstalled(NULL); |
118 manager.SetBackgroundAppCount(1); | 121 manager.SetBackgroundAppCount(1); |
119 manager.OnApplicationListChanged(profile); | 122 manager.OnApplicationListChanged(profile); |
120 AssertBackgroundModeInactive(manager); | 123 AssertBackgroundModeInactive(manager); |
121 | 124 |
122 manager.SetBackgroundAppCount(0); | 125 manager.SetBackgroundAppCount(0); |
123 manager.OnApplicationListChanged(profile); | 126 manager.OnApplicationListChanged(profile); |
124 AssertBackgroundModeInactive(manager); | 127 AssertBackgroundModeInactive(manager); |
125 | 128 |
126 // Re-enable background mode. | 129 // Re-enable background mode. |
127 manager.SetEnabled(true); | 130 manager.SetEnabled(true); |
128 manager.EnableBackgroundMode(); | 131 manager.EnableBackgroundMode(); |
129 AssertBackgroundModeInactive(manager); | 132 AssertBackgroundModeInactive(manager); |
130 } | 133 } |
131 | 134 |
132 | 135 |
133 // App installs while disabled should do nothing until background mode is | 136 // App installs while disabled should do nothing until background mode is |
134 // enabled.. | 137 // enabled.. |
135 TEST_F(BackgroundModeManagerTest, EnableAfterBackgroundAppInstall) { | 138 TEST_F(BackgroundModeManagerTest, EnableAfterBackgroundAppInstall) { |
136 TestingProfile* profile = profile_manager_.CreateTestingProfile("p1"); | 139 TestingProfile* profile = profile_manager_.CreateTestingProfile("p1"); |
137 TestBackgroundModeManager manager( | 140 TestBackgroundModeManager manager( |
138 command_line_.get(), profile_manager_.profile_info_cache()); | 141 command_line_.get(), profile_manager_.profile_info_cache(), true); |
139 manager.RegisterProfile(profile); | 142 manager.RegisterProfile(profile); |
140 | 143 |
141 // Install app, should show status tray icon. | 144 // Install app, should show status tray icon. |
142 manager.OnBackgroundAppInstalled(NULL); | 145 manager.OnBackgroundAppInstalled(NULL); |
143 // OnBackgroundAppInstalled does not actually add an app to the | 146 // OnBackgroundAppInstalled does not actually add an app to the |
144 // BackgroundApplicationListModel which would result in another | 147 // BackgroundApplicationListModel which would result in another |
145 // call to CreateStatusTray. | 148 // call to CreateStatusTray. |
146 manager.SetBackgroundAppCount(1); | 149 manager.SetBackgroundAppCount(1); |
147 manager.OnApplicationListChanged(profile); | 150 manager.OnApplicationListChanged(profile); |
148 AssertBackgroundModeActive(manager); | 151 AssertBackgroundModeActive(manager); |
(...skipping 12 matching lines...) Expand all Loading... |
161 // Uninstall app, should hide status tray icon again. | 164 // Uninstall app, should hide status tray icon again. |
162 manager.SetBackgroundAppCount(0); | 165 manager.SetBackgroundAppCount(0); |
163 manager.OnApplicationListChanged(profile); | 166 manager.OnApplicationListChanged(profile); |
164 AssertBackgroundModeInactive(manager); | 167 AssertBackgroundModeInactive(manager); |
165 } | 168 } |
166 | 169 |
167 TEST_F(BackgroundModeManagerTest, MultiProfile) { | 170 TEST_F(BackgroundModeManagerTest, MultiProfile) { |
168 TestingProfile* profile1 = profile_manager_.CreateTestingProfile("p1"); | 171 TestingProfile* profile1 = profile_manager_.CreateTestingProfile("p1"); |
169 TestingProfile* profile2 = profile_manager_.CreateTestingProfile("p2"); | 172 TestingProfile* profile2 = profile_manager_.CreateTestingProfile("p2"); |
170 TestBackgroundModeManager manager( | 173 TestBackgroundModeManager manager( |
171 command_line_.get(), profile_manager_.profile_info_cache()); | 174 command_line_.get(), profile_manager_.profile_info_cache(), true); |
172 manager.RegisterProfile(profile1); | 175 manager.RegisterProfile(profile1); |
173 manager.RegisterProfile(profile2); | 176 manager.RegisterProfile(profile2); |
174 EXPECT_FALSE(browser::WillKeepAlive()); | 177 EXPECT_FALSE(browser::WillKeepAlive()); |
175 | 178 |
176 // Install app, should show status tray icon. | 179 // Install app, should show status tray icon. |
177 manager.OnBackgroundAppInstalled(NULL); | 180 manager.OnBackgroundAppInstalled(NULL); |
178 manager.SetBackgroundAppCount(1); | 181 manager.SetBackgroundAppCount(1); |
179 manager.OnApplicationListChanged(profile1); | 182 manager.OnApplicationListChanged(profile1); |
180 AssertBackgroundModeActive(manager); | 183 AssertBackgroundModeActive(manager); |
181 | 184 |
(...skipping 20 matching lines...) Expand all Loading... |
202 | 205 |
203 manager.SetBackgroundAppCount(0); | 206 manager.SetBackgroundAppCount(0); |
204 manager.OnApplicationListChanged(profile1); | 207 manager.OnApplicationListChanged(profile1); |
205 AssertBackgroundModeInactive(manager); | 208 AssertBackgroundModeInactive(manager); |
206 } | 209 } |
207 | 210 |
208 TEST_F(BackgroundModeManagerTest, ProfileInfoCacheStorage) { | 211 TEST_F(BackgroundModeManagerTest, ProfileInfoCacheStorage) { |
209 TestingProfile* profile1 = profile_manager_.CreateTestingProfile("p1"); | 212 TestingProfile* profile1 = profile_manager_.CreateTestingProfile("p1"); |
210 TestingProfile* profile2 = profile_manager_.CreateTestingProfile("p2"); | 213 TestingProfile* profile2 = profile_manager_.CreateTestingProfile("p2"); |
211 TestBackgroundModeManager manager( | 214 TestBackgroundModeManager manager( |
212 command_line_.get(), profile_manager_.profile_info_cache()); | 215 command_line_.get(), profile_manager_.profile_info_cache(), true); |
213 manager.RegisterProfile(profile1); | 216 manager.RegisterProfile(profile1); |
214 manager.RegisterProfile(profile2); | 217 manager.RegisterProfile(profile2); |
215 EXPECT_FALSE(browser::WillKeepAlive()); | 218 EXPECT_FALSE(browser::WillKeepAlive()); |
216 | 219 |
217 ProfileInfoCache* cache = profile_manager_.profile_info_cache(); | 220 ProfileInfoCache* cache = profile_manager_.profile_info_cache(); |
218 EXPECT_EQ(2u, cache->GetNumberOfProfiles()); | 221 EXPECT_EQ(2u, cache->GetNumberOfProfiles()); |
219 | 222 |
220 EXPECT_FALSE(cache->GetBackgroundStatusOfProfileAtIndex(0)); | 223 EXPECT_FALSE(cache->GetBackgroundStatusOfProfileAtIndex(0)); |
221 EXPECT_FALSE(cache->GetBackgroundStatusOfProfileAtIndex(1)); | 224 EXPECT_FALSE(cache->GetBackgroundStatusOfProfileAtIndex(1)); |
222 | 225 |
(...skipping 21 matching lines...) Expand all Loading... |
244 manager.SetBackgroundAppCountForProfile(0); | 247 manager.SetBackgroundAppCountForProfile(0); |
245 manager.OnApplicationListChanged(profile2); | 248 manager.OnApplicationListChanged(profile2); |
246 | 249 |
247 size_t p2_index = cache->GetIndexOfProfileWithPath(profile1->GetPath()); | 250 size_t p2_index = cache->GetIndexOfProfileWithPath(profile1->GetPath()); |
248 EXPECT_FALSE(cache->GetBackgroundStatusOfProfileAtIndex(p2_index)); | 251 EXPECT_FALSE(cache->GetBackgroundStatusOfProfileAtIndex(p2_index)); |
249 | 252 |
250 // Even though neither has background status on, there should still be two | 253 // Even though neither has background status on, there should still be two |
251 // profiles in the cache. | 254 // profiles in the cache. |
252 EXPECT_EQ(2u, cache->GetNumberOfProfiles()); | 255 EXPECT_EQ(2u, cache->GetNumberOfProfiles()); |
253 } | 256 } |
| 257 |
254 TEST_F(BackgroundModeManagerTest, ProfileInfoCacheObserver) { | 258 TEST_F(BackgroundModeManagerTest, ProfileInfoCacheObserver) { |
255 TestingProfile* profile1 = profile_manager_.CreateTestingProfile("p1"); | 259 TestingProfile* profile1 = profile_manager_.CreateTestingProfile("p1"); |
256 TestBackgroundModeManager manager( | 260 TestBackgroundModeManager manager( |
257 command_line_.get(), profile_manager_.profile_info_cache()); | 261 command_line_.get(), profile_manager_.profile_info_cache(), true); |
258 manager.RegisterProfile(profile1); | 262 manager.RegisterProfile(profile1); |
259 EXPECT_FALSE(browser::WillKeepAlive()); | 263 EXPECT_FALSE(browser::WillKeepAlive()); |
260 | 264 |
261 // Install app, should show status tray icon. | 265 // Install app, should show status tray icon. |
262 manager.OnBackgroundAppInstalled(NULL); | 266 manager.OnBackgroundAppInstalled(NULL); |
263 manager.SetBackgroundAppCount(1); | 267 manager.SetBackgroundAppCount(1); |
264 manager.SetBackgroundAppCountForProfile(1); | 268 manager.SetBackgroundAppCountForProfile(1); |
265 manager.OnApplicationListChanged(profile1); | 269 manager.OnApplicationListChanged(profile1); |
266 | 270 |
267 manager.OnProfileNameChanged( | 271 manager.OnProfileNameChanged( |
(...skipping 11 matching lines...) Expand all Loading... |
279 EXPECT_EQ(UTF8ToUTF16("p2"), | 283 EXPECT_EQ(UTF8ToUTF16("p2"), |
280 manager.GetBackgroundModeData(profile2)->name()); | 284 manager.GetBackgroundModeData(profile2)->name()); |
281 | 285 |
282 manager.OnProfileWillBeRemoved(profile2->GetPath()); | 286 manager.OnProfileWillBeRemoved(profile2->GetPath()); |
283 EXPECT_EQ(1, manager.NumberOfBackgroundModeData()); | 287 EXPECT_EQ(1, manager.NumberOfBackgroundModeData()); |
284 | 288 |
285 // Check that the background mode data we think is in the map actually is. | 289 // Check that the background mode data we think is in the map actually is. |
286 EXPECT_EQ(UTF8ToUTF16("p1"), | 290 EXPECT_EQ(UTF8ToUTF16("p1"), |
287 manager.GetBackgroundModeData(profile1)->name()); | 291 manager.GetBackgroundModeData(profile1)->name()); |
288 } | 292 } |
| 293 |
| 294 TEST_F(BackgroundModeManagerTest, DisableBackgroundModeUnderTestFlag) { |
| 295 TestingProfile* profile1 = profile_manager_.CreateTestingProfile("p1"); |
| 296 command_line_->AppendSwitch(switches::kKeepAliveForTest); |
| 297 TestBackgroundModeManager manager( |
| 298 command_line_.get(), profile_manager_.profile_info_cache(), true); |
| 299 manager.RegisterProfile(profile1); |
| 300 EXPECT_TRUE(manager.ShouldBeInBackgroundMode()); |
| 301 manager.SetEnabled(false); |
| 302 EXPECT_FALSE(manager.ShouldBeInBackgroundMode()); |
| 303 } |
| 304 |
| 305 TEST_F(BackgroundModeManagerTest, |
| 306 BackgroundModeDisabledPreventsKeepAliveOnStartup) { |
| 307 TestingProfile* profile1 = profile_manager_.CreateTestingProfile("p1"); |
| 308 command_line_->AppendSwitch(switches::kKeepAliveForTest); |
| 309 TestBackgroundModeManager manager( |
| 310 command_line_.get(), profile_manager_.profile_info_cache(), false); |
| 311 manager.RegisterProfile(profile1); |
| 312 EXPECT_FALSE(manager.ShouldBeInBackgroundMode()); |
| 313 } |
OLD | NEW |