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

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

Issue 62203007: Implement memory-persistent registration (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: 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_context_unittest.cc
diff --git a/content/browser/service_worker/service_worker_context_unittest.cc b/content/browser/service_worker/service_worker_context_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..714dae0072bd20be122827bda851eeaf029f18d3
--- /dev/null
+++ b/content/browser/service_worker/service_worker_context_unittest.cc
@@ -0,0 +1,151 @@
+// 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 "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 "testing/gtest/include/gtest/gtest.h"
+
+namespace content {
+
+class ServiceWorkerContextTest : public testing::Test {
+ public:
+ ServiceWorkerContextTest() : io_thread_(BrowserThread::IO, &message_loop_) {}
+
+ virtual void SetUp() OVERRIDE {
+ context_ = new ServiceWorkerContext(base::FilePath(), NULL);
+ }
+
+ static ServiceWorkerContext::ResponseCallback MakeRegisteredCallback(
+ bool* called,
+ int64* store_result) {
+ return base::Bind(SaveResponseCallback, called, store_result);
+ }
+
+ static base::Closure MakeUnregisteredCallback(bool* called) {
+ return base::Bind(CallCompletedCallback, called);
+ }
+
+ protected:
+ scoped_refptr<ServiceWorkerContext> context_;
+
+ 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(ServiceWorkerContextTest, RegisterMatch) {
+ ASSERT_TRUE(ServiceWorkerContext::PatternMatches(
+ GURL("http://www.example.com/*"), GURL("http://www.example.com/")));
+ ASSERT_TRUE(ServiceWorkerContext::PatternMatches(
+ GURL("http://www.example.com/*"),
+ GURL("http://www.example.com/page.html")));
+
+ ASSERT_FALSE(ServiceWorkerContext::PatternMatches(
+ GURL("http://www.example.com/*"), GURL("https://www.example.com/")));
+ ASSERT_FALSE(ServiceWorkerContext::PatternMatches(
+ GURL("http://www.example.com/*"),
+ GURL("https://www.example.com/page.html")));
+
+ ASSERT_FALSE(ServiceWorkerContext::PatternMatches(
+ GURL("http://www.example.com/*"), GURL("http://www.foo.com/")));
+ ASSERT_FALSE(ServiceWorkerContext::PatternMatches(
+ GURL("http://www.example.com/*"), GURL("https://www.foo.com/page.html")));
+}
+
+TEST_F(ServiceWorkerContextTest, SameDocumentSameRegistration) {
+ context_->Register(GURL("http://www.example.com/*"),
+ GURL("http://www.example.com/service_worker.js"));
+
+ scoped_refptr<ServiceWorkerRegistration> registration1 =
+ context_->RegistrationForDocument(GURL("http://www.example.com/"));
+ scoped_refptr<ServiceWorkerRegistration> registration2 =
+ context_->RegistrationForDocument(GURL("http://www.example.com/"));
+
+ ASSERT_EQ(registration1, registration2);
+}
+
+TEST_F(ServiceWorkerContextTest, SameMatchSameRegistration) {
+ context_->Register(GURL("http://www.example.com/*"),
+ GURL("http://www.example.com/service_worker.js"));
+
+ ServiceWorkerRegistration* registration1 =
+ context_->RegistrationForDocument(GURL("http://www.example.com/one"));
+ ServiceWorkerRegistration* registration2 =
+ context_->RegistrationForDocument(GURL("http://www.example.com/two"));
+
+ ASSERT_EQ(registration1, registration2);
+}
+
+TEST_F(ServiceWorkerContextTest, DifferentMatchDifferentRegistration) {
+ context_->Register(GURL("http://www.example.com/two/*"),
+ GURL("http://www.example.com/service_worker.js"));
+
+ context_->Register(GURL("http://www.example.com/one/*"),
+ GURL("http://www.example.com/service_worker.js"));
+
+ scoped_refptr<ServiceWorkerRegistration> registration1 =
+ context_->RegistrationForDocument(GURL("http://www.example.com/one/"));
+ scoped_refptr<ServiceWorkerRegistration> registration2 =
+ context_->RegistrationForDocument(GURL("http://www.example.com/two/"));
+
+ ASSERT_NE(registration1, registration2);
+}
+
+TEST_F(ServiceWorkerContextTest, Register) {
+ int64 registration_id = -1L;
+ bool called = false;
+ context_->RegisterServiceWorker(
+ GURL("http://www.example.com/*"),
+ GURL("http://www.example.com/service_worker.js"),
+ MakeRegisteredCallback(&called, &registration_id));
+
+ ASSERT_FALSE(called);
+ message_loop_.RunUntilIdle();
+ ASSERT_TRUE(called);
+
+ ASSERT_NE(-1L, registration_id);
+}
+
+TEST_F(ServiceWorkerContextTest, Unregister) {
+ GURL pattern("http://www.example.com/*");
+
+ bool called;
+ int64 registration_id = -1L;
+ context_->RegisterServiceWorker(
+ pattern,
+ GURL("http://www.example.com/service_worker.js"),
+ MakeRegisteredCallback(&called, &registration_id));
+
+ ASSERT_FALSE(called);
+ message_loop_.RunUntilIdle();
+ ASSERT_TRUE(called);
+
+ scoped_refptr<ServiceWorkerRegistration> registration(
+ context_->RegistrationForPattern(pattern));
+
+ called = false;
+ context_->UnregisterServiceWorker(pattern, MakeUnregisteredCallback(&called));
+
+ ASSERT_FALSE(called);
+ message_loop_.RunUntilIdle();
+ ASSERT_TRUE(called);
+
+ ASSERT_TRUE(registration->HasOneRef());
+
+ ASSERT_EQ(NULL, context_->RegistrationForPattern(pattern).get());
+}
+
+} // content

Powered by Google App Engine
This is Rietveld 408576698