Commit 3372a247 authored by Matt Falkenhagen's avatar Matt Falkenhagen Committed by Commit Bot

service worker: Refactoring in job unittest to use signal instead of RunUntilIdle.

This is more preparation for changing EmbeddedWorkerTestHelper tests
to use Mojo.

The job tests were overly constrained about what tasks ran when, when
they just need to make assertions about whether a version became
activated, etc. This introduces a mechanism to wait for a version status
change, instead of relying on various RunUntilIdle()-like calls.

More details: My plan is to change the RequestTermination() mechanism in
this file to use the Mojo method
EmbeddedWorkerInstanceHost::RequestTermination() rather than calling
ServiceWorkerVersion::RequestTermination() directly. But in the Mojo
version, it's not easy to just wait for the callback, since the browser
actually sends back StopWorker() first and then the callback. The
default mock client would immediately simulate stopping and sever
the Mojo connection in StopWorker(), so the RequestTermination callback
is never invoked.

But these tests shouldn't be concerned with the details of
RequestTermination() callback and StopWorker() order. They just want to
assert that an update occurred.

Bug: 927651
Change-Id: I0b4729f731e1880f0731d7ba4f766b8b52e8b93c
Reviewed-on: https://chromium-review.googlesource.com/c/1454203
Commit-Queue: Matt Falkenhagen <falken@chromium.org>
Reviewed-by: default avatarMakoto Shimazu <shimazu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#629619}
parent 5c4417e5
// Copyright 2019 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 "content/browser/service_worker/test_service_worker_observer.h"
#include <utility>
#include "base/run_loop.h"
#include "base/test/test_simple_task_runner.h"
#include "content/browser/service_worker/service_worker_context_wrapper.h"
namespace content {
TestServiceWorkerObserver::TestServiceWorkerObserver(
scoped_refptr<ServiceWorkerContextWrapper> wrapper)
: wrapper_(std::move(wrapper)) {
wrapper_->AddObserver(this);
}
TestServiceWorkerObserver::~TestServiceWorkerObserver() {
wrapper_->RemoveObserver(this);
}
void TestServiceWorkerObserver::RunUntilStatusChange(
ServiceWorkerVersion* version,
ServiceWorkerVersion::Status status) {
if (version->status() == status)
return;
base::RunLoop loop;
version_id_for_status_change_ = version->version_id();
status_for_status_change_ = status;
DCHECK(!quit_closure_for_status_change_);
quit_closure_for_status_change_ = loop.QuitClosure();
loop.Run();
}
void TestServiceWorkerObserver::RunUntilActivated(
ServiceWorkerVersion* version,
scoped_refptr<base::TestSimpleTaskRunner> runner) {
if (version->status() == ServiceWorkerVersion::ACTIVATED)
return;
// Call runner->RunUntilIdle() to skip the delay for the activate event in
// ServiceWorkerRegistration.
RunUntilStatusChange(version, ServiceWorkerVersion::ACTIVATING);
runner->RunUntilIdle();
RunUntilStatusChange(version, ServiceWorkerVersion::ACTIVATED);
}
void TestServiceWorkerObserver::OnVersionStateChanged(
int64_t version_id,
const GURL& scope,
ServiceWorkerVersion::Status status) {
if (version_id == version_id_for_status_change_ &&
status == status_for_status_change_ && quit_closure_for_status_change_) {
std::move(quit_closure_for_status_change_).Run();
}
}
} // namespace content
// Copyright 2019 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.
#ifndef CONTENT_BROWSER_SERVICE_WORKER_TEST_SERVICE_WORKER_OBSERVER_H_
#define CONTENT_BROWSER_SERVICE_WORKER_TEST_SERVICE_WORKER_OBSERVER_H_
#include "base/macros.h"
#include "base/memory/scoped_refptr.h"
#include "content/browser/service_worker/service_worker_context_core_observer.h"
#include "content/browser/service_worker/service_worker_version.h"
namespace base {
class TestSimpleTaskRunner;
}
namespace content {
class ServiceWorkerContextWrapper;
// Observes events related to service workers. Exposes convenience methods for
// use in tests.
class TestServiceWorkerObserver : public ServiceWorkerContextCoreObserver {
public:
explicit TestServiceWorkerObserver(
scoped_refptr<ServiceWorkerContextWrapper> wrapper);
~TestServiceWorkerObserver() override;
// Returns when |version| reaches |status|.
void RunUntilStatusChange(ServiceWorkerVersion* version,
ServiceWorkerVersion::Status status);
// Returns when |version| reaches ACTIVATED. |runner| should
// be the version's registration's task runner. This function is
// useful for skipping the 1 second wall time delay for the activate event in
// ServiceWorkerRegistration. If not for that delay, a single
// RunUntilStatusChange() call for ACTIVATED would suffice.
void RunUntilActivated(ServiceWorkerVersion* version,
scoped_refptr<base::TestSimpleTaskRunner> runner);
private:
// ServiceWorkerContextCoreObserver overrides:
void OnVersionStateChanged(int64_t version_id,
const GURL& scope,
ServiceWorkerVersion::Status status) override;
scoped_refptr<ServiceWorkerContextWrapper> wrapper_;
int64_t version_id_for_status_change_ = -1;
ServiceWorkerVersion::Status status_for_status_change_ =
ServiceWorkerVersion::NEW;
base::OnceClosure quit_closure_for_status_change_;
DISALLOW_COPY_AND_ASSIGN(TestServiceWorkerObserver);
};
} // namespace content
#endif // CONTENT_BROWSER_SERVICE_WORKER_TEST_SERVICE_WORKER_OBSERVER_H_
......@@ -56,6 +56,8 @@ jumbo_static_library("test_support") {
"../browser/service_worker/embedded_worker_test_helper.h",
"../browser/service_worker/service_worker_test_utils.cc",
"../browser/service_worker/service_worker_test_utils.h",
"../browser/service_worker/test_service_worker_observer.cc",
"../browser/service_worker/test_service_worker_observer.h",
"../browser/web_package/mock_signed_exchange_handler.cc",
"../browser/web_package/mock_signed_exchange_handler.h",
"../public/test/audio_service_test_helper.cc",
......
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