Cast: Skip receiver log messages with time delta that can't be encoded.
[chromium-blink-merge.git] / content / browser / service_worker / embedded_worker_instance_unittest.cc
blob3aaf820950d265918ddf0a55a6f9b1239f37e5d1
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/common/service_worker/embedded_worker_messages.h"
13 #include "content/public/test/test_browser_thread_bundle.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 namespace content {
18 static const int kRenderProcessId = 11;
20 class EmbeddedWorkerInstanceTest : public testing::Test {
21 protected:
22 EmbeddedWorkerInstanceTest()
23 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {}
25 virtual void SetUp() OVERRIDE {
26 context_.reset(new ServiceWorkerContextCore(base::FilePath(), NULL));
27 helper_.reset(new EmbeddedWorkerTestHelper(
28 context_.get(), kRenderProcessId));
31 virtual void TearDown() OVERRIDE {
32 helper_.reset();
33 context_.reset();
36 EmbeddedWorkerRegistry* embedded_worker_registry() {
37 DCHECK(context_);
38 return context_->embedded_worker_registry();
41 IPC::TestSink* ipc_sink() { return helper_->ipc_sink(); }
43 TestBrowserThreadBundle thread_bundle_;
44 scoped_ptr<ServiceWorkerContextCore> context_;
45 scoped_ptr<EmbeddedWorkerTestHelper> helper_;
47 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerInstanceTest);
50 TEST_F(EmbeddedWorkerInstanceTest, StartAndStop) {
51 scoped_ptr<EmbeddedWorkerInstance> worker =
52 embedded_worker_registry()->CreateWorker();
53 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED, worker->status());
55 const int embedded_worker_id = worker->embedded_worker_id();
56 const int64 service_worker_version_id = 55L;
57 const GURL url("http://example.com/worker.js");
59 // This fails as we have no available process yet.
60 EXPECT_EQ(SERVICE_WORKER_ERROR_PROCESS_NOT_FOUND,
61 worker->Start(service_worker_version_id, url));
62 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED, worker->status());
64 // Simulate adding one process to the worker.
65 helper_->SimulateAddProcessToWorker(embedded_worker_id, kRenderProcessId);
67 // Start should succeed.
68 EXPECT_EQ(SERVICE_WORKER_OK,
69 worker->Start(service_worker_version_id, url));
70 EXPECT_EQ(EmbeddedWorkerInstance::STARTING, worker->status());
71 base::RunLoop().RunUntilIdle();
73 // Worker started message should be notified (by EmbeddedWorkerTestHelper).
74 EXPECT_EQ(EmbeddedWorkerInstance::RUNNING, worker->status());
75 EXPECT_EQ(kRenderProcessId, worker->process_id());
77 // Stop the worker.
78 EXPECT_EQ(SERVICE_WORKER_OK, worker->Stop());
79 EXPECT_EQ(EmbeddedWorkerInstance::STOPPING, worker->status());
80 base::RunLoop().RunUntilIdle();
82 // Worker stopped message should be notified (by EmbeddedWorkerTestHelper).
83 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED, worker->status());
85 // Verify that we've sent two messages to start and terminate the worker.
86 ASSERT_TRUE(ipc_sink()->GetUniqueMessageMatching(
87 EmbeddedWorkerMsg_StartWorker::ID));
88 ASSERT_TRUE(ipc_sink()->GetUniqueMessageMatching(
89 EmbeddedWorkerMsg_StopWorker::ID));
92 TEST_F(EmbeddedWorkerInstanceTest, ChooseProcess) {
93 scoped_ptr<EmbeddedWorkerInstance> worker =
94 embedded_worker_registry()->CreateWorker();
95 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED, worker->status());
97 // Simulate adding processes to the worker.
98 // Process 1 has 1 ref, 2 has 2 refs and 3 has 3 refs.
99 const int embedded_worker_id = worker->embedded_worker_id();
100 helper_->SimulateAddProcessToWorker(embedded_worker_id, 1);
101 helper_->SimulateAddProcessToWorker(embedded_worker_id, 2);
102 helper_->SimulateAddProcessToWorker(embedded_worker_id, 2);
103 helper_->SimulateAddProcessToWorker(embedded_worker_id, 3);
104 helper_->SimulateAddProcessToWorker(embedded_worker_id, 3);
105 helper_->SimulateAddProcessToWorker(embedded_worker_id, 3);
107 // Process 3 has the biggest # of references and it should be chosen.
108 EXPECT_EQ(SERVICE_WORKER_OK,
109 worker->Start(1L, GURL("http://example.com/worker.js")));
110 EXPECT_EQ(EmbeddedWorkerInstance::STARTING, worker->status());
111 EXPECT_EQ(3, worker->process_id());
113 // Wait until started message is sent back.
114 base::RunLoop().RunUntilIdle();
115 EXPECT_EQ(EmbeddedWorkerInstance::RUNNING, worker->status());
118 } // namespace content