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

Side by Side Diff: chrome/browser/android/webapk/webapk_installer_unittest.cc

Issue 2453423002: Send all of the icon URLs listed in Web Manifest to WebAPK Server. (Closed)
Patch Set: Nits. Created 4 years, 1 month 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 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 29 matching lines...) Expand all
40 const base::FilePath::CharType kTestDataDir[] = 40 const base::FilePath::CharType kTestDataDir[] =
41 FILE_PATH_LITERAL("chrome/test/data"); 41 FILE_PATH_LITERAL("chrome/test/data");
42 42
43 // Mock field trial name and field trial group to use for "WebApks" feature. 43 // Mock field trial name and field trial group to use for "WebApks" feature.
44 const char kFieldTrialName[] = "MockFieldTrial"; 44 const char kFieldTrialName[] = "MockFieldTrial";
45 const char kFieldTrialGroup[] = "MockFieldTrialGroup"; 45 const char kFieldTrialGroup[] = "MockFieldTrialGroup";
46 46
47 // URL of mock WebAPK server. 47 // URL of mock WebAPK server.
48 const char* kServerUrl = "/webapkserver/"; 48 const char* kServerUrl = "/webapkserver/";
49 49
50 // Icon URL from Web Manifest. We use a random file in the test data directory. 50 // The best icon URL from Web Manifest. We use a random file in the test data
51 // Since WebApkInstaller does not try to decode the file as an image it is OK 51 // directory. Since WebApkInstaller does not try to decode the file as an image
52 // that the file is not an image. 52 // it is OK that the file is not an image.
53 const char* kIconUrl = "/simple.html"; 53 const char* kBestIconUrl = "/simple.html";
54 54
55 // URL of file to download from the WebAPK server. We use a random file in the 55 // URL of file to download from the WebAPK server. We use a random file in the
56 // test data directory. 56 // test data directory.
57 const char* kDownloadUrl = "/simple.html"; 57 const char* kDownloadUrl = "/simple.html";
58 58
59 // The package name of the downloaded WebAPK. 59 // The package name of the downloaded WebAPK.
60 const char* kDownloadedWebApkPackageName = "party.unicode"; 60 const char* kDownloadedWebApkPackageName = "party.unicode";
61 61
62 // WebApkInstaller subclass where 62 // WebApkInstaller subclass where
63 // WebApkInstaller::StartInstallingDownloadedWebApk() and 63 // WebApkInstaller::StartInstallingDownloadedWebApk() and
(...skipping 24 matching lines...) Expand all
88 base::Bind(&TestWebApkInstaller::OnSuccess, base::Unretained(this))); 88 base::Bind(&TestWebApkInstaller::OnSuccess, base::Unretained(this)));
89 } 89 }
90 90
91 private: 91 private:
92 DISALLOW_COPY_AND_ASSIGN(TestWebApkInstaller); 92 DISALLOW_COPY_AND_ASSIGN(TestWebApkInstaller);
93 }; 93 };
94 94
95 // Runs the WebApkInstaller installation process/update and blocks till done. 95 // Runs the WebApkInstaller installation process/update and blocks till done.
96 class WebApkInstallerRunner { 96 class WebApkInstallerRunner {
97 public: 97 public:
98 explicit WebApkInstallerRunner(const GURL& icon_url) 98 explicit WebApkInstallerRunner(const GURL& best_icon_url)
99 : url_request_context_getter_(new net::TestURLRequestContextGetter( 99 : url_request_context_getter_(new net::TestURLRequestContextGetter(
100 base::ThreadTaskRunnerHandle::Get())), 100 base::ThreadTaskRunnerHandle::Get())),
101 icon_url_(icon_url) {} 101 best_icon_url_(best_icon_url) {}
102 ~WebApkInstallerRunner() {} 102 ~WebApkInstallerRunner() {}
103 103
104 void RunInstallWebApk() { 104 void RunInstallWebApk() {
105 WebApkInstaller* installer = CreateWebApkInstaller(); 105 WebApkInstaller* installer = CreateWebApkInstaller();
106 106
107 installer->InstallAsyncWithURLRequestContextGetter( 107 installer->InstallAsyncWithURLRequestContextGetter(
108 url_request_context_getter_.get(), 108 url_request_context_getter_.get(),
109 base::Bind(&WebApkInstallerRunner::OnCompleted, 109 base::Bind(&WebApkInstallerRunner::OnCompleted,
110 base::Unretained(this))); 110 base::Unretained(this)));
111 Run(); 111 Run();
(...skipping 10 matching lines...) Expand all
122 base::Bind(&WebApkInstallerRunner::OnCompleted, base::Unretained(this)), 122 base::Bind(&WebApkInstallerRunner::OnCompleted, base::Unretained(this)),
123 kIconMurmur2Hash, 123 kIconMurmur2Hash,
124 kDownloadedWebApkPackageName, 124 kDownloadedWebApkPackageName,
125 kWebApkVersion); 125 kWebApkVersion);
126 126
127 Run(); 127 Run();
128 } 128 }
129 129
130 WebApkInstaller* CreateWebApkInstaller() { 130 WebApkInstaller* CreateWebApkInstaller() {
131 ShortcutInfo info(GURL::EmptyGURL()); 131 ShortcutInfo info(GURL::EmptyGURL());
132 info.icon_url = icon_url_; 132 info.best_icon_url = best_icon_url_;
133 133
134 // WebApkInstaller owns itself. 134 // WebApkInstaller owns itself.
135 WebApkInstaller* installer = new TestWebApkInstaller(info, SkBitmap()); 135 WebApkInstaller* installer = new TestWebApkInstaller(info, SkBitmap());
136 installer->SetTimeoutMs(100); 136 installer->SetTimeoutMs(100);
137 return installer; 137 return installer;
138 } 138 }
139 139
140 void Run() { 140 void Run() {
141 base::RunLoop run_loop; 141 base::RunLoop run_loop;
142 on_completed_callback_ = run_loop.QuitClosure(); 142 on_completed_callback_ = run_loop.QuitClosure();
143 run_loop.Run(); 143 run_loop.Run();
144 } 144 }
145 145
146 bool success() { return success_; } 146 bool success() { return success_; }
147 147
148 private: 148 private:
149 void OnCompleted(bool success, const std::string& webapk_package) { 149 void OnCompleted(bool success, const std::string& webapk_package) {
150 success_ = success; 150 success_ = success;
151 on_completed_callback_.Run(); 151 on_completed_callback_.Run();
152 } 152 }
153 153
154 scoped_refptr<net::TestURLRequestContextGetter> 154 scoped_refptr<net::TestURLRequestContextGetter>
155 url_request_context_getter_; 155 url_request_context_getter_;
156 156
157 // The Web Manifest's icon URL. 157 // The Web Manifest's icon URL.
158 const GURL icon_url_; 158 const GURL best_icon_url_;
159 159
160 // Called after the installation process has succeeded or failed. 160 // Called after the installation process has succeeded or failed.
161 base::Closure on_completed_callback_; 161 base::Closure on_completed_callback_;
162 162
163 // Whether the installation process succeeded. 163 // Whether the installation process succeeded.
164 bool success_; 164 bool success_;
165 165
166 DISALLOW_COPY_AND_ASSIGN(WebApkInstallerRunner); 166 DISALLOW_COPY_AND_ASSIGN(WebApkInstallerRunner);
167 }; 167 };
168 168
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
210 std::unique_ptr<base::FeatureList> feature_list = 210 std::unique_ptr<base::FeatureList> feature_list =
211 base::MakeUnique<base::FeatureList>(); 211 base::MakeUnique<base::FeatureList>();
212 feature_list->RegisterFieldTrialOverride( 212 feature_list->RegisterFieldTrialOverride(
213 chrome::android::kWebApks.name, 213 chrome::android::kWebApks.name,
214 base::FeatureList::OVERRIDE_ENABLE_FEATURE, field_trial); 214 base::FeatureList::OVERRIDE_ENABLE_FEATURE, field_trial);
215 scoped_feature_list_.InitWithFeatureList(std::move(feature_list)); 215 scoped_feature_list_.InitWithFeatureList(std::move(feature_list));
216 216
217 SetDefaults(); 217 SetDefaults();
218 } 218 }
219 219
220 // Sets the Web Manifest's icon URL. 220 // Sets the best Web Manifest's icon URL.
221 void SetIconUrl(const GURL& icon_url) { icon_url_ = icon_url; } 221 void SetBestIconUrl(const GURL& best_icon_url) {
222 best_icon_url_ = best_icon_url;
223 }
222 224
223 // Sets the URL to send the webapk::CreateWebApkRequest to. WebApkInstaller 225 // Sets the URL to send the webapk::CreateWebApkRequest to. WebApkInstaller
224 // should fail if the URL is not |kServerUrl|. 226 // should fail if the URL is not |kServerUrl|.
225 void SetWebApkServerUrl(const GURL& server_url) { 227 void SetWebApkServerUrl(const GURL& server_url) {
226 variations::testing::ClearAllVariationParams(); 228 variations::testing::ClearAllVariationParams();
227 std::map<std::string, std::string> params; 229 std::map<std::string, std::string> params;
228 params["ServerUrl"] = server_url.spec(); 230 params["ServerUrl"] = server_url.spec();
229 ASSERT_TRUE(variations::AssociateVariationParams(kFieldTrialName, 231 ASSERT_TRUE(variations::AssociateVariationParams(kFieldTrialName,
230 kFieldTrialGroup, params)); 232 kFieldTrialGroup, params));
231 } 233 }
232 234
233 // Sets the function that should be used to build the response to the 235 // Sets the function that should be used to build the response to the
234 // WebAPK creation request. 236 // WebAPK creation request.
235 void SetWebApkResponseBuilder(const WebApkResponseBuilder& builder) { 237 void SetWebApkResponseBuilder(const WebApkResponseBuilder& builder) {
236 webapk_response_builder_ = builder; 238 webapk_response_builder_ = builder;
237 } 239 }
238 240
239 std::unique_ptr<WebApkInstallerRunner> CreateWebApkInstallerRunner() { 241 std::unique_ptr<WebApkInstallerRunner> CreateWebApkInstallerRunner() {
240 return std::unique_ptr<WebApkInstallerRunner>( 242 return std::unique_ptr<WebApkInstallerRunner>(
241 new WebApkInstallerRunner(icon_url_)); 243 new WebApkInstallerRunner(best_icon_url_));
242 } 244 }
243 245
244 net::test_server::EmbeddedTestServer* test_server() { return &test_server_; } 246 net::test_server::EmbeddedTestServer* test_server() { return &test_server_; }
245 247
246 private: 248 private:
247 // Sets default configuration for running WebApkInstaller. 249 // Sets default configuration for running WebApkInstaller.
248 void SetDefaults() { 250 void SetDefaults() {
249 GURL icon_url = test_server_.GetURL(kIconUrl); 251 GURL best_icon_url = test_server_.GetURL(kBestIconUrl);
250 SetIconUrl(icon_url); 252 SetBestIconUrl(best_icon_url);
251 GURL server_url = test_server_.GetURL(kServerUrl); 253 GURL server_url = test_server_.GetURL(kServerUrl);
252 SetWebApkServerUrl(server_url); 254 SetWebApkServerUrl(server_url);
253 GURL download_url = test_server_.GetURL(kDownloadUrl); 255 GURL download_url = test_server_.GetURL(kDownloadUrl);
254 SetWebApkResponseBuilder( 256 SetWebApkResponseBuilder(
255 base::Bind(&BuildValidWebApkResponse, download_url)); 257 base::Bind(&BuildValidWebApkResponse, download_url));
256 } 258 }
257 259
258 std::unique_ptr<net::test_server::HttpResponse> HandleWebApkRequest( 260 std::unique_ptr<net::test_server::HttpResponse> HandleWebApkRequest(
259 const net::test_server::HttpRequest& request) { 261 const net::test_server::HttpRequest& request) {
260 return (request.relative_url == kServerUrl) 262 return (request.relative_url == kServerUrl)
261 ? webapk_response_builder_.Run() 263 ? webapk_response_builder_.Run()
262 : std::unique_ptr<net::test_server::HttpResponse>(); 264 : std::unique_ptr<net::test_server::HttpResponse>();
263 } 265 }
264 266
265 content::TestBrowserThreadBundle thread_bundle_; 267 content::TestBrowserThreadBundle thread_bundle_;
266 net::EmbeddedTestServer test_server_; 268 net::EmbeddedTestServer test_server_;
267 269
268 std::unique_ptr<base::FieldTrialList> field_trial_list_; 270 std::unique_ptr<base::FieldTrialList> field_trial_list_;
269 base::test::ScopedFeatureList scoped_feature_list_; 271 base::test::ScopedFeatureList scoped_feature_list_;
270 272
271 // Web Manifest's icon URL. 273 // Web Manifest's icon URL.
272 GURL icon_url_; 274 GURL best_icon_url_;
273 275
274 // Builds response to the WebAPK creation request. 276 // Builds response to the WebAPK creation request.
275 WebApkResponseBuilder webapk_response_builder_; 277 WebApkResponseBuilder webapk_response_builder_;
276 278
277 DISALLOW_COPY_AND_ASSIGN(WebApkInstallerTest); 279 DISALLOW_COPY_AND_ASSIGN(WebApkInstallerTest);
278 }; 280 };
279 281
280 // Test installation succeeding. 282 // Test installation succeeding.
281 TEST_F(WebApkInstallerTest, Success) { 283 TEST_F(WebApkInstallerTest, Success) {
282 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); 284 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner();
283 runner->RunInstallWebApk(); 285 runner->RunInstallWebApk();
284 EXPECT_TRUE(runner->success()); 286 EXPECT_TRUE(runner->success());
285 } 287 }
286 288
287 // Test that installation fails if fetching the bitmap at the icon URL times 289 // Test that installation fails if fetching the bitmap at the best icon URL
288 // out. In a perfect world the fetch would never time out because the bitmap at 290 // times out. In a perfect world the fetch would never time out because the
289 // the icon URL should be in the HTTP cache. 291 // bitmap at the best icon URL should be in the HTTP cache.
290 TEST_F(WebApkInstallerTest, IconUrlDownloadTimesOut) { 292 TEST_F(WebApkInstallerTest, BestIconUrlDownloadTimesOut) {
291 GURL icon_url = test_server()->GetURL("/slow?1000"); 293 GURL best_icon_url = test_server()->GetURL("/slow?1000");
292 SetIconUrl(icon_url); 294 SetBestIconUrl(best_icon_url);
293 295
294 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); 296 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner();
295 runner->RunInstallWebApk(); 297 runner->RunInstallWebApk();
296 EXPECT_FALSE(runner->success()); 298 EXPECT_FALSE(runner->success());
297 } 299 }
298 300
299 // Test that installation fails if the WebAPK creation request times out. 301 // Test that installation fails if the WebAPK creation request times out.
300 TEST_F(WebApkInstallerTest, CreateWebApkRequestTimesOut) { 302 TEST_F(WebApkInstallerTest, CreateWebApkRequestTimesOut) {
301 GURL server_url = test_server()->GetURL("/slow?1000"); 303 GURL server_url = test_server()->GetURL("/slow?1000");
302 SetWebApkServerUrl(server_url); 304 SetWebApkServerUrl(server_url);
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
349 runner->RunInstallWebApk(); 351 runner->RunInstallWebApk();
350 EXPECT_FALSE(runner->success()); 352 EXPECT_FALSE(runner->success());
351 } 353 }
352 354
353 // Test update succeeding. 355 // Test update succeeding.
354 TEST_F(WebApkInstallerTest, UpdateSuccess) { 356 TEST_F(WebApkInstallerTest, UpdateSuccess) {
355 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); 357 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner();
356 runner->RunUpdateWebApk(); 358 runner->RunUpdateWebApk();
357 EXPECT_TRUE(runner->success()); 359 EXPECT_TRUE(runner->success());
358 } 360 }
OLDNEW
« no previous file with comments | « chrome/browser/android/webapk/webapk_installer.cc ('k') | chrome/browser/android/webapk/webapk_update_manager.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698