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 ASSERT_TRUE(dir_.CreateUniqueTempDir()); | |
michaeln
2013/10/25 21:41:25
we can probably pass an empty path for these tests
alecflett
2013/10/25 23:41:56
Done.
| |
23 context_ = new ServiceWorkerContext(dir_.path(), nullptr); | |
michaeln
2013/10/25 21:41:25
NULL in chromium speak
alecflett
2013/10/25 23:41:56
Done.
| |
24 } | |
25 | |
26 virtual void TearDown() { | |
27 DCHECK(context_->HasOneRef()); | |
28 context_ = NULL; | |
29 } | |
30 | |
31 scoped_refptr<ServiceWorkerContext> context_; | |
32 base::ScopedTempDir dir_; | |
33 }; | |
34 | |
35 namespace { | |
36 | |
37 static const int kRenderProcessId = 1; | |
38 | |
39 class TestingServiceWorkerDispatcherHost : public ServiceWorkerDispatcherHost { | |
40 public: | |
41 TestingServiceWorkerDispatcherHost(int process_id, | |
42 ServiceWorkerContext* context) | |
43 : ServiceWorkerDispatcherHost(process_id, context) {} | |
44 | |
45 virtual bool Send(IPC::Message* message) OVERRIDE { | |
46 sent_messages_.push_back(message); | |
47 return true; | |
48 } | |
49 | |
50 ScopedVector<IPC::Message> sent_messages_; | |
51 | |
52 protected: | |
53 virtual ~TestingServiceWorkerDispatcherHost() {} | |
54 }; | |
55 | |
56 } // namespace | |
57 | |
58 TEST_F(ServiceWorkerDispatcherHostTest, DisabledCausesError) { | |
59 DCHECK(!CommandLine::ForCurrentProcess()->HasSwitch( | |
60 switches::kEnableServiceWorker)); | |
61 | |
62 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host = | |
63 new TestingServiceWorkerDispatcherHost(kRenderProcessId, context_); | |
64 | |
65 ChildProcessSecurityPolicyImpl* policy = | |
66 ChildProcessSecurityPolicyImpl::GetInstance(); | |
67 policy->Add(kRenderProcessId); | |
68 | |
69 bool handled; | |
70 dispatcher_host->OnMessageReceived( | |
71 ServiceWorkerHostMsg_RegisterServiceWorker(-1, -1, GURL(), GURL()), | |
72 &handled); | |
73 DCHECK(handled); | |
74 | |
75 // TODO(alecflett): Pump the message loop when this becomes async. | |
76 DCHECK_EQ(1UL, dispatcher_host->sent_messages_.size()); | |
77 DCHECK_EQ(ServiceWorkerMsg_ServiceWorkerRegistered::ID, | |
78 dispatcher_host->sent_messages_[0]->type()); | |
79 } | |
80 | |
81 TEST_F(ServiceWorkerDispatcherHostTest, Enabled) { | |
82 DCHECK(!CommandLine::ForCurrentProcess()->HasSwitch( | |
83 switches::kEnableServiceWorker)); | |
84 CommandLine::ForCurrentProcess()->AppendSwitch( | |
85 switches::kEnableServiceWorker); | |
86 | |
87 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host = | |
88 new TestingServiceWorkerDispatcherHost(kRenderProcessId, context_); | |
89 | |
90 ChildProcessSecurityPolicyImpl* policy = | |
91 ChildProcessSecurityPolicyImpl::GetInstance(); | |
92 policy->Add(kRenderProcessId); | |
93 | |
94 bool handled; | |
95 dispatcher_host->OnMessageReceived( | |
96 ServiceWorkerHostMsg_RegisterServiceWorker(-1, -1, GURL(), GURL()), | |
97 &handled); | |
98 DCHECK(handled); | |
99 | |
100 // TODO(alecflett): Pump the message loop when this becomes async. | |
101 DCHECK_EQ(1UL, dispatcher_host->sent_messages_.size()); | |
102 DCHECK_EQ(ServiceWorkerMsg_ServiceWorkerRegistrationError::ID, | |
103 dispatcher_host->sent_messages_[0]->type()); | |
104 } | |
105 | |
106 } // namespace content | |
OLD | NEW |