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 virtual void SetUp() override
{
28 helper_
.reset(new EmbeddedWorkerTestHelper(kRenderProcessId
));
31 virtual void TearDown() override
{
35 ServiceWorkerContextCore
* context() { return helper_
->context(); }
37 EmbeddedWorkerRegistry
* embedded_worker_registry() {
39 return context()->embedded_worker_registry();
42 IPC::TestSink
* ipc_sink() { return helper_
->ipc_sink(); }
44 TestBrowserThreadBundle thread_bundle_
;
45 scoped_ptr
<EmbeddedWorkerTestHelper
> helper_
;
48 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerInstanceTest
);
51 static void SaveStatusAndCall(ServiceWorkerStatusCode
* out
,
52 const base::Closure
& callback
,
53 ServiceWorkerStatusCode status
) {
58 TEST_F(EmbeddedWorkerInstanceTest
, StartAndStop
) {
59 scoped_ptr
<EmbeddedWorkerInstance
> worker
=
60 embedded_worker_registry()->CreateWorker();
61 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED
, worker
->status());
63 const int64 service_worker_version_id
= 55L;
64 const GURL
pattern("http://example.com/");
65 const GURL
url("http://example.com/worker.js");
67 // Simulate adding one process to the pattern.
68 helper_
->SimulateAddProcessToPattern(pattern
, kRenderProcessId
);
70 // Start should succeed.
71 ServiceWorkerStatusCode status
;
72 base::RunLoop run_loop
;
74 service_worker_version_id
,
78 base::Bind(&SaveStatusAndCall
, &status
, run_loop
.QuitClosure()));
80 EXPECT_EQ(SERVICE_WORKER_OK
, status
);
81 EXPECT_EQ(EmbeddedWorkerInstance::STARTING
, worker
->status());
82 base::RunLoop().RunUntilIdle();
84 // Worker started message should be notified (by EmbeddedWorkerTestHelper).
85 EXPECT_EQ(EmbeddedWorkerInstance::RUNNING
, worker
->status());
86 EXPECT_EQ(kRenderProcessId
, worker
->process_id());
89 EXPECT_EQ(SERVICE_WORKER_OK
, worker
->Stop());
90 EXPECT_EQ(EmbeddedWorkerInstance::STOPPING
, worker
->status());
91 base::RunLoop().RunUntilIdle();
93 // Worker stopped message should be notified (by EmbeddedWorkerTestHelper).
94 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED
, worker
->status());
96 // Verify that we've sent two messages to start and terminate the worker.
97 ASSERT_TRUE(ipc_sink()->GetUniqueMessageMatching(
98 EmbeddedWorkerMsg_StartWorker::ID
));
99 ASSERT_TRUE(ipc_sink()->GetUniqueMessageMatching(
100 EmbeddedWorkerMsg_StopWorker::ID
));
103 TEST_F(EmbeddedWorkerInstanceTest
, InstanceDestroyedBeforeStartFinishes
) {
104 scoped_ptr
<EmbeddedWorkerInstance
> worker
=
105 embedded_worker_registry()->CreateWorker();
106 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED
, worker
->status());
108 const int64 service_worker_version_id
= 55L;
109 const GURL
pattern("http://example.com/");
110 const GURL
url("http://example.com/worker.js");
112 ServiceWorkerStatusCode status
;
113 base::RunLoop run_loop
;
114 // Begin starting the worker.
115 context()->process_manager()->AddProcessReferenceToPattern(
116 pattern
, kRenderProcessId
);
118 service_worker_version_id
,
122 base::Bind(&SaveStatusAndCall
, &status
, run_loop
.QuitClosure()));
123 // But destroy it before it gets a chance to complete.
126 EXPECT_EQ(SERVICE_WORKER_ERROR_ABORT
, status
);
128 // Verify that we didn't send the message to start the worker.
130 ipc_sink()->GetUniqueMessageMatching(EmbeddedWorkerMsg_StartWorker::ID
));
133 } // namespace content