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_service.h" |
17 #include "content/test/test_notification_tracker.h" | 20 #include "content/test/test_notification_tracker.h" |
18 #include "net/base/mock_host_resolver.h" | 21 #include "net/base/mock_host_resolver.h" |
19 | 22 |
20 class AppBackgroundPageApiTest : public ExtensionApiTest { | 23 class AppBackgroundPageApiTest : public ExtensionApiTest { |
21 public: | 24 public: |
22 void SetUpCommandLine(CommandLine* command_line) OVERRIDE { | 25 void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
23 ExtensionApiTest::SetUpCommandLine(command_line); | 26 ExtensionApiTest::SetUpCommandLine(command_line); |
24 command_line->AppendSwitch(switches::kDisablePopupBlocking); | 27 command_line->AppendSwitch(switches::kDisablePopupBlocking); |
25 command_line->AppendSwitch(switches::kAllowHTTPBackgroundPage); | 28 command_line->AppendSwitch(switches::kAllowHTTPBackgroundPage); |
26 } | 29 } |
(...skipping 10 matching lines...) Expand all Loading... |
37 app_manifest.size()); | 40 app_manifest.size()); |
38 if (bytes_written != static_cast<int>(app_manifest.size())) { | 41 if (bytes_written != static_cast<int>(app_manifest.size())) { |
39 LOG(ERROR) << "Unable to write complete manifest to file. Return code=" | 42 LOG(ERROR) << "Unable to write complete manifest to file. Return code=" |
40 << bytes_written; | 43 << bytes_written; |
41 return false; | 44 return false; |
42 } | 45 } |
43 *app_dir = app_dir_.path(); | 46 *app_dir = app_dir_.path(); |
44 return true; | 47 return true; |
45 } | 48 } |
46 | 49 |
| 50 bool WaitForBackgroundMode(bool expected_background_mode) { |
| 51 #if defined(OS_CHROMEOS) |
| 52 // BackgroundMode is not supported on chromeos, so we should test the |
| 53 // behavior of BackgroundContents, but not the background mode state itself. |
| 54 return true; |
| 55 #else |
| 56 BackgroundModeManager* manager = |
| 57 g_browser_process->background_mode_manager(); |
| 58 // If background mode is disabled on this platform (e.g. cros), then skip |
| 59 // this check. |
| 60 if (!manager || !manager->IsBackgroundModePrefEnabled()) { |
| 61 DLOG(WARNING) << "Skipping check - background mode disabled"; |
| 62 return true; |
| 63 } |
| 64 if (manager->IsBackgroundModeActiveForTest() == expected_background_mode) |
| 65 return true; |
| 66 |
| 67 // We are not currently in the expected state - wait for the state to |
| 68 // change. |
| 69 ui_test_utils::WindowedNotificationObserver watcher( |
| 70 chrome::NOTIFICATION_BACKGROUND_MODE_CHANGED, |
| 71 content::NotificationService::AllSources()); |
| 72 watcher.Wait(); |
| 73 return manager->IsBackgroundModeActiveForTest() == expected_background_mode; |
| 74 #endif |
| 75 } |
| 76 |
47 private: | 77 private: |
48 ScopedTempDir app_dir_; | 78 ScopedTempDir app_dir_; |
49 }; | 79 }; |
50 | 80 |
51 // Disable on Mac only. http://crbug.com/95139 | 81 // Disable on Mac only. http://crbug.com/95139 |
52 #if defined(OS_MACOSX) | 82 #if defined(OS_MACOSX) |
53 #define MAYBE_Basic DISABLED_Basic | 83 #define MAYBE_Basic DISABLED_Basic |
54 #else | 84 #else |
55 #define MAYBE_Basic Basic | 85 #define MAYBE_Basic Basic |
56 #endif | 86 #endif |
(...skipping 15 matching lines...) Expand all Loading... |
72 " \"web_url\": \"http://a.com:%d/\"" | 102 " \"web_url\": \"http://a.com:%d/\"" |
73 " }" | 103 " }" |
74 " }," | 104 " }," |
75 " \"permissions\": [\"background\"]" | 105 " \"permissions\": [\"background\"]" |
76 "}", | 106 "}", |
77 test_server()->host_port_pair().port()); | 107 test_server()->host_port_pair().port()); |
78 | 108 |
79 FilePath app_dir; | 109 FilePath app_dir; |
80 ASSERT_TRUE(CreateApp(app_manifest, &app_dir)); | 110 ASSERT_TRUE(CreateApp(app_manifest, &app_dir)); |
81 ASSERT_TRUE(LoadExtension(app_dir)); | 111 ASSERT_TRUE(LoadExtension(app_dir)); |
| 112 // Background mode should not be active until a background page is created. |
| 113 ASSERT_TRUE(WaitForBackgroundMode(false)); |
82 ASSERT_TRUE(RunExtensionTest("app_background_page/basic")) << message_; | 114 ASSERT_TRUE(RunExtensionTest("app_background_page/basic")) << message_; |
| 115 // The test closes the background contents, so we should fall back to no |
| 116 // background mode at the end. |
| 117 ASSERT_TRUE(WaitForBackgroundMode(false)); |
83 } | 118 } |
84 | 119 |
85 // Crashy, http://crbug.com/69215. | 120 // Crashy, http://crbug.com/69215. |
86 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, DISABLED_LacksPermission) { | 121 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, DISABLED_LacksPermission) { |
87 host_resolver()->AddRule("a.com", "127.0.0.1"); | 122 host_resolver()->AddRule("a.com", "127.0.0.1"); |
88 ASSERT_TRUE(StartTestServer()); | 123 ASSERT_TRUE(StartTestServer()); |
89 | 124 |
90 std::string app_manifest = base::StringPrintf( | 125 std::string app_manifest = base::StringPrintf( |
91 "{" | 126 "{" |
92 " \"name\": \"App\"," | 127 " \"name\": \"App\"," |
93 " \"version\": \"0.1\"," | 128 " \"version\": \"0.1\"," |
94 " \"manifest_version\": 2," | 129 " \"manifest_version\": 2," |
95 " \"app\": {" | 130 " \"app\": {" |
96 " \"urls\": [" | 131 " \"urls\": [" |
97 " \"http://a.com/\"" | 132 " \"http://a.com/\"" |
98 " ]," | 133 " ]," |
99 " \"launch\": {" | 134 " \"launch\": {" |
100 " \"web_url\": \"http://a.com:%d/\"" | 135 " \"web_url\": \"http://a.com:%d/\"" |
101 " }" | 136 " }" |
102 " }" | 137 " }" |
103 "}", | 138 "}", |
104 test_server()->host_port_pair().port()); | 139 test_server()->host_port_pair().port()); |
105 | 140 |
106 FilePath app_dir; | 141 FilePath app_dir; |
107 ASSERT_TRUE(CreateApp(app_manifest, &app_dir)); | 142 ASSERT_TRUE(CreateApp(app_manifest, &app_dir)); |
108 ASSERT_TRUE(LoadExtension(app_dir)); | 143 ASSERT_TRUE(LoadExtension(app_dir)); |
109 ASSERT_TRUE(RunExtensionTest("app_background_page/lacks_permission")) | 144 ASSERT_TRUE(RunExtensionTest("app_background_page/lacks_permission")) |
110 << message_; | 145 << message_; |
| 146 ASSERT_TRUE(WaitForBackgroundMode(false)); |
111 } | 147 } |
112 | 148 |
113 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, ManifestBackgroundPage) { | 149 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, ManifestBackgroundPage) { |
114 host_resolver()->AddRule("a.com", "127.0.0.1"); | 150 host_resolver()->AddRule("a.com", "127.0.0.1"); |
115 ASSERT_TRUE(StartTestServer()); | 151 ASSERT_TRUE(StartTestServer()); |
116 | 152 |
117 std::string app_manifest = base::StringPrintf( | 153 std::string app_manifest = base::StringPrintf( |
118 "{" | 154 "{" |
119 " \"name\": \"App\"," | 155 " \"name\": \"App\"," |
120 " \"version\": \"0.1\"," | 156 " \"version\": \"0.1\"," |
121 " \"manifest_version\": 2," | 157 " \"manifest_version\": 2," |
122 " \"app\": {" | 158 " \"app\": {" |
123 " \"urls\": [" | 159 " \"urls\": [" |
124 " \"http://a.com/\"" | 160 " \"http://a.com/\"" |
125 " ]," | 161 " ]," |
126 " \"launch\": {" | 162 " \"launch\": {" |
127 " \"web_url\": \"http://a.com:%d/\"" | 163 " \"web_url\": \"http://a.com:%d/\"" |
128 " }" | 164 " }" |
129 " }," | 165 " }," |
130 " \"permissions\": [\"background\"]," | 166 " \"permissions\": [\"background\"]," |
131 " \"background\": {" | 167 " \"background\": {" |
132 " \"page\": \"http://a.com:%d/test.html\"" | 168 " \"page\": \"http://a.com:%d/test.html\"" |
133 " }" | 169 " }" |
134 "}", | 170 "}", |
135 test_server()->host_port_pair().port(), | 171 test_server()->host_port_pair().port(), |
136 test_server()->host_port_pair().port()); | 172 test_server()->host_port_pair().port()); |
137 | 173 |
138 FilePath app_dir; | 174 FilePath app_dir; |
139 ASSERT_TRUE(CreateApp(app_manifest, &app_dir)); | 175 ASSERT_TRUE(CreateApp(app_manifest, &app_dir)); |
| 176 // Background mode should not be active now because no background app was |
| 177 // loaded. |
140 ASSERT_TRUE(LoadExtension(app_dir)); | 178 ASSERT_TRUE(LoadExtension(app_dir)); |
| 179 // Background mode be active now because a background page was created when |
| 180 // the app was loaded. |
| 181 ASSERT_TRUE(WaitForBackgroundMode(true)); |
141 | 182 |
142 const Extension* extension = GetSingleLoadedExtension(); | 183 const Extension* extension = GetSingleLoadedExtension(); |
143 ASSERT_TRUE( | 184 ASSERT_TRUE( |
144 BackgroundContentsServiceFactory::GetForProfile(browser()->profile())-> | 185 BackgroundContentsServiceFactory::GetForProfile(browser()->profile())-> |
145 GetAppBackgroundContents(ASCIIToUTF16(extension->id()))); | 186 GetAppBackgroundContents(ASCIIToUTF16(extension->id()))); |
146 } | 187 } |
147 | 188 |
148 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, NoJsBackgroundPage) { | 189 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, NoJsBackgroundPage) { |
149 // Make sure that no BackgroundContentses get deleted (a signal that repeated | 190 // Make sure that no BackgroundContentses get deleted (a signal that repeated |
150 // window.open calls recreate instances, instead of being no-ops). | 191 // 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\"]" | 357 " \"permissions\": [\"background\"]" |
317 "}", | 358 "}", |
318 test_server()->host_port_pair().port(), | 359 test_server()->host_port_pair().port(), |
319 test_server()->host_port_pair().port()); | 360 test_server()->host_port_pair().port()); |
320 | 361 |
321 FilePath app_dir; | 362 FilePath app_dir; |
322 ASSERT_TRUE(CreateApp(app_manifest, &app_dir)); | 363 ASSERT_TRUE(CreateApp(app_manifest, &app_dir)); |
323 ASSERT_TRUE(LoadExtension(app_dir)); | 364 ASSERT_TRUE(LoadExtension(app_dir)); |
324 ASSERT_TRUE(RunExtensionTest("app_background_page/bg_open")) << message_; | 365 ASSERT_TRUE(RunExtensionTest("app_background_page/bg_open")) << message_; |
325 } | 366 } |
| 367 |
| 368 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, OpenThenClose) { |
| 369 host_resolver()->AddRule("a.com", "127.0.0.1"); |
| 370 ASSERT_TRUE(StartTestServer()); |
| 371 |
| 372 std::string app_manifest = base::StringPrintf( |
| 373 "{" |
| 374 " \"name\": \"App\"," |
| 375 " \"version\": \"0.1\"," |
| 376 " \"manifest_version\": 2," |
| 377 " \"app\": {" |
| 378 " \"urls\": [" |
| 379 " \"http://a.com/\"" |
| 380 " ]," |
| 381 " \"launch\": {" |
| 382 " \"web_url\": \"http://a.com:%d/\"" |
| 383 " }" |
| 384 " }," |
| 385 " \"permissions\": [\"background\"]" |
| 386 "}", |
| 387 test_server()->host_port_pair().port()); |
| 388 |
| 389 FilePath app_dir; |
| 390 ASSERT_TRUE(CreateApp(app_manifest, &app_dir)); |
| 391 ASSERT_TRUE(LoadExtension(app_dir)); |
| 392 // There isn't a background page loaded initially. |
| 393 const Extension* extension = GetSingleLoadedExtension(); |
| 394 ASSERT_FALSE( |
| 395 BackgroundContentsServiceFactory::GetForProfile(browser()->profile())-> |
| 396 GetAppBackgroundContents(ASCIIToUTF16(extension->id()))); |
| 397 // Background mode should not be active until a background page is created. |
| 398 ASSERT_TRUE(WaitForBackgroundMode(false)); |
| 399 ASSERT_TRUE(RunExtensionTest("app_background_page/basic_open")) << message_; |
| 400 // Background mode should be active now because a background page was created. |
| 401 ASSERT_TRUE(WaitForBackgroundMode(true)); |
| 402 ASSERT_TRUE( |
| 403 BackgroundContentsServiceFactory::GetForProfile(browser()->profile())-> |
| 404 GetAppBackgroundContents(ASCIIToUTF16(extension->id()))); |
| 405 // Now close the BackgroundContents. |
| 406 ASSERT_TRUE(RunExtensionTest("app_background_page/basic_close")) << message_; |
| 407 // Background mode should no longer be active. |
| 408 ASSERT_TRUE(WaitForBackgroundMode(false)); |
| 409 ASSERT_FALSE( |
| 410 BackgroundContentsServiceFactory::GetForProfile(browser()->profile())-> |
| 411 GetAppBackgroundContents(ASCIIToUTF16(extension->id()))); |
| 412 } |
OLD | NEW |