This source file includes following definitions.
- SaveRegistrationCallback
- SaveFoundRegistrationCallback
- SaveRegistration
- SaveFoundRegistration
- SaveUnregistrationCallback
- SaveUnregistration
- render_process_id_
- SetUp
- TearDown
- job_coordinator
- storage
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- OnStartWorker
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "base/files/scoped_temp_dir.h"
#include "base/logging.h"
#include "base/run_loop.h"
#include "content/browser/browser_thread_impl.h"
#include "content/browser/service_worker/embedded_worker_registry.h"
#include "content/browser/service_worker/embedded_worker_test_helper.h"
#include "content/browser/service_worker/service_worker_context_core.h"
#include "content/browser/service_worker/service_worker_job_coordinator.h"
#include "content/browser/service_worker/service_worker_registration.h"
#include "content/browser/service_worker/service_worker_registration_status.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "ipc/ipc_test_sink.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace content {
namespace {
void SaveRegistrationCallback(
ServiceWorkerStatusCode expected_status,
bool* called,
scoped_refptr<ServiceWorkerRegistration>* registration,
ServiceWorkerStatusCode status,
const scoped_refptr<ServiceWorkerRegistration>& result) {
EXPECT_EQ(expected_status, status);
*called = true;
*registration = result;
}
void SaveFoundRegistrationCallback(
ServiceWorkerStatusCode expected_status,
bool* called,
scoped_refptr<ServiceWorkerRegistration>* registration,
ServiceWorkerStatusCode status,
const scoped_refptr<ServiceWorkerRegistration>& result) {
EXPECT_EQ(expected_status, status);
*called = true;
*registration = result;
}
ServiceWorkerRegisterJob::RegistrationCallback SaveRegistration(
ServiceWorkerStatusCode expected_status,
bool* called,
scoped_refptr<ServiceWorkerRegistration>* registration) {
*called = false;
return base::Bind(
&SaveRegistrationCallback, expected_status, called, registration);
}
ServiceWorkerStorage::FindRegistrationCallback SaveFoundRegistration(
ServiceWorkerStatusCode expected_status,
bool* called,
scoped_refptr<ServiceWorkerRegistration>* registration) {
*called = false;
return base::Bind(&SaveFoundRegistrationCallback,
expected_status,
called,
registration);
}
void SaveUnregistrationCallback(ServiceWorkerStatusCode expected_status,
bool* called,
ServiceWorkerStatusCode status) {
EXPECT_EQ(expected_status, status);
*called = true;
}
ServiceWorkerUnregisterJob::UnregistrationCallback SaveUnregistration(
ServiceWorkerStatusCode expected_status,
bool* called) {
*called = false;
return base::Bind(&SaveUnregistrationCallback, expected_status, called);
}
}
class ServiceWorkerJobTest : public testing::Test {
public:
ServiceWorkerJobTest()
: browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP),
render_process_id_(88) {}
virtual void SetUp() OVERRIDE {
context_.reset(new ServiceWorkerContextCore(base::FilePath(), NULL));
helper_.reset(new EmbeddedWorkerTestHelper(context_.get(),
render_process_id_));
}
virtual void TearDown() OVERRIDE {
helper_.reset();
context_.reset();
}
ServiceWorkerJobCoordinator* job_coordinator() const {
return context_->job_coordinator();
}
ServiceWorkerStorage* storage() const { return context_->storage(); }
protected:
TestBrowserThreadBundle browser_thread_bundle_;
scoped_ptr<ServiceWorkerContextCore> context_;
scoped_ptr<EmbeddedWorkerTestHelper> helper_;
int render_process_id_;
};
TEST_F(ServiceWorkerJobTest, SameDocumentSameRegistration) {
scoped_refptr<ServiceWorkerRegistration> original_registration;
bool called;
job_coordinator()->Register(
GURL("http://www.example.com/*"),
GURL("http://www.example.com/service_worker.js"),
render_process_id_,
SaveRegistration(SERVICE_WORKER_OK, &called, &original_registration));
EXPECT_FALSE(called);
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(called);
scoped_refptr<ServiceWorkerRegistration> registration1;
storage()->FindRegistrationForDocument(
GURL("http://www.example.com/"),
SaveFoundRegistration(SERVICE_WORKER_OK, &called, ®istration1));
scoped_refptr<ServiceWorkerRegistration> registration2;
storage()->FindRegistrationForDocument(
GURL("http://www.example.com/"),
SaveFoundRegistration(SERVICE_WORKER_OK, &called, ®istration2));
ServiceWorkerRegistration* null_registration(NULL);
ASSERT_EQ(null_registration, registration1);
ASSERT_EQ(null_registration, registration2);
EXPECT_FALSE(called);
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(called);
ASSERT_NE(null_registration, registration1);
ASSERT_NE(null_registration, registration2);
ASSERT_EQ(registration1, registration2);
}
TEST_F(ServiceWorkerJobTest, SameMatchSameRegistration) {
bool called;
scoped_refptr<ServiceWorkerRegistration> original_registration;
job_coordinator()->Register(
GURL("http://www.example.com/*"),
GURL("http://www.example.com/service_worker.js"),
render_process_id_,
SaveRegistration(SERVICE_WORKER_OK, &called, &original_registration));
EXPECT_FALSE(called);
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(called);
ASSERT_NE(static_cast<ServiceWorkerRegistration*>(NULL),
original_registration.get());
scoped_refptr<ServiceWorkerRegistration> registration1;
storage()->FindRegistrationForDocument(
GURL("http://www.example.com/one"),
SaveFoundRegistration(SERVICE_WORKER_OK, &called, ®istration1));
EXPECT_FALSE(called);
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(called);
scoped_refptr<ServiceWorkerRegistration> registration2;
storage()->FindRegistrationForDocument(
GURL("http://www.example.com/two"),
SaveFoundRegistration(SERVICE_WORKER_OK, &called, ®istration2));
EXPECT_FALSE(called);
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(called);
ASSERT_EQ(registration1, registration2);
}
TEST_F(ServiceWorkerJobTest, DifferentMatchDifferentRegistration) {
bool called1;
scoped_refptr<ServiceWorkerRegistration> original_registration1;
job_coordinator()->Register(
GURL("http://www.example.com/one/*"),
GURL("http://www.example.com/service_worker.js"),
render_process_id_,
SaveRegistration(SERVICE_WORKER_OK, &called1, &original_registration1));
bool called2;
scoped_refptr<ServiceWorkerRegistration> original_registration2;
job_coordinator()->Register(
GURL("http://www.example.com/two/*"),
GURL("http://www.example.com/service_worker.js"),
render_process_id_,
SaveRegistration(SERVICE_WORKER_OK, &called2, &original_registration2));
EXPECT_FALSE(called1);
EXPECT_FALSE(called2);
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(called2);
EXPECT_TRUE(called1);
scoped_refptr<ServiceWorkerRegistration> registration1;
storage()->FindRegistrationForDocument(
GURL("http://www.example.com/one/"),
SaveFoundRegistration(SERVICE_WORKER_OK, &called1, ®istration1));
scoped_refptr<ServiceWorkerRegistration> registration2;
storage()->FindRegistrationForDocument(
GURL("http://www.example.com/two/"),
SaveFoundRegistration(SERVICE_WORKER_OK, &called2, ®istration2));
EXPECT_FALSE(called1);
EXPECT_FALSE(called2);
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(called2);
EXPECT_TRUE(called1);
ASSERT_NE(registration1, registration2);
}
TEST_F(ServiceWorkerJobTest, Register) {
bool called = false;
scoped_refptr<ServiceWorkerRegistration> registration;
job_coordinator()->Register(
GURL("http://www.example.com/*"),
GURL("http://www.example.com/service_worker.js"),
render_process_id_,
SaveRegistration(SERVICE_WORKER_OK, &called, ®istration));
ASSERT_FALSE(called);
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(called);
ASSERT_NE(scoped_refptr<ServiceWorkerRegistration>(NULL), registration);
}
TEST_F(ServiceWorkerJobTest, Unregister) {
GURL pattern("http://www.example.com/*");
bool called;
scoped_refptr<ServiceWorkerRegistration> registration;
job_coordinator()->Register(
pattern,
GURL("http://www.example.com/service_worker.js"),
render_process_id_,
SaveRegistration(SERVICE_WORKER_OK, &called, ®istration));
ASSERT_FALSE(called);
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(called);
job_coordinator()->Unregister(pattern,
render_process_id_,
SaveUnregistration(SERVICE_WORKER_OK, &called));
ASSERT_FALSE(called);
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(called);
ASSERT_TRUE(registration->HasOneRef());
storage()->FindRegistrationForPattern(
pattern,
SaveFoundRegistration(SERVICE_WORKER_ERROR_NOT_FOUND,
&called, ®istration));
ASSERT_FALSE(called);
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(called);
ASSERT_EQ(scoped_refptr<ServiceWorkerRegistration>(NULL), registration);
}
TEST_F(ServiceWorkerJobTest, RegisterNewScript) {
GURL pattern("http://www.example.com/*");
bool called;
scoped_refptr<ServiceWorkerRegistration> old_registration;
job_coordinator()->Register(
pattern,
GURL("http://www.example.com/service_worker.js"),
render_process_id_,
SaveRegistration(SERVICE_WORKER_OK, &called, &old_registration));
ASSERT_FALSE(called);
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(called);
scoped_refptr<ServiceWorkerRegistration> old_registration_by_pattern;
storage()->FindRegistrationForPattern(
pattern,
SaveFoundRegistration(
SERVICE_WORKER_OK, &called, &old_registration_by_pattern));
ASSERT_FALSE(called);
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(called);
ASSERT_EQ(old_registration, old_registration_by_pattern);
old_registration_by_pattern = NULL;
scoped_refptr<ServiceWorkerRegistration> new_registration;
job_coordinator()->Register(
pattern,
GURL("http://www.example.com/service_worker_new.js"),
render_process_id_,
SaveRegistration(SERVICE_WORKER_OK, &called, &new_registration));
ASSERT_FALSE(called);
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(called);
ASSERT_TRUE(old_registration->HasOneRef());
ASSERT_NE(old_registration, new_registration);
scoped_refptr<ServiceWorkerRegistration> new_registration_by_pattern;
storage()->FindRegistrationForPattern(
pattern,
SaveFoundRegistration(
SERVICE_WORKER_OK, &called, &new_registration));
ASSERT_FALSE(called);
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(called);
ASSERT_NE(new_registration_by_pattern, old_registration);
}
TEST_F(ServiceWorkerJobTest, RegisterDuplicateScript) {
GURL pattern("http://www.example.com/*");
GURL script_url("http://www.example.com/service_worker.js");
bool called;
scoped_refptr<ServiceWorkerRegistration> old_registration;
job_coordinator()->Register(
pattern,
script_url,
render_process_id_,
SaveRegistration(SERVICE_WORKER_OK, &called, &old_registration));
ASSERT_FALSE(called);
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(called);
scoped_refptr<ServiceWorkerRegistration> old_registration_by_pattern;
storage()->FindRegistrationForPattern(
pattern,
SaveFoundRegistration(
SERVICE_WORKER_OK, &called, &old_registration_by_pattern));
ASSERT_FALSE(called);
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(called);
ASSERT_TRUE(old_registration_by_pattern);
scoped_refptr<ServiceWorkerRegistration> new_registration;
job_coordinator()->Register(
pattern,
script_url,
render_process_id_,
SaveRegistration(SERVICE_WORKER_OK, &called, &new_registration));
ASSERT_FALSE(called);
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(called);
ASSERT_EQ(old_registration, new_registration);
ASSERT_FALSE(old_registration->HasOneRef());
scoped_refptr<ServiceWorkerRegistration> new_registration_by_pattern;
storage()->FindRegistrationForPattern(
pattern,
SaveFoundRegistration(
SERVICE_WORKER_OK, &called, &new_registration_by_pattern));
ASSERT_FALSE(called);
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(called);
ASSERT_EQ(new_registration, old_registration);
}
class FailToStartWorkerTestHelper : public EmbeddedWorkerTestHelper {
public:
FailToStartWorkerTestHelper(ServiceWorkerContextCore* context,
int mock_render_process_id)
: EmbeddedWorkerTestHelper(context, mock_render_process_id) {}
virtual void OnStartWorker(int embedded_worker_id,
int64 service_worker_version_id,
const GURL& script_url) OVERRIDE {
EmbeddedWorkerInstance* worker = registry()->GetWorker(embedded_worker_id);
registry()->OnWorkerStopped(worker->process_id(), embedded_worker_id);
}
};
TEST_F(ServiceWorkerJobTest, Register_FailToStartWorker) {
helper_.reset(
new FailToStartWorkerTestHelper(context_.get(), render_process_id_));
bool called = false;
scoped_refptr<ServiceWorkerRegistration> registration;
job_coordinator()->Register(
GURL("http://www.example.com/*"),
GURL("http://www.example.com/service_worker.js"),
render_process_id_,
SaveRegistration(
SERVICE_WORKER_ERROR_START_WORKER_FAILED, &called, ®istration));
ASSERT_FALSE(called);
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(called);
ASSERT_EQ(scoped_refptr<ServiceWorkerRegistration>(NULL), registration);
}
TEST_F(ServiceWorkerJobTest, ParallelRegUnreg) {
GURL pattern("http://www.example.com/*");
GURL script_url("http://www.example.com/service_worker.js");
bool registration_called = false;
scoped_refptr<ServiceWorkerRegistration> registration;
job_coordinator()->Register(
pattern,
script_url,
render_process_id_,
SaveRegistration(SERVICE_WORKER_OK, ®istration_called, ®istration));
bool unregistration_called = false;
job_coordinator()->Unregister(
pattern,
render_process_id_,
SaveUnregistration(SERVICE_WORKER_OK, &unregistration_called));
ASSERT_FALSE(registration_called);
ASSERT_FALSE(unregistration_called);
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(registration_called);
ASSERT_TRUE(unregistration_called);
ASSERT_TRUE(registration->is_shutdown());
bool find_called = false;
storage()->FindRegistrationForPattern(
pattern,
SaveFoundRegistration(
SERVICE_WORKER_ERROR_NOT_FOUND, &find_called, ®istration));
base::RunLoop().RunUntilIdle();
ASSERT_EQ(scoped_refptr<ServiceWorkerRegistration>(), registration);
}
TEST_F(ServiceWorkerJobTest, ParallelRegNewScript) {
GURL pattern("http://www.example.com/*");
GURL script_url1("http://www.example.com/service_worker1.js");
bool registration1_called = false;
scoped_refptr<ServiceWorkerRegistration> registration1;
job_coordinator()->Register(
pattern,
script_url1,
render_process_id_,
SaveRegistration(
SERVICE_WORKER_OK, ®istration1_called, ®istration1));
GURL script_url2("http://www.example.com/service_worker2.js");
bool registration2_called = false;
scoped_refptr<ServiceWorkerRegistration> registration2;
job_coordinator()->Register(
pattern,
script_url2,
render_process_id_,
SaveRegistration(
SERVICE_WORKER_OK, ®istration2_called, ®istration2));
ASSERT_FALSE(registration1_called);
ASSERT_FALSE(registration2_called);
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(registration1_called);
ASSERT_TRUE(registration2_called);
scoped_refptr<ServiceWorkerRegistration> registration;
bool find_called = false;
storage()->FindRegistrationForPattern(
pattern,
SaveFoundRegistration(
SERVICE_WORKER_OK, &find_called, ®istration));
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(registration1->is_shutdown());
EXPECT_FALSE(registration2->is_shutdown());
ASSERT_EQ(registration2, registration);
}
TEST_F(ServiceWorkerJobTest, ParallelRegSameScript) {
GURL pattern("http://www.example.com/*");
GURL script_url("http://www.example.com/service_worker1.js");
bool registration1_called = false;
scoped_refptr<ServiceWorkerRegistration> registration1;
job_coordinator()->Register(
pattern,
script_url,
render_process_id_,
SaveRegistration(
SERVICE_WORKER_OK, ®istration1_called, ®istration1));
bool registration2_called = false;
scoped_refptr<ServiceWorkerRegistration> registration2;
job_coordinator()->Register(
pattern,
script_url,
render_process_id_,
SaveRegistration(
SERVICE_WORKER_OK, ®istration2_called, ®istration2));
ASSERT_FALSE(registration1_called);
ASSERT_FALSE(registration2_called);
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(registration1_called);
ASSERT_TRUE(registration2_called);
ASSERT_EQ(registration1, registration2);
scoped_refptr<ServiceWorkerRegistration> registration;
bool find_called = false;
storage()->FindRegistrationForPattern(
pattern,
SaveFoundRegistration(
SERVICE_WORKER_OK, &find_called, ®istration));
base::RunLoop().RunUntilIdle();
ASSERT_EQ(registration, registration1);
}
TEST_F(ServiceWorkerJobTest, ParallelUnreg) {
GURL pattern("http://www.example.com/*");
GURL script_url("http://www.example.com/service_worker.js");
bool unregistration1_called = false;
job_coordinator()->Unregister(
pattern,
render_process_id_,
SaveUnregistration(SERVICE_WORKER_OK, &unregistration1_called));
bool unregistration2_called = false;
job_coordinator()->Unregister(
pattern,
render_process_id_,
SaveUnregistration(SERVICE_WORKER_OK, &unregistration2_called));
ASSERT_FALSE(unregistration1_called);
ASSERT_FALSE(unregistration2_called);
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(unregistration1_called);
ASSERT_TRUE(unregistration2_called);
scoped_refptr<ServiceWorkerRegistration> registration;
bool find_called = false;
storage()->FindRegistrationForPattern(
pattern,
SaveFoundRegistration(
SERVICE_WORKER_ERROR_NOT_FOUND, &find_called, ®istration));
base::RunLoop().RunUntilIdle();
ASSERT_EQ(scoped_refptr<ServiceWorkerRegistration>(), registration);
}
}