Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/stringprintf.h" | 5 #include "base/stringprintf.h" |
| 6 #include "base/utf_string_conversions.h" | 6 #include "base/utf_string_conversions.h" |
| 7 #include "chrome/browser/background/background_contents_service.h" | 7 #include "chrome/browser/background/background_contents_service.h" |
| 8 #include "chrome/browser/background/background_contents_service_factory.h" | 8 #include "chrome/browser/background/background_contents_service_factory.h" |
| 9 #include "chrome/browser/background/background_mode_manager.h" | |
| 10 #include "chrome/browser/browser_process.h" | |
| 9 #include "chrome/browser/extensions/extension_apitest.h" | 11 #include "chrome/browser/extensions/extension_apitest.h" |
| 10 #include "chrome/browser/extensions/extension_service.h" | 12 #include "chrome/browser/extensions/extension_service.h" |
| 11 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/browser/ui/browser.h" | 14 #include "chrome/browser/ui/browser.h" |
| 13 #include "chrome/common/chrome_notification_types.h" | 15 #include "chrome/common/chrome_notification_types.h" |
| 14 #include "chrome/common/chrome_switches.h" | 16 #include "chrome/common/chrome_switches.h" |
| 15 #include "chrome/common/extensions/extension.h" | 17 #include "chrome/common/extensions/extension.h" |
| 16 #include "chrome/test/base/ui_test_utils.h" | 18 #include "chrome/test/base/ui_test_utils.h" |
| 19 #include "content/public/browser/notification_registrar.h" | |
| 20 #include "content/public/browser/notification_service.h" | |
| 17 #include "content/test/test_notification_tracker.h" | 21 #include "content/test/test_notification_tracker.h" |
| 18 #include "net/base/mock_host_resolver.h" | 22 #include "net/base/mock_host_resolver.h" |
| 19 | 23 |
| 20 class AppBackgroundPageApiTest : public ExtensionApiTest { | 24 class AppBackgroundPageApiTest : public ExtensionApiTest { |
| 21 public: | 25 public: |
| 22 void SetUpCommandLine(CommandLine* command_line) OVERRIDE { | 26 void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
| 23 ExtensionApiTest::SetUpCommandLine(command_line); | 27 ExtensionApiTest::SetUpCommandLine(command_line); |
| 24 command_line->AppendSwitch(switches::kDisablePopupBlocking); | 28 command_line->AppendSwitch(switches::kDisablePopupBlocking); |
| 25 command_line->AppendSwitch(switches::kAllowHTTPBackgroundPage); | 29 command_line->AppendSwitch(switches::kAllowHTTPBackgroundPage); |
| 26 } | 30 } |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 37 app_manifest.size()); | 41 app_manifest.size()); |
| 38 if (bytes_written != static_cast<int>(app_manifest.size())) { | 42 if (bytes_written != static_cast<int>(app_manifest.size())) { |
| 39 LOG(ERROR) << "Unable to write complete manifest to file. Return code=" | 43 LOG(ERROR) << "Unable to write complete manifest to file. Return code=" |
| 40 << bytes_written; | 44 << bytes_written; |
| 41 return false; | 45 return false; |
| 42 } | 46 } |
| 43 *app_dir = app_dir_.path(); | 47 *app_dir = app_dir_.path(); |
| 44 return true; | 48 return true; |
| 45 } | 49 } |
| 46 | 50 |
| 51 // Blocks waiting for BackgroundModeManager to transition background mode | |
| 52 // to the expected state. | |
| 53 class BackgroundModeWatcher : public content::NotificationObserver { | |
| 54 public: | |
| 55 explicit BackgroundModeWatcher(bool expected) : expected_(expected) { | |
| 56 registrar_.Add( | |
| 57 this, chrome::NOTIFICATION_BACKGROUND_MODE_CHANGED, | |
|
Mihai Parparita -not on Chrome
2012/05/03 19:32:35
Does this end up firing more than once, before it
| |
| 58 content::NotificationService::AllSources()); | |
| 59 } | |
| 60 | |
| 61 void WaitForChange() { | |
| 62 ui_test_utils::RunMessageLoop(); | |
| 63 } | |
| 64 | |
| 65 virtual void Observe(int type, | |
| 66 const content::NotificationSource& source, | |
| 67 const content::NotificationDetails& details) OVERRIDE { | |
| 68 // If BackgroundMode is finally in the expected state, exit. | |
| 69 if (expected_ == *content::Details<bool>(details).ptr()) | |
| 70 MessageLoopForUI::current()->Quit(); | |
| 71 } | |
| 72 | |
| 73 private: | |
| 74 content::NotificationRegistrar registrar_; | |
| 75 // The background mode value we are waiting for. | |
| 76 bool expected_; | |
| 77 }; | |
| 78 | |
| 79 bool WaitForBackgroundMode(bool expected_background_mode) { | |
| 80 BackgroundModeManager* manager = | |
| 81 g_browser_process->background_mode_manager(); | |
| 82 // If background mode is disabled on this platform (e.g. cros), then skip | |
| 83 // this check. | |
| 84 if (!manager || !manager->IsBackgroundModePrefEnabled()) { | |
| 85 DLOG(WARNING) << "Skipping check - background mode disabled"; | |
| 86 return true; | |
| 87 } | |
| 88 if (manager->IsBackgroundModeActiveForTest() == expected_background_mode) | |
| 89 return true; | |
| 90 | |
| 91 // We are not currently in the expected state - wait for the state to | |
| 92 // change. | |
| 93 BackgroundModeWatcher watcher(expected_background_mode); | |
| 94 watcher.WaitForChange(); | |
| 95 return manager->IsBackgroundModeActiveForTest() == expected_background_mode; | |
| 96 } | |
| 97 | |
| 47 private: | 98 private: |
| 48 ScopedTempDir app_dir_; | 99 ScopedTempDir app_dir_; |
| 49 }; | 100 }; |
| 50 | 101 |
| 51 // Disable on Mac only. http://crbug.com/95139 | 102 // Disable on Mac only. http://crbug.com/95139 |
| 52 #if defined(OS_MACOSX) | 103 #if defined(OS_MACOSX) |
| 53 #define MAYBE_Basic DISABLED_Basic | 104 #define MAYBE_Basic DISABLED_Basic |
| 54 #else | 105 #else |
| 55 #define MAYBE_Basic Basic | 106 #define MAYBE_Basic Basic |
| 56 #endif | 107 #endif |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 72 " \"web_url\": \"http://a.com:%d/\"" | 123 " \"web_url\": \"http://a.com:%d/\"" |
| 73 " }" | 124 " }" |
| 74 " }," | 125 " }," |
| 75 " \"permissions\": [\"background\"]" | 126 " \"permissions\": [\"background\"]" |
| 76 "}", | 127 "}", |
| 77 test_server()->host_port_pair().port()); | 128 test_server()->host_port_pair().port()); |
| 78 | 129 |
| 79 FilePath app_dir; | 130 FilePath app_dir; |
| 80 ASSERT_TRUE(CreateApp(app_manifest, &app_dir)); | 131 ASSERT_TRUE(CreateApp(app_manifest, &app_dir)); |
| 81 ASSERT_TRUE(LoadExtension(app_dir)); | 132 ASSERT_TRUE(LoadExtension(app_dir)); |
| 133 // Background mode should not be active until a background page is created. | |
| 134 ASSERT_TRUE(WaitForBackgroundMode(false)); | |
| 82 ASSERT_TRUE(RunExtensionTest("app_background_page/basic")) << message_; | 135 ASSERT_TRUE(RunExtensionTest("app_background_page/basic")) << message_; |
| 136 // The test closes the background contents, so we should fall back to no | |
| 137 // background mode at the end. | |
| 138 ASSERT_TRUE(WaitForBackgroundMode(false)); | |
| 83 } | 139 } |
| 84 | 140 |
| 85 // Crashy, http://crbug.com/69215. | 141 // Crashy, http://crbug.com/69215. |
| 86 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, DISABLED_LacksPermission) { | 142 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, DISABLED_LacksPermission) { |
| 87 host_resolver()->AddRule("a.com", "127.0.0.1"); | 143 host_resolver()->AddRule("a.com", "127.0.0.1"); |
| 88 ASSERT_TRUE(StartTestServer()); | 144 ASSERT_TRUE(StartTestServer()); |
| 89 | 145 |
| 90 std::string app_manifest = base::StringPrintf( | 146 std::string app_manifest = base::StringPrintf( |
| 91 "{" | 147 "{" |
| 92 " \"name\": \"App\"," | 148 " \"name\": \"App\"," |
| 93 " \"version\": \"0.1\"," | 149 " \"version\": \"0.1\"," |
| 94 " \"manifest_version\": 2," | 150 " \"manifest_version\": 2," |
| 95 " \"app\": {" | 151 " \"app\": {" |
| 96 " \"urls\": [" | 152 " \"urls\": [" |
| 97 " \"http://a.com/\"" | 153 " \"http://a.com/\"" |
| 98 " ]," | 154 " ]," |
| 99 " \"launch\": {" | 155 " \"launch\": {" |
| 100 " \"web_url\": \"http://a.com:%d/\"" | 156 " \"web_url\": \"http://a.com:%d/\"" |
| 101 " }" | 157 " }" |
| 102 " }" | 158 " }" |
| 103 "}", | 159 "}", |
| 104 test_server()->host_port_pair().port()); | 160 test_server()->host_port_pair().port()); |
| 105 | 161 |
| 106 FilePath app_dir; | 162 FilePath app_dir; |
| 107 ASSERT_TRUE(CreateApp(app_manifest, &app_dir)); | 163 ASSERT_TRUE(CreateApp(app_manifest, &app_dir)); |
| 108 ASSERT_TRUE(LoadExtension(app_dir)); | 164 ASSERT_TRUE(LoadExtension(app_dir)); |
| 109 ASSERT_TRUE(RunExtensionTest("app_background_page/lacks_permission")) | 165 ASSERT_TRUE(RunExtensionTest("app_background_page/lacks_permission")) |
| 110 << message_; | 166 << message_; |
| 167 ASSERT_TRUE(WaitForBackgroundMode(false)); | |
| 111 } | 168 } |
| 112 | 169 |
| 113 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, ManifestBackgroundPage) { | 170 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, ManifestBackgroundPage) { |
| 114 host_resolver()->AddRule("a.com", "127.0.0.1"); | 171 host_resolver()->AddRule("a.com", "127.0.0.1"); |
| 115 ASSERT_TRUE(StartTestServer()); | 172 ASSERT_TRUE(StartTestServer()); |
| 116 | 173 |
| 117 std::string app_manifest = base::StringPrintf( | 174 std::string app_manifest = base::StringPrintf( |
| 118 "{" | 175 "{" |
| 119 " \"name\": \"App\"," | 176 " \"name\": \"App\"," |
| 120 " \"version\": \"0.1\"," | 177 " \"version\": \"0.1\"," |
| 121 " \"manifest_version\": 2," | 178 " \"manifest_version\": 2," |
| 122 " \"app\": {" | 179 " \"app\": {" |
| 123 " \"urls\": [" | 180 " \"urls\": [" |
| 124 " \"http://a.com/\"" | 181 " \"http://a.com/\"" |
| 125 " ]," | 182 " ]," |
| 126 " \"launch\": {" | 183 " \"launch\": {" |
| 127 " \"web_url\": \"http://a.com:%d/\"" | 184 " \"web_url\": \"http://a.com:%d/\"" |
| 128 " }" | 185 " }" |
| 129 " }," | 186 " }," |
| 130 " \"permissions\": [\"background\"]," | 187 " \"permissions\": [\"background\"]," |
| 131 " \"background\": {" | 188 " \"background\": {" |
| 132 " \"page\": \"http://a.com:%d/test.html\"" | 189 " \"page\": \"http://a.com:%d/test.html\"" |
| 133 " }" | 190 " }" |
| 134 "}", | 191 "}", |
| 135 test_server()->host_port_pair().port(), | 192 test_server()->host_port_pair().port(), |
| 136 test_server()->host_port_pair().port()); | 193 test_server()->host_port_pair().port()); |
| 137 | 194 |
| 138 FilePath app_dir; | 195 FilePath app_dir; |
| 139 ASSERT_TRUE(CreateApp(app_manifest, &app_dir)); | 196 ASSERT_TRUE(CreateApp(app_manifest, &app_dir)); |
| 197 // Background mode should not be active now because no background app was | |
| 198 // loaded. | |
| 140 ASSERT_TRUE(LoadExtension(app_dir)); | 199 ASSERT_TRUE(LoadExtension(app_dir)); |
| 200 // Background mode be active now because a background page was created when | |
| 201 // the app was loaded. | |
| 202 ASSERT_TRUE(WaitForBackgroundMode(true)); | |
| 141 | 203 |
| 142 const Extension* extension = GetSingleLoadedExtension(); | 204 const Extension* extension = GetSingleLoadedExtension(); |
| 143 ASSERT_TRUE( | 205 ASSERT_TRUE( |
| 144 BackgroundContentsServiceFactory::GetForProfile(browser()->profile())-> | 206 BackgroundContentsServiceFactory::GetForProfile(browser()->profile())-> |
| 145 GetAppBackgroundContents(ASCIIToUTF16(extension->id()))); | 207 GetAppBackgroundContents(ASCIIToUTF16(extension->id()))); |
| 146 } | 208 } |
| 147 | 209 |
| 148 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, NoJsBackgroundPage) { | 210 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, NoJsBackgroundPage) { |
| 149 // Make sure that no BackgroundContentses get deleted (a signal that repeated | 211 // Make sure that no BackgroundContentses get deleted (a signal that repeated |
| 150 // window.open calls recreate instances, instead of being no-ops). | 212 // window.open calls recreate instances, instead of being no-ops). |
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 316 " \"permissions\": [\"background\"]" | 378 " \"permissions\": [\"background\"]" |
| 317 "}", | 379 "}", |
| 318 test_server()->host_port_pair().port(), | 380 test_server()->host_port_pair().port(), |
| 319 test_server()->host_port_pair().port()); | 381 test_server()->host_port_pair().port()); |
| 320 | 382 |
| 321 FilePath app_dir; | 383 FilePath app_dir; |
| 322 ASSERT_TRUE(CreateApp(app_manifest, &app_dir)); | 384 ASSERT_TRUE(CreateApp(app_manifest, &app_dir)); |
| 323 ASSERT_TRUE(LoadExtension(app_dir)); | 385 ASSERT_TRUE(LoadExtension(app_dir)); |
| 324 ASSERT_TRUE(RunExtensionTest("app_background_page/bg_open")) << message_; | 386 ASSERT_TRUE(RunExtensionTest("app_background_page/bg_open")) << message_; |
| 325 } | 387 } |
| 388 | |
| 389 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, OpenThenClose) { | |
| 390 host_resolver()->AddRule("a.com", "127.0.0.1"); | |
| 391 ASSERT_TRUE(StartTestServer()); | |
| 392 | |
| 393 std::string app_manifest = base::StringPrintf( | |
| 394 "{" | |
| 395 " \"name\": \"App\"," | |
| 396 " \"version\": \"0.1\"," | |
| 397 " \"manifest_version\": 2," | |
| 398 " \"app\": {" | |
| 399 " \"urls\": [" | |
| 400 " \"http://a.com/\"" | |
| 401 " ]," | |
| 402 " \"launch\": {" | |
| 403 " \"web_url\": \"http://a.com:%d/\"" | |
| 404 " }" | |
| 405 " }," | |
| 406 " \"permissions\": [\"background\"]" | |
| 407 "}", | |
| 408 test_server()->host_port_pair().port()); | |
| 409 | |
| 410 FilePath app_dir; | |
| 411 ASSERT_TRUE(CreateApp(app_manifest, &app_dir)); | |
| 412 ASSERT_TRUE(LoadExtension(app_dir)); | |
| 413 // There isn't a background page loaded initially. | |
| 414 const Extension* extension = GetSingleLoadedExtension(); | |
| 415 ASSERT_FALSE( | |
| 416 BackgroundContentsServiceFactory::GetForProfile(browser()->profile())-> | |
| 417 GetAppBackgroundContents(ASCIIToUTF16(extension->id()))); | |
| 418 // Background mode should not be active until a background page is created. | |
| 419 ASSERT_TRUE(WaitForBackgroundMode(false)); | |
| 420 ASSERT_TRUE(RunExtensionTest("app_background_page/basic_open")) << message_; | |
| 421 // Background mode should be active now because a background page was created. | |
| 422 ASSERT_TRUE(WaitForBackgroundMode(true)); | |
| 423 ASSERT_TRUE( | |
| 424 BackgroundContentsServiceFactory::GetForProfile(browser()->profile())-> | |
| 425 GetAppBackgroundContents(ASCIIToUTF16(extension->id()))); | |
| 426 // Now close the BackgroundContents. | |
| 427 ASSERT_TRUE(RunExtensionTest("app_background_page/basic_close")) << message_; | |
| 428 // Background mode should no longer be active. | |
| 429 ASSERT_TRUE(WaitForBackgroundMode(false)); | |
| 430 ASSERT_FALSE( | |
| 431 BackgroundContentsServiceFactory::GetForProfile(browser()->profile())-> | |
| 432 GetAppBackgroundContents(ASCIIToUTF16(extension->id()))); | |
| 433 | |
|
Mihai Parparita -not on Chrome
2012/05/03 19:32:35
Nit: extra newline.
| |
| 434 } | |
| OLD | NEW |