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