Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / content / browser / service_worker / service_worker_provider_host_unittest.cc
index 1f52514..94baf11 100644 (file)
@@ -27,11 +27,12 @@ class ServiceWorkerProviderHostTest : public testing::Test {
         new ServiceWorkerContextCore(base::FilePath(),
                                      base::MessageLoopProxy::current(),
                                      base::MessageLoopProxy::current(),
+                                     base::MessageLoopProxy::current(),
                                      NULL,
                                      NULL,
                                      NULL));
 
-    scope_ = GURL("http://www.example.com/*");
+    scope_ = GURL("http://www.example.com/");
     script_url_ = GURL("http://www.example.com/service_worker.js");
     registration_ = new ServiceWorkerRegistration(
         scope_, script_url_, 1L, context_->AsWeakPtr());
@@ -46,15 +47,10 @@ class ServiceWorkerProviderHostTest : public testing::Test {
     scoped_ptr<ServiceWorkerProviderHost> host2(new ServiceWorkerProviderHost(
         kRenderProcessId, 2 /* provider_id */,
         context_->AsWeakPtr(), NULL));
-    scoped_ptr<ServiceWorkerProviderHost> host3(new ServiceWorkerProviderHost(
-        kRenderProcessId, 3 /* provider_id */,
-        context_->AsWeakPtr(), NULL));
     provider_host1_ = host1->AsWeakPtr();
     provider_host2_ = host2->AsWeakPtr();
-    provider_host3_ = host3->AsWeakPtr();
     context_->AddProviderHost(make_scoped_ptr(host1.release()));
     context_->AddProviderHost(make_scoped_ptr(host2.release()));
-    context_->AddProviderHost(make_scoped_ptr(host3.release()));
   }
 
   virtual void TearDown() OVERRIDE {
@@ -63,13 +59,23 @@ class ServiceWorkerProviderHostTest : public testing::Test {
     context_.reset();
   }
 
+  void VerifyVersionAttributes(
+      base::WeakPtr<ServiceWorkerProviderHost> provider_host,
+      ServiceWorkerVersion* installing,
+      ServiceWorkerVersion* waiting,
+      ServiceWorkerVersion* active) {
+    EXPECT_EQ(installing, provider_host->installing_version_);
+    EXPECT_EQ(waiting, provider_host->waiting_version_);
+    EXPECT_EQ(active, provider_host->active_version_);
+    EXPECT_FALSE(provider_host->controlling_version_);
+  }
+
   content::TestBrowserThreadBundle thread_bundle_;
   scoped_ptr<ServiceWorkerContextCore> context_;
   scoped_refptr<ServiceWorkerRegistration> registration_;
   scoped_refptr<ServiceWorkerVersion> version_;
   base::WeakPtr<ServiceWorkerProviderHost> provider_host1_;
   base::WeakPtr<ServiceWorkerProviderHost> provider_host2_;
-  base::WeakPtr<ServiceWorkerProviderHost> provider_host3_;
   GURL scope_;
   GURL script_url_;
 
@@ -78,206 +84,146 @@ class ServiceWorkerProviderHostTest : public testing::Test {
 };
 
 TEST_F(ServiceWorkerProviderHostTest, SetActiveVersion_ProcessStatus) {
+  provider_host1_->AssociateRegistration(registration_);
   ASSERT_FALSE(version_->HasProcessToRun());
 
   // Associating version_ to a provider_host's active version will internally
   // add the provider_host's process ref to the version.
-  provider_host1_->SetActiveVersion(version_);
+  registration_->SetActiveVersion(version_);
   ASSERT_TRUE(version_->HasProcessToRun());
 
   // Re-associating the same version and provider_host should just work too.
-  provider_host1_->SetActiveVersion(version_);
+  registration_->SetActiveVersion(version_);
   ASSERT_TRUE(version_->HasProcessToRun());
 
   // Resetting the provider_host's active version should remove process refs
   // from the version.
-  provider_host1_->SetActiveVersion(NULL);
+  provider_host1_->UnassociateRegistration();
   ASSERT_FALSE(version_->HasProcessToRun());
 }
 
 TEST_F(ServiceWorkerProviderHostTest,
        SetActiveVersion_MultipleHostsForSameProcess) {
+  provider_host1_->AssociateRegistration(registration_);
+  provider_host2_->AssociateRegistration(registration_);
   ASSERT_FALSE(version_->HasProcessToRun());
 
   // Associating version_ to two providers as active version.
-  provider_host1_->SetActiveVersion(version_);
-  provider_host2_->SetActiveVersion(version_);
+  registration_->SetActiveVersion(version_);
   ASSERT_TRUE(version_->HasProcessToRun());
 
   // Disassociating one provider_host shouldn't remove all process refs
   // from the version yet.
-  provider_host1_->SetActiveVersion(NULL);
+  provider_host1_->UnassociateRegistration();
   ASSERT_TRUE(version_->HasProcessToRun());
 
   // Disassociating the other provider_host will remove all process refs.
-  provider_host2_->SetActiveVersion(NULL);
+  provider_host2_->UnassociateRegistration();
   ASSERT_FALSE(version_->HasProcessToRun());
 }
 
 TEST_F(ServiceWorkerProviderHostTest, SetWaitingVersion_ProcessStatus) {
+  provider_host1_->AssociateRegistration(registration_);
   ASSERT_FALSE(version_->HasProcessToRun());
 
   // Associating version_ to a provider_host's waiting version will internally
   // add the provider_host's process ref to the version.
-  provider_host1_->SetWaitingVersion(version_);
+  registration_->SetWaitingVersion(version_);
   ASSERT_TRUE(version_->HasProcessToRun());
 
   // Re-associating the same version and provider_host should just work too.
-  provider_host1_->SetWaitingVersion(version_);
+  registration_->SetWaitingVersion(version_);
   ASSERT_TRUE(version_->HasProcessToRun());
 
   // Resetting the provider_host's waiting version should remove process refs
   // from the version.
-  provider_host1_->SetWaitingVersion(NULL);
+  provider_host1_->UnassociateRegistration();
   ASSERT_FALSE(version_->HasProcessToRun());
 }
 
 TEST_F(ServiceWorkerProviderHostTest,
        SetWaitingVersion_MultipleHostsForSameProcess) {
+  provider_host1_->AssociateRegistration(registration_);
+  provider_host2_->AssociateRegistration(registration_);
   ASSERT_FALSE(version_->HasProcessToRun());
 
-  // Associating version_ to two providers as active version.
-  provider_host1_->SetWaitingVersion(version_);
-  provider_host2_->SetWaitingVersion(version_);
+  // Associating version_ to two providers as waiting version.
+  registration_->SetWaitingVersion(version_);
   ASSERT_TRUE(version_->HasProcessToRun());
 
   // Disassociating one provider_host shouldn't remove all process refs
   // from the version yet.
-  provider_host1_->SetWaitingVersion(NULL);
+  provider_host1_->UnassociateRegistration();
   ASSERT_TRUE(version_->HasProcessToRun());
 
   // Disassociating the other provider_host will remove all process refs.
-  provider_host2_->SetWaitingVersion(NULL);
+  provider_host2_->UnassociateRegistration();
   ASSERT_FALSE(version_->HasProcessToRun());
 }
 
-class ServiceWorkerProviderHostWaitingVersionTest : public testing::Test {
- protected:
-  ServiceWorkerProviderHostWaitingVersionTest()
-      : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP),
-        next_provider_id_(1L) {}
-  virtual ~ServiceWorkerProviderHostWaitingVersionTest() {}
-
-  virtual void SetUp() OVERRIDE {
-    context_.reset(
-        new ServiceWorkerContextCore(base::FilePath(),
-                                     base::MessageLoopProxy::current(),
-                                     base::MessageLoopProxy::current(),
-                                     NULL,
-                                     NULL,
-                                     NULL));
-
-    // Prepare provider hosts (for the same process).
-    provider_host1_ = CreateProviderHost(GURL("http://www.example.com/foo"));
-    provider_host2_ = CreateProviderHost(GURL("http://www.example.com/bar"));
-    provider_host3_ = CreateProviderHost(GURL("http://www.example.ca/foo"));
-  }
-
-  base::WeakPtr<ServiceWorkerProviderHost> CreateProviderHost(
-      const GURL& document_url) {
-    scoped_ptr<ServiceWorkerProviderHost> host(new ServiceWorkerProviderHost(
-        kRenderProcessId, next_provider_id_++, context_->AsWeakPtr(), NULL));
-    host->SetDocumentUrl(document_url);
-    base::WeakPtr<ServiceWorkerProviderHost> provider_host = host->AsWeakPtr();
-    context_->AddProviderHost(host.Pass());
-    return provider_host;
-  }
-
-  virtual void TearDown() OVERRIDE {
-    context_.reset();
-  }
-
-  content::TestBrowserThreadBundle thread_bundle_;
-  scoped_ptr<ServiceWorkerContextCore> context_;
-  base::WeakPtr<ServiceWorkerProviderHost> provider_host1_;
-  base::WeakPtr<ServiceWorkerProviderHost> provider_host2_;
-  base::WeakPtr<ServiceWorkerProviderHost> provider_host3_;
-
- private:
-  int64 next_provider_id_;
-
-  DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderHostWaitingVersionTest);
-};
-
-TEST_F(ServiceWorkerProviderHostWaitingVersionTest,
-       AssociateWaitingVersionToDocuments) {
-  const GURL scope1("http://www.example.com/*");
-  const GURL script_url1("http://www.example.com/service_worker1.js");
-  scoped_refptr<ServiceWorkerRegistration> registration1(
-      new ServiceWorkerRegistration(
-          scope1, script_url1, 1L, context_->AsWeakPtr()));
-  scoped_refptr<ServiceWorkerVersion> version1(
-      new ServiceWorkerVersion(registration1, 1L, context_->AsWeakPtr()));
-
-  ServiceWorkerRegisterJob::AssociateWaitingVersionToDocuments(
-      context_->AsWeakPtr(), version1.get());
-  EXPECT_EQ(version1.get(), provider_host1_->waiting_version());
-  EXPECT_EQ(version1.get(), provider_host2_->waiting_version());
-  EXPECT_EQ(NULL, provider_host3_->waiting_version());
-
-  // Version2 is associated with the same registration as version1, so the
-  // waiting version of host1 and host2 should be replaced.
-  scoped_refptr<ServiceWorkerVersion> version2(
-      new ServiceWorkerVersion(registration1, 2L, context_->AsWeakPtr()));
-  ServiceWorkerRegisterJob::AssociateWaitingVersionToDocuments(
-      context_->AsWeakPtr(), version2.get());
-  EXPECT_EQ(version2.get(), provider_host1_->waiting_version());
-  EXPECT_EQ(version2.get(), provider_host2_->waiting_version());
-  EXPECT_EQ(NULL, provider_host3_->waiting_version());
-
-  const GURL scope3(provider_host1_->document_url());
-  const GURL script_url3("http://www.example.com/service_worker3.js");
-  scoped_refptr<ServiceWorkerRegistration> registration3(
-      new ServiceWorkerRegistration(
-          scope3, script_url3, 3L, context_->AsWeakPtr()));
-  scoped_refptr<ServiceWorkerVersion> version3(
-      new ServiceWorkerVersion(registration3, 3L, context_->AsWeakPtr()));
-
-  // Although version3 can match longer than version2 for host1, it should be
-  // ignored because version3 is associated with a different registration from
-  // version2.
-  ServiceWorkerRegisterJob::AssociateWaitingVersionToDocuments(
-      context_->AsWeakPtr(), version3.get());
-  EXPECT_EQ(version2.get(), provider_host1_->waiting_version());
-  EXPECT_EQ(version2.get(), provider_host2_->waiting_version());
-  EXPECT_EQ(NULL, provider_host3_->waiting_version());
+TEST_F(ServiceWorkerProviderHostTest,
+       ObserveVersionAttributesChanged_Basic) {
+  provider_host1_->AssociateRegistration(registration_);
+  provider_host2_->AssociateRegistration(registration_);
+  VerifyVersionAttributes(provider_host1_, NULL, NULL, NULL);
+  VerifyVersionAttributes(provider_host2_, NULL, NULL, NULL);
+
+  registration_->SetInstallingVersion(version_);
+  VerifyVersionAttributes(provider_host1_, version_, NULL, NULL);
+  VerifyVersionAttributes(provider_host2_, version_, NULL, NULL);
+
+  registration_->SetWaitingVersion(version_);
+  VerifyVersionAttributes(provider_host1_, NULL, version_, NULL);
+  VerifyVersionAttributes(provider_host2_, NULL, version_, NULL);
+
+  // Disassociating the registration should clear all version attributes.
+  provider_host2_->UnassociateRegistration();
+  VerifyVersionAttributes(provider_host1_, NULL, version_, NULL);
+  VerifyVersionAttributes(provider_host2_, NULL, NULL, NULL);
+
+  // Shouldn't notify the disassociated provider of the change.
+  registration_->SetActiveVersion(version_);
+  VerifyVersionAttributes(provider_host1_, NULL, NULL, version_);
+  VerifyVersionAttributes(provider_host2_, NULL, NULL, NULL);
 }
 
-TEST_F(ServiceWorkerProviderHostWaitingVersionTest,
-       DisassociateWaitingVersionFromDocuments) {
-  const GURL scope1("http://www.example.com/*");
-  const GURL script_url1("http://www.example.com/service_worker.js");
-  scoped_refptr<ServiceWorkerRegistration> registration1(
-      new ServiceWorkerRegistration(
-          scope1, script_url1, 1L, context_->AsWeakPtr()));
-  scoped_refptr<ServiceWorkerVersion> version1(
-      new ServiceWorkerVersion(registration1, 1L, context_->AsWeakPtr()));
-
-  const GURL scope2("http://www.example.ca/*");
-  const GURL script_url2("http://www.example.ca/service_worker.js");
-  scoped_refptr<ServiceWorkerRegistration> registration2(
-      new ServiceWorkerRegistration(
-          scope2, script_url2, 2L, context_->AsWeakPtr()));
-  scoped_refptr<ServiceWorkerVersion> version2(
-      new ServiceWorkerVersion(registration2, 2L, context_->AsWeakPtr()));
-
-  ServiceWorkerRegisterJob::AssociateWaitingVersionToDocuments(
-      context_->AsWeakPtr(), version1.get());
-  ServiceWorkerRegisterJob::AssociateWaitingVersionToDocuments(
-      context_->AsWeakPtr(), version2.get());
-
-  // Host1 and host2 are associated with version1 as a waiting version, whereas
-  // host3 is associated with version2.
-  EXPECT_EQ(version1.get(), provider_host1_->waiting_version());
-  EXPECT_EQ(version1.get(), provider_host2_->waiting_version());
-  EXPECT_EQ(version2.get(), provider_host3_->waiting_version());
-
-  // Disassociate version1 from host1 and host2.
-  ServiceWorkerRegisterJob::DisassociateWaitingVersionFromDocuments(
-      context_->AsWeakPtr(), version1->version_id());
-  EXPECT_EQ(NULL, provider_host1_->waiting_version());
-  EXPECT_EQ(NULL, provider_host2_->waiting_version());
-  EXPECT_EQ(version2.get(), provider_host3_->waiting_version());
+TEST_F(ServiceWorkerProviderHostTest,
+       ObserveVersionAttributesChanged_MultipleVersions) {
+  provider_host1_->AssociateRegistration(registration_);
+  provider_host2_->AssociateRegistration(registration_);
+  VerifyVersionAttributes(provider_host1_, NULL, NULL, NULL);
+  VerifyVersionAttributes(provider_host2_, NULL, NULL, NULL);
+
+  scoped_refptr<ServiceWorkerVersion> version1 =
+      new ServiceWorkerVersion(registration_, 10L, context_->AsWeakPtr());
+  scoped_refptr<ServiceWorkerVersion> version2 =
+      new ServiceWorkerVersion(registration_, 20L, context_->AsWeakPtr());
+
+  registration_->SetInstallingVersion(version1);
+  VerifyVersionAttributes(provider_host1_, version1, NULL, NULL);
+  VerifyVersionAttributes(provider_host2_, version1, NULL, NULL);
+
+  registration_->SetWaitingVersion(version1);
+  VerifyVersionAttributes(provider_host1_, NULL, version1, NULL);
+  VerifyVersionAttributes(provider_host2_, NULL, version1, NULL);
+
+  registration_->SetInstallingVersion(version2);
+  VerifyVersionAttributes(provider_host1_, version2, version1, NULL);
+  VerifyVersionAttributes(provider_host2_, version2, version1, NULL);
+
+  // Disassociating the registration should clear all version attributes.
+  provider_host2_->UnassociateRegistration();
+  VerifyVersionAttributes(provider_host1_, version2, version1, NULL);
+  VerifyVersionAttributes(provider_host2_, NULL, NULL, NULL);
+
+  // Shouldn't notify the disassociated provider of the change.
+  registration_->SetActiveVersion(version1);
+  VerifyVersionAttributes(provider_host1_, version2, NULL, version1);
+  VerifyVersionAttributes(provider_host2_, NULL, NULL, NULL);
+
+  registration_->SetActiveVersion(version2);
+  VerifyVersionAttributes(provider_host1_, NULL, NULL, version2);
+  VerifyVersionAttributes(provider_host2_, NULL, NULL, NULL);
 }
 
 }  // namespace content