background_mode_manager_unittest.cc revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2011 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/command_line.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
70f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#include "base/message_loop/message_loop.h"
8868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/utf_string_conversions.h"
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/background/background_mode_manager.h"
10f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "chrome/browser/browser_shutdown.h"
110f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#include "chrome/browser/extensions/extension_function_test_utils.h"
120f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#include "chrome/browser/extensions/extension_service.h"
130f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#include "chrome/browser/extensions/test_extension_system.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/lifetime/application_lifetime.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/profiles/profile_info_cache.h"
160f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#include "chrome/browser/status_icons/status_icon_menu_model.h"
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/chrome_switches.h"
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/test/base/testing_browser_process.h"
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/test/base/testing_profile.h"
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/test/base/testing_profile_manager.h"
210f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#include "content/public/test/test_browser_thread_bundle.h"
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/gfx/image/image.h"
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/gfx/image/image_unittest_util.h"
250f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#include "ui/message_center/message_center.h"
260f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
270f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#if defined(OS_CHROMEOS)
280f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#include "chrome/browser/chromeos/login/user_manager.h"
290f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#include "chrome/browser/chromeos/settings/cros_settings.h"
300f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#include "chrome/browser/chromeos/settings/device_settings_service.h"
310f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#endif
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class BackgroundModeManagerTest : public testing::Test {
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
350f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  BackgroundModeManagerTest() {}
362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual ~BackgroundModeManagerTest() {}
372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void SetUp() {
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    command_line_.reset(new CommandLine(CommandLine::NO_PROGRAM));
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<CommandLine> command_line_;
410f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
420f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) protected:
430f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  scoped_refptr<extensions::Extension> CreateExtension(
440f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      extensions::Manifest::Location location,
450f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      const std::string& data,
460f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      const std::string& id) {
470f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    scoped_ptr<base::DictionaryValue> parsed_manifest(
480f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)        extension_function_test_utils::ParseDictionary(data));
490f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    return extension_function_test_utils::CreateExtension(
500f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)        location,
510f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)        parsed_manifest.get(),
520f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)        id);
530f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  }
540f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
550f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  scoped_ptr<TestingProfileManager> CreateTestingProfileManager() {
560f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    scoped_ptr<TestingProfileManager> profile_manager
570f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)        (new TestingProfileManager(TestingBrowserProcess::GetGlobal()));
580f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    EXPECT_TRUE(profile_manager->SetUp());
590f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    return profile_manager.Pass();
600f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  }
610f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
62f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // From views::MenuModelAdapter::IsCommandEnabled with modification.
63f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  bool IsCommandEnabled(ui::MenuModel* model, int id) const {
64f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    int index = 0;
65f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    if (ui::MenuModel::GetModelAndIndexForCommandId(id, &model, &index))
66f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      return model->IsEnabledAt(index);
67f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
68f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return false;
69f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
70f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
710f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) private:
720f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(BackgroundModeManagerTest);
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class TestBackgroundModeManager : public BackgroundModeManager {
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TestBackgroundModeManager(
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      CommandLine* command_line, ProfileInfoCache* cache, bool enabled)
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      : BackgroundModeManager(command_line, cache),
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        enabled_(enabled),
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        app_count_(0),
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        profile_app_count_(0),
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        have_status_tray_(false),
841e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        launch_on_startup_(false) {
851e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    ResumeBackgroundMode();
861e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  }
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void EnableLaunchOnStartup(bool launch) OVERRIDE {
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    launch_on_startup_ = launch;
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
900f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  virtual void DisplayAppInstalledNotification(
910f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      const extensions::Extension* extension) OVERRIDE {}
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void CreateStatusTrayIcon() OVERRIDE { have_status_tray_ = true; }
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void RemoveStatusTrayIcon() OVERRIDE { have_status_tray_ = false; }
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual int GetBackgroundAppCount() const OVERRIDE { return app_count_; }
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual int GetBackgroundAppCountForProfile(
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      Profile* const profile) const OVERRIDE {
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return profile_app_count_;
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool IsBackgroundModePrefEnabled() const OVERRIDE { return enabled_; }
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SetBackgroundAppCount(int count) { app_count_ = count; }
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SetBackgroundAppCountForProfile(int count) {
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    profile_app_count_ = count;
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SetEnabled(bool enabled) {
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    enabled_ = enabled;
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    OnBackgroundModeEnabledPrefChanged();
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool HaveStatusTray() const { return have_status_tray_; }
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool IsLaunchOnStartup() const { return launch_on_startup_; }
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool enabled_;
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int app_count_;
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int profile_app_count_;
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Flags to track whether we are launching on startup/have a status tray.
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool have_status_tray_;
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool launch_on_startup_;
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
120f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)class TestStatusIcon : public StatusIcon {
121f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  virtual void SetImage(const gfx::ImageSkia& image) OVERRIDE {}
122f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  virtual void SetPressedImage(const gfx::ImageSkia& image) OVERRIDE {}
123a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  virtual void SetToolTip(const base::string16& tool_tip) OVERRIDE {}
124f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  virtual void DisplayBalloon(const gfx::ImageSkia& icon,
125a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                              const base::string16& title,
126a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                              const base::string16& contents) OVERRIDE {}
127f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  virtual void UpdatePlatformContextMenu(
128f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      StatusIconMenuModel* menu) OVERRIDE {}
129f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)};
130f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void AssertBackgroundModeActive(
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const TestBackgroundModeManager& manager) {
1332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(chrome::WillKeepAlive());
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(manager.HaveStatusTray());
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(manager.IsLaunchOnStartup());
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void AssertBackgroundModeInactive(
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const TestBackgroundModeManager& manager) {
1402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_FALSE(chrome::WillKeepAlive());
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_FALSE(manager.HaveStatusTray());
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_FALSE(manager.IsLaunchOnStartup());
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1451e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)static void AssertBackgroundModeSuspended(
1461e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    const TestBackgroundModeManager& manager) {
1471e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  EXPECT_FALSE(chrome::WillKeepAlive());
1481e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  EXPECT_FALSE(manager.HaveStatusTray());
1491e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  EXPECT_TRUE(manager.IsLaunchOnStartup());
1501e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
1511e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(BackgroundModeManagerTest, BackgroundAppLoadUnload) {
1530f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  scoped_ptr<TestingProfileManager> profile_manager =
1540f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      CreateTestingProfileManager();
1550f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  TestingProfile* profile = profile_manager->CreateTestingProfile("p1");
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TestBackgroundModeManager manager(
1570f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      command_line_.get(), profile_manager->profile_info_cache(), true);
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  manager.RegisterProfile(profile);
1592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_FALSE(chrome::WillKeepAlive());
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Mimic app load.
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  manager.OnBackgroundAppInstalled(NULL);
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  manager.SetBackgroundAppCount(1);
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  manager.OnApplicationListChanged(profile);
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  AssertBackgroundModeActive(manager);
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1671e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  manager.SuspendBackgroundMode();
1681e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  AssertBackgroundModeSuspended(manager);
1691e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  manager.ResumeBackgroundMode();
1701e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Mimic app unload.
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  manager.SetBackgroundAppCount(0);
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  manager.OnApplicationListChanged(profile);
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  AssertBackgroundModeInactive(manager);
1751e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1761e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  manager.SuspendBackgroundMode();
1771e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  AssertBackgroundModeInactive(manager);
1781e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1791e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // Mimic app load while suspended, e.g. from sync. This should enable and
1801e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // resume background mode.
1811e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  manager.OnBackgroundAppInstalled(NULL);
1821e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  manager.SetBackgroundAppCount(1);
1831e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  manager.OnApplicationListChanged(profile);
1841e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  AssertBackgroundModeActive(manager);
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// App installs while background mode is disabled should do nothing.
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(BackgroundModeManagerTest, BackgroundAppInstallUninstallWhileDisabled) {
1890f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  scoped_ptr<TestingProfileManager> profile_manager =
1900f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      CreateTestingProfileManager();
1910f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  TestingProfile* profile = profile_manager->CreateTestingProfile("p1");
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TestBackgroundModeManager manager(
1930f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      command_line_.get(), profile_manager->profile_info_cache(), true);
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  manager.RegisterProfile(profile);
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Turn off background mode.
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  manager.SetEnabled(false);
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  manager.DisableBackgroundMode();
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  AssertBackgroundModeInactive(manager);
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Status tray icons will not be created, launch on startup status will not
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // be modified.
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  manager.OnBackgroundAppInstalled(NULL);
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  manager.SetBackgroundAppCount(1);
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  manager.OnApplicationListChanged(profile);
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  AssertBackgroundModeInactive(manager);
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  manager.SetBackgroundAppCount(0);
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  manager.OnApplicationListChanged(profile);
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  AssertBackgroundModeInactive(manager);
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Re-enable background mode.
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  manager.SetEnabled(true);
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  manager.EnableBackgroundMode();
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  AssertBackgroundModeInactive(manager);
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// App installs while disabled should do nothing until background mode is
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// enabled..
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(BackgroundModeManagerTest, EnableAfterBackgroundAppInstall) {
2210f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  scoped_ptr<TestingProfileManager> profile_manager =
2220f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      CreateTestingProfileManager();
2230f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  TestingProfile* profile = profile_manager->CreateTestingProfile("p1");
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TestBackgroundModeManager manager(
2250f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      command_line_.get(), profile_manager->profile_info_cache(), true);
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  manager.RegisterProfile(profile);
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Install app, should show status tray icon.
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  manager.OnBackgroundAppInstalled(NULL);
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // OnBackgroundAppInstalled does not actually add an app to the
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // BackgroundApplicationListModel which would result in another
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // call to CreateStatusTray.
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  manager.SetBackgroundAppCount(1);
2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  manager.OnApplicationListChanged(profile);
2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  AssertBackgroundModeActive(manager);
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Turn off background mode - should hide status tray icon.
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  manager.SetEnabled(false);
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  manager.DisableBackgroundMode();
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  AssertBackgroundModeInactive(manager);
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Turn back on background mode - again, no status tray icon
2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // will show up since we didn't actually add anything to the list.
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  manager.SetEnabled(true);
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  manager.EnableBackgroundMode();
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  AssertBackgroundModeActive(manager);
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Uninstall app, should hide status tray icon again.
2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  manager.SetBackgroundAppCount(0);
2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  manager.OnApplicationListChanged(profile);
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  AssertBackgroundModeInactive(manager);
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(BackgroundModeManagerTest, MultiProfile) {
2550f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  scoped_ptr<TestingProfileManager> profile_manager =
2560f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      CreateTestingProfileManager();
2570f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  TestingProfile* profile1 = profile_manager->CreateTestingProfile("p1");
2580f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  TestingProfile* profile2 = profile_manager->CreateTestingProfile("p2");
2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TestBackgroundModeManager manager(
2600f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      command_line_.get(), profile_manager->profile_info_cache(), true);
2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  manager.RegisterProfile(profile1);
2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  manager.RegisterProfile(profile2);
2632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_FALSE(chrome::WillKeepAlive());
2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Install app, should show status tray icon.
2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  manager.OnBackgroundAppInstalled(NULL);
2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  manager.SetBackgroundAppCount(1);
2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  manager.OnApplicationListChanged(profile1);
2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  AssertBackgroundModeActive(manager);
2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Install app for other profile, hsould show other status tray icon.
2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  manager.OnBackgroundAppInstalled(NULL);
2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  manager.SetBackgroundAppCount(2);
2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  manager.OnApplicationListChanged(profile2);
2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  AssertBackgroundModeActive(manager);
2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Should hide both status tray icons.
2785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  manager.SetEnabled(false);
2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  manager.DisableBackgroundMode();
2805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  AssertBackgroundModeInactive(manager);
2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Turn back on background mode - should show both status tray icons.
2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  manager.SetEnabled(true);
2845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  manager.EnableBackgroundMode();
2855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  AssertBackgroundModeActive(manager);
2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  manager.SetBackgroundAppCount(1);
2885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  manager.OnApplicationListChanged(profile2);
2895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // There is still one background app alive
2905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  AssertBackgroundModeActive(manager);
2915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  manager.SetBackgroundAppCount(0);
2935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  manager.OnApplicationListChanged(profile1);
2945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  AssertBackgroundModeInactive(manager);
2955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(BackgroundModeManagerTest, ProfileInfoCacheStorage) {
2980f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  scoped_ptr<TestingProfileManager> profile_manager =
2990f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      CreateTestingProfileManager();
3000f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  TestingProfile* profile1 = profile_manager->CreateTestingProfile("p1");
3010f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  TestingProfile* profile2 = profile_manager->CreateTestingProfile("p2");
3025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TestBackgroundModeManager manager(
3030f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      command_line_.get(), profile_manager->profile_info_cache(), true);
3045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  manager.RegisterProfile(profile1);
3055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  manager.RegisterProfile(profile2);
3062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_FALSE(chrome::WillKeepAlive());
3075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3080f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  ProfileInfoCache* cache = profile_manager->profile_info_cache();
3095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(2u, cache->GetNumberOfProfiles());
3105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_FALSE(cache->GetBackgroundStatusOfProfileAtIndex(0));
3125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_FALSE(cache->GetBackgroundStatusOfProfileAtIndex(1));
3135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Install app, should show status tray icon.
3155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  manager.OnBackgroundAppInstalled(NULL);
3165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  manager.SetBackgroundAppCount(1);
3175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  manager.SetBackgroundAppCountForProfile(1);
3185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  manager.OnApplicationListChanged(profile1);
3195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Install app for other profile.
3215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  manager.OnBackgroundAppInstalled(NULL);
3225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  manager.SetBackgroundAppCount(1);
3235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  manager.SetBackgroundAppCountForProfile(1);
3245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  manager.OnApplicationListChanged(profile2);
3255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(cache->GetBackgroundStatusOfProfileAtIndex(0));
3275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(cache->GetBackgroundStatusOfProfileAtIndex(1));
3285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  manager.SetBackgroundAppCountForProfile(0);
3305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  manager.OnApplicationListChanged(profile1);
3315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  size_t p1_index = cache->GetIndexOfProfileWithPath(profile1->GetPath());
3335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_FALSE(cache->GetBackgroundStatusOfProfileAtIndex(p1_index));
3345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  manager.SetBackgroundAppCountForProfile(0);
3365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  manager.OnApplicationListChanged(profile2);
3375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  size_t p2_index = cache->GetIndexOfProfileWithPath(profile1->GetPath());
3395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_FALSE(cache->GetBackgroundStatusOfProfileAtIndex(p2_index));
3405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Even though neither has background status on, there should still be two
3425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // profiles in the cache.
3435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(2u, cache->GetNumberOfProfiles());
3445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(BackgroundModeManagerTest, ProfileInfoCacheObserver) {
3470f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  scoped_ptr<TestingProfileManager> profile_manager =
3480f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      CreateTestingProfileManager();
3490f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  TestingProfile* profile1 = profile_manager->CreateTestingProfile("p1");
3505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TestBackgroundModeManager manager(
3510f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      command_line_.get(), profile_manager->profile_info_cache(), true);
3525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  manager.RegisterProfile(profile1);
3532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_FALSE(chrome::WillKeepAlive());
3545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Install app, should show status tray icon.
3565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  manager.OnBackgroundAppInstalled(NULL);
3575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  manager.SetBackgroundAppCount(1);
3585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  manager.SetBackgroundAppCountForProfile(1);
3595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  manager.OnApplicationListChanged(profile1);
3605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  manager.OnProfileNameChanged(
3625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      profile1->GetPath(),
3635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      manager.GetBackgroundModeData(profile1)->name());
3645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_EQ(base::UTF8ToUTF16("p1"),
3665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            manager.GetBackgroundModeData(profile1)->name());
3675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3680f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  TestingProfile* profile2 = profile_manager->CreateTestingProfile("p2");
3695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  manager.RegisterProfile(profile2);
3705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(2, manager.NumberOfBackgroundModeData());
3715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  manager.OnProfileAdded(profile2->GetPath());
3735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_EQ(base::UTF8ToUTF16("p2"),
3745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            manager.GetBackgroundModeData(profile2)->name());
3755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  manager.OnProfileWillBeRemoved(profile2->GetPath());
3775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(1, manager.NumberOfBackgroundModeData());
3785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Check that the background mode data we think is in the map actually is.
3805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_EQ(base::UTF8ToUTF16("p1"),
3815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            manager.GetBackgroundModeData(profile1)->name());
3825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(BackgroundModeManagerTest, DisableBackgroundModeUnderTestFlag) {
3850f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  scoped_ptr<TestingProfileManager> profile_manager =
3860f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      CreateTestingProfileManager();
3870f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  TestingProfile* profile1 = profile_manager->CreateTestingProfile("p1");
3885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  command_line_->AppendSwitch(switches::kKeepAliveForTest);
3895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TestBackgroundModeManager manager(
3900f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      command_line_.get(), profile_manager->profile_info_cache(), true);
3915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  manager.RegisterProfile(profile1);
3925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(manager.ShouldBeInBackgroundMode());
3935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  manager.SetEnabled(false);
3945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_FALSE(manager.ShouldBeInBackgroundMode());
3955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(BackgroundModeManagerTest,
3985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)       BackgroundModeDisabledPreventsKeepAliveOnStartup) {
3990f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  scoped_ptr<TestingProfileManager> profile_manager =
4000f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      CreateTestingProfileManager();
4010f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  TestingProfile* profile1 = profile_manager->CreateTestingProfile("p1");
4025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  command_line_->AppendSwitch(switches::kKeepAliveForTest);
4035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TestBackgroundModeManager manager(
4040f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      command_line_.get(), profile_manager->profile_info_cache(), false);
4055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  manager.RegisterProfile(profile1);
4065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_FALSE(manager.ShouldBeInBackgroundMode());
4075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4080f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
4090f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)TEST_F(BackgroundModeManagerTest, BackgroundMenuGeneration) {
4100f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // Aura clears notifications from the message center at shutdown.
4110f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  message_center::MessageCenter::Initialize();
4120f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
4130f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // Required for extension service.
4140f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  content::TestBrowserThreadBundle thread_bundle;
4150f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
4160f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  scoped_ptr<TestingProfileManager> profile_manager =
4170f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      CreateTestingProfileManager();
4180f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
4190f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // BackgroundModeManager actually affects Chrome start/stop state,
4200f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // tearing down our thread bundle before we've had chance to clean
4210f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // everything up. Keeping Chrome alive prevents this.
4220f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // We aren't interested in if the keep alive works correctly in this test.
4230f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  chrome::StartKeepAlive();
4240f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  TestingProfile* profile = profile_manager->CreateTestingProfile("p");
4250f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
4260f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#if defined(OS_CHROMEOS)
4270f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // ChromeOS needs extra services to run in the following order.
4280f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  chromeos::ScopedTestDeviceSettingsService test_device_settings_service;
4290f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  chromeos::ScopedTestCrosSettings test_cros_settings;
4300f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  chromeos::ScopedTestUserManager test_user_manager;
4310f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
4320f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // On ChromeOS shutdown, HandleAppExitingForPlatform will call
4330f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // chrome::EndKeepAlive because it assumes the aura shell
4340f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // called chrome::StartKeepAlive. Simulate the call here.
4350f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  chrome::StartKeepAlive();
4360f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#endif
4370f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
4380f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  scoped_refptr<extensions::Extension> component_extension(
4390f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    CreateExtension(
4400f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)        extensions::Manifest::COMPONENT,
4410f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)        "{\"name\": \"Component Extension\","
4420f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)        "\"version\": \"1.0\","
4430f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)        "\"manifest_version\": 2,"
4440f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)        "\"permissions\": [\"background\"]}",
4450f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)        "ID-1"));
4460f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
4470f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  scoped_refptr<extensions::Extension> component_extension_with_options(
4480f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    CreateExtension(
4490f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)        extensions::Manifest::COMPONENT,
4500f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)        "{\"name\": \"Component Extension with Options\","
4510f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)        "\"version\": \"1.0\","
4520f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)        "\"manifest_version\": 2,"
4530f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)        "\"permissions\": [\"background\"],"
4540f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)        "\"options_page\": \"test.html\"}",
4550f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)        "ID-2"));
4560f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
4570f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  scoped_refptr<extensions::Extension> regular_extension(
4580f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    CreateExtension(
4590f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)        extensions::Manifest::COMMAND_LINE,
4600f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)        "{\"name\": \"Regular Extension\", "
4610f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)        "\"version\": \"1.0\","
4620f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)        "\"manifest_version\": 2,"
4630f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)        "\"permissions\": [\"background\"]}",
4640f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)        "ID-3"));
4650f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
4660f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  scoped_refptr<extensions::Extension> regular_extension_with_options(
4670f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    CreateExtension(
4680f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)        extensions::Manifest::COMMAND_LINE,
4690f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)        "{\"name\": \"Regular Extension with Options\","
4700f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)        "\"version\": \"1.0\","
4710f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)        "\"manifest_version\": 2,"
4720f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)        "\"permissions\": [\"background\"],"
4730f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)        "\"options_page\": \"test.html\"}",
4740f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)        "ID-4"));
4750f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
4760f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  static_cast<extensions::TestExtensionSystem*>(
4770f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      extensions::ExtensionSystem::Get(profile))->CreateExtensionService(
4780f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)          CommandLine::ForCurrentProcess(),
4790f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)          base::FilePath(),
4800f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)          false);
4810f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  ExtensionService* service = profile->GetExtensionService();
4820f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  service->Init();
4830f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
4840f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  service->AddComponentExtension(component_extension);
4850f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  service->AddComponentExtension(component_extension_with_options);
4860f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  service->AddExtension(regular_extension);
4870f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  service->AddExtension(regular_extension_with_options);
4880f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
4890f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  scoped_ptr<TestBackgroundModeManager> manager(new TestBackgroundModeManager(
4900f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      command_line_.get(), profile_manager->profile_info_cache(), true));
4910f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  manager->RegisterProfile(profile);
4920f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
4930f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  scoped_ptr<StatusIconMenuModel> menu(new StatusIconMenuModel(NULL));
4940f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  scoped_ptr<StatusIconMenuModel> submenu(new StatusIconMenuModel(NULL));
4950f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  BackgroundModeManager::BackgroundModeData* bmd =
4960f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      manager->GetBackgroundModeData(profile);
4970f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  bmd->BuildProfileMenu(submenu.get(), menu.get());
4980f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  EXPECT_TRUE(
4990f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      submenu->GetLabelAt(0) ==
5005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          base::UTF8ToUTF16("Component Extension"));
5010f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  EXPECT_FALSE(submenu->IsCommandIdEnabled(submenu->GetCommandIdAt(0)));
5020f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  EXPECT_TRUE(
5030f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      submenu->GetLabelAt(1) ==
5045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          base::UTF8ToUTF16("Component Extension with Options"));
5050f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  EXPECT_TRUE(submenu->IsCommandIdEnabled(submenu->GetCommandIdAt(1)));
5060f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  EXPECT_TRUE(
5070f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      submenu->GetLabelAt(2) ==
5085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          base::UTF8ToUTF16("Regular Extension"));
5090f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  EXPECT_TRUE(submenu->IsCommandIdEnabled(submenu->GetCommandIdAt(2)));
5100f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  EXPECT_TRUE(
5110f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      submenu->GetLabelAt(3) ==
5125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          base::UTF8ToUTF16("Regular Extension with Options"));
5130f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  EXPECT_TRUE(submenu->IsCommandIdEnabled(submenu->GetCommandIdAt(3)));
5140f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
5150f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // We have to destroy the profile now because we created it with real thread
5160f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // state. This causes a lot of machinery to spin up that stops working
5170f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // when we tear down our thread state at the end of the test.
5180f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  profile_manager->DeleteTestingProfile("p");
5190f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
5200f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // We're getting ready to shutdown the message loop. Clear everything out!
5210f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  base::MessageLoop::current()->RunUntilIdle();
5220f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  chrome::EndKeepAlive(); // Matching the above chrome::StartKeepAlive().
5230f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
5240f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // TestBackgroundModeManager has dependencies on the infrastructure.
5250f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // It should get cleared first.
5260f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  manager.reset();
5270f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
5280f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // The Profile Manager references the Browser Process.
5290f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // The Browser Process references the Notification UI Manager.
5300f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // The Notification UI Manager references the Message Center.
5310f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // As a result, we have to clear the browser process state here
5320f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // before tearing down the Message Center.
5330f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  profile_manager.reset();
5340f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
5350f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // Message Center shutdown must occur after the EndKeepAlive because
5360f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // EndKeepAlive will end up referencing the message center during cleanup.
5370f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  message_center::MessageCenter::Shutdown();
538f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
539f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Clear the shutdown flag to isolate the remaining effect of this test.
540f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  browser_shutdown::SetTryingToQuit(false);
541f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
542f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
543f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)TEST_F(BackgroundModeManagerTest, BackgroundMenuGenerationMultipleProfile) {
544f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Aura clears notifications from the message center at shutdown.
545f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  message_center::MessageCenter::Initialize();
546f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
547f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Required for extension service.
548f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  content::TestBrowserThreadBundle thread_bundle;
549f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
550f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  scoped_ptr<TestingProfileManager> profile_manager =
551f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      CreateTestingProfileManager();
552f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
553f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // BackgroundModeManager actually affects Chrome start/stop state,
554f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // tearing down our thread bundle before we've had chance to clean
555f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // everything up. Keeping Chrome alive prevents this.
556f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // We aren't interested in if the keep alive works correctly in this test.
557f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  chrome::StartKeepAlive();
558f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  TestingProfile* profile1 = profile_manager->CreateTestingProfile("p1");
559f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  TestingProfile* profile2 = profile_manager->CreateTestingProfile("p2");
560f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
561f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#if defined(OS_CHROMEOS)
562f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // ChromeOS needs extensionsra services to run in the following order.
563f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  chromeos::ScopedTestDeviceSettingsService test_device_settings_service;
564f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  chromeos::ScopedTestCrosSettings test_cros_settings;
565f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  chromeos::ScopedTestUserManager test_user_manager;
566f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
567f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // On ChromeOS shutdown, HandleAppExitingForPlatform will call
568f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // chrome::EndKeepAlive because it assumes the aura shell
569f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // called chrome::StartKeepAlive. Simulate the call here.
570f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  chrome::StartKeepAlive();
571f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#endif
572f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
573f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  scoped_refptr<extensions::Extension> component_extension(
574f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    CreateExtension(
575f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        extensions::Manifest::COMPONENT,
576f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        "{\"name\": \"Component Extension\","
577f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        "\"version\": \"1.0\","
578f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        "\"manifest_version\": 2,"
579f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        "\"permissions\": [\"background\"]}",
580f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        "ID-1"));
581f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
582f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  scoped_refptr<extensions::Extension> component_extension_with_options(
583f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    CreateExtension(
584f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        extensions::Manifest::COMPONENT,
585f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        "{\"name\": \"Component Extension with Options\","
586f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        "\"version\": \"1.0\","
587f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        "\"manifest_version\": 2,"
588f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        "\"permissions\": [\"background\"],"
589f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        "\"options_page\": \"test.html\"}",
590f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        "ID-2"));
591f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
592f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  scoped_refptr<extensions::Extension> regular_extension(
593f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    CreateExtension(
594f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        extensions::Manifest::COMMAND_LINE,
595f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        "{\"name\": \"Regular Extension\", "
596f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        "\"version\": \"1.0\","
597f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        "\"manifest_version\": 2,"
598f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        "\"permissions\": [\"background\"]}",
599f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        "ID-3"));
600f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
601f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  scoped_refptr<extensions::Extension> regular_extension_with_options(
602f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    CreateExtension(
603f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        extensions::Manifest::COMMAND_LINE,
604f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        "{\"name\": \"Regular Extension with Options\","
605f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        "\"version\": \"1.0\","
606f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        "\"manifest_version\": 2,"
607f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        "\"permissions\": [\"background\"],"
608f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        "\"options_page\": \"test.html\"}",
609f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        "ID-4"));
610f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
611f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  static_cast<extensions::TestExtensionSystem*>(
612f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      extensions::ExtensionSystem::Get(profile1))->CreateExtensionService(
613f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)          CommandLine::ForCurrentProcess(),
614f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)          base::FilePath(),
615f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)          false);
616f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  ExtensionService* service1 = profile1->GetExtensionService();
617f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  service1->Init();
618f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
619f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  service1->AddComponentExtension(component_extension);
620f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  service1->AddComponentExtension(component_extension_with_options);
621f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  service1->AddExtension(regular_extension);
622f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  service1->AddExtension(regular_extension_with_options);
623f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
624f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  static_cast<extensions::TestExtensionSystem*>(
625f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      extensions::ExtensionSystem::Get(profile2))->CreateExtensionService(
626f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)          CommandLine::ForCurrentProcess(),
627f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)          base::FilePath(),
628f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)          false);
629f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  ExtensionService* service2 = profile2->GetExtensionService();
630f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  service2->Init();
631f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
632f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  service2->AddComponentExtension(component_extension);
633f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  service2->AddExtension(regular_extension);
634f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  service2->AddExtension(regular_extension_with_options);
635f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
636f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  scoped_ptr<TestBackgroundModeManager> manager(new TestBackgroundModeManager(
637f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      command_line_.get(), profile_manager->profile_info_cache(), true));
638f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  manager->RegisterProfile(profile1);
639f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  manager->RegisterProfile(profile2);
640f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
641f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  manager->status_icon_ = new TestStatusIcon();
642f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  manager->UpdateStatusTrayIconContextMenu();
643f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  StatusIconMenuModel* context_menu = manager->context_menu_;
644f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_TRUE(context_menu != NULL);
645f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
646f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Background Profile Enable Checks
6475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_TRUE(context_menu->GetLabelAt(3) == base::UTF8ToUTF16("p1"));
648f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_TRUE(
649f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      context_menu->IsCommandIdEnabled(context_menu->GetCommandIdAt(3)));
650f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_TRUE(context_menu->GetCommandIdAt(3) == 4);
651f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
6525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_TRUE(context_menu->GetLabelAt(4) == base::UTF8ToUTF16("p2"));
653f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_TRUE(
654f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      context_menu->IsCommandIdEnabled(context_menu->GetCommandIdAt(4)));
655f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_TRUE(context_menu->GetCommandIdAt(4) == 8);
656f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
657f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Profile 1 Submenu Checks
658f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  StatusIconMenuModel* profile1_submenu =
659f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      static_cast<StatusIconMenuModel*>(context_menu->GetSubmenuModelAt(3));
660f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_TRUE(
661f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      profile1_submenu->GetLabelAt(0) ==
6625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          base::UTF8ToUTF16("Component Extension"));
663f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_FALSE(
664f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      profile1_submenu->IsCommandIdEnabled(
665f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)          profile1_submenu->GetCommandIdAt(0)));
666f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_TRUE(profile1_submenu->GetCommandIdAt(0) == 0);
667f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_TRUE(
668f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      profile1_submenu->GetLabelAt(1) ==
6695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          base::UTF8ToUTF16("Component Extension with Options"));
670f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_TRUE(
671f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      profile1_submenu->IsCommandIdEnabled(
672f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)          profile1_submenu->GetCommandIdAt(1)));
673f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_TRUE(profile1_submenu->GetCommandIdAt(1) == 1);
674f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_TRUE(
675f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      profile1_submenu->GetLabelAt(2) ==
6765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          base::UTF8ToUTF16("Regular Extension"));
677f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_TRUE(
678f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      profile1_submenu->IsCommandIdEnabled(
679f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)          profile1_submenu->GetCommandIdAt(2)));
680f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_TRUE(profile1_submenu->GetCommandIdAt(2) == 2);
681f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_TRUE(
682f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      profile1_submenu->GetLabelAt(3) ==
6835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          base::UTF8ToUTF16("Regular Extension with Options"));
684f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_TRUE(
685f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      profile1_submenu->IsCommandIdEnabled(
686f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)          profile1_submenu->GetCommandIdAt(3)));
687f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_TRUE(profile1_submenu->GetCommandIdAt(3) == 3);
688f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
689f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Profile 2 Submenu Checks
690f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  StatusIconMenuModel* profile2_submenu =
691f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      static_cast<StatusIconMenuModel*>(context_menu->GetSubmenuModelAt(4));
692f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_TRUE(
693f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      profile2_submenu->GetLabelAt(0) ==
6945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          base::UTF8ToUTF16("Component Extension"));
695f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_FALSE(
696f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      profile2_submenu->IsCommandIdEnabled(
697f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)          profile2_submenu->GetCommandIdAt(0)));
698f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_TRUE(profile2_submenu->GetCommandIdAt(0) == 5);
699f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_TRUE(
700f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      profile2_submenu->GetLabelAt(1) ==
7015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          base::UTF8ToUTF16("Regular Extension"));
702f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_TRUE(
703f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      profile2_submenu->IsCommandIdEnabled(
704f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)          profile2_submenu->GetCommandIdAt(1)));
705f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_TRUE(profile2_submenu->GetCommandIdAt(1) == 6);
706f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_TRUE(
707f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      profile2_submenu->GetLabelAt(2) ==
7085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          base::UTF8ToUTF16("Regular Extension with Options"));
709f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_TRUE(
710f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      profile2_submenu->IsCommandIdEnabled(
711f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)          profile2_submenu->GetCommandIdAt(2)));
712f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_TRUE(profile2_submenu->GetCommandIdAt(2) == 7);
713f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
714f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Model Adapter Checks for crbug.com/315164
715f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // P1: Profile 1 Menu Item
716f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // P2: Profile 2 Menu Item
717f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // CE: Component Extension Menu Item
718f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // CEO: Component Extenison with Options Menu Item
719f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // RE: Regular Extension Menu Item
720f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // REO: Regular Extension with Options Menu Item
721f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_FALSE(IsCommandEnabled(context_menu, 0)); // P1 - CE
722f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_TRUE(IsCommandEnabled(context_menu, 1));  // P1 - CEO
723f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_TRUE(IsCommandEnabled(context_menu, 2));  // P1 - RE
724f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_TRUE(IsCommandEnabled(context_menu, 3));  // P1 - REO
725f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_TRUE(IsCommandEnabled(context_menu, 4));  // P1
726f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_FALSE(IsCommandEnabled(context_menu, 5)); // P2 - CE
727f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_TRUE(IsCommandEnabled(context_menu, 6));  // P2 - RE
728f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_TRUE(IsCommandEnabled(context_menu, 7));  // P2 - REO
729f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_TRUE(IsCommandEnabled(context_menu, 8));  // P2
730f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
731f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Clean up the status icon. If this is not done before profile deletes,
732f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // the context menu updates will DCHECK with the now deleted profiles.
733f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  StatusIcon* status_icon = manager->status_icon_;
734f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  manager->status_icon_ = NULL;
735f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  delete status_icon;
736f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
737f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // We have to destroy the profiles now because we created them with real
738f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // thread state. This causes a lot of machinery to spin up that stops working
739f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // when we tear down our thread state at the end of the test.
740f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  profile_manager->DeleteTestingProfile("p2");
741f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  profile_manager->DeleteTestingProfile("p1");
742f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
743f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // We're getting ready to shutdown the message loop. Clear everything out!
744f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  base::MessageLoop::current()->RunUntilIdle();
745f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  chrome::EndKeepAlive(); // Matching the above chrome::StartKeepAlive().
746f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
747f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // TestBackgroundModeManager has dependencies on the infrastructure.
748f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // It should get cleared first.
749f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  manager.reset();
750f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
751f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // The Profile Manager references the Browser Process.
752f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // The Browser Process references the Notification UI Manager.
753f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // The Notification UI Manager references the Message Center.
754f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // As a result, we have to clear the browser process state here
755f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // before tearing down the Message Center.
756f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  profile_manager.reset();
757f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
758f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Message Center shutdown must occur after the EndKeepAlive because
759f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // EndKeepAlive will end up referencing the message center during cleanup.
760f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  message_center::MessageCenter::Shutdown();
761f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
762f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Clear the shutdown flag to isolate the remaining effect of this test.
763f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  browser_shutdown::SetTryingToQuit(false);
7640f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)}
765