Commit 684cd02c authored by Rayan Kanso's avatar Rayan Kanso Committed by Commit Bot

[Background Fetch] Add e2e browsertests for the full lifetime of a fetch

Change-Id: I172418ee124e867393cfb4a207fe4d898a06ec31
Reviewed-on: https://chromium-review.googlesource.com/1136447
Commit-Queue: Rayan Kanso <rayankans@chromium.org>
Reviewed-by: default avatarPeter Beverloo <peter@chromium.org>
Cr-Commit-Position: refs/heads/master@{#575274}
parent ef2808b2
...@@ -88,7 +88,7 @@ class WaitableDownloadLoggerObserver : public download::Logger::Observer { ...@@ -88,7 +88,7 @@ class WaitableDownloadLoggerObserver : public download::Logger::Observer {
const std::string& result = service_request.FindKey("result")->GetString(); const std::string& result = service_request.FindKey("result")->GetString();
if (client != kBackgroundFetchClient) if (client != kBackgroundFetchClient)
return; // this event is not targeted to us return; // This event is not targeted to us.
if (result == kResultAccepted && download_accepted_callback_) if (result == kResultAccepted && download_accepted_callback_)
std::move(download_accepted_callback_).Run(guid); std::move(download_accepted_callback_).Run(guid);
...@@ -217,6 +217,15 @@ class BackgroundFetchBrowserTest : public InProcessBrowserTest { ...@@ -217,6 +217,15 @@ class BackgroundFetchBrowserTest : public InProcessBrowserTest {
run_loop.Run(); run_loop.Run();
} }
// Runs the |script| and waits for a background fetch event.
// Wrap in ASSERT_NO_FATAL_FAILURE().
void RunScriptAndCheckResultingEvent(const std::string& script,
const std::string& expected_event) {
std::string result;
ASSERT_NO_FATAL_FAILURE(RunScript(script, &result));
ASSERT_EQ(expected_event, result);
}
void GetVisualsForOfflineItemSync( void GetVisualsForOfflineItemSync(
const ContentId& offline_item_id, const ContentId& offline_item_id,
std::unique_ptr<OfflineItemVisuals>* out_visuals) { std::unique_ptr<OfflineItemVisuals>* out_visuals) {
...@@ -475,4 +484,12 @@ IN_PROC_BROWSER_TEST_F( ...@@ -475,4 +484,12 @@ IN_PROC_BROWSER_TEST_F(
EXPECT_EQ(offline_item.progress.unit, OfflineItemProgressUnit::PERCENTAGE); EXPECT_EQ(offline_item.progress.unit, OfflineItemProgressUnit::PERCENTAGE);
} }
IN_PROC_BROWSER_TEST_F(BackgroundFetchBrowserTest, FetchesRunToCompletion) {
// Starts two seperate multifile fetches and waits for them to complete.
ASSERT_NO_FATAL_FAILURE(RunScriptAndCheckResultingEvent(
"RunFetchTillCompletion()", "backgroundfetched"));
ASSERT_NO_FATAL_FAILURE(RunScriptAndCheckResultingEvent(
"RunFetchTillCompletionWithMissingResource()", "backgroundfetchfail"));
}
} // namespace } // namespace
...@@ -84,3 +84,35 @@ function StartSingleFileDownloadWithCorrectDownloadTotal() { ...@@ -84,3 +84,35 @@ function StartSingleFileDownloadWithCorrectDownloadTotal() {
sendResultToTest('ok'); sendResultToTest('ok');
}).catch(sendErrorToTest); }).catch(sendErrorToTest);
} }
// Listens for a postMessage from sw.js and sends the result to the test.
navigator.serviceWorker.addEventListener('message', (event) => {
if (['backgroundfetched', 'backgroundfetchfail'].includes(event.data))
sendResultToTest(event.data);
else
sendErrorToTest(Error('Unexpected message received: ' + event.data));
});
// Starts a Backgound Fetch that should succeed.
function RunFetchTillCompletion() {
const resources = [
'/background_fetch/types_of_cheese.txt?idx=1',
'/background_fetch/types_of_cheese.txt?idx=2',
];
navigator.serviceWorker.ready.then(swRegistration => {
return swRegistration.backgroundFetch.fetch(
kBackgroundFetchId, resources);
}).catch(sendErrorToTest);
}
// Starts a Background Fetch that should fail due to a missing resource.
function RunFetchTillCompletionWithMissingResource() {
const resources = [
'/background_fetch/types_of_cheese.txt',
'/background_fetch/missing_cat.txt',
];
navigator.serviceWorker.ready.then(swRegistration => {
return swRegistration.backgroundFetch.fetch(
kBackgroundFetchId, resources);
}).catch(sendErrorToTest);
}
...@@ -2,4 +2,17 @@ ...@@ -2,4 +2,17 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
// Deliberately left empty. // Service Worker initialization listeners.
self.addEventListener('install', e => e.waitUntil(skipWaiting()));
self.addEventListener('activate', e => e.waitUntil(clients.claim()));
// Posts |msg| to background_fetch.js.
function postToWindowClients(msg) {
clients.matchAll({ type: 'window' }).then(clientWindows => {
for (const client of clientWindows) client.postMessage(msg);
});
}
// Background Fetch event listeners.
self.addEventListener('backgroundfetched', e => postToWindowClients(e.type));
self.addEventListener('backgroundfetchfail', e => postToWindowClients(e.type));
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment