-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
shared worker: Add tests for Chrome extensions.
We didn't have specific test coverage for shared worker from Chrome extensions, just incidental ones via the ExtensionApiTest.Debugger and ExtensionApiTestWithSwitch.ExtensionDebugger. Shared workers go through a very different loading path than other resources, so it's useful to have tests for them especially including interaction with service workers. This adds tests that currently fail with NetworkService, a next patch will have a fix. Cq-Include-Trybots: master.tryserver.chromium.linux:linux_mojo Change-Id: Ifa6bb3dec8bdea34c3fbefbaeab324fadcb5c929 Bug: 839982 Reviewed-on: https://chromium-review.googlesource.com/1074848 Commit-Queue: Matt Falkenhagen <falken@chromium.org> Reviewed-by: Istiaque Ahmed <lazyboy@chromium.org> Cr-Commit-Position: refs/heads/master@{#562700}
- Loading branch information
Showing
14 changed files
with
255 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright 2018 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "base/files/file_path.h" | ||
#include "chrome/browser/extensions/extension_apitest.h" | ||
#include "extensions/browser/extension_host.h" | ||
#include "extensions/browser/process_manager.h" | ||
#include "extensions/test/background_page_watcher.h" | ||
#include "extensions/test/extension_test_message_listener.h" | ||
|
||
namespace extensions { | ||
|
||
// This tests an extension that starts a shared worker. | ||
IN_PROC_BROWSER_TEST_F(ExtensionApiTest, SharedWorker) { | ||
EXPECT_TRUE(RunExtensionTest("shared_worker/basic")) << message_; | ||
} | ||
|
||
// This tests an extension that is controlled by a service worker and starts a | ||
// shared worker. The requests for the shared worker scripts and the requests | ||
// initiated by the shared worker should be seen by the service worker. | ||
IN_PROC_BROWSER_TEST_F(ExtensionApiTest, | ||
SharedWorker_ControlledByServiceWorker) { | ||
// Load the extension. It will register a service worker. | ||
ExtensionTestMessageListener listener("READY", false); | ||
listener.set_failure_message("FAIL"); | ||
const Extension* extension = LoadExtension( | ||
test_data_dir_.AppendASCII("shared_worker/service_worker_controlled")); | ||
EXPECT_TRUE(listener.WaitUntilSatisfied()); | ||
EXPECT_TRUE(extension); | ||
ProcessManager* process_manager = ProcessManager::Get(profile()); | ||
ExtensionHost* background_page = | ||
process_manager->GetBackgroundHostForExtension(extension->id()); | ||
|
||
// Close the background page and start it again, so it is controlled | ||
// by the service worker. | ||
ExtensionTestMessageListener listener2("CONTROLLED", false); | ||
listener2.set_failure_message("FAIL"); | ||
background_page->Close(); | ||
BackgroundPageWatcher(process_manager, extension).WaitForClose(); | ||
background_page = nullptr; | ||
process_manager->WakeEventPage(extension->id(), base::DoNothing()); | ||
BackgroundPageWatcher(process_manager, extension).WaitForOpen(); | ||
EXPECT_TRUE(listener2.WaitUntilSatisfied()); | ||
|
||
// The background page should conduct the tests. | ||
ExtensionTestMessageListener listener3("PASS", false); | ||
listener3.set_failure_message("FAIL"); | ||
EXPECT_TRUE(listener3.WaitUntilSatisfied()); | ||
} | ||
|
||
} // namespace extensions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
chrome/test/data/extensions/api_test/shared_worker/basic/background.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright 2018 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
chrome.test.runTests([ | ||
function worker() { | ||
const workerPort = new SharedWorker("worker.js").port; | ||
workerPort.onmessage = (evt) => { | ||
if (evt.data != 'hullo there!') { | ||
chrome.test.fail(); | ||
} else { | ||
chrome.test.succeed(); | ||
} | ||
}; | ||
workerPort.start(); | ||
}, | ||
|
||
function workerWithImport() { | ||
const workerPort = new SharedWorker("worker-with-import.js").port; | ||
workerPort.onmessage = (evt) => { | ||
if (evt.data != 'hullo there!') { | ||
chrome.test.fail(); | ||
} else { | ||
chrome.test.succeed(); | ||
} | ||
}; | ||
workerPort.start(); | ||
} | ||
]); |
8 changes: 8 additions & 0 deletions
8
chrome/test/data/extensions/api_test/shared_worker/basic/manifest.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"name": "Shared worker", | ||
"version": "1.0", | ||
"manifest_version": 2, | ||
"background": { | ||
"scripts": ["background.js"] | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
chrome/test/data/extensions/api_test/shared_worker/basic/worker-with-import.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// Copyright 2018 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
importScripts('worker.js'); |
9 changes: 9 additions & 0 deletions
9
chrome/test/data/extensions/api_test/shared_worker/basic/worker.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Copyright 2018 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
self.addEventListener('connect', function(e) { | ||
var port = e.ports[0]; | ||
port.start(); | ||
port.postMessage('hullo there!'); | ||
}); |
10 changes: 10 additions & 0 deletions
10
chrome/test/data/extensions/api_test/shared_worker/service_worker_controlled/background.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<!-- | ||
Copyright 2018 The Chromium Authors. All rights reserved. | ||
Use of this source code is governed by a BSD-style license that can be | ||
found in the LICENSE file. | ||
--> | ||
|
||
<html><body> | ||
background.html contents for testing. | ||
<script src="background.js"></script> | ||
</body></html> |
86 changes: 86 additions & 0 deletions
86
chrome/test/data/extensions/api_test/shared_worker/service_worker_controlled/background.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// Copyright 2018 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
async function setup() { | ||
const reg = await navigator.serviceWorker.register('service_worker.js'); | ||
await navigator.serviceWorker.ready; | ||
if (navigator.serviceWorker.controller) { | ||
chrome.test.sendMessage('CONTROLLED'); | ||
} else { | ||
chrome.test.sendMessage('READY'); | ||
} | ||
} | ||
|
||
async function getMessageFromWorker(worker) { | ||
return new Promise(resolve => { | ||
worker.port.onmessage = evt => { | ||
resolve(evt.data); | ||
} | ||
}); | ||
} | ||
|
||
async function getMessageFromServiceWorker() { | ||
return new Promise(resolve => { | ||
navigator.serviceWorker.onmessage = evt => { | ||
resolve(evt.data); | ||
} | ||
}); | ||
} | ||
|
||
|
||
async function start() { | ||
await setup(); | ||
if (!navigator.serviceWorker.controller) { | ||
// The browser will reload this background page so it gets controlled. | ||
return; | ||
} | ||
|
||
// Start the shared worker. It should send a message about the resources it | ||
// loaded. | ||
const sharedWorker = new SharedWorker('shared_worker.js'); | ||
sharedWorker.port.start(); | ||
const kExpectedMessage = [ | ||
'CONNECTED', | ||
'SCRIPT_IMPORTED', | ||
'FETCHED' | ||
]; | ||
const data = await getMessageFromWorker(sharedWorker); | ||
if (data.length != kExpectedMessage.length) { | ||
throw new Error('bad message length: ' + | ||
`expected ${kExpectedMessage.length}, got ${data.length}`); | ||
} | ||
for (let i = 0; i < data.length; i++) { | ||
if (data[i] != kExpectedMessage[i]) { | ||
throw new Error( | ||
`bad message: expected ${kExpectedMessage[i]}, got ${data[i]}`); | ||
} | ||
} | ||
|
||
// Ask the service worker what URLs it intercepted. | ||
navigator.serviceWorker.controller.postMessage('tell me what urls you saw'); | ||
const urls = await getMessageFromServiceWorker(); | ||
const kExpectedUrls = [ | ||
'background.html', | ||
'background.js', | ||
'shared_worker.js', | ||
'shared_worker_import.js', | ||
'data_for_fetch' | ||
]; | ||
if (urls.length != kExpectedUrls.length) { | ||
throw new Error( | ||
`bad urls: expected ${kExpectedUrls.length}, got ${urls.length}`); | ||
} | ||
for (let i = 0; i < urls.length; i++) { | ||
const expected = new URL(kExpectedUrls[i], self.location).toString(); | ||
if (urls[i] != expected) | ||
throw new Error(`bad url: expected ${expected}, got ${urls[i]}`); | ||
} | ||
|
||
chrome.test.sendMessage('PASS'); | ||
} | ||
|
||
start().catch(err => { | ||
console.error(err.name + ': ' + err.message); | ||
chrome.test.sendMessage('FAIL'); | ||
}); |
1 change: 1 addition & 0 deletions
1
chrome/test/data/extensions/api_test/shared_worker/service_worker_controlled/data_for_fetch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
FETCHED |
9 changes: 9 additions & 0 deletions
9
chrome/test/data/extensions/api_test/shared_worker/service_worker_controlled/manifest.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"name": "Shared worker", | ||
"version": "1.0", | ||
"manifest_version": 2, | ||
"background": { | ||
"page": "background.html", | ||
"persistent": false | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...e/test/data/extensions/api_test/shared_worker/service_worker_controlled/service_worker.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Copyright 2018 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
let urlsSeen = []; | ||
|
||
self.addEventListener('fetch', e => { | ||
urlsSeen.push(e.request.url); | ||
}); | ||
|
||
self.addEventListener('message', e => { | ||
e.source.postMessage(urlsSeen); | ||
urlsSeen = []; | ||
}); |
22 changes: 22 additions & 0 deletions
22
...me/test/data/extensions/api_test/shared_worker/service_worker_controlled/shared_worker.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright 2018 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
let importScriptsGreeting; | ||
let message = []; | ||
|
||
self.onconnect = async function(e) { | ||
const port = e.ports[0]; | ||
port.start(); | ||
message.push('CONNECTED'); | ||
|
||
// The import scripts writes to |importScriptsGreeting|. | ||
importScripts('shared_worker_import.js'); | ||
message.push(importScriptsGreeting); | ||
|
||
const resp = await fetch(new URL('data_for_fetch', self.location)); | ||
const text = await resp.text(); | ||
message.push(text.trim()); | ||
|
||
port.postMessage(message); | ||
}; |
5 changes: 5 additions & 0 deletions
5
.../data/extensions/api_test/shared_worker/service_worker_controlled/shared_worker_import.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// Copyright 2018 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
importScriptsGreeting = 'SCRIPT_IMPORTED'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters