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

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

Issue 380093002: Update installed ServiceWorkers. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 5 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 | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 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 <string> 5 #include <string>
6 6
7 #include "base/files/scoped_temp_dir.h" 7 #include "base/files/scoped_temp_dir.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/message_loop/message_loop.h" 9 #include "base/message_loop/message_loop.h"
10 #include "base/run_loop.h" 10 #include "base/run_loop.h"
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
76 ServiceWorkerStorage::GetAllRegistrationInfosCallback MakeGetAllCallback( 76 ServiceWorkerStorage::GetAllRegistrationInfosCallback MakeGetAllCallback(
77 bool* was_called, 77 bool* was_called,
78 std::vector<ServiceWorkerRegistrationInfo>* all) { 78 std::vector<ServiceWorkerRegistrationInfo>* all) {
79 return base::Bind(&GetAllCallback, was_called, all); 79 return base::Bind(&GetAllCallback, was_called, all);
80 } 80 }
81 81
82 void OnIOComplete(int* rv_out, int rv) { 82 void OnIOComplete(int* rv_out, int rv) {
83 *rv_out = rv; 83 *rv_out = rv;
84 } 84 }
85 85
86 void WriteBasicResponse(ServiceWorkerStorage* storage, int64 id) { 86 void OnCompareComplete(
87 ServiceWorkerStatusCode* status_out, bool* are_equal_out,
88 ServiceWorkerStatusCode status, bool are_equal) {
89 *status_out = status;
90 *are_equal_out = are_equal;
91 }
92
93 void WriteResponse(
94 ServiceWorkerStorage* storage, int64 id,
95 const std::string& headers,
96 IOBuffer* body, int length) {
87 scoped_ptr<ServiceWorkerResponseWriter> writer = 97 scoped_ptr<ServiceWorkerResponseWriter> writer =
88 storage->CreateResponseWriter(id); 98 storage->CreateResponseWriter(id);
89 99
90 const char kHttpHeaders[] =
91 "HTTP/1.0 200 HONKYDORY\0Content-Length: 6\0\0";
92 const char kHttpBody[] = "Hello\0";
93 scoped_refptr<IOBuffer> body(new WrappedIOBuffer(kHttpBody));
94 std::string raw_headers(kHttpHeaders, arraysize(kHttpHeaders));
95 scoped_ptr<net::HttpResponseInfo> info(new net::HttpResponseInfo); 100 scoped_ptr<net::HttpResponseInfo> info(new net::HttpResponseInfo);
96 info->request_time = base::Time::Now(); 101 info->request_time = base::Time::Now();
97 info->response_time = base::Time::Now(); 102 info->response_time = base::Time::Now();
98 info->was_cached = false; 103 info->was_cached = false;
99 info->headers = new net::HttpResponseHeaders(raw_headers); 104 info->headers = new net::HttpResponseHeaders(headers);
100 scoped_refptr<HttpResponseInfoIOBuffer> info_buffer = 105 scoped_refptr<HttpResponseInfoIOBuffer> info_buffer =
101 new HttpResponseInfoIOBuffer(info.release()); 106 new HttpResponseInfoIOBuffer(info.release());
102 107
103 int rv = -1234; 108 int rv = -1234;
104 writer->WriteInfo(info_buffer, base::Bind(&OnIOComplete, &rv)); 109 writer->WriteInfo(info_buffer, base::Bind(&OnIOComplete, &rv));
105 base::RunLoop().RunUntilIdle(); 110 base::RunLoop().RunUntilIdle();
106 EXPECT_LT(0, rv); 111 EXPECT_LT(0, rv);
107 112
108 rv = -1234; 113 rv = -1234;
109 writer->WriteData(body, arraysize(kHttpBody), 114 writer->WriteData(body, length, base::Bind(&OnIOComplete, &rv));
110 base::Bind(&OnIOComplete, &rv));
111 base::RunLoop().RunUntilIdle(); 115 base::RunLoop().RunUntilIdle();
112 EXPECT_EQ(static_cast<int>(arraysize(kHttpBody)), rv); 116 EXPECT_EQ(length, rv);
117 }
118
119 void WriteStringResponse(
120 ServiceWorkerStorage* storage, int64 id,
121 const std::string& headers,
122 const std::string& body) {
123 scoped_refptr<IOBuffer> body_buffer(new WrappedIOBuffer(body.data()));
124 WriteResponse(storage, id, headers, body_buffer, body.length());
125 }
126
127 void WriteBasicResponse(ServiceWorkerStorage* storage, int64 id) {
128 scoped_ptr<ServiceWorkerResponseWriter> writer =
129 storage->CreateResponseWriter(id);
130
131 const char kHttpHeaders[] = "HTTP/1.0 200 HONKYDORY\0Content-Length: 5\0\0";
132 const char kHttpBody[] = "Hello";
133 std::string headers(kHttpHeaders, arraysize(kHttpHeaders));
134 WriteStringResponse(storage, id, headers, std::string(kHttpBody));
113 } 135 }
114 136
115 bool VerifyBasicResponse(ServiceWorkerStorage* storage, int64 id, 137 bool VerifyBasicResponse(ServiceWorkerStorage* storage, int64 id,
116 bool expected_positive_result) { 138 bool expected_positive_result) {
117 const char kExpectedHttpBody[] = "Hello\0"; 139 const std::string kExpectedHttpBody("Hello");
118 scoped_ptr<ServiceWorkerResponseReader> reader = 140 scoped_ptr<ServiceWorkerResponseReader> reader =
119 storage->CreateResponseReader(id); 141 storage->CreateResponseReader(id);
120 scoped_refptr<HttpResponseInfoIOBuffer> info_buffer = 142 scoped_refptr<HttpResponseInfoIOBuffer> info_buffer =
121 new HttpResponseInfoIOBuffer(); 143 new HttpResponseInfoIOBuffer();
122 { 144 {
123 TestCompletionCallback cb; 145 TestCompletionCallback cb;
124 reader->ReadInfo(info_buffer, cb.callback()); 146 reader->ReadInfo(info_buffer, cb.callback());
125 int rv = cb.WaitForResult(); 147 int rv = cb.WaitForResult();
126 if (expected_positive_result) 148 if (expected_positive_result)
127 EXPECT_LT(0, rv); 149 EXPECT_LT(0, rv);
128 if (rv <= 0) 150 if (rv <= 0)
129 return false; 151 return false;
130 } 152 }
131 153
132 const int kBigEnough = 512; 154 std::string received_body;
133 scoped_refptr<net::IOBuffer> buffer = new IOBuffer(kBigEnough);
134 { 155 {
156 const int kBigEnough = 512;
157 scoped_refptr<net::IOBuffer> buffer = new IOBuffer(kBigEnough);
135 TestCompletionCallback cb; 158 TestCompletionCallback cb;
136 reader->ReadData(buffer, kBigEnough, cb.callback()); 159 reader->ReadData(buffer, kBigEnough, cb.callback());
137 int rv = cb.WaitForResult(); 160 int rv = cb.WaitForResult();
138 EXPECT_EQ(static_cast<int>(arraysize(kExpectedHttpBody)), rv); 161 EXPECT_EQ(static_cast<int>(kExpectedHttpBody.size()), rv);
139 if (rv <= 0) 162 if (rv <= 0)
140 return false; 163 return false;
164 received_body.assign(buffer->data(), rv);
141 } 165 }
142 166
143 bool status_match = 167 bool status_match =
144 std::string("HONKYDORY") == 168 std::string("HONKYDORY") ==
145 info_buffer->http_info->headers->GetStatusText(); 169 info_buffer->http_info->headers->GetStatusText();
146 bool data_match = 170 bool data_match = kExpectedHttpBody == received_body;
147 std::string(kExpectedHttpBody) == std::string(buffer->data());
148 171
149 EXPECT_TRUE(status_match); 172 EXPECT_TRUE(status_match);
150 EXPECT_TRUE(data_match); 173 EXPECT_TRUE(data_match);
151 return status_match && data_match; 174 return status_match && data_match;
152 } 175 }
153 176
177 void WriteResponseOfSize(ServiceWorkerStorage* storage, int64 id,
178 char val, int size) {
179 const char kHttpHeaders[] = "HTTP/1.0 200 HONKYDORY\00";
180 std::string headers(kHttpHeaders, arraysize(kHttpHeaders));
181 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(size);
182 memset(buffer->data(), val, size);
183 WriteResponse(storage, id, headers, buffer, size);
184 }
185
154 } // namespace 186 } // namespace
155 187
156 class ServiceWorkerStorageTest : public testing::Test { 188 class ServiceWorkerStorageTest : public testing::Test {
157 public: 189 public:
158 ServiceWorkerStorageTest() 190 ServiceWorkerStorageTest()
159 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) { 191 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {
160 } 192 }
161 193
162 virtual void SetUp() OVERRIDE { 194 virtual void SetUp() OVERRIDE {
163 context_.reset( 195 context_.reset(
(...skipping 761 matching lines...) Expand 10 before | Expand all | Expand 10 after
925 live_registration2, NULL, SERVICE_WORKER_OK); 957 live_registration2, NULL, SERVICE_WORKER_OK);
926 storage()->NotifyDoneInstallingRegistration( 958 storage()->NotifyDoneInstallingRegistration(
927 live_registration3, NULL, SERVICE_WORKER_OK); 959 live_registration3, NULL, SERVICE_WORKER_OK);
928 960
929 // Find a registration among installed ones. 961 // Find a registration among installed ones.
930 EXPECT_EQ(SERVICE_WORKER_OK, 962 EXPECT_EQ(SERVICE_WORKER_OK,
931 FindRegistrationForDocument(kDocumentUrl, &found_registration)); 963 FindRegistrationForDocument(kDocumentUrl, &found_registration));
932 EXPECT_EQ(live_registration2, found_registration); 964 EXPECT_EQ(live_registration2, found_registration);
933 } 965 }
934 966
967 TEST_F(ServiceWorkerStorageTest, CompareResources) {
968 // Compare two small responses containing the same data.
969 WriteBasicResponse(storage(), 1);
970 WriteBasicResponse(storage(), 2);
971 ServiceWorkerStatusCode status = static_cast<ServiceWorkerStatusCode>(-1);
972 bool are_equal = false;
973 storage()->CompareScriptResources(
974 1, 2,
975 base::Bind(&OnCompareComplete, &status, &are_equal));
976 base::RunLoop().RunUntilIdle();
977 EXPECT_EQ(SERVICE_WORKER_OK, status);
978 EXPECT_TRUE(are_equal);
979
980 // Compare two small responses with different data.
981 const char kHttpHeaders[] = "HTTP/1.0 200 HONKYDORY\0\0";
982 const char kHttpBody[] = "Goodbye";
983 std::string headers(kHttpHeaders, arraysize(kHttpHeaders));
984 WriteStringResponse(storage(), 3, headers, std::string(kHttpBody));
985 status = static_cast<ServiceWorkerStatusCode>(-1);
986 are_equal = true;
987 storage()->CompareScriptResources(
988 1, 3,
989 base::Bind(&OnCompareComplete, &status, &are_equal));
990 base::RunLoop().RunUntilIdle();
991 EXPECT_EQ(SERVICE_WORKER_OK, status);
992 EXPECT_FALSE(are_equal);
993
994 // Compare two large responses with the same data.
995 const int k32K = 32 * 1024;
996 WriteResponseOfSize(storage(), 4, 'a', k32K);
997 WriteResponseOfSize(storage(), 5, 'a', k32K);
998 status = static_cast<ServiceWorkerStatusCode>(-1);
999 are_equal = false;
1000 storage()->CompareScriptResources(
1001 4, 5,
1002 base::Bind(&OnCompareComplete, &status, &are_equal));
1003 base::RunLoop().RunUntilIdle();
1004 EXPECT_EQ(SERVICE_WORKER_OK, status);
1005 EXPECT_TRUE(are_equal);
1006
1007 // Compare a large and small response.
1008 status = static_cast<ServiceWorkerStatusCode>(-1);
1009 are_equal = true;
1010 storage()->CompareScriptResources(
1011 1, 5,
1012 base::Bind(&OnCompareComplete, &status, &are_equal));
1013 base::RunLoop().RunUntilIdle();
1014 EXPECT_EQ(SERVICE_WORKER_OK, status);
1015 EXPECT_FALSE(are_equal);
1016
1017 // Compare two large responses with different data.
1018 WriteResponseOfSize(storage(), 6, 'b', k32K);
1019 status = static_cast<ServiceWorkerStatusCode>(-1);
1020 are_equal = true;
1021 storage()->CompareScriptResources(
1022 5, 6,
1023 base::Bind(&OnCompareComplete, &status, &are_equal));
1024 base::RunLoop().RunUntilIdle();
1025 EXPECT_EQ(SERVICE_WORKER_OK, status);
1026 EXPECT_FALSE(are_equal);
1027 }
1028
935 } // namespace content 1029 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698