OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "chrome/browser/android/webapk/webapk_installer.h" | 5 #include "chrome/browser/android/webapk/webapk_installer.h" |
6 | 6 |
7 #include <jni.h> | 7 #include <jni.h> |
8 #include <memory> | 8 #include <memory> |
9 #include <string> | 9 #include <string> |
10 | 10 |
(...skipping 18 matching lines...) Expand all Loading... | |
29 #include "testing/gtest/include/gtest/gtest.h" | 29 #include "testing/gtest/include/gtest/gtest.h" |
30 #include "third_party/skia/include/core/SkBitmap.h" | 30 #include "third_party/skia/include/core/SkBitmap.h" |
31 #include "url/gurl.h" | 31 #include "url/gurl.h" |
32 | 32 |
33 namespace { | 33 namespace { |
34 | 34 |
35 const base::FilePath::CharType kTestDataDir[] = | 35 const base::FilePath::CharType kTestDataDir[] = |
36 FILE_PATH_LITERAL("chrome/test/data"); | 36 FILE_PATH_LITERAL("chrome/test/data"); |
37 | 37 |
38 // URL of mock WebAPK server. | 38 // URL of mock WebAPK server. |
39 const std::string kServerUrl = "/webapkserver"; | 39 const std::string kServerUrl = "/webapkserver/"; |
40 | |
41 // The response format type expected from the WebAPK server. | |
42 const std::string kWebApkServerUrlResponseType = "?alt=proto"; | |
40 | 43 |
41 // URL of file to download from the WebAPK server. We use a random file in the | 44 // URL of file to download from the WebAPK server. We use a random file in the |
42 // test data directory. | 45 // test data directory. |
43 const std::string kDownloadUrl = "/simple.html"; | 46 const std::string kDownloadUrl = "/simple.html"; |
44 | 47 |
45 // The package name of the downloaded WebAPK. | 48 // The package name of the downloaded WebAPK. |
46 const std::string kDownloadedWebApkPackageName = "party.unicode"; | 49 const std::string kDownloadedWebApkPackageName = "party.unicode"; |
47 | 50 |
48 // WebApkInstaller subclass where | 51 // WebApkInstaller subclass where |
49 // WebApkInstaller::StartDownloadedWebApkInstall() is stubbed out. | 52 // WebApkInstaller::StartInstallingDownloadedWebApk() is stubbed out. |
pkotwicz
2016/08/11 22:02:45
Nit: "WebApkInstaller::StartInstallingDownloadedWe
Xi Han
2016/08/15 21:38:45
Done.
| |
53 // WebApkInstaller::StartUpdateUsingDownloadedWebApk() is stubbed out. | |
50 class TestWebApkInstaller : public WebApkInstaller { | 54 class TestWebApkInstaller : public WebApkInstaller { |
51 public: | 55 public: |
52 TestWebApkInstaller(const ShortcutInfo& shortcut_info, | 56 TestWebApkInstaller(const ShortcutInfo& shortcut_info, |
53 const SkBitmap& shortcut_icon) | 57 const SkBitmap& shortcut_icon) |
54 : WebApkInstaller(shortcut_info, shortcut_icon) {} | 58 : WebApkInstaller(shortcut_info, shortcut_icon) {} |
55 | 59 |
56 bool StartDownloadedWebApkInstall( | 60 bool StartInstallingDownloadedWebApk( |
57 JNIEnv* env, | 61 JNIEnv* env, |
58 const base::android::ScopedJavaLocalRef<jstring>& file_path, | 62 const base::android::ScopedJavaLocalRef<jstring>& file_path, |
59 const base::android::ScopedJavaLocalRef<jstring>& package_name) override { | 63 const base::android::ScopedJavaLocalRef<jstring>& package_name) override { |
64 return true; | |
65 } | |
66 | |
67 bool StartUpdateUsingDownloadedWebApk( | |
68 JNIEnv* env, | |
69 const base::android::ScopedJavaLocalRef<jstring>& file_path, | |
70 const base::android::ScopedJavaLocalRef<jstring>& package_name) override { | |
60 return true; | 71 return true; |
61 } | 72 } |
62 | 73 |
63 private: | 74 private: |
64 DISALLOW_COPY_AND_ASSIGN(TestWebApkInstaller); | 75 DISALLOW_COPY_AND_ASSIGN(TestWebApkInstaller); |
65 }; | 76 }; |
66 | 77 |
67 // Runs the WebApkInstaller installation process and blocks till done. | 78 // Runs the WebApkInstaller installation process and blocks till done. |
pkotwicz
2016/08/11 22:02:44
Nit: "installation" -> "installation/update"
Xi Han
2016/08/15 21:38:45
Done.
| |
68 class WebApkInstallerRunner { | 79 class WebApkInstallerRunner { |
69 public: | 80 public: |
70 WebApkInstallerRunner() | 81 WebApkInstallerRunner() |
71 : url_request_context_getter_(new net::TestURLRequestContextGetter( | 82 : url_request_context_getter_(new net::TestURLRequestContextGetter( |
72 base::ThreadTaskRunnerHandle::Get())) {} | 83 base::ThreadTaskRunnerHandle::Get())) {} |
73 ~WebApkInstallerRunner() {} | 84 ~WebApkInstallerRunner() {} |
74 | 85 |
75 void Run() { | 86 void RunCreateWebApk() { |
76 ShortcutInfo info(GURL::EmptyGURL()); | 87 WebApkInstaller* installer = createWebApkInstaller(); |
77 | 88 |
78 // WebApkInstaller owns itself. | |
79 WebApkInstaller* installer = new TestWebApkInstaller(info, SkBitmap()); | |
80 | |
81 installer->SetTimeoutMs(100); | |
82 installer->InstallAsyncWithURLRequestContextGetter( | 89 installer->InstallAsyncWithURLRequestContextGetter( |
83 url_request_context_getter_.get(), | 90 url_request_context_getter_.get(), |
84 base::Bind(&WebApkInstallerRunner::OnCompleted, | 91 base::Bind(&WebApkInstallerRunner::OnCompleted, |
85 base::Unretained(this))); | 92 base::Unretained(this))); |
86 | 93 |
94 setupCallback(); | |
95 } | |
96 | |
97 void RunUpdateWebApk() { | |
pkotwicz
2016/08/11 22:02:44
Nit: "int webapk_version" -> "const int kWebApkVer
Xi Han
2016/08/15 21:38:45
Done.
| |
98 int webapk_version = 1; | |
99 | |
100 // WebApkInstaller owns itself. | |
101 WebApkInstaller* installer = createWebApkInstaller(); | |
102 | |
103 installer->UpdateAsyncWithURLRequestContextGetter( | |
104 url_request_context_getter_.get(), | |
105 base::Bind(&WebApkInstallerRunner::OnCompleted, base::Unretained(this)), | |
106 kDownloadedWebApkPackageName, webapk_version); | |
107 | |
108 setupCallback(); | |
109 } | |
110 | |
111 WebApkInstaller* createWebApkInstaller() { | |
pkotwicz
2016/08/11 22:02:44
Functions start with uppercase in C++
Xi Han
2016/08/15 21:38:45
Done.
| |
112 ShortcutInfo info(GURL::EmptyGURL()); | |
113 | |
114 // WebApkInstaller owns itself. | |
115 WebApkInstaller* installer = new TestWebApkInstaller(info, SkBitmap()); | |
116 installer->SetTimeoutMs(100); | |
117 return installer; | |
118 } | |
119 | |
120 void setupCallback() { | |
pkotwicz
2016/08/11 22:02:45
I think that Run() would be a more appropriate nam
Xi Han
2016/08/15 21:38:45
Done.
| |
87 base::RunLoop run_loop; | 121 base::RunLoop run_loop; |
88 on_completed_callback_ = run_loop.QuitClosure(); | 122 on_completed_callback_ = run_loop.QuitClosure(); |
89 run_loop.Run(); | 123 run_loop.Run(); |
90 } | 124 } |
91 | 125 |
92 bool success() { return success_; } | 126 bool success() { return success_; } |
93 | 127 |
94 private: | 128 private: |
95 void OnCompleted(bool success) { | 129 void OnCompleted(bool success) { |
96 success_ = success; | 130 success_ = success; |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
133 public: | 167 public: |
134 typedef base::Callback<std::unique_ptr<net::test_server::HttpResponse>(void)> | 168 typedef base::Callback<std::unique_ptr<net::test_server::HttpResponse>(void)> |
135 WebApkResponseBuilder; | 169 WebApkResponseBuilder; |
136 | 170 |
137 WebApkInstallerTest() | 171 WebApkInstallerTest() |
138 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {} | 172 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {} |
139 ~WebApkInstallerTest() override {} | 173 ~WebApkInstallerTest() override {} |
140 | 174 |
141 void SetUp() override { | 175 void SetUp() override { |
142 test_server_.AddDefaultHandlers(base::FilePath(kTestDataDir)); | 176 test_server_.AddDefaultHandlers(base::FilePath(kTestDataDir)); |
143 test_server_.RegisterRequestHandler( | 177 test_server_.RegisterRequestHandler(base::Bind( |
144 base::Bind(&WebApkInstallerTest::HandleCreateWebApkRequest, | 178 &WebApkInstallerTest::HandleWebApkRequest, base::Unretained(this))); |
145 base::Unretained(this))); | |
146 ASSERT_TRUE(test_server_.Start()); | 179 ASSERT_TRUE(test_server_.Start()); |
147 | 180 |
148 SetDefaults(); | 181 SetDefaults(); |
149 } | 182 } |
150 | 183 |
151 // Sets the URL to send the webapk::CreateWebApkRequest to. WebApkInstaller | 184 // Sets the URL to send the webapk::CreateWebApkRequest to. WebApkInstaller |
152 // should fail if the URL is not |kServerUrl|. | 185 // should fail if the URL is not |kServerUrl|. |
153 void SetWebApkServerUrl(const GURL& server_url) { | 186 void SetWebApkServerUrl(const GURL& server_url) { |
154 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( | 187 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
155 switches::kWebApkServerUrl, server_url.spec()); | 188 switches::kWebApkServerUrl, server_url.spec()); |
(...skipping 10 matching lines...) Expand all Loading... | |
166 private: | 199 private: |
167 // Sets default configuration for running WebApkInstaller. | 200 // Sets default configuration for running WebApkInstaller. |
168 void SetDefaults() { | 201 void SetDefaults() { |
169 GURL server_url = test_server_.GetURL(kServerUrl); | 202 GURL server_url = test_server_.GetURL(kServerUrl); |
170 SetWebApkServerUrl(server_url); | 203 SetWebApkServerUrl(server_url); |
171 GURL download_url = test_server_.GetURL(kDownloadUrl); | 204 GURL download_url = test_server_.GetURL(kDownloadUrl); |
172 SetWebApkResponseBuilder( | 205 SetWebApkResponseBuilder( |
173 base::Bind(&BuildValidWebApkResponse, download_url)); | 206 base::Bind(&BuildValidWebApkResponse, download_url)); |
174 } | 207 } |
175 | 208 |
176 std::unique_ptr<net::test_server::HttpResponse> HandleCreateWebApkRequest( | 209 std::unique_ptr<net::test_server::HttpResponse> HandleWebApkRequest( |
177 const net::test_server::HttpRequest& request) { | 210 const net::test_server::HttpRequest& request) { |
178 return (request.relative_url == kServerUrl) | 211 return (request.relative_url == GetServerUrlForCreateWebApk() || |
212 request.relative_url == GetServerUrlForUpdateWebApk()) | |
179 ? webapk_response_builder_.Run() | 213 ? webapk_response_builder_.Run() |
180 : std::unique_ptr<net::test_server::HttpResponse>(); | 214 : std::unique_ptr<net::test_server::HttpResponse>(); |
181 } | 215 } |
182 | 216 |
217 std::string GetServerUrlForCreateWebApk() { | |
pkotwicz
2016/08/11 22:02:44
Nit: This should be a const function
Xi Han
2016/08/15 21:38:45
Done.
| |
218 return kServerUrl + kWebApkServerUrlResponseType; | |
219 } | |
220 | |
221 std::string GetServerUrlForUpdateWebApk() { | |
pkotwicz
2016/08/11 22:02:44
Nit: This should be a const function
Xi Han
2016/08/15 21:38:45
Done.
| |
222 return kServerUrl + kDownloadedWebApkPackageName + "/" + | |
223 kWebApkServerUrlResponseType; | |
224 } | |
225 | |
183 content::TestBrowserThreadBundle thread_bundle_; | 226 content::TestBrowserThreadBundle thread_bundle_; |
184 net::EmbeddedTestServer test_server_; | 227 net::EmbeddedTestServer test_server_; |
185 | 228 |
186 // Builds response to the WebAPK creation request. | 229 // Builds response to the WebAPK creation request. |
187 WebApkResponseBuilder webapk_response_builder_; | 230 WebApkResponseBuilder webapk_response_builder_; |
188 | 231 |
189 DISALLOW_COPY_AND_ASSIGN(WebApkInstallerTest); | 232 DISALLOW_COPY_AND_ASSIGN(WebApkInstallerTest); |
190 }; | 233 }; |
191 | 234 |
192 // Test installation succeeding. | 235 // Test installation succeeding. |
193 TEST_F(WebApkInstallerTest, Success) { | 236 TEST_F(WebApkInstallerTest, Success) { |
194 WebApkInstallerRunner runner; | 237 WebApkInstallerRunner runner; |
195 runner.Run(); | 238 runner.RunCreateWebApk(); |
196 EXPECT_TRUE(runner.success()); | 239 EXPECT_TRUE(runner.success()); |
197 } | 240 } |
198 | 241 |
199 // Test that installation fails if the WebAPK creation request times out. | 242 // Test that installation fails if the WebAPK creation request times out. |
200 TEST_F(WebApkInstallerTest, CreateWebApkRequestTimesOut) { | 243 TEST_F(WebApkInstallerTest, CreateWebApkRequestTimesOut) { |
201 GURL server_url = test_server()->GetURL("/slow?1000"); | 244 GURL server_url = test_server()->GetURL("/slow?1000"); |
202 SetWebApkServerUrl(server_url); | 245 SetWebApkServerUrl(server_url); |
203 | 246 |
204 WebApkInstallerRunner runner; | 247 WebApkInstallerRunner runner; |
205 runner.Run(); | 248 runner.RunCreateWebApk(); |
206 EXPECT_FALSE(runner.success()); | 249 EXPECT_FALSE(runner.success()); |
207 } | 250 } |
208 | 251 |
209 // Test that installation fails if the WebAPK download times out. | 252 // Test that installation fails if the WebAPK download times out. |
210 TEST_F(WebApkInstallerTest, WebApkDownloadTimesOut) { | 253 TEST_F(WebApkInstallerTest, WebApkDownloadTimesOut) { |
211 GURL download_url = test_server()->GetURL("/slow?1000"); | 254 GURL download_url = test_server()->GetURL("/slow?1000"); |
212 SetWebApkResponseBuilder(base::Bind(&BuildValidWebApkResponse, download_url)); | 255 SetWebApkResponseBuilder(base::Bind(&BuildValidWebApkResponse, download_url)); |
213 | 256 |
214 WebApkInstallerRunner runner; | 257 WebApkInstallerRunner runner; |
215 runner.Run(); | 258 runner.RunCreateWebApk(); |
216 EXPECT_FALSE(runner.success()); | 259 EXPECT_FALSE(runner.success()); |
217 } | 260 } |
218 | 261 |
219 // Test that installation fails if the WebAPK download fails. | 262 // Test that installation fails if the WebAPK download fails. |
220 TEST_F(WebApkInstallerTest, WebApkDownloadFails) { | 263 TEST_F(WebApkInstallerTest, WebApkDownloadFails) { |
221 GURL download_url = test_server()->GetURL("/nocontent"); | 264 GURL download_url = test_server()->GetURL("/nocontent"); |
222 SetWebApkResponseBuilder(base::Bind(&BuildValidWebApkResponse, download_url)); | 265 SetWebApkResponseBuilder(base::Bind(&BuildValidWebApkResponse, download_url)); |
223 | 266 |
224 WebApkInstallerRunner runner; | 267 WebApkInstallerRunner runner; |
225 runner.Run(); | 268 runner.RunCreateWebApk(); |
226 EXPECT_FALSE(runner.success()); | 269 EXPECT_FALSE(runner.success()); |
227 } | 270 } |
228 | 271 |
229 namespace { | 272 namespace { |
230 | 273 |
231 // Returns an HttpResponse which cannot be parsed as a webapk::WebApkResponse. | 274 // Returns an HttpResponse which cannot be parsed as a webapk::WebApkResponse. |
232 std::unique_ptr<net::test_server::HttpResponse> | 275 std::unique_ptr<net::test_server::HttpResponse> |
233 BuildUnparsableWebApkResponse() { | 276 BuildUnparsableWebApkResponse() { |
234 std::unique_ptr<net::test_server::BasicHttpResponse> response( | 277 std::unique_ptr<net::test_server::BasicHttpResponse> response( |
235 new net::test_server::BasicHttpResponse()); | 278 new net::test_server::BasicHttpResponse()); |
236 response->set_code(net::HTTP_OK); | 279 response->set_code(net::HTTP_OK); |
237 response->set_content("😀"); | 280 response->set_content("😀"); |
238 return std::move(response); | 281 return std::move(response); |
239 } | 282 } |
240 | 283 |
241 } // anonymous namespace | 284 } // anonymous namespace |
242 | 285 |
243 // Test that an HTTP response which cannot be parsed as a webapk::WebApkResponse | 286 // Test that an HTTP response which cannot be parsed as a webapk::WebApkResponse |
244 // is handled properly. | 287 // is handled properly. |
245 TEST_F(WebApkInstallerTest, UnparsableCreateWebApkResponse) { | 288 TEST_F(WebApkInstallerTest, UnparsableCreateWebApkResponse) { |
246 SetWebApkResponseBuilder(base::Bind(&BuildUnparsableWebApkResponse)); | 289 SetWebApkResponseBuilder(base::Bind(&BuildUnparsableWebApkResponse)); |
247 | 290 |
248 WebApkInstallerRunner runner; | 291 WebApkInstallerRunner runner; |
249 runner.Run(); | 292 runner.RunCreateWebApk(); |
250 EXPECT_FALSE(runner.success()); | 293 EXPECT_FALSE(runner.success()); |
251 } | 294 } |
295 | |
296 // Test update succeeding. | |
297 TEST_F(WebApkInstallerTest, UpdateSuccess) { | |
298 WebApkInstallerRunner runner; | |
299 runner.RunUpdateWebApk(); | |
300 EXPECT_TRUE(runner.success()); | |
301 } | |
pkotwicz
2016/08/11 22:02:45
Thanks for adding the test!
Xi Han
2016/08/15 21:38:45
The tests are well written, so it is not difficult
| |
OLD | NEW |