Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / content / browser / service_worker / service_worker_provider_host_unittest.cc
index 94baf11..a29495f 100644 (file)
@@ -4,6 +4,8 @@
 
 #include "base/basictypes.h"
 #include "base/memory/weak_ptr.h"
+#include "base/thread_task_runner_handle.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_provider_host.h"
 #include "content/browser/service_worker/service_worker_register_job.h"
@@ -23,22 +25,14 @@ class ServiceWorkerProviderHostTest : public testing::Test {
   virtual ~ServiceWorkerProviderHostTest() {}
 
   virtual void SetUp() OVERRIDE {
-    context_.reset(
-        new ServiceWorkerContextCore(base::FilePath(),
-                                     base::MessageLoopProxy::current(),
-                                     base::MessageLoopProxy::current(),
-                                     base::MessageLoopProxy::current(),
-                                     NULL,
-                                     NULL,
-                                     NULL));
-
-    scope_ = GURL("http://www.example.com/");
+    helper_.reset(new EmbeddedWorkerTestHelper(kRenderProcessId));
+    context_ = helper_->context();
+    pattern_ = GURL("http://www.example.com/");
     script_url_ = GURL("http://www.example.com/service_worker.js");
     registration_ = new ServiceWorkerRegistration(
-        scope_, script_url_, 1L, context_->AsWeakPtr());
+        pattern_, 1L, context_->AsWeakPtr());
     version_ = new ServiceWorkerVersion(
-        registration_,
-        1L, context_->AsWeakPtr());
+        registration_.get(), script_url_, 1L, context_->AsWeakPtr());
 
     // Prepare provider hosts (for the same process).
     scoped_ptr<ServiceWorkerProviderHost> host1(new ServiceWorkerProviderHost(
@@ -56,27 +50,21 @@ class ServiceWorkerProviderHostTest : public testing::Test {
   virtual void TearDown() OVERRIDE {
     version_ = 0;
     registration_ = 0;
-    context_.reset();
+    helper_.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_);
+  bool HasProcessToRun() const {
+    return context_->process_manager()->PatternHasProcessToRun(pattern_);
   }
 
   content::TestBrowserThreadBundle thread_bundle_;
-  scoped_ptr<ServiceWorkerContextCore> context_;
+  scoped_ptr<EmbeddedWorkerTestHelper> helper_;
+  ServiceWorkerContextCore* context_;
   scoped_refptr<ServiceWorkerRegistration> registration_;
   scoped_refptr<ServiceWorkerVersion> version_;
   base::WeakPtr<ServiceWorkerProviderHost> provider_host1_;
   base::WeakPtr<ServiceWorkerProviderHost> provider_host2_;
-  GURL scope_;
+  GURL pattern_;
   GURL script_url_;
 
  private:
@@ -84,146 +72,81 @@ class ServiceWorkerProviderHostTest : public testing::Test {
 };
 
 TEST_F(ServiceWorkerProviderHostTest, SetActiveVersion_ProcessStatus) {
-  provider_host1_->AssociateRegistration(registration_);
-  ASSERT_FALSE(version_->HasProcessToRun());
+  provider_host1_->AssociateRegistration(registration_.get());
+  ASSERT_TRUE(HasProcessToRun());
 
   // Associating version_ to a provider_host's active version will internally
   // add the provider_host's process ref to the version.
-  registration_->SetActiveVersion(version_);
-  ASSERT_TRUE(version_->HasProcessToRun());
+  registration_->SetActiveVersion(version_.get());
+  ASSERT_TRUE(HasProcessToRun());
 
   // Re-associating the same version and provider_host should just work too.
-  registration_->SetActiveVersion(version_);
-  ASSERT_TRUE(version_->HasProcessToRun());
+  registration_->SetActiveVersion(version_.get());
+  ASSERT_TRUE(HasProcessToRun());
 
   // Resetting the provider_host's active version should remove process refs
   // from the version.
-  provider_host1_->UnassociateRegistration();
-  ASSERT_FALSE(version_->HasProcessToRun());
+  provider_host1_->DisassociateRegistration();
+  ASSERT_FALSE(HasProcessToRun());
 }
 
 TEST_F(ServiceWorkerProviderHostTest,
        SetActiveVersion_MultipleHostsForSameProcess) {
-  provider_host1_->AssociateRegistration(registration_);
-  provider_host2_->AssociateRegistration(registration_);
-  ASSERT_FALSE(version_->HasProcessToRun());
+  provider_host1_->AssociateRegistration(registration_.get());
+  provider_host2_->AssociateRegistration(registration_.get());
+  ASSERT_TRUE(HasProcessToRun());
 
   // Associating version_ to two providers as active version.
-  registration_->SetActiveVersion(version_);
-  ASSERT_TRUE(version_->HasProcessToRun());
+  registration_->SetActiveVersion(version_.get());
+  ASSERT_TRUE(HasProcessToRun());
 
   // Disassociating one provider_host shouldn't remove all process refs
   // from the version yet.
-  provider_host1_->UnassociateRegistration();
-  ASSERT_TRUE(version_->HasProcessToRun());
+  provider_host1_->DisassociateRegistration();
+  ASSERT_TRUE(HasProcessToRun());
 
   // Disassociating the other provider_host will remove all process refs.
-  provider_host2_->UnassociateRegistration();
-  ASSERT_FALSE(version_->HasProcessToRun());
+  provider_host2_->DisassociateRegistration();
+  ASSERT_FALSE(HasProcessToRun());
 }
 
 TEST_F(ServiceWorkerProviderHostTest, SetWaitingVersion_ProcessStatus) {
-  provider_host1_->AssociateRegistration(registration_);
-  ASSERT_FALSE(version_->HasProcessToRun());
+  provider_host1_->AssociateRegistration(registration_.get());
+  ASSERT_TRUE(HasProcessToRun());
 
   // Associating version_ to a provider_host's waiting version will internally
   // add the provider_host's process ref to the version.
-  registration_->SetWaitingVersion(version_);
-  ASSERT_TRUE(version_->HasProcessToRun());
+  registration_->SetWaitingVersion(version_.get());
+  ASSERT_TRUE(HasProcessToRun());
 
   // Re-associating the same version and provider_host should just work too.
-  registration_->SetWaitingVersion(version_);
-  ASSERT_TRUE(version_->HasProcessToRun());
+  registration_->SetWaitingVersion(version_.get());
+  ASSERT_TRUE(HasProcessToRun());
 
   // Resetting the provider_host's waiting version should remove process refs
   // from the version.
-  provider_host1_->UnassociateRegistration();
-  ASSERT_FALSE(version_->HasProcessToRun());
+  provider_host1_->DisassociateRegistration();
+  ASSERT_FALSE(HasProcessToRun());
 }
 
 TEST_F(ServiceWorkerProviderHostTest,
        SetWaitingVersion_MultipleHostsForSameProcess) {
-  provider_host1_->AssociateRegistration(registration_);
-  provider_host2_->AssociateRegistration(registration_);
-  ASSERT_FALSE(version_->HasProcessToRun());
+  provider_host1_->AssociateRegistration(registration_.get());
+  provider_host2_->AssociateRegistration(registration_.get());
+  ASSERT_TRUE(HasProcessToRun());
 
   // Associating version_ to two providers as waiting version.
-  registration_->SetWaitingVersion(version_);
-  ASSERT_TRUE(version_->HasProcessToRun());
+  registration_->SetWaitingVersion(version_.get());
+  ASSERT_TRUE(HasProcessToRun());
 
   // Disassociating one provider_host shouldn't remove all process refs
   // from the version yet.
-  provider_host1_->UnassociateRegistration();
-  ASSERT_TRUE(version_->HasProcessToRun());
+  provider_host1_->DisassociateRegistration();
+  ASSERT_TRUE(HasProcessToRun());
 
   // Disassociating the other provider_host will remove all process refs.
-  provider_host2_->UnassociateRegistration();
-  ASSERT_FALSE(version_->HasProcessToRun());
-}
-
-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(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);
+  provider_host2_->DisassociateRegistration();
+  ASSERT_FALSE(HasProcessToRun());
 }
 
 }  // namespace content