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