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

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

Issue 62203007: Implement memory-persistent registration (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Working version Created 7 years, 1 month 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_storage_unittest.cc
diff --git a/content/browser/service_worker/service_worker_storage_unittest.cc b/content/browser/service_worker/service_worker_storage_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..42abde192b1c0b8a7bcebff46d80174a1bd04e1a
--- /dev/null
+++ b/content/browser/service_worker/service_worker_storage_unittest.cc
@@ -0,0 +1,341 @@
+// Copyright (c) 2012 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/service_worker_storage.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/service_worker_registration.h"
+#include "content/browser/service_worker/service_worker_storage.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace content {
+
+class ServiceWorkerStorageTest : public testing::Test {
+ public:
+ ServiceWorkerStorageTest() : io_thread_(BrowserThread::IO, &message_loop_) {}
+
+ virtual void SetUp() OVERRIDE {
+ storage_ = new ServiceWorkerStorage(base::FilePath(), NULL);
+ }
+
+ virtual void TearDown() OVERRIDE {}
+
+ static base::Closure MakeUnregisteredCallback(bool* called) {
+ return base::Bind(&CallCompletedCallback, called);
+ }
+
+ protected:
+ ServiceWorkerStorage* storage_;
+
+ static void SaveResponseCallback(bool* called,
+ int64* store_result,
+ int64 result) {
+ *called = true;
+ *store_result = result;
+ }
+
+ static void CallCompletedCallback(bool* called) { *called = true; }
+
+ base::MessageLoopForIO message_loop_;
+ BrowserThreadImpl io_thread_;
+};
+
+TEST_F(ServiceWorkerStorageTest, RegisterMatch) {
+ ASSERT_TRUE(ServiceWorkerStorage::PatternMatches(
+ GURL("http://www.example.com/*"), GURL("http://www.example.com/")));
+ ASSERT_TRUE(ServiceWorkerStorage::PatternMatches(
+ GURL("http://www.example.com/*"),
+ GURL("http://www.example.com/page.html")));
+
+ ASSERT_FALSE(ServiceWorkerStorage::PatternMatches(
+ GURL("http://www.example.com/*"), GURL("https://www.example.com/")));
+ ASSERT_FALSE(ServiceWorkerStorage::PatternMatches(
+ GURL("http://www.example.com/*"),
+ GURL("https://www.example.com/page.html")));
+
+ ASSERT_FALSE(ServiceWorkerStorage::PatternMatches(
+ GURL("http://www.example.com/*"), GURL("http://www.foo.com/")));
+ ASSERT_FALSE(ServiceWorkerStorage::PatternMatches(
+ GURL("http://www.example.com/*"), GURL("https://www.foo.com/page.html")));
+}
+
+void RegistrationCallback(
+ bool* called,
+ scoped_refptr<ServiceWorkerRegistration>* registration,
+ const scoped_refptr<ServiceWorkerRegistration>& result) {
+ *called = true;
+ *registration = result;
+}
+
+// Creates a callback which both keeps track of if it's been called,
+// as well as the resulting registration. The 'called' is useful for
+// making sure a sychronous fastpath is or isn't called.
+ServiceWorkerStorage::ResponseCallback SaveRegistration(
+ bool* called,
+ scoped_refptr<ServiceWorkerRegistration>* registration) {
+ return base::Bind(&RegistrationCallback, called, registration);
+}
+
+void ClosureCallback(bool* called) { *called = true; }
+
+base::Closure SaveCalled(bool* called) {
+ return base::Bind(&ClosureCallback, called);
+}
+
+TEST_F(ServiceWorkerStorageTest, SameDocumentSameRegistration) {
+ scoped_refptr<ServiceWorkerRegistration> original_registration;
+ bool called = false;
+ storage_->Register(GURL("http://www.example.com/*"),
+ GURL("http://www.example.com/service_worker.js"),
+ SaveRegistration(&called, &original_registration));
+ DCHECK(!called);
+ message_loop_.RunUntilIdle();
+ DCHECK(called);
+
+ scoped_refptr<ServiceWorkerRegistration> registration1;
+ storage_->FindRegistrationForDocument(
+ GURL("http://www.example.com/"),
+ SaveRegistration(&called, &registration1));
+ scoped_refptr<ServiceWorkerRegistration> registration2;
+ storage_->FindRegistrationForDocument(
+ GURL("http://www.example.com/"),
+ SaveRegistration(&called, &registration2));
+
+ ServiceWorkerRegistration* null_registration(NULL);
+ ASSERT_EQ(null_registration, registration1);
+ ASSERT_EQ(null_registration, registration2);
+ DCHECK(!called);
+ message_loop_.RunUntilIdle();
+ DCHECK(called);
+ ASSERT_NE(null_registration, registration1);
+ ASSERT_NE(null_registration, registration2);
+
+ ASSERT_EQ(registration1, registration2);
+}
+
+TEST_F(ServiceWorkerStorageTest, SameMatchSameRegistration) {
+ bool called = false;
+ scoped_refptr<ServiceWorkerRegistration> original_registration;
+ storage_->Register(GURL("http://www.example.com/*"),
+ GURL("http://www.example.com/service_worker.js"),
+ SaveRegistration(&called, &original_registration));
+ DCHECK(!called);
+ message_loop_.RunUntilIdle();
+ DCHECK(called);
+ ASSERT_NE(static_cast<ServiceWorkerRegistration*>(NULL),
+ original_registration.get());
+
+ scoped_refptr<ServiceWorkerRegistration> registration1;
+ storage_->FindRegistrationForDocument(
+ GURL("http://www.example.com/one"),
+ SaveRegistration(&called, &registration1));
+
+ DCHECK(!called);
+ message_loop_.RunUntilIdle();
+ DCHECK(called);
+
+ scoped_refptr<ServiceWorkerRegistration> registration2;
+ storage_->FindRegistrationForDocument(
+ GURL("http://www.example.com/two"),
+ SaveRegistration(&called, &registration2));
+ DCHECK(!called);
+ message_loop_.RunUntilIdle();
+ DCHECK(called);
+
+ ASSERT_EQ(registration1, registration2);
+}
+
+TEST_F(ServiceWorkerStorageTest, DifferentMatchDifferentRegistration) {
+ bool called1 = false;
+ scoped_refptr<ServiceWorkerRegistration> original_registration1;
+ storage_->Register(GURL("http://www.example.com/one/*"),
+ GURL("http://www.example.com/service_worker.js"),
+ SaveRegistration(&called1, &original_registration1));
+
+ bool called2 = false;
+ scoped_refptr<ServiceWorkerRegistration> original_registration2;
+ storage_->Register(GURL("http://www.example.com/two/*"),
+ GURL("http://www.example.com/service_worker.js"),
+ SaveRegistration(&called2, &original_registration2));
+
+ DCHECK(!called1);
+ DCHECK(!called2);
+ message_loop_.RunUntilIdle();
+ DCHECK(called2);
+ DCHECK(called1);
+
+ scoped_refptr<ServiceWorkerRegistration> registration1;
+ storage_->FindRegistrationForDocument(
+ GURL("http://www.example.com/one/"),
+ SaveRegistration(&called1, &registration1));
+ scoped_refptr<ServiceWorkerRegistration> registration2;
+ storage_->FindRegistrationForDocument(
+ GURL("http://www.example.com/two/"),
+ SaveRegistration(&called2, &registration2));
+
+ DCHECK(!called1);
+ DCHECK(!called2);
+ message_loop_.RunUntilIdle();
+ DCHECK(called2);
+ DCHECK(called1);
+
+ ASSERT_NE(registration1, registration2);
+}
+
+// Make sure basic registration is working.
+TEST_F(ServiceWorkerStorageTest, Register) {
+ bool called = false;
+ scoped_refptr<ServiceWorkerRegistration> registration;
+ storage_->Register(GURL("http://www.example.com/*"),
+ GURL("http://www.example.com/service_worker.js"),
+ SaveRegistration(&called, &registration));
+
+ ASSERT_FALSE(called);
+ message_loop_.RunUntilIdle();
+ ASSERT_TRUE(called);
+
+ ASSERT_NE(scoped_refptr<ServiceWorkerRegistration>(NULL), registration);
+}
+
+// Make sure registrations are cleaned up when they are unregistered.
+TEST_F(ServiceWorkerStorageTest, Unregister) {
+ GURL pattern("http://www.example.com/*");
+
+ bool called = false;
+ scoped_refptr<ServiceWorkerRegistration> registration;
+ storage_->Register(pattern,
+ GURL("http://www.example.com/service_worker.js"),
+ SaveRegistration(&called, &registration));
+
+ ASSERT_FALSE(called);
+ message_loop_.RunUntilIdle();
+ ASSERT_TRUE(called);
+
+ called = false;
+ storage_->Unregister(pattern, MakeUnregisteredCallback(&called));
+
+ ASSERT_FALSE(called);
+ message_loop_.RunUntilIdle();
+ ASSERT_TRUE(called);
+
+ ASSERT_TRUE(registration->HasOneRef());
+
+ called = false;
+ storage_->FindRegistrationForPattern(
+ pattern, SaveRegistration(&called, &registration));
+
+ ASSERT_FALSE(called);
+ message_loop_.RunUntilIdle();
+ ASSERT_TRUE(called);
+
+ ASSERT_EQ(scoped_refptr<ServiceWorkerRegistration>(NULL), registration);
+}
+
+// Make sure that when a new registration replaces an existing
+// registration, that the old one is cleaned up.
+TEST_F(ServiceWorkerStorageTest, RegisterNewScript) {
+ GURL pattern("http://www.example.com/*");
+
+ bool called = false;
+ scoped_refptr<ServiceWorkerRegistration> old_registration;
+ storage_->Register(pattern,
+ GURL("http://www.example.com/service_worker.js"),
+ SaveRegistration(&called, &old_registration));
+
+ ASSERT_FALSE(called);
+ message_loop_.RunUntilIdle();
+ ASSERT_TRUE(called);
+
+ called = false;
+ scoped_refptr<ServiceWorkerRegistration> old_registration_by_pattern;
+ storage_->FindRegistrationForPattern(
+ pattern, SaveRegistration(&called, &old_registration_by_pattern));
+
+ ASSERT_FALSE(called);
+ message_loop_.RunUntilIdle();
+ ASSERT_TRUE(called);
+
+ ASSERT_EQ(old_registration, old_registration_by_pattern);
+ old_registration_by_pattern = NULL;
+
+ called = false;
+ scoped_refptr<ServiceWorkerRegistration> new_registration;
+ storage_->Register(pattern,
+ GURL("http://www.example.com/service_worker_new.js"),
+ SaveRegistration(&called, &new_registration));
+
+ ASSERT_FALSE(called);
+ message_loop_.RunUntilIdle();
+ ASSERT_TRUE(called);
+
+ ASSERT_TRUE(old_registration->HasOneRef());
+
+ ASSERT_NE(old_registration, new_registration);
+
+ called = false;
+ scoped_refptr<ServiceWorkerRegistration> new_registration_by_pattern;
+ storage_->FindRegistrationForPattern(
+ pattern, SaveRegistration(&called, &new_registration));
+
+ ASSERT_FALSE(called);
+ message_loop_.RunUntilIdle();
+ ASSERT_TRUE(called);
+
+ ASSERT_NE(new_registration_by_pattern, old_registration);
+}
+
+// Make sure that when registering a duplicate pattern+script_url
+// combination, that the same registration is used.
+TEST_F(ServiceWorkerStorageTest, RegisterDuplicateScript) {
+ GURL pattern("http://www.example.com/*");
+ GURL script_url("http://www.example.com/service_worker.js");
+
+ bool called = false;
+ scoped_refptr<ServiceWorkerRegistration> old_registration;
+ storage_->Register(
+ pattern, script_url, SaveRegistration(&called, &old_registration));
+
+ ASSERT_FALSE(called);
+ message_loop_.RunUntilIdle();
+ ASSERT_TRUE(called);
+
+ called = false;
+ scoped_refptr<ServiceWorkerRegistration> old_registration_by_pattern;
+ storage_->FindRegistrationForPattern(
+ pattern, SaveRegistration(&called, &old_registration_by_pattern));
+ ASSERT_FALSE(called);
+ message_loop_.RunUntilIdle();
+ ASSERT_TRUE(called);
+
+ ASSERT_TRUE(old_registration_by_pattern);
+
+ called = false;
+ scoped_refptr<ServiceWorkerRegistration> new_registration;
+ storage_->Register(
+ pattern, script_url, SaveRegistration(&called, &new_registration));
+
+ ASSERT_FALSE(called);
+ message_loop_.RunUntilIdle();
+ ASSERT_TRUE(called);
+
+ ASSERT_EQ(old_registration, new_registration);
+
+ ASSERT_FALSE(old_registration->HasOneRef());
+
+ called = false;
+ scoped_refptr<ServiceWorkerRegistration> new_registration_by_pattern;
+ storage_->FindRegistrationForPattern(
+ pattern, SaveRegistration(&called, &new_registration_by_pattern));
+
+ ASSERT_FALSE(called);
+ message_loop_.RunUntilIdle();
+ ASSERT_TRUE(called);
+
+ ASSERT_EQ(new_registration, old_registration);
+}
+
+} // namespace content

Powered by Google App Engine
This is Rietveld 408576698