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

Side by Side Diff: chrome/browser/android/webapps/add_to_homescreen_data_fetcher_unittest.cc

Issue 2960103002: Improve add to homescreen data fetcher unit tests. (Closed)
Patch Set: Comments Created 3 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
« no previous file with comments | « no previous file | chrome/browser/installable/installable_manager.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/webapps/add_to_homescreen_data_fetcher.h" 5 #include "chrome/browser/android/webapps/add_to_homescreen_data_fetcher.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <string> 8 #include <string>
9 9
10 #include "base/callback_forward.h"
11 #include "base/files/file_path.h"
12 #include "base/macros.h" 10 #include "base/macros.h"
13 #include "base/memory/ref_counted.h" 11 #include "base/memory/ref_counted.h"
14 #include "base/message_loop/message_loop.h"
15 #include "base/run_loop.h" 12 #include "base/run_loop.h"
16 #include "base/strings/nullable_string16.h" 13 #include "base/strings/nullable_string16.h"
17 #include "base/strings/string_util.h" 14 #include "base/strings/string_util.h"
18 #include "base/strings/utf_string_conversions.h" 15 #include "base/strings/utf_string_conversions.h"
19 #include "base/time/time.h"
20 #include "chrome/browser/installable/installable_manager.h" 16 #include "chrome/browser/installable/installable_manager.h"
21 #include "chrome/common/web_application_info.h" 17 #include "chrome/common/web_application_info.h"
22 #include "chrome/test/base/chrome_render_view_host_test_harness.h" 18 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
23 #include "chrome/test/base/testing_profile.h" 19 #include "chrome/test/base/testing_profile.h"
24 #include "content/browser/service_worker/embedded_worker_test_helper.h"
25 #include "content/browser/service_worker/service_worker_context_core.h"
26 #include "content/common/service_worker/service_worker_status_code.h"
27 #include "content/public/browser/browser_thread.h"
28 #include "content/public/browser/site_instance.h"
29 #include "content/public/browser/web_contents.h" 20 #include "content/public/browser/web_contents.h"
30 #include "content/public/common/manifest.h" 21 #include "content/public/common/manifest.h"
31 #include "content/test/test_web_contents.h"
32 #include "net/http/http_status_code.h"
33 #include "third_party/WebKit/public/platform/WebDisplayMode.h" 22 #include "third_party/WebKit/public/platform/WebDisplayMode.h"
34 #include "ui/gfx/image/image_unittest_util.h" 23 #include "ui/gfx/image/image_unittest_util.h"
35 #include "url/gurl.h" 24 #include "url/gurl.h"
36 25
37 namespace { 26 namespace {
38 27
39 const char* kWebApplicationInfoTitle = "Meta Title"; 28 const char* kWebApplicationInfoTitle = "Meta Title";
40 const char* kDefaultManifestUrl = "https://www.example.com/manifest.json"; 29 const char* kDefaultManifestUrl = "https://www.example.com/manifest.json";
30 const char* kDefaultIconUrl = "https://www.example.com/icon.png";
41 const char* kDefaultManifestName = "Default Name"; 31 const char* kDefaultManifestName = "Default Name";
42 const char* kDefaultManifestShortName = "Default Short Name"; 32 const char* kDefaultManifestShortName = "Default Short Name";
43 const char* kDefaultStartUrl = "https://www.example.com/index.html"; 33 const char* kDefaultStartUrl = "https://www.example.com/index.html";
44 const blink::WebDisplayMode kDefaultManifestDisplayMode = 34 const blink::WebDisplayMode kDefaultManifestDisplayMode =
45 blink::kWebDisplayModeStandalone; 35 blink::kWebDisplayModeStandalone;
36 const int kIconSizePx = 144;
46 37
47 // WebContents subclass which mocks out image and manifest fetching. 38 // Tracks which of the AddToHomescreenDataFetcher::Observer methods have been
48 class MockWebContents : public content::TestWebContents {
49 public:
50 explicit MockWebContents(content::BrowserContext* browser_context)
51 : content::TestWebContents(browser_context),
52 should_image_time_out_(false),
53 should_manifest_time_out_(false) {}
54
55 ~MockWebContents() override {}
56
57 void SetManifest(const GURL& manifest_url,
58 const content::Manifest& manifest) {
59 manifest_url_ = manifest_url;
60 manifest_ = manifest;
61 }
62
63 int DownloadImage(const GURL& url,
64 bool is_favicon,
65 uint32_t max_bitmap_size,
66 bool bypass_cache,
67 const ImageDownloadCallback& callback) override {
68 if (should_image_time_out_)
69 return 0;
70
71 const int kIconSizePx = 144;
72 SkBitmap icon = gfx::test::CreateBitmap(kIconSizePx, kIconSizePx);
73 std::vector<SkBitmap> icons(1u, icon);
74 std::vector<gfx::Size> pixel_sizes(1u, gfx::Size(kIconSizePx, kIconSizePx));
75 content::BrowserThread::GetTaskRunnerForThread(content::BrowserThread::UI)
76 ->PostTask(FROM_HERE, base::Bind(callback, 0, net::HTTP_OK, url, icons,
77 pixel_sizes));
78 return 0;
79 }
80
81 void GetManifest(const GetManifestCallback& callback) override {
82 if (should_manifest_time_out_)
83 return;
84
85 content::BrowserThread::GetTaskRunnerForThread(content::BrowserThread::UI)
86 ->PostTask(FROM_HERE, base::Bind(callback, manifest_url_, manifest_));
87 }
88
89 void SetShouldImageTimeOut(bool should_time_out) {
90 should_image_time_out_ = should_time_out;
91 }
92
93 void SetShouldManifestTimeOut(bool should_time_out) {
94 should_manifest_time_out_ = should_time_out;
95 }
96
97 private:
98 GURL manifest_url_;
99 content::Manifest manifest_;
100 bool should_image_time_out_;
101 bool should_manifest_time_out_;
102
103 DISALLOW_COPY_AND_ASSIGN(MockWebContents);
104 };
105
106 // Tracks which of the AddToHomescreenDataFetcher::Observer callbacks have been
107 // called. 39 // called.
108 class ObserverWaiter : public AddToHomescreenDataFetcher::Observer { 40 class ObserverWaiter : public AddToHomescreenDataFetcher::Observer {
109 public: 41 public:
110 ObserverWaiter() 42 ObserverWaiter()
111 : is_webapk_compatible_(false), 43 : is_webapk_compatible_(false),
112 determined_webapk_compatibility_(false), 44 determined_webapk_compatibility_(false),
113 title_available_(false), 45 title_available_(false),
114 data_available_(false) {} 46 data_available_(false) {}
115 ~ObserverWaiter() override {} 47 ~ObserverWaiter() override {}
116 48
117 // Waits till the OnDataAvailable() callback is called. 49 // Waits till the OnDataAvailable() callback is called.
118 void WaitForDataAvailable() { 50 void WaitForDataAvailable() {
119 if (data_available_) 51 if (data_available_)
120 return; 52 return;
121 53
122 base::RunLoop run_loop; 54 base::RunLoop run_loop;
123 quit_closure_ = run_loop.QuitClosure(); 55 quit_closure_ = run_loop.QuitClosure();
124 run_loop.Run(); 56 run_loop.Run();
125 } 57 }
126 58
127 void OnDidDetermineWebApkCompatibility(bool is_webapk_compatible) override { 59 void OnDidDetermineWebApkCompatibility(bool is_webapk_compatible) override {
60 // This should only be called once.
61 EXPECT_FALSE(determined_webapk_compatibility_);
128 EXPECT_FALSE(title_available_); 62 EXPECT_FALSE(title_available_);
129 determined_webapk_compatibility_ = true; 63 determined_webapk_compatibility_ = true;
130 is_webapk_compatible_ = is_webapk_compatible; 64 is_webapk_compatible_ = is_webapk_compatible;
131 } 65 }
132 66
133 void OnUserTitleAvailable(const base::string16& title) override { 67 void OnUserTitleAvailable(const base::string16& title) override {
68 // This should only be called once.
69 EXPECT_FALSE(title_available_);
134 EXPECT_FALSE(data_available_); 70 EXPECT_FALSE(data_available_);
135 title_available_ = true; 71 title_available_ = true;
136 title_ = title; 72 title_ = title;
137 } 73 }
138 74
139 SkBitmap FinalizeLauncherIconInBackground(const SkBitmap& icon, 75 SkBitmap FinalizeLauncherIconInBackground(const SkBitmap& icon,
140 const GURL& url, 76 const GURL& url,
141 bool* is_generated) override { 77 bool* is_generated) override {
142 *is_generated = false; 78 *is_generated = false;
143 return icon; 79 return icon;
144 } 80 }
145 81
146 void OnDataAvailable(const ShortcutInfo& info, 82 void OnDataAvailable(const ShortcutInfo& info,
147 const SkBitmap& primary_icon, 83 const SkBitmap& primary_icon,
148 const SkBitmap& badge_icon) override { 84 const SkBitmap& badge_icon) override {
85 // This should only be called once.
86 EXPECT_FALSE(data_available_);
149 EXPECT_TRUE(title_available_); 87 EXPECT_TRUE(title_available_);
150 data_available_ = true; 88 data_available_ = true;
151 if (!quit_closure_.is_null()) 89 if (!quit_closure_.is_null())
152 quit_closure_.Run(); 90 quit_closure_.Run();
153 } 91 }
154 92
155 base::string16 title() const { return title_; } 93 base::string16 title() const { return title_; }
156 bool is_webapk_compatible() const { return is_webapk_compatible_; } 94 bool is_webapk_compatible() const { return is_webapk_compatible_; }
157 bool determined_webapk_compatibility() const { 95 bool determined_webapk_compatibility() const {
158 return determined_webapk_compatibility_; 96 return determined_webapk_compatibility_;
159 } 97 }
160 bool title_available() const { return title_available_; } 98 bool title_available() const { return title_available_; }
161 99
162 private: 100 private:
163 base::string16 title_; 101 base::string16 title_;
164 bool is_webapk_compatible_; 102 bool is_webapk_compatible_;
165 bool determined_webapk_compatibility_; 103 bool determined_webapk_compatibility_;
166 bool title_available_; 104 bool title_available_;
167 bool data_available_; 105 bool data_available_;
168 base::Closure quit_closure_; 106 base::Closure quit_closure_;
169 107
170 DISALLOW_COPY_AND_ASSIGN(ObserverWaiter); 108 DISALLOW_COPY_AND_ASSIGN(ObserverWaiter);
171 }; 109 };
172 110
173 // Builds non-null base::NullableString16 from a UTF8 string. 111 // Builds non-null base::NullableString16 from a UTF8 string.
174 base::NullableString16 NullableStringFromUTF8(const std::string& value) { 112 base::NullableString16 NullableStringFromUTF8(const std::string& value) {
175 return base::NullableString16(base::UTF8ToUTF16(value), false); 113 return base::NullableString16(base::UTF8ToUTF16(value), false);
176 } 114 }
177 115
178 content::Manifest BuildEmptyManifest() {
179 return content::Manifest();
180 }
181
182 // Builds WebAPK compatible content::Manifest. 116 // Builds WebAPK compatible content::Manifest.
183 content::Manifest BuildDefaultManifest() { 117 content::Manifest BuildDefaultManifest() {
184 content::Manifest manifest; 118 content::Manifest manifest;
185 manifest.name = NullableStringFromUTF8(kDefaultManifestName); 119 manifest.name = NullableStringFromUTF8(kDefaultManifestName);
186 manifest.short_name = NullableStringFromUTF8(kDefaultManifestShortName); 120 manifest.short_name = NullableStringFromUTF8(kDefaultManifestShortName);
187 manifest.start_url = GURL(kDefaultStartUrl); 121 manifest.start_url = GURL(kDefaultStartUrl);
188 manifest.display = kDefaultManifestDisplayMode; 122 manifest.display = kDefaultManifestDisplayMode;
189 123
190 content::Manifest::Icon primary_icon; 124 content::Manifest::Icon primary_icon;
191 primary_icon.type = base::ASCIIToUTF16("image/png"); 125 primary_icon.type = base::ASCIIToUTF16("image/png");
192 primary_icon.sizes.push_back(gfx::Size(144, 144)); 126 primary_icon.sizes.push_back(gfx::Size(144, 144));
193 primary_icon.purpose.push_back(content::Manifest::Icon::IconPurpose::ANY); 127 primary_icon.purpose.push_back(content::Manifest::Icon::IconPurpose::ANY);
194 primary_icon.src = GURL("https://www.google.com/image.png"); 128 primary_icon.src = GURL(kDefaultIconUrl);
195 manifest.icons.push_back(primary_icon); 129 manifest.icons.push_back(primary_icon);
196 130
197 return manifest; 131 return manifest;
198 } 132 }
199 133
200 } // anonymous namespace 134 } // anonymous namespace
201 135
136 class TestInstallableManager : public InstallableManager {
137 public:
138 explicit TestInstallableManager(content::WebContents* web_contents)
139 : InstallableManager(web_contents) {}
140
141 void GetData(const InstallableParams& params,
142 const InstallableCallback& callback) override {
143 if (should_manifest_time_out_ ||
144 (params.check_installable && should_installable_time_out_)) {
pkotwicz 2017/07/06 20:49:30 The default value of |is_installable_| depends on
dominickn 2017/07/10 02:37:44 Good catch, done.
145 return;
146 }
147
148 InstallableStatusCode code = code_;
149 if (params.check_installable) {
150 if (!IsManifestValidForWebApp(manifest_))
151 code = valid_manifest_->error;
152 else if (!is_installable_)
153 code = NOT_OFFLINE_CAPABLE;
154 }
155
156 callback.Run(
157 {code, GURL(kDefaultManifestUrl), manifest_,
158 params.fetch_valid_primary_icon ? primary_icon_url_ : GURL(),
159 params.fetch_valid_primary_icon ? primary_icon_.get() : nullptr,
160 params.fetch_valid_badge_icon ? badge_icon_url_ : GURL(),
161 params.fetch_valid_badge_icon ? badge_icon_.get() : nullptr,
162 params.check_installable ? is_installable_ : false});
163 }
164
165 void SetInstallable(bool is_installable) { is_installable_ = is_installable; }
166
167 void SetManifest(const content::Manifest& manifest) {
168 manifest_ = manifest;
169
170 if (manifest.icons.empty()) {
171 code_ = NO_ACCEPTABLE_ICON;
172 is_installable_ = false;
173 } else {
174 primary_icon_url_ = manifest_.icons[0].src;
175 primary_icon_.reset(
176 new SkBitmap(gfx::test::CreateBitmap(kIconSizePx, kIconSizePx)));
177
178 badge_icon_url_ = manifest_.icons[0].src;
179 badge_icon_.reset(
180 new SkBitmap(gfx::test::CreateBitmap(kIconSizePx, kIconSizePx)));
181 code_ = NO_ERROR_DETECTED;
182 is_installable_ = true;
183 }
184 }
185
186 void SetShouldManifestTimeOut(bool should_time_out) {
187 should_manifest_time_out_ = should_time_out;
188 }
189
190 void SetShouldInstallableTimeOut(bool should_time_out) {
191 should_installable_time_out_ = should_time_out;
192 }
193
194 private:
195 InstallableStatusCode code_;
196 content::Manifest manifest_;
197 GURL primary_icon_url_;
198 GURL badge_icon_url_;
199 std::unique_ptr<SkBitmap> primary_icon_;
200 std::unique_ptr<SkBitmap> badge_icon_;
201
202 bool is_installable_ = false;
203
204 bool should_manifest_time_out_ = false;
205 bool should_installable_time_out_ = false;
206 };
207
202 // Tests AddToHomescreenDataFetcher. These tests should be browser tests but 208 // Tests AddToHomescreenDataFetcher. These tests should be browser tests but
203 // Android does not support browser tests yet (crbug.com/611756). 209 // Android does not support browser tests yet (crbug.com/611756).
204 class AddToHomescreenDataFetcherTest : public ChromeRenderViewHostTestHarness { 210 class AddToHomescreenDataFetcherTest : public ChromeRenderViewHostTestHarness {
205 public: 211 public:
206 AddToHomescreenDataFetcherTest() {} 212 AddToHomescreenDataFetcherTest() {}
207 ~AddToHomescreenDataFetcherTest() override {} 213 ~AddToHomescreenDataFetcherTest() override {}
208 214
209 void SetUp() override { 215 void SetUp() override {
210 ChromeRenderViewHostTestHarness::SetUp(); 216 ChromeRenderViewHostTestHarness::SetUp();
211 217
212 ASSERT_TRUE(profile()->CreateHistoryService(false, true)); 218 ASSERT_TRUE(profile()->CreateHistoryService(false, true));
213 profile()->CreateFaviconService(); 219 profile()->CreateFaviconService();
214 220
215 embedded_worker_test_helper_.reset( 221 // Manually inject the TestInstallableManager as a "InstallableManager"
216 new content::EmbeddedWorkerTestHelper(base::FilePath())); 222 // WebContentsUserData. We can't directly call ::CreateForWebContents due to
217 223 // typing issues since TestInstallableManager doesn't directly inherit from
218 scoped_refptr<content::SiteInstance> site_instance = 224 // WebContentsUserData.
219 content::SiteInstance::Create(browser_context()); 225 web_contents()->SetUserData(
220 site_instance->GetProcess()->Init(); 226 TestInstallableManager::UserDataKey(),
221 MockWebContents* mock_web_contents = new MockWebContents(browser_context()); 227 base::WrapUnique(new TestInstallableManager(web_contents())));
222 mock_web_contents->Init(content::WebContents::CreateParams( 228 installable_manager_ = static_cast<TestInstallableManager*>(
223 browser_context(), std::move(site_instance))); 229 web_contents()->GetUserData(TestInstallableManager::UserDataKey()));
224 InstallableManager::CreateForWebContents(mock_web_contents);
225 SetContents(mock_web_contents);
226 NavigateAndCommit(GURL(kDefaultStartUrl));
227 }
228
229 void TearDown() override {
230 embedded_worker_test_helper_.reset();
231 ChromeRenderViewHostTestHarness::TearDown();
232 } 230 }
233 231
234 scoped_refptr<AddToHomescreenDataFetcher> BuildFetcher( 232 scoped_refptr<AddToHomescreenDataFetcher> BuildFetcher(
235 bool check_webapk_compatible, 233 bool check_webapk_compatible,
236 AddToHomescreenDataFetcher::Observer* observer) { 234 AddToHomescreenDataFetcher::Observer* observer) {
237 return new AddToHomescreenDataFetcher(web_contents(), 1, 1, 1, 1, 1, 500, 235 return new AddToHomescreenDataFetcher(web_contents(), 1, 1, 1, 1, 1, 500,
238 check_webapk_compatible, observer); 236 check_webapk_compatible, observer);
239 } 237 }
240 238
241 // Set the manifest to be returned as a result of WebContents::GetManifest(). 239 void RunFetcher(scoped_refptr<AddToHomescreenDataFetcher> fetcher,
242 void SetManifest(const GURL& manifest_url, 240 ObserverWaiter& waiter,
243 const content::Manifest& manifest) { 241 const char* expected_title,
244 MockWebContents* mock_web_contents = 242 blink::WebDisplayMode display_mode,
245 static_cast<MockWebContents*>(web_contents()); 243 bool is_webapk_compatible) {
246 mock_web_contents->SetManifest(manifest_url, manifest); 244 WebApplicationInfo web_application_info;
245 web_application_info.title = base::UTF8ToUTF16(kWebApplicationInfoTitle);
246
247 fetcher->OnDidGetWebApplicationInfo(web_application_info);
248 waiter.WaitForDataAvailable();
249
250 EXPECT_EQ(check_webapk_compatibility(),
251 waiter.determined_webapk_compatibility());
252 EXPECT_EQ(is_webapk_compatible, waiter.is_webapk_compatible());
253 EXPECT_TRUE(waiter.title_available());
254 EXPECT_TRUE(base::EqualsASCII(waiter.title(), expected_title));
255 EXPECT_TRUE(
256 base::EqualsASCII(fetcher->shortcut_info().user_title, expected_title));
257 EXPECT_EQ(display_mode, fetcher->shortcut_info().display);
247 } 258 }
248 259
249 void SetShouldImageTimeOut(bool should_time_out) { 260 void SetManifest(const content::Manifest& manifest) {
250 MockWebContents* mock_web_contents = 261 installable_manager_->SetManifest(manifest);
251 static_cast<MockWebContents*>(web_contents()); 262 }
252 mock_web_contents->SetShouldImageTimeOut(should_time_out); 263
264 void SetInstallable(bool is_installable) {
265 installable_manager_->SetInstallable(is_installable);
253 } 266 }
254 267
255 void SetShouldManifestTimeOut(bool should_time_out) { 268 void SetShouldManifestTimeOut(bool should_time_out) {
256 MockWebContents* mock_web_contents = 269 installable_manager_->SetShouldManifestTimeOut(should_time_out);
257 static_cast<MockWebContents*>(web_contents());
258 mock_web_contents->SetShouldManifestTimeOut(should_time_out);
259 } 270 }
260 271
261 // Registers service worker at |url|. Blocks till the service worker is 272 void SetShouldInstallableTimeOut(bool should_time_out) {
262 // registered. 273 installable_manager_->SetShouldInstallableTimeOut(should_time_out);
263 void RegisterServiceWorker(const GURL& url) {
264 base::RunLoop run_loop;
265 embedded_worker_test_helper_->context()->RegisterServiceWorker(
266 url, GURL(url.spec() + "/service_worker.js"), nullptr,
267 base::Bind(&AddToHomescreenDataFetcherTest::OnServiceWorkerRegistered,
268 base::Unretained(this), run_loop.QuitClosure()));
269 } 274 }
270 275
276 virtual bool check_webapk_compatibility() { return true; }
277
271 private: 278 private:
272 // Callback for RegisterServiceWorker() for when service worker registration 279 TestInstallableManager* installable_manager_;
273 // has completed.
274 void OnServiceWorkerRegistered(const base::Closure& callback,
275 content::ServiceWorkerStatusCode status,
276 const std::string& status_message,
277 int64_t registration_id) {
278 ASSERT_EQ(content::SERVICE_WORKER_OK, status)
279 << content::ServiceWorkerStatusToString(status);
280 callback.Run();
281 }
282
283 std::unique_ptr<content::EmbeddedWorkerTestHelper>
284 embedded_worker_test_helper_;
285 280
286 DISALLOW_COPY_AND_ASSIGN(AddToHomescreenDataFetcherTest); 281 DISALLOW_COPY_AND_ASSIGN(AddToHomescreenDataFetcherTest);
287 }; 282 };
288 283
289 // Class for tests which should be run with AddToHomescreenDataFetcher built 284 // Class for tests which should be run with AddToHomescreenDataFetcher built
290 // with both true and false values of |check_webapk_compatible|. 285 // with both true and false values of |check_webapk_compatible|.
291 class AddToHomescreenDataFetcherTestCommon 286 class AddToHomescreenDataFetcherTestCommon
292 : public AddToHomescreenDataFetcherTest, 287 : public AddToHomescreenDataFetcherTest,
293 public testing::WithParamInterface<bool> { 288 public testing::WithParamInterface<bool> {
294 public: 289 public:
295 AddToHomescreenDataFetcherTestCommon() {} 290 AddToHomescreenDataFetcherTestCommon() {}
296 ~AddToHomescreenDataFetcherTestCommon() override {} 291 ~AddToHomescreenDataFetcherTestCommon() override {}
297 292
298 scoped_refptr<AddToHomescreenDataFetcher> BuildFetcher( 293 scoped_refptr<AddToHomescreenDataFetcher> BuildFetcher(
299 AddToHomescreenDataFetcher::Observer* observer) { 294 AddToHomescreenDataFetcher::Observer* observer) {
300 return AddToHomescreenDataFetcherTest::BuildFetcher( 295 return AddToHomescreenDataFetcherTest::BuildFetcher(
301 check_webapk_compatibility(), observer); 296 check_webapk_compatibility(), observer);
302 } 297 }
303 298
304 // The value of |check_webapk_compatible| used when building the 299 // The value of |check_webapk_compatible| used when building the
305 // AddToHomescreenDataFetcher. 300 // AddToHomescreenDataFetcher.
306 bool check_webapk_compatibility() { return GetParam(); } 301 bool check_webapk_compatibility() override { return GetParam(); }
307 302
308 private: 303 private:
309 DISALLOW_COPY_AND_ASSIGN(AddToHomescreenDataFetcherTestCommon); 304 DISALLOW_COPY_AND_ASSIGN(AddToHomescreenDataFetcherTestCommon);
310 }; 305 };
311 306
312 // Checks that AddToHomescreenDataFetcher::Observer::OnUserTitleAvailable() is
313 // called when the web manifest returned is empty. The add-to-homescreen dialog
314 // makes the dialog's text field editable once OnUserTitleAvailable() is called.
315 TEST_P(AddToHomescreenDataFetcherTestCommon, EmptyManifest) { 307 TEST_P(AddToHomescreenDataFetcherTestCommon, EmptyManifest) {
316 WebApplicationInfo web_application_info; 308 // Check that an empty manifest has the appropriate methods run.
317 web_application_info.title = base::UTF8ToUTF16(kWebApplicationInfoTitle); 309 ObserverWaiter waiter;
318 310 scoped_refptr<AddToHomescreenDataFetcher> fetcher(BuildFetcher(&waiter));
319 SetManifest(GURL(kDefaultManifestUrl), BuildEmptyManifest()); 311 RunFetcher(fetcher, waiter, kWebApplicationInfoTitle,
320 312 blink::kWebDisplayModeBrowser, false);
321 ObserverWaiter waiter; 313 fetcher->set_weak_observer(nullptr);
322 scoped_refptr<AddToHomescreenDataFetcher> fetcher(BuildFetcher(&waiter)); 314 }
323 fetcher->OnDidGetWebApplicationInfo(web_application_info); 315
324 waiter.WaitForDataAvailable(); 316 TEST_P(AddToHomescreenDataFetcherTestCommon, NoIconManifest) {
325 317 // Test a manifest with no icons. This should use the short name and have an
326 EXPECT_EQ(check_webapk_compatibility(), 318 // empty icon in this test runner (in production the empty icon would be
327 waiter.determined_webapk_compatibility()); 319 // replaced by a favicon or a generated icon).
328 EXPECT_FALSE(waiter.is_webapk_compatible()); 320 content::Manifest manifest = BuildDefaultManifest();
329 EXPECT_TRUE(waiter.title_available()); 321 manifest.icons.clear();
330 EXPECT_TRUE(base::EqualsASCII(waiter.title(), kWebApplicationInfoTitle)); 322 SetManifest(manifest);
331 323
332 fetcher->set_weak_observer(nullptr); 324 ObserverWaiter waiter;
333 } 325 scoped_refptr<AddToHomescreenDataFetcher> fetcher(BuildFetcher(&waiter));
334 326 RunFetcher(fetcher, waiter, kDefaultManifestShortName,
335 // Test that when the manifest provides Manifest::short_name but not 327 blink::kWebDisplayModeStandalone, false);
336 // Manifest::name that Manifest::short_name is used as the name instead of 328
337 // WebApplicationInfo::title. 329 EXPECT_TRUE(fetcher->primary_icon().drawsNothing());
330 EXPECT_TRUE(fetcher->shortcut_info().best_primary_icon_url.is_empty());
331 EXPECT_TRUE(fetcher->badge_icon().drawsNothing());
332 EXPECT_TRUE(fetcher->shortcut_info().best_badge_icon_url.is_empty());
333
334 fetcher->set_weak_observer(nullptr);
335 }
336
337 TEST_P(AddToHomescreenDataFetcherTestCommon, ManifestFetchTimesOut) {
338 // Check that the AddToHomescreenDataFetcher::Observer methods are called
339 // if the first call to InstallableManager::GetData() times out. This should
340 // fall back to the metadata title and have an empty icon.
341 SetShouldManifestTimeOut(true);
342 SetManifest(BuildDefaultManifest());
343
344 // Check a site with no offline-capable service worker.
345 ObserverWaiter waiter;
346 scoped_refptr<AddToHomescreenDataFetcher> fetcher(BuildFetcher(&waiter));
347 RunFetcher(fetcher, waiter, kWebApplicationInfoTitle,
348 blink::kWebDisplayModeBrowser, false);
349
350 EXPECT_TRUE(fetcher->primary_icon().drawsNothing());
351 EXPECT_TRUE(fetcher->shortcut_info().best_primary_icon_url.is_empty());
352
353 fetcher->set_weak_observer(nullptr);
354 }
355
356 TEST_F(AddToHomescreenDataFetcherTest, ServiceWorkerCheckTimesOut) {
357 // Check that the AddToHomescreenDataFetcher::Observer methods are called if
358 // the service worker check times out on a page that is installable (i.e. it's
359 // taken too long). This should use the short_name and icon from the manifest,
360 // but not be WebAPK-compatible. Only relevant when checking WebAPK
361 // compatibility.
362 SetManifest(BuildDefaultManifest());
363 SetShouldInstallableTimeOut(true);
364
365 ObserverWaiter waiter;
366 scoped_refptr<AddToHomescreenDataFetcher> fetcher(
367 BuildFetcher(true, &waiter));
368 RunFetcher(fetcher, waiter, kDefaultManifestShortName,
369 blink::kWebDisplayModeStandalone, false);
370
371 EXPECT_FALSE(fetcher->primary_icon().drawsNothing());
372 EXPECT_EQ(fetcher->shortcut_info().best_primary_icon_url,
373 GURL(kDefaultIconUrl));
374
375 fetcher->set_weak_observer(nullptr);
376 }
377
378 TEST_P(AddToHomescreenDataFetcherTestCommon, InstallableManifest) {
379 // Test an installable manifest for a site that has a service worker.
380 content::Manifest manifest(BuildDefaultManifest());
381 SetManifest(manifest);
382
383 ObserverWaiter waiter;
384 scoped_refptr<AddToHomescreenDataFetcher> fetcher(BuildFetcher(&waiter));
385 RunFetcher(fetcher, waiter, kDefaultManifestShortName,
386 blink::kWebDisplayModeStandalone, check_webapk_compatibility());
387
388 // There should always be a primary icon. The badge icon should only be
389 // present if we checked for WebAPK compatibility.
390 EXPECT_FALSE(fetcher->primary_icon().drawsNothing());
391 EXPECT_EQ(fetcher->shortcut_info().best_primary_icon_url,
392 GURL(kDefaultIconUrl));
393
394 // Check that the badge icon is requested only when AddToHomescreenDataFetcher
395 // checks for WebAPK compatibility.
396 if (check_webapk_compatibility()) {
397 EXPECT_FALSE(fetcher->badge_icon().drawsNothing());
398 EXPECT_EQ(fetcher->shortcut_info().best_badge_icon_url,
399 GURL(kDefaultIconUrl));
400 } else {
401 EXPECT_TRUE(fetcher->badge_icon().drawsNothing());
402 EXPECT_TRUE(fetcher->shortcut_info().best_badge_icon_url.is_empty());
403 }
404
405 fetcher->set_weak_observer(nullptr);
406 }
407
338 TEST_P(AddToHomescreenDataFetcherTestCommon, 408 TEST_P(AddToHomescreenDataFetcherTestCommon,
339 ManifestShortNameClobbersWebApplicationName) { 409 ManifestNameClobbersWebApplicationName) {
340 WebApplicationInfo web_application_info; 410 // Test that when the manifest provides Manifest::name but not
341 web_application_info.title = base::UTF8ToUTF16(kWebApplicationInfoTitle); 411 // Manifest::short_name that Manifest::name is used as the title.
412 {
413 // Check the case where we have no icons.
414 content::Manifest manifest = BuildDefaultManifest();
415 manifest.icons.clear();
416 manifest.short_name = base::NullableString16();
417 SetManifest(manifest);
418
419 ObserverWaiter waiter;
420 scoped_refptr<AddToHomescreenDataFetcher> fetcher(BuildFetcher(&waiter));
421 RunFetcher(fetcher, waiter, kDefaultManifestName,
422 blink::kWebDisplayModeStandalone, false);
423
424 EXPECT_TRUE(fetcher->primary_icon().drawsNothing());
425 EXPECT_TRUE(fetcher->shortcut_info().best_primary_icon_url.is_empty());
426 EXPECT_TRUE(base::EqualsASCII(fetcher->shortcut_info().short_name,
427 kDefaultManifestName));
428
429 fetcher->set_weak_observer(nullptr);
430 }
342 431
343 content::Manifest manifest(BuildDefaultManifest()); 432 content::Manifest manifest(BuildDefaultManifest());
344 manifest.name = base::NullableString16(); 433 manifest.short_name = base::NullableString16();
345 434 SetManifest(manifest);
346 RegisterServiceWorker(GURL(kDefaultStartUrl)); 435 SetShouldInstallableTimeOut(true);
347 SetManifest(GURL(kDefaultManifestUrl), manifest); 436
348 437 {
349 ObserverWaiter waiter; 438 // Check a site with no offline-capable service worker.
pkotwicz 2017/07/06 20:49:30 The comment for this test case is confusing. I am
dominickn 2017/07/10 02:37:44 Fixed. The intention is to exercise the three code
350 scoped_refptr<AddToHomescreenDataFetcher> fetcher(BuildFetcher(&waiter)); 439 ObserverWaiter waiter;
351 fetcher->OnDidGetWebApplicationInfo(web_application_info); 440 scoped_refptr<AddToHomescreenDataFetcher> fetcher(BuildFetcher(&waiter));
352 waiter.WaitForDataAvailable(); 441 RunFetcher(fetcher, waiter, kDefaultManifestName,
353 442 blink::kWebDisplayModeStandalone, false);
354 EXPECT_TRUE(base::EqualsASCII(waiter.title(), kDefaultManifestShortName)); 443
355 EXPECT_TRUE(base::EqualsASCII(fetcher->shortcut_info().name, 444 EXPECT_FALSE(fetcher->primary_icon().drawsNothing());
356 kDefaultManifestShortName)); 445 EXPECT_EQ(fetcher->shortcut_info().best_primary_icon_url,
357 446 GURL(kDefaultIconUrl));
358 fetcher->set_weak_observer(nullptr); 447 EXPECT_TRUE(base::EqualsASCII(fetcher->shortcut_info().short_name,
359 } 448 kDefaultManifestName));
360 449
361 // Test that when the manifest does not provide either Manifest::short_name nor 450 fetcher->set_weak_observer(nullptr);
362 // Manifest::name that: 451 }
363 // - The page is not WebAPK compatible. 452
364 // - WebApplicationInfo::title is used as the "name". 453 {
454 // Check a site with a service worker.
455 SetShouldInstallableTimeOut(false);
456 SetInstallable(true);
457 ObserverWaiter waiter;
458 scoped_refptr<AddToHomescreenDataFetcher> fetcher(BuildFetcher(&waiter));
459 RunFetcher(fetcher, waiter, kDefaultManifestName,
460 blink::kWebDisplayModeStandalone, check_webapk_compatibility());
461
462 EXPECT_FALSE(fetcher->primary_icon().drawsNothing());
463 EXPECT_EQ(fetcher->shortcut_info().best_primary_icon_url,
464 GURL(kDefaultIconUrl));
465 EXPECT_TRUE(base::EqualsASCII(fetcher->shortcut_info().short_name,
466 kDefaultManifestName));
467
468 fetcher->set_weak_observer(nullptr);
469 }
470 }
471
365 TEST_P(AddToHomescreenDataFetcherTestCommon, ManifestNoNameNoShortName) { 472 TEST_P(AddToHomescreenDataFetcherTestCommon, ManifestNoNameNoShortName) {
366 WebApplicationInfo web_application_info; 473 // Test that when the manifest does not provide either Manifest::short_name
367 web_application_info.title = base::UTF8ToUTF16(kWebApplicationInfoTitle); 474 // nor Manifest::name that:
368 475 // - The page is not WebAPK compatible.
476 // - WebApplicationInfo::title is used as the "name".
477 // - We still use the icons from the manifest.
369 content::Manifest manifest(BuildDefaultManifest()); 478 content::Manifest manifest(BuildDefaultManifest());
370 manifest.name = base::NullableString16(); 479 manifest.name = base::NullableString16();
371 manifest.short_name = base::NullableString16(); 480 manifest.short_name = base::NullableString16();
372 481
373 RegisterServiceWorker(GURL(kDefaultStartUrl)); 482 // Check the case where we don't time out waiting for the service worker.
374 SetManifest(GURL(kDefaultManifestUrl), manifest); 483 SetManifest(manifest);
375 484 ObserverWaiter waiter;
376 ObserverWaiter waiter; 485 scoped_refptr<AddToHomescreenDataFetcher> fetcher(BuildFetcher(&waiter));
377 scoped_refptr<AddToHomescreenDataFetcher> fetcher(BuildFetcher(&waiter)); 486 RunFetcher(fetcher, waiter, kWebApplicationInfoTitle,
378 fetcher->OnDidGetWebApplicationInfo(web_application_info); 487 blink::kWebDisplayModeStandalone, false);
379 waiter.WaitForDataAvailable(); 488
380
381 EXPECT_EQ(check_webapk_compatibility(),
382 waiter.determined_webapk_compatibility());
383 EXPECT_FALSE(waiter.is_webapk_compatible());
384 EXPECT_TRUE(base::EqualsASCII(waiter.title(), kWebApplicationInfoTitle));
385 EXPECT_TRUE(base::EqualsASCII(fetcher->shortcut_info().name, 489 EXPECT_TRUE(base::EqualsASCII(fetcher->shortcut_info().name,
386 kWebApplicationInfoTitle)); 490 kWebApplicationInfoTitle));
387 491 EXPECT_TRUE(base::EqualsASCII(fetcher->shortcut_info().short_name,
388 fetcher->set_weak_observer(nullptr); 492 kWebApplicationInfoTitle));
389 } 493 EXPECT_FALSE(fetcher->primary_icon().drawsNothing());
390 494 EXPECT_EQ(fetcher->shortcut_info().best_primary_icon_url,
391 // Checks that the AddToHomescreenDataFetcher::Observer callbacks are called 495 GURL(kDefaultIconUrl));
392 // when the manifest fetch times out. 496
393 TEST_P(AddToHomescreenDataFetcherTestCommon, ManifestFetchTimesOut) { 497 fetcher->set_weak_observer(nullptr);
394 WebApplicationInfo web_application_info; 498 }
395 web_application_info.title = base::UTF8ToUTF16(kWebApplicationInfoTitle);
396
397 RegisterServiceWorker(GURL(kDefaultStartUrl));
398 SetManifest(GURL(kDefaultManifestUrl), BuildDefaultManifest());
399 SetShouldManifestTimeOut(true);
400 SetShouldImageTimeOut(false);
401
402 ObserverWaiter waiter;
403 scoped_refptr<AddToHomescreenDataFetcher> fetcher(BuildFetcher(&waiter));
404 fetcher->OnDidGetWebApplicationInfo(web_application_info);
405 waiter.WaitForDataAvailable();
406
407 EXPECT_EQ(check_webapk_compatibility(),
408 waiter.determined_webapk_compatibility());
409 EXPECT_FALSE(waiter.is_webapk_compatible());
410 EXPECT_TRUE(base::EqualsASCII(waiter.title(), kWebApplicationInfoTitle));
411 EXPECT_TRUE(waiter.title_available());
412
413 fetcher->set_weak_observer(nullptr);
414 }
415
416 // Checks that the AddToHomescreenDataFetcher::Observer callbacks are called
417 // when the image fetch times out.
418 TEST_P(AddToHomescreenDataFetcherTestCommon, ImageFetchTimesOut) {
419 WebApplicationInfo web_application_info;
420 web_application_info.title = base::UTF8ToUTF16(kWebApplicationInfoTitle);
421
422 RegisterServiceWorker(GURL(kDefaultStartUrl));
423 SetManifest(GURL(kDefaultManifestUrl), BuildDefaultManifest());
424 SetShouldManifestTimeOut(false);
425 SetShouldImageTimeOut(true);
426
427 ObserverWaiter waiter;
428 scoped_refptr<AddToHomescreenDataFetcher> fetcher(BuildFetcher(&waiter));
429 fetcher->OnDidGetWebApplicationInfo(web_application_info);
430 waiter.WaitForDataAvailable();
431
432 EXPECT_EQ(check_webapk_compatibility(),
433 waiter.determined_webapk_compatibility());
434 EXPECT_FALSE(waiter.is_webapk_compatible());
435 EXPECT_TRUE(waiter.title_available());
436 EXPECT_TRUE(base::EqualsASCII(waiter.title(), kWebApplicationInfoTitle));
437
438 fetcher->set_weak_observer(nullptr);
439 }
440
441 // Checks that the AddToHomescreenDataFetcher::Observer callbacks are called
442 // when the service worker check times out.
443 TEST_P(AddToHomescreenDataFetcherTestCommon, ServiceWorkerCheckTimesOut) {
444 WebApplicationInfo web_application_info;
445 web_application_info.title = base::UTF8ToUTF16(kWebApplicationInfoTitle);
446
447 // Not registering a service worker means we'll wait and time out for the
448 // worker.
449 SetManifest(GURL(kDefaultManifestUrl), BuildDefaultManifest());
450 SetShouldManifestTimeOut(false);
451 SetShouldImageTimeOut(false);
452
453 ObserverWaiter waiter;
454 scoped_refptr<AddToHomescreenDataFetcher> fetcher(BuildFetcher(&waiter));
455 fetcher->OnDidGetWebApplicationInfo(web_application_info);
456 waiter.WaitForDataAvailable();
457
458 EXPECT_EQ(check_webapk_compatibility(),
459 waiter.determined_webapk_compatibility());
460 EXPECT_FALSE(waiter.is_webapk_compatible());
461 EXPECT_TRUE(waiter.title_available());
462 EXPECT_TRUE(base::EqualsASCII(waiter.title(), kDefaultManifestShortName));
463 EXPECT_TRUE(base::EqualsASCII(fetcher->shortcut_info().user_title,
464 kDefaultManifestShortName));
465
466 fetcher->set_weak_observer(nullptr);
467 }
468 499
469 INSTANTIATE_TEST_CASE_P(CheckWebApkCompatibility, 500 INSTANTIATE_TEST_CASE_P(CheckWebApkCompatibility,
470 AddToHomescreenDataFetcherTestCommon, 501 AddToHomescreenDataFetcherTestCommon,
471 ::testing::Values(false, true)); 502 ::testing::Values(false, true));
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/installable/installable_manager.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698