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.
5 #include "base/command_line.h"
6 #include "base/memory/scoped_ptr.h"
7 #include "base/message_loop/message_loop.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/background/background_mode_manager.h"
10 #include "chrome/browser/browser_shutdown.h"
11 #include "chrome/browser/extensions/extension_function_test_utils.h"
12 #include "chrome/browser/extensions/extension_service.h"
13 #include "chrome/browser/extensions/test_extension_system.h"
14 #include "chrome/browser/lifetime/application_lifetime.h"
15 #include "chrome/browser/profiles/profile_info_cache.h"
16 #include "chrome/browser/status_icons/status_icon_menu_model.h"
17 #include "chrome/common/chrome_switches.h"
18 #include "chrome/test/base/testing_browser_process.h"
19 #include "chrome/test/base/testing_profile.h"
20 #include "chrome/test/base/testing_profile_manager.h"
21 #include "content/public/test/test_browser_thread_bundle.h"
22 #include "extensions/browser/extension_system.h"
23 #include "testing/gtest/include/gtest/gtest.h"
24 #include "ui/gfx/image/image.h"
25 #include "ui/gfx/image/image_unittest_util.h"
26 #include "ui/message_center/message_center.h"
28 #if defined(OS_CHROMEOS)
29 #include "chrome/browser/chromeos/login/users/user_manager.h"
30 #include "chrome/browser/chromeos/settings/cros_settings.h"
31 #include "chrome/browser/chromeos/settings/device_settings_service.h"
34 class BackgroundModeManagerTest
: public testing::Test
{
36 BackgroundModeManagerTest() {}
37 virtual ~BackgroundModeManagerTest() {}
38 virtual void SetUp() {
39 command_line_
.reset(new CommandLine(CommandLine::NO_PROGRAM
));
41 scoped_ptr
<CommandLine
> command_line_
;
44 scoped_refptr
<extensions::Extension
> CreateExtension(
45 extensions::Manifest::Location location
,
46 const std::string
& data
,
47 const std::string
& id
) {
48 scoped_ptr
<base::DictionaryValue
> parsed_manifest(
49 extension_function_test_utils::ParseDictionary(data
));
50 return extension_function_test_utils::CreateExtension(
52 parsed_manifest
.get(),
56 scoped_ptr
<TestingProfileManager
> CreateTestingProfileManager() {
57 scoped_ptr
<TestingProfileManager
> profile_manager
58 (new TestingProfileManager(TestingBrowserProcess::GetGlobal()));
59 EXPECT_TRUE(profile_manager
->SetUp());
60 return profile_manager
.Pass();
63 // From views::MenuModelAdapter::IsCommandEnabled with modification.
64 bool IsCommandEnabled(ui::MenuModel
* model
, int id
) const {
66 if (ui::MenuModel::GetModelAndIndexForCommandId(id
, &model
, &index
))
67 return model
->IsEnabledAt(index
);
73 DISALLOW_COPY_AND_ASSIGN(BackgroundModeManagerTest
);
76 class TestBackgroundModeManager
: public BackgroundModeManager
{
78 TestBackgroundModeManager(
79 CommandLine
* command_line
, ProfileInfoCache
* cache
, bool enabled
)
80 : BackgroundModeManager(command_line
, cache
),
83 profile_app_count_(0),
84 have_status_tray_(false),
85 launch_on_startup_(false) {
86 ResumeBackgroundMode();
88 virtual void EnableLaunchOnStartup(bool launch
) OVERRIDE
{
89 launch_on_startup_
= launch
;
91 virtual void DisplayAppInstalledNotification(
92 const extensions::Extension
* extension
) OVERRIDE
{}
93 virtual void CreateStatusTrayIcon() OVERRIDE
{ have_status_tray_
= true; }
94 virtual void RemoveStatusTrayIcon() OVERRIDE
{ have_status_tray_
= false; }
95 virtual int GetBackgroundAppCount() const OVERRIDE
{ return app_count_
; }
96 virtual int GetBackgroundAppCountForProfile(
97 Profile
* const profile
) const OVERRIDE
{
98 return profile_app_count_
;
100 virtual bool IsBackgroundModePrefEnabled() const OVERRIDE
{ return enabled_
; }
101 void SetBackgroundAppCount(int count
) { app_count_
= count
; }
102 void SetBackgroundAppCountForProfile(int count
) {
103 profile_app_count_
= count
;
105 void SetEnabled(bool enabled
) {
107 OnBackgroundModeEnabledPrefChanged();
109 bool HaveStatusTray() const { return have_status_tray_
; }
110 bool IsLaunchOnStartup() const { return launch_on_startup_
; }
115 int profile_app_count_
;
117 // Flags to track whether we are launching on startup/have a status tray.
118 bool have_status_tray_
;
119 bool launch_on_startup_
;
122 class TestStatusIcon
: public StatusIcon
{
123 virtual void SetImage(const gfx::ImageSkia
& image
) OVERRIDE
{}
124 virtual void SetPressedImage(const gfx::ImageSkia
& image
) OVERRIDE
{}
125 virtual void SetToolTip(const base::string16
& tool_tip
) OVERRIDE
{}
126 virtual void DisplayBalloon(const gfx::ImageSkia
& icon
,
127 const base::string16
& title
,
128 const base::string16
& contents
) OVERRIDE
{}
129 virtual void UpdatePlatformContextMenu(
130 StatusIconMenuModel
* menu
) OVERRIDE
{}
133 static void AssertBackgroundModeActive(
134 const TestBackgroundModeManager
& manager
) {
135 EXPECT_TRUE(chrome::WillKeepAlive());
136 EXPECT_TRUE(manager
.HaveStatusTray());
137 EXPECT_TRUE(manager
.IsLaunchOnStartup());
140 static void AssertBackgroundModeInactive(
141 const TestBackgroundModeManager
& manager
) {
142 EXPECT_FALSE(chrome::WillKeepAlive());
143 EXPECT_FALSE(manager
.HaveStatusTray());
144 EXPECT_FALSE(manager
.IsLaunchOnStartup());
147 static void AssertBackgroundModeSuspended(
148 const TestBackgroundModeManager
& manager
) {
149 EXPECT_FALSE(chrome::WillKeepAlive());
150 EXPECT_FALSE(manager
.HaveStatusTray());
151 EXPECT_TRUE(manager
.IsLaunchOnStartup());
154 TEST_F(BackgroundModeManagerTest
, BackgroundAppLoadUnload
) {
155 scoped_ptr
<TestingProfileManager
> profile_manager
=
156 CreateTestingProfileManager();
157 TestingProfile
* profile
= profile_manager
->CreateTestingProfile("p1");
158 TestBackgroundModeManager
manager(
159 command_line_
.get(), profile_manager
->profile_info_cache(), true);
160 manager
.RegisterProfile(profile
);
161 EXPECT_FALSE(chrome::WillKeepAlive());
164 manager
.OnBackgroundAppInstalled(NULL
);
165 manager
.SetBackgroundAppCount(1);
166 manager
.OnApplicationListChanged(profile
);
167 AssertBackgroundModeActive(manager
);
169 manager
.SuspendBackgroundMode();
170 AssertBackgroundModeSuspended(manager
);
171 manager
.ResumeBackgroundMode();
174 manager
.SetBackgroundAppCount(0);
175 manager
.OnApplicationListChanged(profile
);
176 AssertBackgroundModeInactive(manager
);
178 manager
.SuspendBackgroundMode();
179 AssertBackgroundModeInactive(manager
);
181 // Mimic app load while suspended, e.g. from sync. This should enable and
182 // resume background mode.
183 manager
.OnBackgroundAppInstalled(NULL
);
184 manager
.SetBackgroundAppCount(1);
185 manager
.OnApplicationListChanged(profile
);
186 AssertBackgroundModeActive(manager
);
189 // App installs while background mode is disabled should do nothing.
190 TEST_F(BackgroundModeManagerTest
, BackgroundAppInstallUninstallWhileDisabled
) {
191 scoped_ptr
<TestingProfileManager
> profile_manager
=
192 CreateTestingProfileManager();
193 TestingProfile
* profile
= profile_manager
->CreateTestingProfile("p1");
194 TestBackgroundModeManager
manager(
195 command_line_
.get(), profile_manager
->profile_info_cache(), true);
196 manager
.RegisterProfile(profile
);
197 // Turn off background mode.
198 manager
.SetEnabled(false);
199 manager
.DisableBackgroundMode();
200 AssertBackgroundModeInactive(manager
);
202 // Status tray icons will not be created, launch on startup status will not
204 manager
.OnBackgroundAppInstalled(NULL
);
205 manager
.SetBackgroundAppCount(1);
206 manager
.OnApplicationListChanged(profile
);
207 AssertBackgroundModeInactive(manager
);
209 manager
.SetBackgroundAppCount(0);
210 manager
.OnApplicationListChanged(profile
);
211 AssertBackgroundModeInactive(manager
);
213 // Re-enable background mode.
214 manager
.SetEnabled(true);
215 manager
.EnableBackgroundMode();
216 AssertBackgroundModeInactive(manager
);
220 // App installs while disabled should do nothing until background mode is
222 TEST_F(BackgroundModeManagerTest
, EnableAfterBackgroundAppInstall
) {
223 scoped_ptr
<TestingProfileManager
> profile_manager
=
224 CreateTestingProfileManager();
225 TestingProfile
* profile
= profile_manager
->CreateTestingProfile("p1");
226 TestBackgroundModeManager
manager(
227 command_line_
.get(), profile_manager
->profile_info_cache(), true);
228 manager
.RegisterProfile(profile
);
230 // Install app, should show status tray icon.
231 manager
.OnBackgroundAppInstalled(NULL
);
232 // OnBackgroundAppInstalled does not actually add an app to the
233 // BackgroundApplicationListModel which would result in another
234 // call to CreateStatusTray.
235 manager
.SetBackgroundAppCount(1);
236 manager
.OnApplicationListChanged(profile
);
237 AssertBackgroundModeActive(manager
);
239 // Turn off background mode - should hide status tray icon.
240 manager
.SetEnabled(false);
241 manager
.DisableBackgroundMode();
242 AssertBackgroundModeInactive(manager
);
244 // Turn back on background mode - again, no status tray icon
245 // will show up since we didn't actually add anything to the list.
246 manager
.SetEnabled(true);
247 manager
.EnableBackgroundMode();
248 AssertBackgroundModeActive(manager
);
250 // Uninstall app, should hide status tray icon again.
251 manager
.SetBackgroundAppCount(0);
252 manager
.OnApplicationListChanged(profile
);
253 AssertBackgroundModeInactive(manager
);
256 TEST_F(BackgroundModeManagerTest
, MultiProfile
) {
257 scoped_ptr
<TestingProfileManager
> profile_manager
=
258 CreateTestingProfileManager();
259 TestingProfile
* profile1
= profile_manager
->CreateTestingProfile("p1");
260 TestingProfile
* profile2
= profile_manager
->CreateTestingProfile("p2");
261 TestBackgroundModeManager
manager(
262 command_line_
.get(), profile_manager
->profile_info_cache(), true);
263 manager
.RegisterProfile(profile1
);
264 manager
.RegisterProfile(profile2
);
265 EXPECT_FALSE(chrome::WillKeepAlive());
267 // Install app, should show status tray icon.
268 manager
.OnBackgroundAppInstalled(NULL
);
269 manager
.SetBackgroundAppCount(1);
270 manager
.OnApplicationListChanged(profile1
);
271 AssertBackgroundModeActive(manager
);
273 // Install app for other profile, hsould show other status tray icon.
274 manager
.OnBackgroundAppInstalled(NULL
);
275 manager
.SetBackgroundAppCount(2);
276 manager
.OnApplicationListChanged(profile2
);
277 AssertBackgroundModeActive(manager
);
279 // Should hide both status tray icons.
280 manager
.SetEnabled(false);
281 manager
.DisableBackgroundMode();
282 AssertBackgroundModeInactive(manager
);
284 // Turn back on background mode - should show both status tray icons.
285 manager
.SetEnabled(true);
286 manager
.EnableBackgroundMode();
287 AssertBackgroundModeActive(manager
);
289 manager
.SetBackgroundAppCount(1);
290 manager
.OnApplicationListChanged(profile2
);
291 // There is still one background app alive
292 AssertBackgroundModeActive(manager
);
294 manager
.SetBackgroundAppCount(0);
295 manager
.OnApplicationListChanged(profile1
);
296 AssertBackgroundModeInactive(manager
);
299 TEST_F(BackgroundModeManagerTest
, ProfileInfoCacheStorage
) {
300 scoped_ptr
<TestingProfileManager
> profile_manager
=
301 CreateTestingProfileManager();
302 TestingProfile
* profile1
= profile_manager
->CreateTestingProfile("p1");
303 TestingProfile
* profile2
= profile_manager
->CreateTestingProfile("p2");
304 TestBackgroundModeManager
manager(
305 command_line_
.get(), profile_manager
->profile_info_cache(), true);
306 manager
.RegisterProfile(profile1
);
307 manager
.RegisterProfile(profile2
);
308 EXPECT_FALSE(chrome::WillKeepAlive());
310 ProfileInfoCache
* cache
= profile_manager
->profile_info_cache();
311 EXPECT_EQ(2u, cache
->GetNumberOfProfiles());
313 EXPECT_FALSE(cache
->GetBackgroundStatusOfProfileAtIndex(0));
314 EXPECT_FALSE(cache
->GetBackgroundStatusOfProfileAtIndex(1));
316 // Install app, should show status tray icon.
317 manager
.OnBackgroundAppInstalled(NULL
);
318 manager
.SetBackgroundAppCount(1);
319 manager
.SetBackgroundAppCountForProfile(1);
320 manager
.OnApplicationListChanged(profile1
);
322 // Install app for other profile.
323 manager
.OnBackgroundAppInstalled(NULL
);
324 manager
.SetBackgroundAppCount(1);
325 manager
.SetBackgroundAppCountForProfile(1);
326 manager
.OnApplicationListChanged(profile2
);
328 EXPECT_TRUE(cache
->GetBackgroundStatusOfProfileAtIndex(0));
329 EXPECT_TRUE(cache
->GetBackgroundStatusOfProfileAtIndex(1));
331 manager
.SetBackgroundAppCountForProfile(0);
332 manager
.OnApplicationListChanged(profile1
);
334 size_t p1_index
= cache
->GetIndexOfProfileWithPath(profile1
->GetPath());
335 EXPECT_FALSE(cache
->GetBackgroundStatusOfProfileAtIndex(p1_index
));
337 manager
.SetBackgroundAppCountForProfile(0);
338 manager
.OnApplicationListChanged(profile2
);
340 size_t p2_index
= cache
->GetIndexOfProfileWithPath(profile1
->GetPath());
341 EXPECT_FALSE(cache
->GetBackgroundStatusOfProfileAtIndex(p2_index
));
343 // Even though neither has background status on, there should still be two
344 // profiles in the cache.
345 EXPECT_EQ(2u, cache
->GetNumberOfProfiles());
348 TEST_F(BackgroundModeManagerTest
, ProfileInfoCacheObserver
) {
349 scoped_ptr
<TestingProfileManager
> profile_manager
=
350 CreateTestingProfileManager();
351 TestingProfile
* profile1
= profile_manager
->CreateTestingProfile("p1");
352 TestBackgroundModeManager
manager(
353 command_line_
.get(), profile_manager
->profile_info_cache(), true);
354 manager
.RegisterProfile(profile1
);
355 EXPECT_FALSE(chrome::WillKeepAlive());
357 // Install app, should show status tray icon.
358 manager
.OnBackgroundAppInstalled(NULL
);
359 manager
.SetBackgroundAppCount(1);
360 manager
.SetBackgroundAppCountForProfile(1);
361 manager
.OnApplicationListChanged(profile1
);
363 manager
.OnProfileNameChanged(
365 manager
.GetBackgroundModeData(profile1
)->name());
367 EXPECT_EQ(base::UTF8ToUTF16("p1"),
368 manager
.GetBackgroundModeData(profile1
)->name());
370 EXPECT_TRUE(chrome::WillKeepAlive());
371 TestingProfile
* profile2
= profile_manager
->CreateTestingProfile("p2");
372 manager
.RegisterProfile(profile2
);
373 EXPECT_EQ(2, manager
.NumberOfBackgroundModeData());
375 manager
.OnProfileAdded(profile2
->GetPath());
376 EXPECT_EQ(base::UTF8ToUTF16("p2"),
377 manager
.GetBackgroundModeData(profile2
)->name());
379 manager
.OnProfileWillBeRemoved(profile2
->GetPath());
380 // Should still be in background mode after deleting profile.
381 EXPECT_TRUE(chrome::WillKeepAlive());
382 EXPECT_EQ(1, manager
.NumberOfBackgroundModeData());
384 // Check that the background mode data we think is in the map actually is.
385 EXPECT_EQ(base::UTF8ToUTF16("p1"),
386 manager
.GetBackgroundModeData(profile1
)->name());
389 TEST_F(BackgroundModeManagerTest
, DeleteBackgroundProfile
) {
390 // Tests whether deleting the only profile when it is a BG profile works
391 // or not (http://crbug.com/346214).
392 scoped_ptr
<TestingProfileManager
> profile_manager
=
393 CreateTestingProfileManager();
394 TestingProfile
* profile
= profile_manager
->CreateTestingProfile("p1");
395 TestBackgroundModeManager
manager(
396 command_line_
.get(), profile_manager
->profile_info_cache(), true);
397 manager
.RegisterProfile(profile
);
398 EXPECT_FALSE(chrome::WillKeepAlive());
400 // Install app, should show status tray icon.
401 manager
.OnBackgroundAppInstalled(NULL
);
402 manager
.SetBackgroundAppCount(1);
403 manager
.SetBackgroundAppCountForProfile(1);
404 manager
.OnApplicationListChanged(profile
);
406 manager
.OnProfileNameChanged(
408 manager
.GetBackgroundModeData(profile
)->name());
410 EXPECT_TRUE(chrome::WillKeepAlive());
411 manager
.SetBackgroundAppCount(0);
412 manager
.SetBackgroundAppCountForProfile(0);
413 manager
.OnProfileWillBeRemoved(profile
->GetPath());
414 EXPECT_FALSE(chrome::WillKeepAlive());
417 TEST_F(BackgroundModeManagerTest
, DisableBackgroundModeUnderTestFlag
) {
418 scoped_ptr
<TestingProfileManager
> profile_manager
=
419 CreateTestingProfileManager();
420 TestingProfile
* profile1
= profile_manager
->CreateTestingProfile("p1");
421 command_line_
->AppendSwitch(switches::kKeepAliveForTest
);
422 TestBackgroundModeManager
manager(
423 command_line_
.get(), profile_manager
->profile_info_cache(), true);
424 manager
.RegisterProfile(profile1
);
425 EXPECT_TRUE(manager
.ShouldBeInBackgroundMode());
426 manager
.SetEnabled(false);
427 EXPECT_FALSE(manager
.ShouldBeInBackgroundMode());
430 TEST_F(BackgroundModeManagerTest
,
431 BackgroundModeDisabledPreventsKeepAliveOnStartup
) {
432 scoped_ptr
<TestingProfileManager
> profile_manager
=
433 CreateTestingProfileManager();
434 TestingProfile
* profile1
= profile_manager
->CreateTestingProfile("p1");
435 command_line_
->AppendSwitch(switches::kKeepAliveForTest
);
436 TestBackgroundModeManager
manager(
437 command_line_
.get(), profile_manager
->profile_info_cache(), false);
438 manager
.RegisterProfile(profile1
);
439 EXPECT_FALSE(manager
.ShouldBeInBackgroundMode());
442 TEST_F(BackgroundModeManagerTest
, BackgroundMenuGeneration
) {
443 // Aura clears notifications from the message center at shutdown.
444 message_center::MessageCenter::Initialize();
446 // Required for extension service.
447 content::TestBrowserThreadBundle thread_bundle
;
449 scoped_ptr
<TestingProfileManager
> profile_manager
=
450 CreateTestingProfileManager();
452 // BackgroundModeManager actually affects Chrome start/stop state,
453 // tearing down our thread bundle before we've had chance to clean
454 // everything up. Keeping Chrome alive prevents this.
455 // We aren't interested in if the keep alive works correctly in this test.
456 chrome::IncrementKeepAliveCount();
457 TestingProfile
* profile
= profile_manager
->CreateTestingProfile("p");
459 #if defined(OS_CHROMEOS)
460 // ChromeOS needs extra services to run in the following order.
461 chromeos::ScopedTestDeviceSettingsService test_device_settings_service
;
462 chromeos::ScopedTestCrosSettings test_cros_settings
;
463 chromeos::ScopedTestUserManager test_user_manager
;
465 // On ChromeOS shutdown, HandleAppExitingForPlatform will call
466 // chrome::DecrementKeepAliveCount because it assumes the aura shell
467 // called chrome::IncrementKeepAliveCount. Simulate the call here.
468 chrome::IncrementKeepAliveCount();
471 scoped_refptr
<extensions::Extension
> component_extension(
473 extensions::Manifest::COMPONENT
,
474 "{\"name\": \"Component Extension\","
475 "\"version\": \"1.0\","
476 "\"manifest_version\": 2,"
477 "\"permissions\": [\"background\"]}",
480 scoped_refptr
<extensions::Extension
> component_extension_with_options(
482 extensions::Manifest::COMPONENT
,
483 "{\"name\": \"Component Extension with Options\","
484 "\"version\": \"1.0\","
485 "\"manifest_version\": 2,"
486 "\"permissions\": [\"background\"],"
487 "\"options_page\": \"test.html\"}",
490 scoped_refptr
<extensions::Extension
> regular_extension(
492 extensions::Manifest::COMMAND_LINE
,
493 "{\"name\": \"Regular Extension\", "
494 "\"version\": \"1.0\","
495 "\"manifest_version\": 2,"
496 "\"permissions\": [\"background\"]}",
499 scoped_refptr
<extensions::Extension
> regular_extension_with_options(
501 extensions::Manifest::COMMAND_LINE
,
502 "{\"name\": \"Regular Extension with Options\","
503 "\"version\": \"1.0\","
504 "\"manifest_version\": 2,"
505 "\"permissions\": [\"background\"],"
506 "\"options_page\": \"test.html\"}",
509 static_cast<extensions::TestExtensionSystem
*>(
510 extensions::ExtensionSystem::Get(profile
))->CreateExtensionService(
511 CommandLine::ForCurrentProcess(),
514 ExtensionService
* service
=
515 extensions::ExtensionSystem::Get(profile
)->extension_service();
518 service
->AddComponentExtension(component_extension
);
519 service
->AddComponentExtension(component_extension_with_options
);
520 service
->AddExtension(regular_extension
);
521 service
->AddExtension(regular_extension_with_options
);
523 scoped_ptr
<TestBackgroundModeManager
> manager(new TestBackgroundModeManager(
524 command_line_
.get(), profile_manager
->profile_info_cache(), true));
525 manager
->RegisterProfile(profile
);
527 scoped_ptr
<StatusIconMenuModel
> menu(new StatusIconMenuModel(NULL
));
528 scoped_ptr
<StatusIconMenuModel
> submenu(new StatusIconMenuModel(NULL
));
529 BackgroundModeManager::BackgroundModeData
* bmd
=
530 manager
->GetBackgroundModeData(profile
);
531 bmd
->BuildProfileMenu(submenu
.get(), menu
.get());
533 submenu
->GetLabelAt(0) ==
534 base::UTF8ToUTF16("Component Extension"));
535 EXPECT_FALSE(submenu
->IsCommandIdEnabled(submenu
->GetCommandIdAt(0)));
537 submenu
->GetLabelAt(1) ==
538 base::UTF8ToUTF16("Component Extension with Options"));
539 EXPECT_TRUE(submenu
->IsCommandIdEnabled(submenu
->GetCommandIdAt(1)));
541 submenu
->GetLabelAt(2) ==
542 base::UTF8ToUTF16("Regular Extension"));
543 EXPECT_TRUE(submenu
->IsCommandIdEnabled(submenu
->GetCommandIdAt(2)));
545 submenu
->GetLabelAt(3) ==
546 base::UTF8ToUTF16("Regular Extension with Options"));
547 EXPECT_TRUE(submenu
->IsCommandIdEnabled(submenu
->GetCommandIdAt(3)));
549 // We have to destroy the profile now because we created it with real thread
550 // state. This causes a lot of machinery to spin up that stops working
551 // when we tear down our thread state at the end of the test.
552 profile_manager
->DeleteTestingProfile("p");
554 // We're getting ready to shutdown the message loop. Clear everything out!
555 base::MessageLoop::current()->RunUntilIdle();
556 chrome::DecrementKeepAliveCount(); // Matching the above
557 // chrome::IncrementKeepAliveCount().
559 // TestBackgroundModeManager has dependencies on the infrastructure.
560 // It should get cleared first.
563 // The Profile Manager references the Browser Process.
564 // The Browser Process references the Notification UI Manager.
565 // The Notification UI Manager references the Message Center.
566 // As a result, we have to clear the browser process state here
567 // before tearing down the Message Center.
568 profile_manager
.reset();
570 // Message Center shutdown must occur after the DecrementKeepAliveCount
571 // because DecrementKeepAliveCount will end up referencing the message
572 // center during cleanup.
573 message_center::MessageCenter::Shutdown();
575 // Clear the shutdown flag to isolate the remaining effect of this test.
576 browser_shutdown::SetTryingToQuit(false);
579 TEST_F(BackgroundModeManagerTest
, BackgroundMenuGenerationMultipleProfile
) {
580 // Aura clears notifications from the message center at shutdown.
581 message_center::MessageCenter::Initialize();
583 // Required for extension service.
584 content::TestBrowserThreadBundle thread_bundle
;
586 scoped_ptr
<TestingProfileManager
> profile_manager
=
587 CreateTestingProfileManager();
589 // BackgroundModeManager actually affects Chrome start/stop state,
590 // tearing down our thread bundle before we've had chance to clean
591 // everything up. Keeping Chrome alive prevents this.
592 // We aren't interested in if the keep alive works correctly in this test.
593 chrome::IncrementKeepAliveCount();
594 TestingProfile
* profile1
= profile_manager
->CreateTestingProfile("p1");
595 TestingProfile
* profile2
= profile_manager
->CreateTestingProfile("p2");
597 #if defined(OS_CHROMEOS)
598 // ChromeOS needs extensionsra services to run in the following order.
599 chromeos::ScopedTestDeviceSettingsService test_device_settings_service
;
600 chromeos::ScopedTestCrosSettings test_cros_settings
;
601 chromeos::ScopedTestUserManager test_user_manager
;
603 // On ChromeOS shutdown, HandleAppExitingForPlatform will call
604 // chrome::DecrementKeepAliveCount because it assumes the aura shell
605 // called chrome::IncrementKeepAliveCount. Simulate the call here.
606 chrome::IncrementKeepAliveCount();
609 scoped_refptr
<extensions::Extension
> component_extension(
611 extensions::Manifest::COMPONENT
,
612 "{\"name\": \"Component Extension\","
613 "\"version\": \"1.0\","
614 "\"manifest_version\": 2,"
615 "\"permissions\": [\"background\"]}",
618 scoped_refptr
<extensions::Extension
> component_extension_with_options(
620 extensions::Manifest::COMPONENT
,
621 "{\"name\": \"Component Extension with Options\","
622 "\"version\": \"1.0\","
623 "\"manifest_version\": 2,"
624 "\"permissions\": [\"background\"],"
625 "\"options_page\": \"test.html\"}",
628 scoped_refptr
<extensions::Extension
> regular_extension(
630 extensions::Manifest::COMMAND_LINE
,
631 "{\"name\": \"Regular Extension\", "
632 "\"version\": \"1.0\","
633 "\"manifest_version\": 2,"
634 "\"permissions\": [\"background\"]}",
637 scoped_refptr
<extensions::Extension
> regular_extension_with_options(
639 extensions::Manifest::COMMAND_LINE
,
640 "{\"name\": \"Regular Extension with Options\","
641 "\"version\": \"1.0\","
642 "\"manifest_version\": 2,"
643 "\"permissions\": [\"background\"],"
644 "\"options_page\": \"test.html\"}",
647 static_cast<extensions::TestExtensionSystem
*>(
648 extensions::ExtensionSystem::Get(profile1
))->CreateExtensionService(
649 CommandLine::ForCurrentProcess(),
652 ExtensionService
* service1
=
653 extensions::ExtensionSystem::Get(profile1
)->extension_service();
656 service1
->AddComponentExtension(component_extension
);
657 service1
->AddComponentExtension(component_extension_with_options
);
658 service1
->AddExtension(regular_extension
);
659 service1
->AddExtension(regular_extension_with_options
);
661 static_cast<extensions::TestExtensionSystem
*>(
662 extensions::ExtensionSystem::Get(profile2
))->CreateExtensionService(
663 CommandLine::ForCurrentProcess(),
666 ExtensionService
* service2
=
667 extensions::ExtensionSystem::Get(profile2
)->extension_service();
670 service2
->AddComponentExtension(component_extension
);
671 service2
->AddExtension(regular_extension
);
672 service2
->AddExtension(regular_extension_with_options
);
674 scoped_ptr
<TestBackgroundModeManager
> manager(new TestBackgroundModeManager(
675 command_line_
.get(), profile_manager
->profile_info_cache(), true));
676 manager
->RegisterProfile(profile1
);
677 manager
->RegisterProfile(profile2
);
679 manager
->status_icon_
= new TestStatusIcon();
680 manager
->UpdateStatusTrayIconContextMenu();
681 StatusIconMenuModel
* context_menu
= manager
->context_menu_
;
682 EXPECT_TRUE(context_menu
!= NULL
);
684 // Background Profile Enable Checks
685 EXPECT_TRUE(context_menu
->GetLabelAt(3) == base::UTF8ToUTF16("p1"));
687 context_menu
->IsCommandIdEnabled(context_menu
->GetCommandIdAt(3)));
688 EXPECT_TRUE(context_menu
->GetCommandIdAt(3) == 4);
690 EXPECT_TRUE(context_menu
->GetLabelAt(4) == base::UTF8ToUTF16("p2"));
692 context_menu
->IsCommandIdEnabled(context_menu
->GetCommandIdAt(4)));
693 EXPECT_TRUE(context_menu
->GetCommandIdAt(4) == 8);
695 // Profile 1 Submenu Checks
696 StatusIconMenuModel
* profile1_submenu
=
697 static_cast<StatusIconMenuModel
*>(context_menu
->GetSubmenuModelAt(3));
699 profile1_submenu
->GetLabelAt(0) ==
700 base::UTF8ToUTF16("Component Extension"));
702 profile1_submenu
->IsCommandIdEnabled(
703 profile1_submenu
->GetCommandIdAt(0)));
704 EXPECT_TRUE(profile1_submenu
->GetCommandIdAt(0) == 0);
706 profile1_submenu
->GetLabelAt(1) ==
707 base::UTF8ToUTF16("Component Extension with Options"));
709 profile1_submenu
->IsCommandIdEnabled(
710 profile1_submenu
->GetCommandIdAt(1)));
711 EXPECT_TRUE(profile1_submenu
->GetCommandIdAt(1) == 1);
713 profile1_submenu
->GetLabelAt(2) ==
714 base::UTF8ToUTF16("Regular Extension"));
716 profile1_submenu
->IsCommandIdEnabled(
717 profile1_submenu
->GetCommandIdAt(2)));
718 EXPECT_TRUE(profile1_submenu
->GetCommandIdAt(2) == 2);
720 profile1_submenu
->GetLabelAt(3) ==
721 base::UTF8ToUTF16("Regular Extension with Options"));
723 profile1_submenu
->IsCommandIdEnabled(
724 profile1_submenu
->GetCommandIdAt(3)));
725 EXPECT_TRUE(profile1_submenu
->GetCommandIdAt(3) == 3);
727 // Profile 2 Submenu Checks
728 StatusIconMenuModel
* profile2_submenu
=
729 static_cast<StatusIconMenuModel
*>(context_menu
->GetSubmenuModelAt(4));
731 profile2_submenu
->GetLabelAt(0) ==
732 base::UTF8ToUTF16("Component Extension"));
734 profile2_submenu
->IsCommandIdEnabled(
735 profile2_submenu
->GetCommandIdAt(0)));
736 EXPECT_TRUE(profile2_submenu
->GetCommandIdAt(0) == 5);
738 profile2_submenu
->GetLabelAt(1) ==
739 base::UTF8ToUTF16("Regular Extension"));
741 profile2_submenu
->IsCommandIdEnabled(
742 profile2_submenu
->GetCommandIdAt(1)));
743 EXPECT_TRUE(profile2_submenu
->GetCommandIdAt(1) == 6);
745 profile2_submenu
->GetLabelAt(2) ==
746 base::UTF8ToUTF16("Regular Extension with Options"));
748 profile2_submenu
->IsCommandIdEnabled(
749 profile2_submenu
->GetCommandIdAt(2)));
750 EXPECT_TRUE(profile2_submenu
->GetCommandIdAt(2) == 7);
752 // Model Adapter Checks for crbug.com/315164
753 // P1: Profile 1 Menu Item
754 // P2: Profile 2 Menu Item
755 // CE: Component Extension Menu Item
756 // CEO: Component Extenison with Options Menu Item
757 // RE: Regular Extension Menu Item
758 // REO: Regular Extension with Options Menu Item
759 EXPECT_FALSE(IsCommandEnabled(context_menu
, 0)); // P1 - CE
760 EXPECT_TRUE(IsCommandEnabled(context_menu
, 1)); // P1 - CEO
761 EXPECT_TRUE(IsCommandEnabled(context_menu
, 2)); // P1 - RE
762 EXPECT_TRUE(IsCommandEnabled(context_menu
, 3)); // P1 - REO
763 EXPECT_TRUE(IsCommandEnabled(context_menu
, 4)); // P1
764 EXPECT_FALSE(IsCommandEnabled(context_menu
, 5)); // P2 - CE
765 EXPECT_TRUE(IsCommandEnabled(context_menu
, 6)); // P2 - RE
766 EXPECT_TRUE(IsCommandEnabled(context_menu
, 7)); // P2 - REO
767 EXPECT_TRUE(IsCommandEnabled(context_menu
, 8)); // P2
769 // Clean up the status icon. If this is not done before profile deletes,
770 // the context menu updates will DCHECK with the now deleted profiles.
771 StatusIcon
* status_icon
= manager
->status_icon_
;
772 manager
->status_icon_
= NULL
;
775 // We have to destroy the profiles now because we created them with real
776 // thread state. This causes a lot of machinery to spin up that stops working
777 // when we tear down our thread state at the end of the test.
778 profile_manager
->DeleteTestingProfile("p2");
779 profile_manager
->DeleteTestingProfile("p1");
781 // We're getting ready to shutdown the message loop. Clear everything out!
782 base::MessageLoop::current()->RunUntilIdle();
783 chrome::DecrementKeepAliveCount(); // Matching the above
784 // chrome::IncrementKeepAliveCount().
786 // TestBackgroundModeManager has dependencies on the infrastructure.
787 // It should get cleared first.
790 // The Profile Manager references the Browser Process.
791 // The Browser Process references the Notification UI Manager.
792 // The Notification UI Manager references the Message Center.
793 // As a result, we have to clear the browser process state here
794 // before tearing down the Message Center.
795 profile_manager
.reset();
797 // Message Center shutdown must occur after the DecrementKeepAliveCount
798 // because DecrementKeepAliveCount will end up referencing the message
799 // center during cleanup.
800 message_center::MessageCenter::Shutdown();
802 // Clear the shutdown flag to isolate the remaining effect of this test.
803 browser_shutdown::SetTryingToQuit(false);