Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / content / browser / service_worker / service_worker_handle_unittest.cc
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
75  private:
76   DISALLOW_COPY_AND_ASSIGN(ServiceWorkerHandleTest);
77 };
78
79 TEST_F(ServiceWorkerHandleTest, OnVersionStateChanged) {
80   scoped_ptr<ServiceWorkerHandle> handle =
81       ServiceWorkerHandle::Create(helper_->context()->AsWeakPtr(),
82                                   helper_.get(),
83                                   1 /* thread_id */,
84                                   33 /* provider_id */,
85                                   version_);
86
87   // Start the worker, and then...
88   ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED;
89   version_->StartWorker(CreateReceiverOnCurrentThread(&status));
90   base::RunLoop().RunUntilIdle();
91   EXPECT_EQ(SERVICE_WORKER_OK, status);
92
93   // ...dispatch install event.
94   status = SERVICE_WORKER_ERROR_FAILED;
95   version_->SetStatus(ServiceWorkerVersion::INSTALLING);
96   version_->DispatchInstallEvent(-1, CreateReceiverOnCurrentThread(&status));
97   base::RunLoop().RunUntilIdle();
98   EXPECT_EQ(SERVICE_WORKER_OK, status);
99
100   version_->SetStatus(ServiceWorkerVersion::INSTALLED);
101
102   ASSERT_EQ(4UL, ipc_sink()->message_count());
103
104   // We should be sending 1. StartWorker,
105   EXPECT_EQ(EmbeddedWorkerMsg_StartWorker::ID,
106             ipc_sink()->GetMessageAt(0)->type());
107   // 2. StateChanged (state == Installing),
108   VerifyStateChangedMessage(handle->handle_id(),
109                             blink::WebServiceWorkerStateInstalling,
110                             ipc_sink()->GetMessageAt(1));
111   // 3. SendMessageToWorker (to send InstallEvent), and
112   EXPECT_EQ(EmbeddedWorkerContextMsg_MessageToWorker::ID,
113             ipc_sink()->GetMessageAt(2)->type());
114   // 4. StateChanged (state == Installed).
115   VerifyStateChangedMessage(handle->handle_id(),
116                             blink::WebServiceWorkerStateInstalled,
117                             ipc_sink()->GetMessageAt(3));
118 }
119
120 }  // namespace content