Home | History | Annotate | Download | only in service_worker
      1 // Copyright 2014 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "base/basictypes.h"
      6 #include "base/run_loop.h"
      7 #include "content/browser/service_worker/embedded_worker_registry.h"
      8 #include "content/browser/service_worker/embedded_worker_test_helper.h"
      9 #include "content/browser/service_worker/service_worker_context_core.h"
     10 #include "content/browser/service_worker/service_worker_handle.h"
     11 #include "content/browser/service_worker/service_worker_registration.h"
     12 #include "content/browser/service_worker/service_worker_test_utils.h"
     13 #include "content/browser/service_worker/service_worker_version.h"
     14 #include "content/common/service_worker/embedded_worker_messages.h"
     15 #include "content/common/service_worker/service_worker_messages.h"
     16 #include "content/public/test/test_browser_thread_bundle.h"
     17 #include "ipc/ipc_message.h"
     18 #include "ipc/ipc_test_sink.h"
     19 #include "testing/gtest/include/gtest/gtest.h"
     20 #include "third_party/WebKit/public/platform/WebServiceWorkerState.h"
     21 
     22 namespace content {
     23 
     24 namespace {
     25 
     26 const int kRenderProcessId = 88;  // A dummy ID for testing.
     27 
     28 void VerifyStateChangedMessage(int expected_handle_id,
     29                               blink::WebServiceWorkerState expected_state,
     30                               const IPC::Message* message) {
     31   ASSERT_TRUE(message != NULL);
     32   ServiceWorkerMsg_ServiceWorkerStateChanged::Param param;
     33   ASSERT_TRUE(ServiceWorkerMsg_ServiceWorkerStateChanged::Read(
     34       message, &param));
     35   EXPECT_EQ(expected_handle_id, param.b);
     36   EXPECT_EQ(expected_state, param.c);
     37 }
     38 
     39 }  // namespace
     40 
     41 class ServiceWorkerHandleTest : public testing::Test {
     42  public:
     43   ServiceWorkerHandleTest()
     44       : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {}
     45 
     46   virtual void SetUp() OVERRIDE {
     47     helper_.reset(new EmbeddedWorkerTestHelper(kRenderProcessId));
     48 
     49     registration_ = new ServiceWorkerRegistration(
     50         GURL("http://www.example.com/*"),
     51         GURL("http://www.example.com/service_worker.js"),
     52         1L,
     53         helper_->context()->AsWeakPtr());
     54     version_ = new ServiceWorkerVersion(
     55         registration_, 1L, helper_->context()->AsWeakPtr());
     56 
     57     // Simulate adding one process to the worker.
     58     int embedded_worker_id = version_->embedded_worker()->embedded_worker_id();
     59     helper_->SimulateAddProcessToWorker(embedded_worker_id, kRenderProcessId);
     60   }
     61 
     62   virtual void TearDown() OVERRIDE {
     63     registration_ = NULL;
     64     version_ = NULL;
     65     helper_.reset();
     66   }
     67 
     68   IPC::TestSink* ipc_sink() { return helper_->ipc_sink(); }
     69 
     70   TestBrowserThreadBundle browser_thread_bundle_;
     71   scoped_ptr<EmbeddedWorkerTestHelper> helper_;
     72   scoped_refptr<ServiceWorkerRegistration> registration_;
     73   scoped_refptr<ServiceWorkerVersion> version_;
     74   DISALLOW_COPY_AND_ASSIGN(ServiceWorkerHandleTest);
     75 };
     76 
     77 TEST_F(ServiceWorkerHandleTest, OnVersionStateChanged) {
     78   scoped_ptr<ServiceWorkerHandle> handle =
     79       ServiceWorkerHandle::Create(helper_->context()->AsWeakPtr(),
     80                                   helper_.get(),
     81                                   1 /* thread_id */,
     82                                   version_);
     83 
     84   // Start the worker, and then...
     85   ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED;
     86   version_->StartWorker(CreateReceiverOnCurrentThread(&status));
     87   base::RunLoop().RunUntilIdle();
     88   EXPECT_EQ(SERVICE_WORKER_OK, status);
     89 
     90   // ...dispatch install event.
     91   status = SERVICE_WORKER_ERROR_FAILED;
     92   version_->DispatchInstallEvent(-1, CreateReceiverOnCurrentThread(&status));
     93   base::RunLoop().RunUntilIdle();
     94   EXPECT_EQ(SERVICE_WORKER_OK, status);
     95 
     96   ASSERT_EQ(4UL, ipc_sink()->message_count());
     97 
     98   // We should be sending 1. StartWorker,
     99   EXPECT_EQ(EmbeddedWorkerMsg_StartWorker::ID,
    100             ipc_sink()->GetMessageAt(0)->type());
    101   // 2. StateChanged (state == Installing),
    102   VerifyStateChangedMessage(handle->handle_id(),
    103                             blink::WebServiceWorkerStateInstalling,
    104                             ipc_sink()->GetMessageAt(1));
    105   // 3. SendMessageToWorker (to send InstallEvent), and
    106   EXPECT_EQ(EmbeddedWorkerContextMsg_MessageToWorker::ID,
    107             ipc_sink()->GetMessageAt(2)->type());
    108   // 4. StateChanged (state == Installed).
    109   VerifyStateChangedMessage(handle->handle_id(),
    110                             blink::WebServiceWorkerStateInstalled,
    111                             ipc_sink()->GetMessageAt(3));
    112 }
    113 
    114 }  // namespace content
    115