Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(762)

Unified Diff: content/browser/service_worker/service_worker_job_unittest.cc

Issue 1223193009: WIP attempt to replace StartWorker/StopWorker IPCs with a new mojo EmbeddedWorker service. Base URL: https://chromium.googlesource.com/chromium/src.git@mojo-event-dispatching-option2
Patch Set: Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: content/browser/service_worker/service_worker_job_unittest.cc
diff --git a/content/browser/service_worker/service_worker_job_unittest.cc b/content/browser/service_worker/service_worker_job_unittest.cc
index e49c8294d64896eb2982149518a22db44f21098e..e31449a37ab2a2d27e2ab7ea0c678386d631405f 100644
--- a/content/browser/service_worker/service_worker_job_unittest.cc
+++ b/content/browser/service_worker/service_worker_job_unittest.cc
@@ -16,6 +16,7 @@
#include "content/browser/service_worker/service_worker_test_utils.h"
#include "content/common/service_worker/service_worker_messages.h"
#include "content/public/test/test_browser_thread_bundle.h"
+#include "content/public/test/test_utils.h"
#include "ipc/ipc_test_sink.h"
#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
@@ -32,10 +33,9 @@ namespace content {
namespace {
-int kMockRenderProcessId = 88;
-
void SaveRegistrationCallback(
ServiceWorkerStatusCode expected_status,
+ const scoped_refptr<MessageLoopRunner>& runner,
bool* called,
scoped_refptr<ServiceWorkerRegistration>* registration_out,
ServiceWorkerStatusCode status,
@@ -44,6 +44,7 @@ void SaveRegistrationCallback(
EXPECT_EQ(expected_status, status);
*called = true;
*registration_out = registration;
+ runner->Quit();
}
void SaveFoundRegistrationCallback(
@@ -64,11 +65,12 @@ void SaveFoundRegistrationCallback(
// isn't called.
ServiceWorkerRegisterJob::RegistrationCallback SaveRegistration(
ServiceWorkerStatusCode expected_status,
+ const scoped_refptr<MessageLoopRunner>& runner,
bool* called,
scoped_refptr<ServiceWorkerRegistration>* registration) {
*called = false;
- return base::Bind(
- &SaveRegistrationCallback, expected_status, called, registration);
+ return base::Bind(&SaveRegistrationCallback, expected_status, runner, called,
+ registration);
}
ServiceWorkerStorage::FindRegistrationCallback SaveFoundRegistration(
@@ -102,12 +104,10 @@ ServiceWorkerUnregisterJob::UnregistrationCallback SaveUnregistration(
class ServiceWorkerJobTest : public testing::Test {
public:
ServiceWorkerJobTest()
- : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP),
- render_process_id_(kMockRenderProcessId) {}
+ : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {}
void SetUp() override {
- helper_.reset(
- new EmbeddedWorkerTestHelper(base::FilePath(), render_process_id_));
+ helper_.reset(new EmbeddedWorkerTestHelper(base::FilePath()));
}
void TearDown() override { helper_.reset(); }
@@ -134,7 +134,6 @@ class ServiceWorkerJobTest : public testing::Test {
TestBrowserThreadBundle browser_thread_bundle_;
scoped_ptr<EmbeddedWorkerTestHelper> helper_;
- int render_process_id_;
};
scoped_refptr<ServiceWorkerRegistration> ServiceWorkerJobTest::RunRegisterJob(
@@ -142,12 +141,13 @@ scoped_refptr<ServiceWorkerRegistration> ServiceWorkerJobTest::RunRegisterJob(
const GURL& script_url,
ServiceWorkerStatusCode expected_status) {
scoped_refptr<ServiceWorkerRegistration> registration;
+ scoped_refptr<MessageLoopRunner> runner(new MessageLoopRunner);
bool called;
job_coordinator()->Register(
pattern, script_url, NULL,
- SaveRegistration(expected_status, &called, &registration));
+ SaveRegistration(expected_status, runner, &called, &registration));
EXPECT_FALSE(called);
- base::RunLoop().RunUntilIdle();
+ runner->Run();
EXPECT_TRUE(called);
return registration;
}
@@ -232,25 +232,28 @@ TEST_F(ServiceWorkerJobTest, SameMatchSameRegistration) {
}
TEST_F(ServiceWorkerJobTest, DifferentMatchDifferentRegistration) {
+ scoped_refptr<MessageLoopRunner> runner1(new MessageLoopRunner);
bool called1;
scoped_refptr<ServiceWorkerRegistration> original_registration1;
job_coordinator()->Register(
GURL("http://www.example.com/one/"),
- GURL("http://www.example.com/service_worker.js"),
- NULL,
- SaveRegistration(SERVICE_WORKER_OK, &called1, &original_registration1));
+ GURL("http://www.example.com/service_worker.js"), NULL,
+ SaveRegistration(SERVICE_WORKER_OK, runner1, &called1,
+ &original_registration1));
+ scoped_refptr<MessageLoopRunner> runner2(new MessageLoopRunner);
bool called2;
scoped_refptr<ServiceWorkerRegistration> original_registration2;
job_coordinator()->Register(
GURL("http://www.example.com/two/"),
- GURL("http://www.example.com/service_worker.js"),
- NULL,
- SaveRegistration(SERVICE_WORKER_OK, &called2, &original_registration2));
+ GURL("http://www.example.com/service_worker.js"), NULL,
+ SaveRegistration(SERVICE_WORKER_OK, runner2, &called2,
+ &original_registration2));
EXPECT_FALSE(called1);
EXPECT_FALSE(called2);
- base::RunLoop().RunUntilIdle();
+ runner1->Run();
+ runner2->Run();
EXPECT_TRUE(called2);
EXPECT_TRUE(called1);
@@ -356,8 +359,8 @@ TEST_F(ServiceWorkerJobTest, RegisterDuplicateScript) {
class FailToStartWorkerTestHelper : public EmbeddedWorkerTestHelper {
public:
- explicit FailToStartWorkerTestHelper(int mock_render_process_id)
- : EmbeddedWorkerTestHelper(base::FilePath(), mock_render_process_id) {}
+ explicit FailToStartWorkerTestHelper()
+ : EmbeddedWorkerTestHelper(base::FilePath()) {}
void OnStartWorker(int embedded_worker_id,
int64 service_worker_version_id,
@@ -369,7 +372,7 @@ class FailToStartWorkerTestHelper : public EmbeddedWorkerTestHelper {
};
TEST_F(ServiceWorkerJobTest, Register_FailToStartWorker) {
- helper_.reset(new FailToStartWorkerTestHelper(render_process_id_));
+ helper_.reset(new FailToStartWorkerTestHelper());
scoped_refptr<ServiceWorkerRegistration> registration =
RunRegisterJob(GURL("http://www.example.com/"),
@@ -385,13 +388,13 @@ TEST_F(ServiceWorkerJobTest, ParallelRegUnreg) {
GURL pattern("http://www.example.com/");
GURL script_url("http://www.example.com/service_worker.js");
+ scoped_refptr<MessageLoopRunner> runner(new MessageLoopRunner);
bool registration_called = false;
scoped_refptr<ServiceWorkerRegistration> registration;
job_coordinator()->Register(
- pattern,
- script_url,
- NULL,
- SaveRegistration(SERVICE_WORKER_OK, &registration_called, &registration));
+ pattern, script_url, NULL,
+ SaveRegistration(SERVICE_WORKER_OK, runner, &registration_called,
+ &registration));
bool unregistration_called = false;
job_coordinator()->Unregister(
@@ -400,6 +403,7 @@ TEST_F(ServiceWorkerJobTest, ParallelRegUnreg) {
ASSERT_FALSE(registration_called);
ASSERT_FALSE(unregistration_called);
+ runner->Run();
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(registration_called);
ASSERT_TRUE(unregistration_called);
@@ -417,28 +421,27 @@ TEST_F(ServiceWorkerJobTest, ParallelRegNewScript) {
GURL pattern("http://www.example.com/");
GURL script_url1("http://www.example.com/service_worker1.js");
+ scoped_refptr<MessageLoopRunner> runner1(new MessageLoopRunner);
bool registration1_called = false;
scoped_refptr<ServiceWorkerRegistration> registration1;
job_coordinator()->Register(
- pattern,
- script_url1,
- NULL,
- SaveRegistration(
- SERVICE_WORKER_OK, &registration1_called, &registration1));
+ pattern, script_url1, NULL,
+ SaveRegistration(SERVICE_WORKER_OK, runner1, &registration1_called,
+ &registration1));
GURL script_url2("http://www.example.com/service_worker2.js");
+ scoped_refptr<MessageLoopRunner> runner2(new MessageLoopRunner);
bool registration2_called = false;
scoped_refptr<ServiceWorkerRegistration> registration2;
job_coordinator()->Register(
- pattern,
- script_url2,
- NULL,
- SaveRegistration(
- SERVICE_WORKER_OK, &registration2_called, &registration2));
+ pattern, script_url2, NULL,
+ SaveRegistration(SERVICE_WORKER_OK, runner2, &registration2_called,
+ &registration2));
ASSERT_FALSE(registration1_called);
ASSERT_FALSE(registration2_called);
- base::RunLoop().RunUntilIdle();
+ runner1->Run();
+ runner2->Run();
ASSERT_TRUE(registration1_called);
ASSERT_TRUE(registration2_called);
@@ -455,27 +458,26 @@ TEST_F(ServiceWorkerJobTest, ParallelRegSameScript) {
GURL pattern("http://www.example.com/");
GURL script_url("http://www.example.com/service_worker1.js");
+ scoped_refptr<MessageLoopRunner> runner1(new MessageLoopRunner);
bool registration1_called = false;
scoped_refptr<ServiceWorkerRegistration> registration1;
job_coordinator()->Register(
- pattern,
- script_url,
- NULL,
- SaveRegistration(
- SERVICE_WORKER_OK, &registration1_called, &registration1));
+ pattern, script_url, NULL,
+ SaveRegistration(SERVICE_WORKER_OK, runner1, &registration1_called,
+ &registration1));
+ scoped_refptr<MessageLoopRunner> runner2(new MessageLoopRunner);
bool registration2_called = false;
scoped_refptr<ServiceWorkerRegistration> registration2;
job_coordinator()->Register(
- pattern,
- script_url,
- NULL,
- SaveRegistration(
- SERVICE_WORKER_OK, &registration2_called, &registration2));
+ pattern, script_url, NULL,
+ SaveRegistration(SERVICE_WORKER_OK, runner2, &registration2_called,
+ &registration2));
ASSERT_FALSE(registration1_called);
ASSERT_FALSE(registration2_called);
- base::RunLoop().RunUntilIdle();
+ runner1->Run();
+ runner2->Run();
ASSERT_TRUE(registration1_called);
ASSERT_TRUE(registration2_called);
@@ -525,29 +527,28 @@ TEST_F(ServiceWorkerJobTest, AbortAll_Register) {
GURL script_url1("http://www1.example.com/service_worker.js");
GURL script_url2("http://www2.example.com/service_worker.js");
+ scoped_refptr<MessageLoopRunner> runner1(new MessageLoopRunner);
bool registration_called1 = false;
scoped_refptr<ServiceWorkerRegistration> registration1;
job_coordinator()->Register(
- pattern1,
- script_url1,
- NULL,
- SaveRegistration(SERVICE_WORKER_ERROR_ABORT,
+ pattern1, script_url1, NULL,
+ SaveRegistration(SERVICE_WORKER_ERROR_ABORT, runner1,
&registration_called1, &registration1));
+ scoped_refptr<MessageLoopRunner> runner2(new MessageLoopRunner);
bool registration_called2 = false;
scoped_refptr<ServiceWorkerRegistration> registration2;
job_coordinator()->Register(
- pattern2,
- script_url2,
- NULL,
- SaveRegistration(SERVICE_WORKER_ERROR_ABORT,
+ pattern2, script_url2, NULL,
+ SaveRegistration(SERVICE_WORKER_ERROR_ABORT, runner2,
&registration_called2, &registration2));
ASSERT_FALSE(registration_called1);
ASSERT_FALSE(registration_called2);
job_coordinator()->AbortAll();
- base::RunLoop().RunUntilIdle();
+ runner1->Run();
+ runner2->Run();
ASSERT_TRUE(registration_called1);
ASSERT_TRUE(registration_called2);
@@ -600,14 +601,13 @@ TEST_F(ServiceWorkerJobTest, AbortAll_RegUnreg) {
GURL pattern("http://www.example.com/");
GURL script_url("http://www.example.com/service_worker.js");
+ scoped_refptr<MessageLoopRunner> runner(new MessageLoopRunner);
bool registration_called = false;
scoped_refptr<ServiceWorkerRegistration> registration;
job_coordinator()->Register(
- pattern,
- script_url,
- NULL,
- SaveRegistration(SERVICE_WORKER_ERROR_ABORT,
- &registration_called, &registration));
+ pattern, script_url, NULL,
+ SaveRegistration(SERVICE_WORKER_ERROR_ABORT, runner, &registration_called,
+ &registration));
bool unregistration_called = false;
job_coordinator()->Unregister(
@@ -619,6 +619,7 @@ TEST_F(ServiceWorkerJobTest, AbortAll_RegUnreg) {
ASSERT_FALSE(unregistration_called);
job_coordinator()->AbortAll();
+ runner->Run();
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(registration_called);
ASSERT_TRUE(unregistration_called);
@@ -642,8 +643,10 @@ TEST_F(ServiceWorkerJobTest, UnregisterWaitingSetsRedundant) {
scoped_refptr<ServiceWorkerVersion> version = new ServiceWorkerVersion(
registration.get(), script_url, 1L, helper_->context()->AsWeakPtr());
ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED;
- version->StartWorker(CreateReceiverOnCurrentThread(&status));
- base::RunLoop().RunUntilIdle();
+ scoped_refptr<MessageLoopRunner> runner(new MessageLoopRunner);
+ version->StartWorker(
+ CreateReceiverOnCurrentThread(&status, runner->QuitClosure()));
+ runner->Run();
ASSERT_EQ(SERVICE_WORKER_OK, status);
version->SetStatus(ServiceWorkerVersion::INSTALLED);
@@ -779,9 +782,8 @@ class UpdateJobTestHelper
ServiceWorkerVersion::Status status;
};
- UpdateJobTestHelper(int mock_render_process_id)
- : EmbeddedWorkerTestHelper(base::FilePath(), mock_render_process_id),
- update_found_(false) {}
+ UpdateJobTestHelper()
+ : EmbeddedWorkerTestHelper(base::FilePath()), update_found_(false) {}
~UpdateJobTestHelper() override {
if (registration_.get())
registration_->RemoveListener(this);
@@ -795,13 +797,12 @@ class UpdateJobTestHelper
scoped_refptr<ServiceWorkerRegistration> SetupInitialRegistration(
const GURL& test_origin) {
scoped_refptr<ServiceWorkerRegistration> registration;
+ scoped_refptr<MessageLoopRunner> runner(new MessageLoopRunner);
bool called = false;
job_coordinator()->Register(
- test_origin.Resolve(kScope),
- test_origin.Resolve(kScript),
- NULL,
- SaveRegistration(SERVICE_WORKER_OK, &called, &registration));
- base::RunLoop().RunUntilIdle();
+ test_origin.Resolve(kScope), test_origin.Resolve(kScript), NULL,
+ SaveRegistration(SERVICE_WORKER_OK, runner, &called, &registration));
+ runner->Run();
EXPECT_TRUE(called);
EXPECT_TRUE(registration.get());
EXPECT_TRUE(registration->active_version());
@@ -871,6 +872,8 @@ class UpdateJobTestHelper
void OnUpdateFound(ServiceWorkerRegistration* registration) override {
ASSERT_FALSE(update_found_);
update_found_ = true;
+ if (wait_for_update_runner_)
+ wait_for_update_runner_->Quit();
}
// ServiceWorkerVersion::Listener overrides
@@ -879,6 +882,8 @@ class UpdateJobTestHelper
entry.version_id = version->version_id();
entry.status = version->status();
state_change_log_.push_back(entry);
+ if (wait_for_state_change_runner_)
+ wait_for_state_change_runner_->Quit();
}
scoped_refptr<ServiceWorkerRegistration> registration_;
@@ -886,14 +891,15 @@ class UpdateJobTestHelper
std::vector<AttributeChangeLogEntry> attribute_change_log_;
std::vector<StateChangeLogEntry> state_change_log_;
bool update_found_;
+ scoped_refptr<MessageLoopRunner> wait_for_update_runner_;
+ scoped_refptr<MessageLoopRunner> wait_for_state_change_runner_;
};
// Helper class for update tests that evicts the active version when the update
// worker is about to be started.
class EvictIncumbentVersionHelper : public UpdateJobTestHelper {
public:
- EvictIncumbentVersionHelper(int mock_render_process_id)
- : UpdateJobTestHelper(mock_render_process_id) {}
+ EvictIncumbentVersionHelper() : UpdateJobTestHelper() {}
~EvictIncumbentVersionHelper() override {}
void OnStartWorker(int embedded_worker_id,
@@ -925,8 +931,7 @@ class EvictIncumbentVersionHelper : public UpdateJobTestHelper {
} // namespace
TEST_F(ServiceWorkerJobTest, Update_NoChange) {
- UpdateJobTestHelper* update_helper =
- new UpdateJobTestHelper(render_process_id_);
+ UpdateJobTestHelper* update_helper = new UpdateJobTestHelper();
helper_.reset(update_helper);
scoped_refptr<ServiceWorkerRegistration> registration =
update_helper->SetupInitialRegistration(kNoChangeOrigin);
@@ -943,10 +948,12 @@ TEST_F(ServiceWorkerJobTest, Update_NoChange) {
update_helper->state_change_log_.clear();
// Run the update job.
+ update_helper->wait_for_state_change_runner_ = new MessageLoopRunner;
registration->AddListener(update_helper);
scoped_refptr<ServiceWorkerVersion> first_version =
registration->active_version();
first_version->StartUpdate();
+ update_helper->wait_for_state_change_runner_->Run();
base::RunLoop().RunUntilIdle();
// Verify results.
@@ -967,8 +974,7 @@ TEST_F(ServiceWorkerJobTest, Update_BumpLastUpdateCheckTime) {
const base::Time kToday = base::Time::Now();
const base::Time kYesterday =
kToday - base::TimeDelta::FromDays(1) - base::TimeDelta::FromHours(1);
- UpdateJobTestHelper* update_helper =
- new UpdateJobTestHelper(render_process_id_);
+ UpdateJobTestHelper* update_helper = new UpdateJobTestHelper();
helper_.reset(update_helper);
scoped_refptr<ServiceWorkerRegistration> registration =
update_helper->SetupInitialRegistration(kNoChangeOrigin);
@@ -989,8 +995,7 @@ TEST_F(ServiceWorkerJobTest, Update_BumpLastUpdateCheckTime) {
}
TEST_F(ServiceWorkerJobTest, Update_NewVersion) {
- UpdateJobTestHelper* update_helper =
- new UpdateJobTestHelper(render_process_id_);
+ UpdateJobTestHelper* update_helper = new UpdateJobTestHelper();
helper_.reset(update_helper);
scoped_refptr<ServiceWorkerRegistration> registration =
update_helper->SetupInitialRegistration(kNewVersionOrigin);
@@ -998,10 +1003,12 @@ TEST_F(ServiceWorkerJobTest, Update_NewVersion) {
update_helper->state_change_log_.clear();
// Run the update job.
+ update_helper->wait_for_update_runner_ = new MessageLoopRunner;
registration->AddListener(update_helper);
scoped_refptr<ServiceWorkerVersion> first_version =
registration->active_version();
first_version->StartUpdate();
+ update_helper->wait_for_update_runner_->Run();
base::RunLoop().RunUntilIdle();
// Verify results.
@@ -1109,7 +1116,7 @@ TEST_F(ServiceWorkerJobTest, Update_NewestVersionChanged) {
// during the update job (this can happen on disk cache failure).
TEST_F(ServiceWorkerJobTest, Update_EvictedIncumbent) {
EvictIncumbentVersionHelper* update_helper =
- new EvictIncumbentVersionHelper(render_process_id_);
+ new EvictIncumbentVersionHelper();
helper_.reset(update_helper);
scoped_refptr<ServiceWorkerRegistration> registration =
update_helper->SetupInitialRegistration(kNewVersionOrigin);
@@ -1117,10 +1124,12 @@ TEST_F(ServiceWorkerJobTest, Update_EvictedIncumbent) {
update_helper->state_change_log_.clear();
// Run the update job.
+ update_helper->wait_for_update_runner_ = new MessageLoopRunner;
registration->AddListener(update_helper);
scoped_refptr<ServiceWorkerVersion> first_version =
registration->active_version();
first_version->StartUpdate();
+ update_helper->wait_for_update_runner_->Run();
base::RunLoop().RunUntilIdle();
// Verify results.
@@ -1351,8 +1360,8 @@ TEST_F(ServiceWorkerJobTest, RegisterMultipleTimesWhileUninstalling) {
class EventCallbackHelper : public EmbeddedWorkerTestHelper {
public:
- explicit EventCallbackHelper(int mock_render_process_id)
- : EmbeddedWorkerTestHelper(base::FilePath(), mock_render_process_id),
+ explicit EventCallbackHelper()
+ : EmbeddedWorkerTestHelper(base::FilePath()),
install_event_result_(blink::WebServiceWorkerEventResultCompleted),
activate_event_result_(blink::WebServiceWorkerEventResultCompleted) {}
@@ -1386,7 +1395,7 @@ private:
};
TEST_F(ServiceWorkerJobTest, RemoveControlleeDuringInstall) {
- EventCallbackHelper* helper = new EventCallbackHelper(render_process_id_);
+ EventCallbackHelper* helper = new EventCallbackHelper();
helper_.reset(helper);
GURL pattern("http://www.example.com/one/");
@@ -1426,7 +1435,7 @@ TEST_F(ServiceWorkerJobTest, RemoveControlleeDuringInstall) {
}
TEST_F(ServiceWorkerJobTest, RemoveControlleeDuringRejectedInstall) {
- EventCallbackHelper* helper = new EventCallbackHelper(render_process_id_);
+ EventCallbackHelper* helper = new EventCallbackHelper();
helper_.reset(helper);
GURL pattern("http://www.example.com/one/");
@@ -1462,7 +1471,7 @@ TEST_F(ServiceWorkerJobTest, RemoveControlleeDuringRejectedInstall) {
}
TEST_F(ServiceWorkerJobTest, RemoveControlleeDuringInstall_RejectActivate) {
- EventCallbackHelper* helper = new EventCallbackHelper(render_process_id_);
+ EventCallbackHelper* helper = new EventCallbackHelper();
helper_.reset(helper);
GURL pattern("http://www.example.com/one/");

Powered by Google App Engine
This is Rietveld 408576698