summaryrefslogtreecommitdiffstats
path: root/chromium/content/browser/service_worker/service_worker_context_unittest.cc
diff options
context:
space:
mode:
Diffstat (limited to 'chromium/content/browser/service_worker/service_worker_context_unittest.cc')
-rw-r--r--chromium/content/browser/service_worker/service_worker_context_unittest.cc301
1 files changed, 259 insertions, 42 deletions
diff --git a/chromium/content/browser/service_worker/service_worker_context_unittest.cc b/chromium/content/browser/service_worker/service_worker_context_unittest.cc
index e7c230f93a0..14f54f7cb50 100644
--- a/chromium/content/browser/service_worker/service_worker_context_unittest.cc
+++ b/chromium/content/browser/service_worker/service_worker_context_unittest.cc
@@ -2,14 +2,19 @@
// 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/service_worker_context.h"
+#include "content/public/browser/service_worker_context.h"
#include "base/files/scoped_temp_dir.h"
#include "base/logging.h"
#include "base/message_loop/message_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_registration.h"
+#include "content/browser/service_worker/service_worker_storage.h"
+#include "content/common/service_worker/embedded_worker_messages.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 "testing/gtest/include/gtest/gtest.h"
@@ -19,20 +24,27 @@ namespace content {
namespace {
void SaveResponseCallback(bool* called,
- int64* store_result,
- ServiceWorkerRegistrationStatus status,
- int64 result) {
+ int64* store_registration_id,
+ int64* store_version_id,
+ ServiceWorkerStatusCode status,
+ int64 registration_id,
+ int64 version_id) {
+ EXPECT_EQ(SERVICE_WORKER_OK, status) << ServiceWorkerStatusToString(status);
*called = true;
- *store_result = result;
+ *store_registration_id = registration_id;
+ *store_version_id = version_id;
}
ServiceWorkerContextCore::RegistrationCallback MakeRegisteredCallback(
bool* called,
- int64* store_result) {
- return base::Bind(&SaveResponseCallback, called, store_result);
+ int64* store_registration_id,
+ int64* store_version_id) {
+ return base::Bind(&SaveResponseCallback, called,
+ store_registration_id,
+ store_version_id);
}
-void CallCompletedCallback(bool* called, ServiceWorkerRegistrationStatus) {
+void CallCompletedCallback(bool* called, ServiceWorkerStatusCode) {
*called = true;
}
@@ -41,44 +53,212 @@ ServiceWorkerContextCore::UnregistrationCallback MakeUnregisteredCallback(
return base::Bind(&CallCompletedCallback, called);
}
+void ExpectRegisteredWorkers(
+ ServiceWorkerStatusCode expect_status,
+ int64 expect_version_id,
+ bool expect_waiting,
+ bool expect_active,
+ ServiceWorkerStatusCode status,
+ const scoped_refptr<ServiceWorkerRegistration>& registration) {
+ ASSERT_EQ(expect_status, status);
+ if (status != SERVICE_WORKER_OK) {
+ EXPECT_FALSE(registration);
+ return;
+ }
+
+ if (expect_waiting) {
+ EXPECT_TRUE(registration->waiting_version());
+ EXPECT_EQ(expect_version_id,
+ registration->waiting_version()->version_id());
+ } else {
+ EXPECT_FALSE(registration->waiting_version());
+ }
+
+ if (expect_active) {
+ EXPECT_TRUE(registration->active_version());
+ EXPECT_EQ(expect_version_id,
+ registration->active_version()->version_id());
+ } else {
+ EXPECT_FALSE(registration->active_version());
+ }
+}
+
+class RejectInstallTestHelper : public EmbeddedWorkerTestHelper {
+ public:
+ RejectInstallTestHelper(int mock_render_process_id)
+ : EmbeddedWorkerTestHelper(mock_render_process_id) {}
+
+ virtual void OnInstallEvent(int embedded_worker_id,
+ int request_id,
+ int active_version_id) OVERRIDE {
+ SimulateSend(
+ new ServiceWorkerHostMsg_InstallEventFinished(
+ embedded_worker_id, request_id,
+ blink::WebServiceWorkerEventResultRejected));
+ }
+};
+
+class RejectActivateTestHelper : public EmbeddedWorkerTestHelper {
+ public:
+ RejectActivateTestHelper(int mock_render_process_id)
+ : EmbeddedWorkerTestHelper(mock_render_process_id) {}
+
+ virtual void OnActivateEvent(int embedded_worker_id,
+ int request_id) OVERRIDE {
+ SimulateSend(
+ new ServiceWorkerHostMsg_ActivateEventFinished(
+ embedded_worker_id, request_id,
+ blink::WebServiceWorkerEventResultRejected));
+ }
+};
+
} // namespace
class ServiceWorkerContextTest : public testing::Test {
public:
ServiceWorkerContextTest()
- : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {}
+ : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP),
+ render_process_id_(99) {}
virtual void SetUp() OVERRIDE {
- context_.reset(new ServiceWorkerContextCore(base::FilePath(), NULL));
+ helper_.reset(new EmbeddedWorkerTestHelper(render_process_id_));
+ }
+
+ virtual void TearDown() OVERRIDE {
+ helper_.reset();
}
- virtual void TearDown() OVERRIDE { context_.reset(); }
+ ServiceWorkerContextCore* context() { return helper_->context(); }
protected:
TestBrowserThreadBundle browser_thread_bundle_;
- scoped_ptr<ServiceWorkerContextCore> context_;
+ scoped_ptr<EmbeddedWorkerTestHelper> helper_;
+ const int render_process_id_;
};
-void RegistrationCallback(
- scoped_refptr<ServiceWorkerRegistration>* registration,
- const scoped_refptr<ServiceWorkerRegistration>& result) {
- *registration = result;
-}
-
// Make sure basic registration is working.
TEST_F(ServiceWorkerContextTest, Register) {
- int64 registration_id = -1L;
+ int64 registration_id = kInvalidServiceWorkerRegistrationId;
+ int64 version_id = kInvalidServiceWorkerVersionId;
bool called = false;
- context_->RegisterServiceWorker(
+ context()->RegisterServiceWorker(
GURL("http://www.example.com/*"),
GURL("http://www.example.com/service_worker.js"),
- MakeRegisteredCallback(&called, &registration_id));
+ render_process_id_,
+ NULL,
+ MakeRegisteredCallback(&called, &registration_id, &version_id));
ASSERT_FALSE(called);
base::RunLoop().RunUntilIdle();
- ASSERT_TRUE(called);
+ EXPECT_TRUE(called);
+
+ EXPECT_EQ(4UL, helper_->ipc_sink()->message_count());
+ EXPECT_TRUE(helper_->ipc_sink()->GetUniqueMessageMatching(
+ EmbeddedWorkerMsg_StartWorker::ID));
+ EXPECT_TRUE(helper_->inner_ipc_sink()->GetUniqueMessageMatching(
+ ServiceWorkerMsg_InstallEvent::ID));
+ EXPECT_TRUE(helper_->inner_ipc_sink()->GetUniqueMessageMatching(
+ ServiceWorkerMsg_ActivateEvent::ID));
+ EXPECT_TRUE(helper_->ipc_sink()->GetUniqueMessageMatching(
+ EmbeddedWorkerMsg_StopWorker::ID));
+ EXPECT_NE(kInvalidServiceWorkerRegistrationId, registration_id);
+ EXPECT_NE(kInvalidServiceWorkerVersionId, version_id);
+
+ context()->storage()->FindRegistrationForId(
+ registration_id,
+ GURL("http://www.example.com"),
+ base::Bind(&ExpectRegisteredWorkers,
+ SERVICE_WORKER_OK,
+ version_id,
+ false /* expect_waiting */,
+ true /* expect_active */));
+ base::RunLoop().RunUntilIdle();
+}
+
+// Test registration when the service worker rejects the install event. The
+// registration callback should indicate success, but there should be no waiting
+// or active worker in the registration.
+TEST_F(ServiceWorkerContextTest, Register_RejectInstall) {
+ helper_.reset(); // Make sure the process lookups stay overridden.
+ helper_.reset(new RejectInstallTestHelper(render_process_id_));
+ int64 registration_id = kInvalidServiceWorkerRegistrationId;
+ int64 version_id = kInvalidServiceWorkerVersionId;
+ bool called = false;
+ context()->RegisterServiceWorker(
+ GURL("http://www.example.com/*"),
+ GURL("http://www.example.com/service_worker.js"),
+ render_process_id_,
+ NULL,
+ MakeRegisteredCallback(&called, &registration_id, &version_id));
+
+ ASSERT_FALSE(called);
+ base::RunLoop().RunUntilIdle();
+ EXPECT_TRUE(called);
+
+ EXPECT_EQ(3UL, helper_->ipc_sink()->message_count());
+ EXPECT_TRUE(helper_->ipc_sink()->GetUniqueMessageMatching(
+ EmbeddedWorkerMsg_StartWorker::ID));
+ EXPECT_TRUE(helper_->inner_ipc_sink()->GetUniqueMessageMatching(
+ ServiceWorkerMsg_InstallEvent::ID));
+ EXPECT_FALSE(helper_->inner_ipc_sink()->GetUniqueMessageMatching(
+ ServiceWorkerMsg_ActivateEvent::ID));
+ EXPECT_TRUE(helper_->ipc_sink()->GetUniqueMessageMatching(
+ EmbeddedWorkerMsg_StopWorker::ID));
+ EXPECT_NE(kInvalidServiceWorkerRegistrationId, registration_id);
+ EXPECT_NE(kInvalidServiceWorkerVersionId, version_id);
+
+ context()->storage()->FindRegistrationForId(
+ registration_id,
+ GURL("http://www.example.com"),
+ base::Bind(&ExpectRegisteredWorkers,
+ SERVICE_WORKER_ERROR_NOT_FOUND,
+ kInvalidServiceWorkerVersionId,
+ false /* expect_waiting */,
+ false /* expect_active */));
+ base::RunLoop().RunUntilIdle();
+}
- ASSERT_NE(-1L, registration_id);
+// Test registration when the service worker rejects the activate event. The
+// registration callback should indicate success, but there should be no waiting
+// or active worker in the registration.
+TEST_F(ServiceWorkerContextTest, Register_RejectActivate) {
+ helper_.reset(); // Make sure the process lookups stay overridden.
+ helper_.reset(new RejectActivateTestHelper(render_process_id_));
+ int64 registration_id = kInvalidServiceWorkerRegistrationId;
+ int64 version_id = kInvalidServiceWorkerVersionId;
+ bool called = false;
+ context()->RegisterServiceWorker(
+ GURL("http://www.example.com/*"),
+ GURL("http://www.example.com/service_worker.js"),
+ render_process_id_,
+ NULL,
+ MakeRegisteredCallback(&called, &registration_id, &version_id));
+
+ ASSERT_FALSE(called);
+ base::RunLoop().RunUntilIdle();
+ EXPECT_TRUE(called);
+
+ EXPECT_EQ(4UL, helper_->ipc_sink()->message_count());
+ EXPECT_TRUE(helper_->ipc_sink()->GetUniqueMessageMatching(
+ EmbeddedWorkerMsg_StartWorker::ID));
+ EXPECT_TRUE(helper_->inner_ipc_sink()->GetUniqueMessageMatching(
+ ServiceWorkerMsg_InstallEvent::ID));
+ EXPECT_TRUE(helper_->inner_ipc_sink()->GetUniqueMessageMatching(
+ ServiceWorkerMsg_ActivateEvent::ID));
+ EXPECT_TRUE(helper_->ipc_sink()->GetUniqueMessageMatching(
+ EmbeddedWorkerMsg_StopWorker::ID));
+ EXPECT_NE(kInvalidServiceWorkerRegistrationId, registration_id);
+ EXPECT_NE(kInvalidServiceWorkerVersionId, version_id);
+
+ context()->storage()->FindRegistrationForId(
+ registration_id,
+ GURL("http://www.example.com"),
+ base::Bind(&ExpectRegisteredWorkers,
+ SERVICE_WORKER_ERROR_NOT_FOUND,
+ kInvalidServiceWorkerVersionId,
+ false /* expect_waiting */,
+ false /* expect_active */));
+ base::RunLoop().RunUntilIdle();
}
// Make sure registrations are cleaned up when they are unregistered.
@@ -86,22 +266,38 @@ TEST_F(ServiceWorkerContextTest, Unregister) {
GURL pattern("http://www.example.com/*");
bool called = false;
- int64 registration_id = -1L;
- context_->RegisterServiceWorker(
+ int64 registration_id = kInvalidServiceWorkerRegistrationId;
+ int64 version_id = kInvalidServiceWorkerVersionId;
+ context()->RegisterServiceWorker(
pattern,
GURL("http://www.example.com/service_worker.js"),
- MakeRegisteredCallback(&called, &registration_id));
+ render_process_id_,
+ NULL,
+ MakeRegisteredCallback(&called, &registration_id, &version_id));
ASSERT_FALSE(called);
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(called);
+ EXPECT_NE(kInvalidServiceWorkerRegistrationId, registration_id);
+ EXPECT_NE(kInvalidServiceWorkerVersionId, version_id);
called = false;
- context_->UnregisterServiceWorker(pattern, MakeUnregisteredCallback(&called));
+ context()->UnregisterServiceWorker(pattern,
+ MakeUnregisteredCallback(&called));
ASSERT_FALSE(called);
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(called);
+
+ context()->storage()->FindRegistrationForId(
+ registration_id,
+ pattern.GetOrigin(),
+ base::Bind(&ExpectRegisteredWorkers,
+ SERVICE_WORKER_ERROR_NOT_FOUND,
+ kInvalidServiceWorkerVersionId,
+ false /* expect_waiting */,
+ false /* expect_active */));
+ base::RunLoop().RunUntilIdle();
}
// Make sure that when a new registration replaces an existing
@@ -110,28 +306,41 @@ TEST_F(ServiceWorkerContextTest, RegisterNewScript) {
GURL pattern("http://www.example.com/*");
bool called = false;
- int64 old_registration_id = -1L;
- context_->RegisterServiceWorker(
+ int64 old_registration_id = kInvalidServiceWorkerRegistrationId;
+ int64 old_version_id = kInvalidServiceWorkerVersionId;
+ context()->RegisterServiceWorker(
pattern,
GURL("http://www.example.com/service_worker.js"),
- MakeRegisteredCallback(&called, &old_registration_id));
+ render_process_id_,
+ NULL,
+ MakeRegisteredCallback(&called, &old_registration_id, &old_version_id));
ASSERT_FALSE(called);
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(called);
+ EXPECT_NE(kInvalidServiceWorkerRegistrationId, old_registration_id);
+ EXPECT_NE(kInvalidServiceWorkerVersionId, old_version_id);
called = false;
- int64 new_registration_id = -1L;
- context_->RegisterServiceWorker(
+ int64 new_registration_id = kInvalidServiceWorkerRegistrationId;
+ int64 new_version_id = kInvalidServiceWorkerVersionId;
+ context()->RegisterServiceWorker(
pattern,
GURL("http://www.example.com/service_worker_new.js"),
- MakeRegisteredCallback(&called, &new_registration_id));
+ render_process_id_,
+ NULL,
+ MakeRegisteredCallback(&called, &new_registration_id, &new_version_id));
ASSERT_FALSE(called);
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(called);
- ASSERT_NE(old_registration_id, new_registration_id);
+ // Returned IDs should be valid, and should differ from the values
+ // returned for the previous registration.
+ EXPECT_NE(kInvalidServiceWorkerRegistrationId, new_registration_id);
+ EXPECT_NE(kInvalidServiceWorkerVersionId, new_version_id);
+ EXPECT_NE(old_registration_id, new_registration_id);
+ EXPECT_NE(old_version_id, new_version_id);
}
// Make sure that when registering a duplicate pattern+script_url
@@ -141,28 +350,36 @@ TEST_F(ServiceWorkerContextTest, RegisterDuplicateScript) {
GURL script_url("http://www.example.com/service_worker.js");
bool called = false;
- int64 old_registration_id = -1L;
- context_->RegisterServiceWorker(
+ int64 old_registration_id = kInvalidServiceWorkerRegistrationId;
+ int64 old_version_id = kInvalidServiceWorkerVersionId;
+ context()->RegisterServiceWorker(
pattern,
script_url,
- MakeRegisteredCallback(&called, &old_registration_id));
+ render_process_id_,
+ NULL,
+ MakeRegisteredCallback(&called, &old_registration_id, &old_version_id));
ASSERT_FALSE(called);
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(called);
+ EXPECT_NE(kInvalidServiceWorkerRegistrationId, old_registration_id);
+ EXPECT_NE(kInvalidServiceWorkerVersionId, old_version_id);
called = false;
- int64 new_registration_id = -1L;
- context_->RegisterServiceWorker(
+ int64 new_registration_id = kInvalidServiceWorkerRegistrationId;
+ int64 new_version_id = kInvalidServiceWorkerVersionId;
+ context()->RegisterServiceWorker(
pattern,
script_url,
- MakeRegisteredCallback(&called, &new_registration_id));
+ render_process_id_,
+ NULL,
+ MakeRegisteredCallback(&called, &new_registration_id, &new_version_id));
ASSERT_FALSE(called);
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(called);
-
- ASSERT_EQ(old_registration_id, new_registration_id);
+ EXPECT_EQ(old_registration_id, new_registration_id);
+ EXPECT_EQ(old_version_id, new_version_id);
}
} // namespace content