Thiemo Nagel | 610eddf | 2018-05-30 09:04:34 | [diff] [blame] | 1 | // Copyright 2018 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. |
| 4 | |
Sebastien Marchand | 17fa278 | 2019-01-25 19:28:10 | [diff] [blame] | 5 | #include "base/bind_helpers.h" |
Thiemo Nagel | 610eddf | 2018-05-30 09:04:34 | [diff] [blame] | 6 | #include "base/files/file_path.h" |
| 7 | #include "chrome/browser/extensions/extension_apitest.h" |
| 8 | #include "extensions/browser/extension_host.h" |
| 9 | #include "extensions/browser/process_manager.h" |
| 10 | #include "extensions/test/background_page_watcher.h" |
| 11 | #include "extensions/test/extension_test_message_listener.h" |
| 12 | |
| 13 | namespace extensions { |
| 14 | |
| 15 | // This tests an extension that starts a shared worker. |
| 16 | IN_PROC_BROWSER_TEST_F(ExtensionApiTest, SharedWorker) { |
| 17 | EXPECT_TRUE(RunExtensionTest("shared_worker/basic")) << message_; |
| 18 | } |
| 19 | |
| 20 | // This tests an extension that is controlled by a service worker and starts a |
| 21 | // shared worker. The requests for the shared worker scripts and the requests |
| 22 | // initiated by the shared worker should be seen by the service worker. |
| 23 | IN_PROC_BROWSER_TEST_F(ExtensionApiTest, |
| 24 | SharedWorker_ControlledByServiceWorker) { |
| 25 | // Load the extension. It will register a service worker. |
| 26 | ExtensionTestMessageListener listener("READY", false); |
| 27 | listener.set_failure_message("FAIL"); |
| 28 | const Extension* extension = LoadExtension( |
| 29 | test_data_dir_.AppendASCII("shared_worker/service_worker_controlled")); |
| 30 | EXPECT_TRUE(listener.WaitUntilSatisfied()); |
| 31 | EXPECT_TRUE(extension); |
| 32 | ProcessManager* process_manager = ProcessManager::Get(profile()); |
| 33 | ExtensionHost* background_page = |
| 34 | process_manager->GetBackgroundHostForExtension(extension->id()); |
| 35 | |
| 36 | // Close the background page and start it again, so it is controlled |
| 37 | // by the service worker. |
| 38 | ExtensionTestMessageListener listener2("CONTROLLED", false); |
| 39 | listener2.set_failure_message("FAIL"); |
| 40 | background_page->Close(); |
| 41 | BackgroundPageWatcher(process_manager, extension).WaitForClose(); |
| 42 | background_page = nullptr; |
| 43 | process_manager->WakeEventPage(extension->id(), base::DoNothing()); |
| 44 | BackgroundPageWatcher(process_manager, extension).WaitForOpen(); |
| 45 | EXPECT_TRUE(listener2.WaitUntilSatisfied()); |
| 46 | |
| 47 | // The background page should conduct the tests. |
| 48 | ExtensionTestMessageListener listener3("PASS", false); |
| 49 | listener3.set_failure_message("FAIL"); |
| 50 | EXPECT_TRUE(listener3.WaitUntilSatisfied()); |
| 51 | } |
| 52 | |
| 53 | } // namespace extensions |