OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 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 "content/browser/service_worker/service_worker_dispatcher_host.h" | |
6 | |
7 #include "base/command_line.h" | |
8 #include "base/files/scoped_temp_dir.h" | |
9 #include "content/browser/child_process_security_policy_impl.h" | |
10 #include "content/browser/service_worker/service_worker_context.h" | |
11 #include "content/common/service_worker_messages.h" | |
12 #include "content/public/common/content_switches.h" | |
13 #include "testing/gtest/include/gtest/gtest.h" | |
14 | |
15 using base::MessageLoop; | |
16 | |
17 namespace content { | |
18 | |
19 class ServiceWorkerDispatcherHostTest : public testing::Test { | |
20 protected: | |
21 virtual void SetUp() { | |
22 context_ = new ServiceWorkerContext(dir_.path(), NULL); | |
michaeln
2013/10/26 00:57:40
dir_ isn't needed now that there is no dir anymore
alecflett
2013/10/28 17:03:14
Done.
| |
23 } | |
24 | |
25 virtual void TearDown() { | |
26 DCHECK(context_->HasOneRef()); | |
27 context_ = NULL; | |
28 } | |
29 | |
30 scoped_refptr<ServiceWorkerContext> context_; | |
31 base::ScopedTempDir dir_; | |
32 }; | |
33 | |
34 namespace { | |
35 | |
36 static const int kRenderProcessId = 1; | |
37 | |
38 class TestingServiceWorkerDispatcherHost : public ServiceWorkerDispatcherHost { | |
39 public: | |
40 TestingServiceWorkerDispatcherHost(int process_id, | |
41 ServiceWorkerContext* context) | |
42 : ServiceWorkerDispatcherHost(process_id, context) {} | |
43 | |
44 virtual bool Send(IPC::Message* message) OVERRIDE { | |
45 sent_messages_.push_back(message); | |
46 return true; | |
47 } | |
48 | |
49 ScopedVector<IPC::Message> sent_messages_; | |
50 | |
51 protected: | |
52 virtual ~TestingServiceWorkerDispatcherHost() {} | |
53 }; | |
54 | |
55 } // namespace | |
56 | |
57 TEST_F(ServiceWorkerDispatcherHostTest, DisabledCausesError) { | |
58 DCHECK(!CommandLine::ForCurrentProcess()->HasSwitch( | |
59 switches::kEnableServiceWorker)); | |
60 | |
61 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host = | |
62 new TestingServiceWorkerDispatcherHost(kRenderProcessId, context_); | |
63 | |
64 ChildProcessSecurityPolicyImpl* policy = | |
65 ChildProcessSecurityPolicyImpl::GetInstance(); | |
66 policy->Add(kRenderProcessId); | |
67 | |
68 bool handled; | |
69 dispatcher_host->OnMessageReceived( | |
70 ServiceWorkerHostMsg_RegisterServiceWorker(-1, -1, GURL(), GURL()), | |
71 &handled); | |
72 DCHECK(handled); | |
73 | |
74 // TODO(alecflett): Pump the message loop when this becomes async. | |
75 DCHECK_EQ(1UL, dispatcher_host->sent_messages_.size()); | |
76 DCHECK_EQ(ServiceWorkerMsg_ServiceWorkerRegistered::ID, | |
77 dispatcher_host->sent_messages_[0]->type()); | |
78 } | |
79 | |
80 TEST_F(ServiceWorkerDispatcherHostTest, Enabled) { | |
81 DCHECK(!CommandLine::ForCurrentProcess()->HasSwitch( | |
82 switches::kEnableServiceWorker)); | |
83 CommandLine::ForCurrentProcess()->AppendSwitch( | |
84 switches::kEnableServiceWorker); | |
85 | |
86 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host = | |
87 new TestingServiceWorkerDispatcherHost(kRenderProcessId, context_); | |
88 | |
89 ChildProcessSecurityPolicyImpl* policy = | |
90 ChildProcessSecurityPolicyImpl::GetInstance(); | |
91 policy->Add(kRenderProcessId); | |
92 | |
93 bool handled; | |
94 dispatcher_host->OnMessageReceived( | |
95 ServiceWorkerHostMsg_RegisterServiceWorker(-1, -1, GURL(), GURL()), | |
96 &handled); | |
97 DCHECK(handled); | |
98 | |
99 // TODO(alecflett): Pump the message loop when this becomes async. | |
100 DCHECK_EQ(1UL, dispatcher_host->sent_messages_.size()); | |
101 DCHECK_EQ(ServiceWorkerMsg_ServiceWorkerRegistrationError::ID, | |
102 dispatcher_host->sent_messages_[0]->type()); | |
103 } | |
104 | |
105 } // namespace content | |
OLD | NEW |