| OLD | NEW |
| (Empty) | |
| 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. |
| 4 |
| 5 #include "base/basictypes.h" |
| 6 #include "base/stl_util.h" |
| 7 #include "content/browser/service_worker/embedded_worker_instance.h" |
| 8 #include "content/browser/service_worker/embedded_worker_registry.h" |
| 9 #include "content/browser/service_worker/service_worker_context_core.h" |
| 10 #include "content/common/service_worker_messages.h" |
| 11 #include "content/public/test/test_browser_thread_bundle.h" |
| 12 #include "ipc/ipc_message.h" |
| 13 #include "ipc/ipc_sender.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 |
| 16 namespace content { |
| 17 |
| 18 namespace { |
| 19 |
| 20 typedef std::vector<IPC::Message*> MessageList; |
| 21 |
| 22 class FakeSender : public IPC::Sender { |
| 23 public: |
| 24 FakeSender() {} |
| 25 virtual ~FakeSender() { |
| 26 STLDeleteContainerPointers(sent_messages_.begin(), sent_messages_.end()); |
| 27 } |
| 28 |
| 29 // IPC::Sender implementation. |
| 30 virtual bool Send(IPC::Message* message) OVERRIDE { |
| 31 sent_messages_.push_back(message); |
| 32 return true; |
| 33 } |
| 34 |
| 35 const MessageList& sent_messages() { return sent_messages_; } |
| 36 |
| 37 private: |
| 38 MessageList sent_messages_; |
| 39 DISALLOW_COPY_AND_ASSIGN(FakeSender); |
| 40 }; |
| 41 |
| 42 } // namespace |
| 43 |
| 44 class EmbeddedWorkerInstanceTest : public testing::Test { |
| 45 protected: |
| 46 EmbeddedWorkerInstanceTest() |
| 47 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} |
| 48 |
| 49 virtual void SetUp() OVERRIDE { |
| 50 context_.reset(new ServiceWorkerContextCore(base::FilePath(), NULL)); |
| 51 } |
| 52 |
| 53 virtual void TearDown() OVERRIDE { |
| 54 context_.reset(); |
| 55 } |
| 56 |
| 57 EmbeddedWorkerRegistry* embedded_worker_registry() { |
| 58 DCHECK(context_); |
| 59 return context_->embedded_worker_registry(); |
| 60 } |
| 61 |
| 62 TestBrowserThreadBundle thread_bundle_; |
| 63 scoped_ptr<ServiceWorkerContextCore> context_; |
| 64 |
| 65 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerInstanceTest); |
| 66 }; |
| 67 |
| 68 TEST_F(EmbeddedWorkerInstanceTest, StartAndStop) { |
| 69 scoped_ptr<EmbeddedWorkerInstance> worker = |
| 70 embedded_worker_registry()->CreateWorker(); |
| 71 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED, worker->status()); |
| 72 |
| 73 FakeSender fake_sender; |
| 74 const int process_id = 11; |
| 75 const int thread_id = 33; |
| 76 const int64 service_worker_version_id = 55L; |
| 77 const GURL url("http://example.com/worker.js"); |
| 78 |
| 79 // This fails as we have no available process yet. |
| 80 EXPECT_FALSE(worker->Start(service_worker_version_id, url)); |
| 81 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED, worker->status()); |
| 82 |
| 83 // Simulate adding one process to the worker. |
| 84 worker->AddProcessReference(process_id); |
| 85 embedded_worker_registry()->AddChildProcessSender(process_id, &fake_sender); |
| 86 |
| 87 // Start should succeed. |
| 88 EXPECT_TRUE(worker->Start(service_worker_version_id, url)); |
| 89 EXPECT_EQ(EmbeddedWorkerInstance::STARTING, worker->status()); |
| 90 |
| 91 // Simulate an upcall from embedded worker to notify that it's started. |
| 92 worker->OnStarted(thread_id); |
| 93 EXPECT_EQ(EmbeddedWorkerInstance::RUNNING, worker->status()); |
| 94 EXPECT_EQ(process_id, worker->process_id()); |
| 95 EXPECT_EQ(thread_id, worker->thread_id()); |
| 96 |
| 97 // Stop the worker. |
| 98 EXPECT_TRUE(worker->Stop()); |
| 99 EXPECT_EQ(EmbeddedWorkerInstance::STOPPING, worker->status()); |
| 100 |
| 101 // Simulate an upcall from embedded worker to notify that it's stopped. |
| 102 worker->OnStopped(); |
| 103 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED, worker->status()); |
| 104 |
| 105 // Verify that we've sent two messages to start and terminate the worker. |
| 106 const MessageList& messages = fake_sender.sent_messages(); |
| 107 ASSERT_EQ(2U, messages.size()); |
| 108 ASSERT_EQ(ServiceWorkerMsg_StartWorker::ID, messages[0]->type()); |
| 109 ASSERT_EQ(ServiceWorkerMsg_TerminateWorker::ID, messages[1]->type()); |
| 110 } |
| 111 |
| 112 TEST_F(EmbeddedWorkerInstanceTest, ChooseProcess) { |
| 113 scoped_ptr<EmbeddedWorkerInstance> worker = |
| 114 embedded_worker_registry()->CreateWorker(); |
| 115 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED, worker->status()); |
| 116 |
| 117 FakeSender fake_sender; |
| 118 |
| 119 // Simulate adding processes to the worker. |
| 120 // Process 1 has 1 ref, 2 has 2 refs and 3 has 3 refs. |
| 121 worker->AddProcessReference(1); |
| 122 worker->AddProcessReference(2); |
| 123 worker->AddProcessReference(2); |
| 124 worker->AddProcessReference(3); |
| 125 worker->AddProcessReference(3); |
| 126 worker->AddProcessReference(3); |
| 127 embedded_worker_registry()->AddChildProcessSender(1, &fake_sender); |
| 128 embedded_worker_registry()->AddChildProcessSender(2, &fake_sender); |
| 129 embedded_worker_registry()->AddChildProcessSender(3, &fake_sender); |
| 130 |
| 131 // Process 3 has the biggest # of references and it should be chosen. |
| 132 EXPECT_TRUE(worker->Start(1L, GURL("http://example.com/worker.js"))); |
| 133 EXPECT_EQ(EmbeddedWorkerInstance::STARTING, worker->status()); |
| 134 EXPECT_EQ(3, worker->process_id()); |
| 135 } |
| 136 |
| 137 } // namespace content |
| OLD | NEW |