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
3 // found in the LICENSE file.
5 #include "base/path_service.h"
6 #include "base/strings/stringprintf.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/browser/background/background_contents_service.h"
9 #include "chrome/browser/background/background_contents_service_factory.h"
10 #include "chrome/browser/background/background_mode_manager.h"
11 #include "chrome/browser/browser_process.h"
12 #include "chrome/browser/chrome_notification_types.h"
13 #include "chrome/browser/extensions/extension_apitest.h"
14 #include "chrome/browser/extensions/extension_service.h"
15 #include "chrome/browser/extensions/extension_test_message_listener.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/ui/browser.h"
18 #include "chrome/browser/ui/browser_dialogs.h"
19 #include "chrome/browser/ui/browser_window.h"
20 #include "chrome/browser/ui/extensions/application_launch.h"
21 #include "chrome/common/chrome_paths.h"
22 #include "chrome/common/chrome_switches.h"
23 #include "content/public/browser/notification_service.h"
24 #include "content/public/test/test_notification_tracker.h"
25 #include "content/public/test/test_utils.h"
26 #include "extensions/common/extension.h"
27 #include "extensions/common/switches.h"
28 #include "net/dns/mock_host_resolver.h"
29 #include "net/test/embedded_test_server/embedded_test_server.h"
31 #if !defined(DISABLE_NACL)
32 #include "components/nacl/browser/nacl_process_host.h"
35 #if defined(OS_MACOSX)
36 #include "base/mac/scoped_nsautorelease_pool.h"
39 using base::ASCIIToUTF16;
40 using extensions::Extension;
42 class AppBackgroundPageApiTest : public ExtensionApiTest {
44 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
45 ExtensionApiTest::SetUpCommandLine(command_line);
46 command_line->AppendSwitch(switches::kDisablePopupBlocking);
47 command_line->AppendSwitch(extensions::switches::kAllowHTTPBackgroundPage);
50 bool CreateApp(const std::string& app_manifest,
51 base::FilePath* app_dir) {
52 if (!app_dir_.CreateUniqueTempDir()) {
53 LOG(ERROR) << "Unable to create a temporary directory.";
56 base::FilePath manifest_path = app_dir_.path().AppendASCII("manifest.json");
57 int bytes_written = base::WriteFile(manifest_path,
60 if (bytes_written != static_cast<int>(app_manifest.size())) {
61 LOG(ERROR) << "Unable to write complete manifest to file. Return code="
65 *app_dir = app_dir_.path();
69 bool WaitForBackgroundMode(bool expected_background_mode) {
70 #if defined(OS_CHROMEOS)
71 // BackgroundMode is not supported on chromeos, so we should test the
72 // behavior of BackgroundContents, but not the background mode state itself.
75 BackgroundModeManager* manager =
76 g_browser_process->background_mode_manager();
77 // If background mode is disabled on this platform (e.g. cros), then skip
79 if (!manager || !manager->IsBackgroundModePrefEnabled()) {
80 DLOG(WARNING) << "Skipping check - background mode disabled";
83 if (manager->IsBackgroundModeActive() == expected_background_mode)
86 // We are not currently in the expected state - wait for the state to
88 content::WindowedNotificationObserver watcher(
89 chrome::NOTIFICATION_BACKGROUND_MODE_CHANGED,
90 content::NotificationService::AllSources());
92 return manager->IsBackgroundModeActive() == expected_background_mode;
96 void CloseBrowser(Browser* browser) {
97 content::WindowedNotificationObserver observer(
98 chrome::NOTIFICATION_BROWSER_CLOSED,
99 content::NotificationService::AllSources());
100 browser->window()->Close();
101 #if defined(OS_MACOSX)
102 // BrowserWindowController depends on the auto release pool being recycled
103 // in the message loop to delete itself, which frees the Browser object
104 // which fires this event.
105 AutoreleasePool()->Recycle();
110 void UnloadExtensionViaTask(const std::string& id) {
111 base::MessageLoop::current()->PostTask(
113 base::Bind(&AppBackgroundPageApiTest::UnloadExtension, this, id));
117 base::ScopedTempDir app_dir_;
122 // Fixture to assist in testing v2 app background pages containing
123 // Native Client embeds.
124 class AppBackgroundPageNaClTest : public AppBackgroundPageApiTest {
126 AppBackgroundPageNaClTest()
127 : extension_(NULL) {}
128 virtual ~AppBackgroundPageNaClTest() {
131 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
132 AppBackgroundPageApiTest::SetUpCommandLine(command_line);
133 #if !defined(DISABLE_NACL)
134 nacl::NaClProcessHost::SetPpapiKeepAliveThrottleForTesting(50);
136 command_line->AppendSwitchASCII(
137 extensions::switches::kEventPageIdleTime, "1000");
138 command_line->AppendSwitchASCII(
139 extensions::switches::kEventPageSuspendingTime, "1000");
142 const Extension* extension() { return extension_; }
145 void LaunchTestingApp() {
146 base::FilePath app_dir;
147 PathService::Get(chrome::DIR_GEN_TEST_DATA, &app_dir);
148 app_dir = app_dir.AppendASCII(
149 "ppapi/tests/extensions/background_keepalive/newlib");
150 extension_ = LoadExtension(app_dir);
151 ASSERT_TRUE(extension_);
155 const Extension* extension_;
158 // Produces an extensions::ProcessManager::ImpulseCallbackForTesting callback
159 // that will match a specified goal and can be waited on.
160 class ImpulseCallbackCounter {
162 explicit ImpulseCallbackCounter(extensions::ProcessManager* manager,
163 const std::string& extension_id)
167 extension_id_(extension_id) {
170 extensions::ProcessManager::ImpulseCallbackForTesting
171 SetGoalAndGetCallback(int goal) {
174 message_loop_runner_ = new content::MessageLoopRunner();
175 return base::Bind(&ImpulseCallbackCounter::ImpulseCallback,
176 base::Unretained(this),
177 message_loop_runner_->QuitClosure(),
182 message_loop_runner_->Run();
185 void ImpulseCallback(
186 const base::Closure& quit_callback,
187 const std::string& extension_id_from_test,
188 const std::string& extension_id_from_manager) {
189 if (extension_id_from_test == extension_id_from_manager) {
190 if (++observed_ >= goal_) {
191 // Clear callback to free reference to message loop.
192 manager_->SetKeepaliveImpulseCallbackForTesting(
193 extensions::ProcessManager::ImpulseCallbackForTesting());
194 manager_->SetKeepaliveImpulseDecrementCallbackForTesting(
195 extensions::ProcessManager::ImpulseCallbackForTesting());
203 extensions::ProcessManager* manager_;
204 const std::string extension_id_;
205 scoped_refptr<content::MessageLoopRunner> message_loop_runner_;
210 // Disable on Mac only. http://crbug.com/95139
211 #if defined(OS_MACOSX)
212 #define MAYBE_Basic DISABLED_Basic
214 #define MAYBE_Basic Basic
217 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, MAYBE_Basic) {
218 host_resolver()->AddRule("a.com", "127.0.0.1");
219 ASSERT_TRUE(StartEmbeddedTestServer());
221 std::string app_manifest = base::StringPrintf(
223 " \"name\": \"App\","
224 " \"version\": \"0.1\","
225 " \"manifest_version\": 2,"
231 " \"web_url\": \"http://a.com:%d/\""
234 " \"permissions\": [\"background\"]"
236 embedded_test_server()->port());
238 base::FilePath app_dir;
239 ASSERT_TRUE(CreateApp(app_manifest, &app_dir));
240 ASSERT_TRUE(LoadExtension(app_dir));
241 // Background mode should not be active until a background page is created.
242 ASSERT_TRUE(WaitForBackgroundMode(false));
243 ASSERT_TRUE(RunExtensionTest("app_background_page/basic")) << message_;
244 // The test closes the background contents, so we should fall back to no
245 // background mode at the end.
246 ASSERT_TRUE(WaitForBackgroundMode(false));
249 // Crashy, http://crbug.com/69215.
250 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, DISABLED_LacksPermission) {
251 host_resolver()->AddRule("a.com", "127.0.0.1");
252 ASSERT_TRUE(StartEmbeddedTestServer());
254 std::string app_manifest = base::StringPrintf(
256 " \"name\": \"App\","
257 " \"version\": \"0.1\","
258 " \"manifest_version\": 2,"
264 " \"web_url\": \"http://a.com:%d/\""
268 embedded_test_server()->port());
270 base::FilePath app_dir;
271 ASSERT_TRUE(CreateApp(app_manifest, &app_dir));
272 ASSERT_TRUE(LoadExtension(app_dir));
273 ASSERT_TRUE(RunExtensionTest("app_background_page/lacks_permission"))
275 ASSERT_TRUE(WaitForBackgroundMode(false));
278 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, ManifestBackgroundPage) {
279 host_resolver()->AddRule("a.com", "127.0.0.1");
280 ASSERT_TRUE(StartEmbeddedTestServer());
282 std::string app_manifest = base::StringPrintf(
284 " \"name\": \"App\","
285 " \"version\": \"0.1\","
286 " \"manifest_version\": 2,"
292 " \"web_url\": \"http://a.com:%d/\""
295 " \"permissions\": [\"background\"],"
297 " \"page\": \"http://a.com:%d/test.html\""
300 embedded_test_server()->port(),
301 embedded_test_server()->port());
303 base::FilePath app_dir;
304 ASSERT_TRUE(CreateApp(app_manifest, &app_dir));
305 // Background mode should not be active now because no background app was
307 ASSERT_TRUE(LoadExtension(app_dir));
308 // Background mode be active now because a background page was created when
309 // the app was loaded.
310 ASSERT_TRUE(WaitForBackgroundMode(true));
312 const Extension* extension = GetSingleLoadedExtension();
314 BackgroundContentsServiceFactory::GetForProfile(browser()->profile())->
315 GetAppBackgroundContents(ASCIIToUTF16(extension->id())));
316 UnloadExtension(extension->id());
319 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, NoJsBackgroundPage) {
320 // Keep the task manager up through this test to verify that a crash doesn't
321 // happen when window.open creates a background page that switches
322 // RenderViewHosts. See http://crbug.com/165138.
323 chrome::ShowTaskManager(browser());
325 // Make sure that no BackgroundContentses get deleted (a signal that repeated
326 // window.open calls recreate instances, instead of being no-ops).
327 content::TestNotificationTracker background_deleted_tracker;
328 background_deleted_tracker.ListenFor(
329 chrome::NOTIFICATION_BACKGROUND_CONTENTS_DELETED,
330 content::Source<Profile>(browser()->profile()));
332 host_resolver()->AddRule("a.com", "127.0.0.1");
333 ASSERT_TRUE(StartEmbeddedTestServer());
335 std::string app_manifest = base::StringPrintf(
337 " \"name\": \"App\","
338 " \"version\": \"0.1\","
339 " \"manifest_version\": 2,"
345 " \"web_url\": \"http://a.com:%d/test.html\""
348 " \"permissions\": [\"background\"],"
350 " \"allow_js_access\": false"
353 embedded_test_server()->port());
355 base::FilePath app_dir;
356 ASSERT_TRUE(CreateApp(app_manifest, &app_dir));
357 ASSERT_TRUE(LoadExtension(app_dir));
359 // There isn't a background page loaded initially.
360 const Extension* extension = GetSingleLoadedExtension();
362 BackgroundContentsServiceFactory::GetForProfile(browser()->profile())->
363 GetAppBackgroundContents(ASCIIToUTF16(extension->id())));
364 // The test makes sure that window.open returns null.
365 ASSERT_TRUE(RunExtensionTest("app_background_page/no_js")) << message_;
366 // And after it runs there should be a background page.
368 BackgroundContentsServiceFactory::GetForProfile(browser()->profile())->
369 GetAppBackgroundContents(ASCIIToUTF16(extension->id())));
371 EXPECT_EQ(0u, background_deleted_tracker.size());
372 UnloadExtension(extension->id());
375 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, NoJsManifestBackgroundPage) {
376 host_resolver()->AddRule("a.com", "127.0.0.1");
377 ASSERT_TRUE(StartEmbeddedTestServer());
379 std::string app_manifest = base::StringPrintf(
381 " \"name\": \"App\","
382 " \"version\": \"0.1\","
383 " \"manifest_version\": 2,"
389 " \"web_url\": \"http://a.com:%d/\""
392 " \"permissions\": [\"background\"],"
394 " \"page\": \"http://a.com:%d/bg.html\","
395 " \"allow_js_access\": false"
398 embedded_test_server()->port(),
399 embedded_test_server()->port());
401 base::FilePath app_dir;
402 ASSERT_TRUE(CreateApp(app_manifest, &app_dir));
403 ASSERT_TRUE(LoadExtension(app_dir));
405 // The background page should load, but window.open should return null.
406 const Extension* extension = GetSingleLoadedExtension();
408 BackgroundContentsServiceFactory::GetForProfile(browser()->profile())->
409 GetAppBackgroundContents(ASCIIToUTF16(extension->id())));
410 ASSERT_TRUE(RunExtensionTest("app_background_page/no_js_manifest")) <<
412 UnloadExtension(extension->id());
415 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, OpenTwoBackgroundPages) {
416 host_resolver()->AddRule("a.com", "127.0.0.1");
417 ASSERT_TRUE(StartEmbeddedTestServer());
419 std::string app_manifest = base::StringPrintf(
421 " \"name\": \"App\","
422 " \"version\": \"0.1\","
423 " \"manifest_version\": 2,"
429 " \"web_url\": \"http://a.com:%d/\""
432 " \"permissions\": [\"background\"]"
434 embedded_test_server()->port());
436 base::FilePath app_dir;
437 ASSERT_TRUE(CreateApp(app_manifest, &app_dir));
438 ASSERT_TRUE(LoadExtension(app_dir));
439 const Extension* extension = GetSingleLoadedExtension();
440 ASSERT_TRUE(RunExtensionTest("app_background_page/two_pages")) << message_;
441 UnloadExtension(extension->id());
444 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, OpenTwoPagesWithManifest) {
445 host_resolver()->AddRule("a.com", "127.0.0.1");
446 ASSERT_TRUE(StartEmbeddedTestServer());
448 std::string app_manifest = base::StringPrintf(
450 " \"name\": \"App\","
451 " \"version\": \"0.1\","
452 " \"manifest_version\": 2,"
458 " \"web_url\": \"http://a.com:%d/\""
462 " \"page\": \"http://a.com:%d/bg.html\""
464 " \"permissions\": [\"background\"]"
466 embedded_test_server()->port(),
467 embedded_test_server()->port());
469 base::FilePath app_dir;
470 ASSERT_TRUE(CreateApp(app_manifest, &app_dir));
471 ASSERT_TRUE(LoadExtension(app_dir));
472 const Extension* extension = GetSingleLoadedExtension();
473 ASSERT_TRUE(RunExtensionTest("app_background_page/two_with_manifest")) <<
475 UnloadExtension(extension->id());
478 // Times out occasionally -- see crbug.com/108493
479 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, DISABLED_OpenPopupFromBGPage) {
480 host_resolver()->AddRule("a.com", "127.0.0.1");
481 ASSERT_TRUE(StartEmbeddedTestServer());
483 std::string app_manifest = base::StringPrintf(
485 " \"name\": \"App\","
486 " \"version\": \"0.1\","
487 " \"manifest_version\": 2,"
493 " \"web_url\": \"http://a.com:%d/\""
496 " \"background\": { \"page\": \"http://a.com:%d/extensions/api_test/"
497 "app_background_page/bg_open/bg_open_bg.html\" },"
498 " \"permissions\": [\"background\"]"
500 embedded_test_server()->port(),
501 embedded_test_server()->port());
503 base::FilePath app_dir;
504 ASSERT_TRUE(CreateApp(app_manifest, &app_dir));
505 ASSERT_TRUE(LoadExtension(app_dir));
506 ASSERT_TRUE(RunExtensionTest("app_background_page/bg_open")) << message_;
509 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, DISABLED_OpenThenClose) {
510 host_resolver()->AddRule("a.com", "127.0.0.1");
511 ASSERT_TRUE(StartEmbeddedTestServer());
513 std::string app_manifest = base::StringPrintf(
515 " \"name\": \"App\","
516 " \"version\": \"0.1\","
517 " \"manifest_version\": 2,"
523 " \"web_url\": \"http://a.com:%d/\""
526 " \"permissions\": [\"background\"]"
528 embedded_test_server()->port());
530 base::FilePath app_dir;
531 ASSERT_TRUE(CreateApp(app_manifest, &app_dir));
532 ASSERT_TRUE(LoadExtension(app_dir));
533 // There isn't a background page loaded initially.
534 const Extension* extension = GetSingleLoadedExtension();
536 BackgroundContentsServiceFactory::GetForProfile(browser()->profile())->
537 GetAppBackgroundContents(ASCIIToUTF16(extension->id())));
538 // Background mode should not be active until a background page is created.
539 ASSERT_TRUE(WaitForBackgroundMode(false));
540 ASSERT_TRUE(RunExtensionTest("app_background_page/basic_open")) << message_;
541 // Background mode should be active now because a background page was created.
542 ASSERT_TRUE(WaitForBackgroundMode(true));
544 BackgroundContentsServiceFactory::GetForProfile(browser()->profile())->
545 GetAppBackgroundContents(ASCIIToUTF16(extension->id())));
546 // Now close the BackgroundContents.
547 ASSERT_TRUE(RunExtensionTest("app_background_page/basic_close")) << message_;
548 // Background mode should no longer be active.
549 ASSERT_TRUE(WaitForBackgroundMode(false));
551 BackgroundContentsServiceFactory::GetForProfile(browser()->profile())->
552 GetAppBackgroundContents(ASCIIToUTF16(extension->id())));
555 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, UnloadExtensionWhileHidden) {
556 host_resolver()->AddRule("a.com", "127.0.0.1");
557 ASSERT_TRUE(StartEmbeddedTestServer());
559 std::string app_manifest = base::StringPrintf(
561 " \"name\": \"App\","
562 " \"version\": \"0.1\","
563 " \"manifest_version\": 2,"
569 " \"web_url\": \"http://a.com:%d/\""
572 " \"permissions\": [\"background\"],"
574 " \"page\": \"http://a.com:%d/test.html\""
577 embedded_test_server()->port(),
578 embedded_test_server()->port());
580 base::FilePath app_dir;
581 ASSERT_TRUE(CreateApp(app_manifest, &app_dir));
582 // Background mode should not be active now because no background app was
584 ASSERT_TRUE(LoadExtension(app_dir));
585 // Background mode be active now because a background page was created when
586 // the app was loaded.
587 ASSERT_TRUE(WaitForBackgroundMode(true));
589 const Extension* extension = GetSingleLoadedExtension();
591 BackgroundContentsServiceFactory::GetForProfile(browser()->profile())->
592 GetAppBackgroundContents(ASCIIToUTF16(extension->id())));
594 // Close all browsers - app should continue running.
595 set_exit_when_last_browser_closes(false);
596 CloseBrowser(browser());
598 // Post a task to unload the extension - this should cause Chrome to exit
599 // cleanly (not crash).
600 UnloadExtensionViaTask(extension->id());
601 content::RunAllPendingInMessageLoop();
602 ASSERT_TRUE(WaitForBackgroundMode(false));
605 // Verify active NaCl embeds cause many keepalive impulses to be sent.
606 // Disabled on Windows due to flakiness: http://crbug.com/346278
608 #define MAYBE_BackgroundKeepaliveActive DISABLED_BackgroundKeepaliveActive
610 #define MAYBE_BackgroundKeepaliveActive BackgroundKeepaliveActive
612 IN_PROC_BROWSER_TEST_F(AppBackgroundPageNaClTest,
613 MAYBE_BackgroundKeepaliveActive) {
614 #if !defined(DISABLE_NACL)
615 ExtensionTestMessageListener nacl_modules_loaded("nacl_modules_loaded", true);
617 extensions::ProcessManager* manager =
618 extensions::ExtensionSystem::Get(browser()->profile())->process_manager();
619 ImpulseCallbackCounter active_impulse_counter(manager, extension()->id());
620 EXPECT_TRUE(nacl_modules_loaded.WaitUntilSatisfied());
622 // Target .5 seconds: .5 seconds / 50ms throttle * 2 embeds == 20 impulses.
623 manager->SetKeepaliveImpulseCallbackForTesting(
624 active_impulse_counter.SetGoalAndGetCallback(20));
625 active_impulse_counter.Wait();
629 // Verify that nacl modules that go idle will not send keepalive impulses.
630 // Disabled on windows due to Win XP failures:
631 // DesktopWindowTreeHostWin::HandleCreate not implemented. crbug.com/331954
633 #define MAYBE_BackgroundKeepaliveIdle DISABLED_BackgroundKeepaliveIdle
635 // ASAN errors appearing: https://crbug.com/332440
636 #define MAYBE_BackgroundKeepaliveIdle DISABLED_BackgroundKeepaliveIdle
638 IN_PROC_BROWSER_TEST_F(AppBackgroundPageNaClTest,
639 MAYBE_BackgroundKeepaliveIdle) {
640 #if !defined(DISABLE_NACL)
641 ExtensionTestMessageListener nacl_modules_loaded("nacl_modules_loaded", true);
643 extensions::ProcessManager* manager =
644 extensions::ExtensionSystem::Get(browser()->profile())->process_manager();
645 ImpulseCallbackCounter idle_impulse_counter(manager, extension()->id());
646 EXPECT_TRUE(nacl_modules_loaded.WaitUntilSatisfied());
648 manager->SetKeepaliveImpulseDecrementCallbackForTesting(
649 idle_impulse_counter.SetGoalAndGetCallback(1));
650 nacl_modules_loaded.Reply("be idle");
651 idle_impulse_counter.Wait();