Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(707)

Side by Side Diff: content/browser/service_worker/service_worker_version_unittest.cc

Issue 1406823002: Start of foreign fetch implementation. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: remove blank line Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/basictypes.h" 5 #include "base/basictypes.h"
6 #include "base/run_loop.h" 6 #include "base/run_loop.h"
7 #include "content/browser/message_port_service.h" 7 #include "content/browser/message_port_service.h"
8 #include "content/browser/service_worker/embedded_worker_registry.h" 8 #include "content/browser/service_worker/embedded_worker_registry.h"
9 #include "content/browser/service_worker/embedded_worker_test_helper.h" 9 #include "content/browser/service_worker/embedded_worker_test_helper.h"
10 #include "content/browser/service_worker/service_worker_context_core.h" 10 #include "content/browser/service_worker/service_worker_context_core.h"
11 #include "content/browser/service_worker/service_worker_registration.h" 11 #include "content/browser/service_worker/service_worker_registration.h"
12 #include "content/browser/service_worker/service_worker_test_utils.h" 12 #include "content/browser/service_worker/service_worker_test_utils.h"
13 #include "content/browser/service_worker/service_worker_version.h" 13 #include "content/browser/service_worker/service_worker_version.h"
14 #include "content/common/service_worker/service_worker_utils.h" 14 #include "content/common/service_worker/service_worker_utils.h"
15 #include "content/public/test/mock_render_process_host.h"
15 #include "content/public/test/test_browser_thread_bundle.h" 16 #include "content/public/test/test_browser_thread_bundle.h"
16 #include "testing/gtest/include/gtest/gtest.h" 17 #include "testing/gtest/include/gtest/gtest.h"
17 18
18 // IPC messages for testing --------------------------------------------------- 19 // IPC messages for testing ---------------------------------------------------
19 20
20 #define IPC_MESSAGE_IMPL 21 #define IPC_MESSAGE_IMPL
21 #include "ipc/ipc_message_macros.h" 22 #include "ipc/ipc_message_macros.h"
22 23
23 #define IPC_MESSAGE_START TestMsgStart 24 #define IPC_MESSAGE_START TestMsgStart
24 25
25 IPC_MESSAGE_CONTROL0(TestMsg_Message); 26 IPC_MESSAGE_CONTROL0(TestMsg_Message);
26 IPC_MESSAGE_ROUTED1(TestMsg_MessageFromWorker, int); 27 IPC_MESSAGE_ROUTED1(TestMsg_MessageFromWorker, int);
27 28
28 // --------------------------------------------------------------------------- 29 // ---------------------------------------------------------------------------
29 30
30 namespace content { 31 namespace content {
31 32
32 namespace { 33 namespace {
33 34
34 static const int kRenderProcessId = 1;
35
36 class MessageReceiver : public EmbeddedWorkerTestHelper { 35 class MessageReceiver : public EmbeddedWorkerTestHelper {
37 public: 36 public:
38 MessageReceiver() 37 MessageReceiver()
39 : EmbeddedWorkerTestHelper(base::FilePath(), kRenderProcessId), 38 : EmbeddedWorkerTestHelper(base::FilePath()),
40 current_embedded_worker_id_(0) {} 39 current_embedded_worker_id_(0) {}
41 ~MessageReceiver() override {} 40 ~MessageReceiver() override {}
42 41
43 bool OnMessageToWorker(int thread_id, 42 bool OnMessageToWorker(int thread_id,
44 int embedded_worker_id, 43 int embedded_worker_id,
45 const IPC::Message& message) override { 44 const IPC::Message& message) override {
46 if (EmbeddedWorkerTestHelper::OnMessageToWorker( 45 if (EmbeddedWorkerTestHelper::OnMessageToWorker(
47 thread_id, embedded_worker_id, message)) { 46 thread_id, embedded_worker_id, message)) {
48 return true; 47 return true;
49 } 48 }
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
144 } 143 }
145 ServiceWorkerVersion::RunningStatus last_status; 144 ServiceWorkerVersion::RunningStatus last_status;
146 }; 145 };
147 146
148 ServiceWorkerVersionTest() 147 ServiceWorkerVersionTest()
149 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} 148 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {}
150 149
151 void SetUp() override { 150 void SetUp() override {
152 helper_ = GetMessageReceiver(); 151 helper_ = GetMessageReceiver();
153 152
154 pattern_ = GURL("http://www.example.com/"); 153 pattern_ = GURL("http://www.example.com/test/");
155 registration_ = new ServiceWorkerRegistration( 154 registration_ = new ServiceWorkerRegistration(
156 pattern_, 155 pattern_,
157 1L, 156 1L,
158 helper_->context()->AsWeakPtr()); 157 helper_->context()->AsWeakPtr());
159 version_ = new ServiceWorkerVersion( 158 version_ = new ServiceWorkerVersion(
160 registration_.get(), 159 registration_.get(),
161 GURL("http://www.example.com/service_worker.js"), 160 GURL("http://www.example.com/test/service_worker.js"), 1L,
162 1L,
163 helper_->context()->AsWeakPtr()); 161 helper_->context()->AsWeakPtr());
164 std::vector<ServiceWorkerDatabase::ResourceRecord> records; 162 std::vector<ServiceWorkerDatabase::ResourceRecord> records;
165 records.push_back( 163 records.push_back(
166 ServiceWorkerDatabase::ResourceRecord(10, version_->script_url(), 100)); 164 ServiceWorkerDatabase::ResourceRecord(10, version_->script_url(), 100));
167 version_->script_cache_map()->SetResources(records); 165 version_->script_cache_map()->SetResources(records);
168 166
169 // Make the registration findable via storage functions. 167 // Make the registration findable via storage functions.
170 helper_->context()->storage()->LazyInitialize(base::Bind(&base::DoNothing)); 168 helper_->context()->storage()->LazyInitialize(base::Bind(&base::DoNothing));
171 base::RunLoop().RunUntilIdle(); 169 base::RunLoop().RunUntilIdle();
172 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED; 170 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED;
173 helper_->context()->storage()->StoreRegistration( 171 helper_->context()->storage()->StoreRegistration(
174 registration_.get(), 172 registration_.get(),
175 version_.get(), 173 version_.get(),
176 CreateReceiverOnCurrentThread(&status)); 174 CreateReceiverOnCurrentThread(&status));
177 base::RunLoop().RunUntilIdle(); 175 base::RunLoop().RunUntilIdle();
178 ASSERT_EQ(SERVICE_WORKER_OK, status); 176 ASSERT_EQ(SERVICE_WORKER_OK, status);
179 177
180 // Simulate adding one process to the pattern. 178 // Simulate adding one process to the pattern.
181 helper_->SimulateAddProcessToPattern(pattern_, kRenderProcessId); 179 helper_->SimulateAddProcessToPattern(pattern_,
180 helper_->mock_render_process_id());
182 ASSERT_TRUE(helper_->context()->process_manager() 181 ASSERT_TRUE(helper_->context()->process_manager()
183 ->PatternHasProcessToRun(pattern_)); 182 ->PatternHasProcessToRun(pattern_));
184 } 183 }
185 184
186 virtual scoped_ptr<MessageReceiver> GetMessageReceiver() { 185 virtual scoped_ptr<MessageReceiver> GetMessageReceiver() {
187 return make_scoped_ptr(new MessageReceiver()); 186 return make_scoped_ptr(new MessageReceiver());
188 } 187 }
189 188
190 void TearDown() override { 189 void TearDown() override {
191 version_ = 0; 190 version_ = 0;
(...skipping 669 matching lines...) Expand 10 before | Expand all | Expand 10 after
861 base::TimeTicks::Now() - 860 base::TimeTicks::Now() -
862 base::TimeDelta::FromSeconds( 861 base::TimeDelta::FromSeconds(
863 ServiceWorkerVersion::kStopWorkerTimeoutSeconds + 1); 862 ServiceWorkerVersion::kStopWorkerTimeoutSeconds + 1);
864 version_->timeout_timer_.user_task().Run(); 863 version_->timeout_timer_.user_task().Run();
865 base::RunLoop().RunUntilIdle(); 864 base::RunLoop().RunUntilIdle();
866 EXPECT_EQ(SERVICE_WORKER_OK, status); 865 EXPECT_EQ(SERVICE_WORKER_OK, status);
867 EXPECT_EQ(SERVICE_WORKER_OK, start_status); 866 EXPECT_EQ(SERVICE_WORKER_OK, start_status);
868 EXPECT_EQ(ServiceWorkerVersion::RUNNING, version_->running_status()); 867 EXPECT_EQ(ServiceWorkerVersion::RUNNING, version_->running_status());
869 } 868 }
870 869
870 TEST_F(ServiceWorkerVersionTest, RegisterForeignFetchScopes) {
871 version_->SetStatus(ServiceWorkerVersion::INSTALLING);
872 // Start a worker.
873 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED;
874 version_->StartWorker(CreateReceiverOnCurrentThread(&status));
875 base::RunLoop().RunUntilIdle();
876 EXPECT_EQ(SERVICE_WORKER_OK, status);
877 EXPECT_EQ(ServiceWorkerVersion::RUNNING, version_->running_status());
878 EXPECT_EQ(0, helper_->mock_render_process_host()->bad_msg_count());
879
880 // Invalid URL, should kill worker (but in tests will only increase bad
881 // message count).
882 version_->OnRegisterForeignFetchScopes(std::vector<GURL>(1, GURL()));
883 base::RunLoop().RunUntilIdle();
884 EXPECT_EQ(1, helper_->mock_render_process_host()->bad_msg_count());
885 EXPECT_EQ(0u, version_->foreign_fetch_scopes_.size());
886
887 // URL outside the scope of the worker.
888 version_->OnRegisterForeignFetchScopes(
889 std::vector<GURL>(1, GURL("http://www.example.com/wrong")));
890 base::RunLoop().RunUntilIdle();
891 EXPECT_EQ(2, helper_->mock_render_process_host()->bad_msg_count());
892 EXPECT_EQ(0u, version_->foreign_fetch_scopes_.size());
893
894 // URL on wrong origin.
895 version_->OnRegisterForeignFetchScopes(
896 std::vector<GURL>(1, GURL("http://example.com/test/")));
897 base::RunLoop().RunUntilIdle();
898 EXPECT_EQ(3, helper_->mock_render_process_host()->bad_msg_count());
899 EXPECT_EQ(0u, version_->foreign_fetch_scopes_.size());
900
901 // Valid URL 1.
902 GURL valid_scope_1("http://www.example.com/test/subscope");
903 version_->OnRegisterForeignFetchScopes(std::vector<GURL>(1, valid_scope_1));
904 base::RunLoop().RunUntilIdle();
905 EXPECT_EQ(3, helper_->mock_render_process_host()->bad_msg_count());
906 EXPECT_EQ(1u, version_->foreign_fetch_scopes_.size());
907 EXPECT_EQ(valid_scope_1, version_->foreign_fetch_scopes_[0]);
908
909 // Valid URL 2.
910 GURL valid_scope_2("http://www.example.com/test/subscope");
911 version_->OnRegisterForeignFetchScopes(std::vector<GURL>(1, valid_scope_2));
912 base::RunLoop().RunUntilIdle();
913 EXPECT_EQ(3, helper_->mock_render_process_host()->bad_msg_count());
914 EXPECT_EQ(2u, version_->foreign_fetch_scopes_.size());
915 EXPECT_EQ(valid_scope_1, version_->foreign_fetch_scopes_[0]);
916 EXPECT_EQ(valid_scope_2, version_->foreign_fetch_scopes_[1]);
917 }
918
871 } // namespace content 919 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/service_worker/service_worker_version.cc ('k') | content/common/service_worker/service_worker_messages.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698