1 // Copyright 2013 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.
5 #include "base/basictypes.h"
6 #include "base/run_loop.h"
7 #include "base/stl_util.h"
8 #include "content/browser/service_worker/embedded_worker_instance.h"
9 #include "content/browser/service_worker/embedded_worker_registry.h"
10 #include "content/browser/service_worker/embedded_worker_test_helper.h"
11 #include "content/browser/service_worker/service_worker_context_core.h"
12 #include "content/browser/service_worker/service_worker_context_wrapper.h"
13 #include "content/common/service_worker/embedded_worker_messages.h"
14 #include "content/public/test/test_browser_thread_bundle.h"
15 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h"
20 static const int kRenderProcessId
= 11;
22 class EmbeddedWorkerInstanceTest
: public testing::Test
{
24 EmbeddedWorkerInstanceTest()
25 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP
) {}
27 void SetUp() override
{
28 helper_
.reset(new EmbeddedWorkerTestHelper(kRenderProcessId
));
31 void TearDown() override
{ helper_
.reset(); }
33 ServiceWorkerContextCore
* context() { return helper_
->context(); }
35 EmbeddedWorkerRegistry
* embedded_worker_registry() {
37 return context()->embedded_worker_registry();
40 IPC::TestSink
* ipc_sink() { return helper_
->ipc_sink(); }
42 TestBrowserThreadBundle thread_bundle_
;
43 scoped_ptr
<EmbeddedWorkerTestHelper
> helper_
;
46 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerInstanceTest
);
49 static void SaveStatusAndCall(ServiceWorkerStatusCode
* out
,
50 const base::Closure
& callback
,
51 ServiceWorkerStatusCode status
) {
56 TEST_F(EmbeddedWorkerInstanceTest
, StartAndStop
) {
57 scoped_ptr
<EmbeddedWorkerInstance
> worker
=
58 embedded_worker_registry()->CreateWorker();
59 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED
, worker
->status());
61 const int64 service_worker_version_id
= 55L;
62 const GURL
pattern("http://example.com/");
63 const GURL
url("http://example.com/worker.js");
65 // Simulate adding one process to the pattern.
66 helper_
->SimulateAddProcessToPattern(pattern
, kRenderProcessId
);
68 // Start should succeed.
69 ServiceWorkerStatusCode status
;
70 base::RunLoop run_loop
;
72 service_worker_version_id
,
76 base::Bind(&SaveStatusAndCall
, &status
, run_loop
.QuitClosure()));
77 EXPECT_EQ(EmbeddedWorkerInstance::STARTING
, worker
->status());
79 EXPECT_EQ(SERVICE_WORKER_OK
, status
);
81 // The 'WorkerStarted' message should have been sent by
82 // EmbeddedWorkerTestHelper.
83 EXPECT_EQ(EmbeddedWorkerInstance::RUNNING
, worker
->status());
84 EXPECT_EQ(kRenderProcessId
, worker
->process_id());
87 EXPECT_EQ(SERVICE_WORKER_OK
, worker
->Stop());
88 EXPECT_EQ(EmbeddedWorkerInstance::STOPPING
, worker
->status());
89 base::RunLoop().RunUntilIdle();
91 // The 'WorkerStopped' message should have been sent by
92 // EmbeddedWorkerTestHelper.
93 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED
, worker
->status());
95 // Verify that we've sent two messages to start and terminate the worker.
96 ASSERT_TRUE(ipc_sink()->GetUniqueMessageMatching(
97 EmbeddedWorkerMsg_StartWorker::ID
));
98 ASSERT_TRUE(ipc_sink()->GetUniqueMessageMatching(
99 EmbeddedWorkerMsg_StopWorker::ID
));
102 TEST_F(EmbeddedWorkerInstanceTest
, InstanceDestroyedBeforeStartFinishes
) {
103 scoped_ptr
<EmbeddedWorkerInstance
> worker
=
104 embedded_worker_registry()->CreateWorker();
105 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED
, worker
->status());
107 const int64 service_worker_version_id
= 55L;
108 const GURL
pattern("http://example.com/");
109 const GURL
url("http://example.com/worker.js");
111 ServiceWorkerStatusCode status
;
112 base::RunLoop run_loop
;
113 // Begin starting the worker.
114 context()->process_manager()->AddProcessReferenceToPattern(
115 pattern
, kRenderProcessId
);
117 service_worker_version_id
,
121 base::Bind(&SaveStatusAndCall
, &status
, run_loop
.QuitClosure()));
122 // But destroy it before it gets a chance to complete.
125 EXPECT_EQ(SERVICE_WORKER_ERROR_ABORT
, status
);
127 // Verify that we didn't send the message to start the worker.
129 ipc_sink()->GetUniqueMessageMatching(EmbeddedWorkerMsg_StartWorker::ID
));
132 } // namespace content