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

Side by Side Diff: chrome/browser/android/shortcut_helper_unittest.cc

Issue 880203004: Break out manifest icon logic from ShortcutHelper (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Changing unittest Created 5 years, 10 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 | « chrome/browser/android/shortcut_helper.cc ('k') | chrome/chrome_browser.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/android/shortcut_helper.h"
6
7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
9 #include "content/public/browser/web_contents.h"
10 #include "ui/gfx/screen.h"
11 #include "ui/gfx/screen_type_delegate.h"
12
13 // A dummy implementation of gfx::Screen, since ShortcutHelper needs access to
14 // a gfx::Display's device scale factor.
15 // This is inspired by web_contents_video_capture_device_unittest.cc
16 // A bug has been opened to merge all those mocks: http://crbug.com/417227
17 class FakeScreen : public gfx::Screen {
18 public:
19 FakeScreen() : display_(0x1337, gfx::Rect(0, 0, 2560, 1440)) {
20 }
21 virtual ~FakeScreen() {}
22
23 void SetDisplayDeviceScaleFactor(float device_scale_factor) {
24 display_.set_device_scale_factor(device_scale_factor);
25 }
26
27 // gfx::Screen implementation (only what's needed for testing).
28 virtual gfx::Point GetCursorScreenPoint() override { return gfx::Point(); }
29 virtual gfx::NativeWindow GetWindowUnderCursor() override { return NULL; }
30 virtual gfx::NativeWindow GetWindowAtScreenPoint(
31 const gfx::Point& point) override { return NULL; }
32 virtual int GetNumDisplays() const override { return 1; }
33 virtual std::vector<gfx::Display> GetAllDisplays() const override {
34 return std::vector<gfx::Display>(1, display_);
35 }
36 virtual gfx::Display GetDisplayNearestWindow(
37 gfx::NativeView view) const override {
38 return display_;
39 }
40 virtual gfx::Display GetDisplayNearestPoint(
41 const gfx::Point& point) const override {
42 return display_;
43 }
44 virtual gfx::Display GetDisplayMatching(
45 const gfx::Rect& match_rect) const override {
46 return display_;
47 }
48 virtual gfx::Display GetPrimaryDisplay() const override {
49 return display_;
50 }
51 virtual void AddObserver(gfx::DisplayObserver* observer) override {}
52 virtual void RemoveObserver(gfx::DisplayObserver* observer) override {}
53
54 private:
55 gfx::Display display_;
56
57 DISALLOW_COPY_AND_ASSIGN(FakeScreen);
58 };
59
60 class ShortcutHelperTest : public ChromeRenderViewHostTestHarness {
61 protected:
62 ShortcutHelperTest() : shortcut_helper_(NULL) {}
63 virtual ~ShortcutHelperTest() {}
64
65 static jobject CreateShortcutHelperJava(JNIEnv* env) {
66 jclass clazz = env->FindClass("org/chromium/chrome/browser/ShortcutHelper");
67 jmethodID constructor =
68 env->GetMethodID(clazz, "<init>",
69 "(Landroid/content/Context;"
70 "Lorg/chromium/chrome/browser/Tab;)V");
71 return env->NewObject(clazz, constructor, jobject(), jobject());
72 }
73
74 void ResetShorcutHelper() {
75 if (shortcut_helper_)
76 delete shortcut_helper_;
77
78 JNIEnv* env = base::android::AttachCurrentThread();
79 shortcut_helper_ =
80 new ShortcutHelper(env, CreateShortcutHelperJava(env), web_contents());
81 }
82
83 virtual void SetUp() override {
84 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, &fake_screen_);
85 ASSERT_EQ(&fake_screen_, gfx::Screen::GetNativeScreen());
86
87 ChromeRenderViewHostTestHarness::SetUp();
88
89 ResetShorcutHelper();
90 }
91
92 virtual void TearDown() override {
93 delete shortcut_helper_;
94 shortcut_helper_ = NULL;
95
96 ChromeRenderViewHostTestHarness::TearDown();
97
98 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, NULL);
99 }
100
101 GURL FindBestMatchingIcon(const std::vector<content::Manifest::Icon>& icons) {
102 return shortcut_helper_->FindBestMatchingIcon(icons);
103 }
104
105 void SetDisplayDeviceScaleFactor(float device_scale_factor) {
106 fake_screen_.SetDisplayDeviceScaleFactor(device_scale_factor);
107
108 ResetShorcutHelper();
109 }
110
111 static int GetPreferredIconSizeInDp() {
112 return ShortcutHelper::kPreferredIconSizeInDp;
113 }
114
115 static content::Manifest::Icon CreateIcon(
116 const std::string& url,
117 const std::string& type,
118 double density,
119 const std::vector<gfx::Size> sizes) {
120 content::Manifest::Icon icon;
121 icon.src = GURL(url);
122 if (!type.empty())
123 icon.type = base::NullableString16(base::UTF8ToUTF16(type), false);
124 icon.density = density;
125 icon.sizes = sizes;
126
127 return icon;
128 }
129
130 private:
131 ShortcutHelper* shortcut_helper_;
132 FakeScreen fake_screen_;
133
134 DISALLOW_COPY_AND_ASSIGN(ShortcutHelperTest);
135 };
136
137 TEST_F(ShortcutHelperTest, NoIcons) {
138 // No icons should return the empty URL.
139 std::vector<content::Manifest::Icon> icons;
140 GURL url = FindBestMatchingIcon(icons);
141 EXPECT_TRUE(url.is_empty());
142 }
143
144 TEST_F(ShortcutHelperTest, NoSizes) {
145 // Icon with no sizes are ignored.
146 std::vector<content::Manifest::Icon> icons;
147 icons.push_back(
148 CreateIcon("http://foo.com/icon.png", "", 1.0, std::vector<gfx::Size>()));
149
150 GURL url = FindBestMatchingIcon(icons);
151 EXPECT_TRUE(url.is_empty());
152 }
153
154 TEST_F(ShortcutHelperTest, MIMETypeFiltering) {
155 // Icons with type specified to a MIME type that isn't a valid image MIME type
156 // are ignored.
157 std::vector<gfx::Size> sizes;
158 sizes.push_back(gfx::Size(10, 10));
159
160 std::vector<content::Manifest::Icon> icons;
161 icons.push_back(
162 CreateIcon("http://foo.com/icon.png", "image/foo_bar", 1.0, sizes));
163 icons.push_back(CreateIcon("http://foo.com/icon.png", "image/", 1.0, sizes));
164 icons.push_back(CreateIcon("http://foo.com/icon.png", "image/", 1.0, sizes));
165 icons.push_back(
166 CreateIcon("http://foo.com/icon.png", "video/mp4", 1.0, sizes));
167
168 GURL url = FindBestMatchingIcon(icons);
169 EXPECT_TRUE(url.is_empty());
170
171 icons.clear();
172 icons.push_back(
173 CreateIcon("http://foo.com/icon.png", "image/png", 1.0, sizes));
174 url = FindBestMatchingIcon(icons);
175 EXPECT_EQ("http://foo.com/icon.png", url.spec());
176
177 icons.clear();
178 icons.push_back(
179 CreateIcon("http://foo.com/icon.png", "image/gif", 1.0, sizes));
180 url = FindBestMatchingIcon(icons);
181 EXPECT_EQ("http://foo.com/icon.png", url.spec());
182
183 icons.clear();
184 icons.push_back(
185 CreateIcon("http://foo.com/icon.png", "image/jpeg", 1.0, sizes));
186 url = FindBestMatchingIcon(icons);
187 EXPECT_EQ("http://foo.com/icon.png", url.spec());
188 }
189
190 TEST_F(ShortcutHelperTest, PreferredSizeOfCurrentDensityIsUsedFirst) {
191 // This test has three icons each are marked with sizes set to the preferred
192 // icon size for the associated density.
193 std::vector<gfx::Size> sizes_1;
194 sizes_1.push_back(gfx::Size(GetPreferredIconSizeInDp(),
195 GetPreferredIconSizeInDp()));
196
197 std::vector<gfx::Size> sizes_2;
198 sizes_2.push_back(gfx::Size(GetPreferredIconSizeInDp() * 2,
199 GetPreferredIconSizeInDp() * 2));
200
201 std::vector<gfx::Size> sizes_3;
202 sizes_3.push_back(gfx::Size(GetPreferredIconSizeInDp() * 3,
203 GetPreferredIconSizeInDp() * 3));
204
205 std::vector<content::Manifest::Icon> icons;
206 icons.push_back(CreateIcon("http://foo.com/icon_x1.png", "", 1.0, sizes_1));
207 icons.push_back(CreateIcon("http://foo.com/icon_x2.png", "", 2.0, sizes_2));
208 icons.push_back(CreateIcon("http://foo.com/icon_x3.png", "", 3.0, sizes_3));
209
210 SetDisplayDeviceScaleFactor(1.0f);
211 GURL url = FindBestMatchingIcon(icons);
212 EXPECT_EQ("http://foo.com/icon_x1.png", url.spec());
213
214 SetDisplayDeviceScaleFactor(2.0f);
215 url = FindBestMatchingIcon(icons);
216 EXPECT_EQ("http://foo.com/icon_x2.png", url.spec());
217
218 SetDisplayDeviceScaleFactor(3.0f);
219 url = FindBestMatchingIcon(icons);
220 EXPECT_EQ("http://foo.com/icon_x3.png", url.spec());
221 }
222
223 TEST_F(ShortcutHelperTest, PreferredSizeOfDefaultDensityIsUsedSecond) {
224 // This test has three icons. The first one is of density zero and is marked
225 // with three sizes which are the preferred icon size for density 1, 2 and 3.
226 // The icon for density 2 and 3 have a size set to 2x2 and 3x3.
227 // Regardless of the device scale factor, the icon of density 1 is going to be
228 // used because it matches the preferred size.
229 std::vector<gfx::Size> sizes_1;
230 sizes_1.push_back(gfx::Size(GetPreferredIconSizeInDp(),
231 GetPreferredIconSizeInDp()));
232 sizes_1.push_back(gfx::Size(GetPreferredIconSizeInDp() * 2,
233 GetPreferredIconSizeInDp() * 2));
234 sizes_1.push_back(gfx::Size(GetPreferredIconSizeInDp() * 3,
235 GetPreferredIconSizeInDp() * 3));
236
237 std::vector<gfx::Size> sizes_2;
238 sizes_2.push_back(gfx::Size(2, 2));
239
240 std::vector<gfx::Size> sizes_3;
241 sizes_3.push_back(gfx::Size(3, 3));
242
243 std::vector<content::Manifest::Icon> icons;
244 icons.push_back(CreateIcon("http://foo.com/icon_x1.png", "", 1.0, sizes_1));
245 icons.push_back(CreateIcon("http://foo.com/icon_x2.png", "", 2.0, sizes_2));
246 icons.push_back(CreateIcon("http://foo.com/icon_x3.png", "", 3.0, sizes_3));
247
248 SetDisplayDeviceScaleFactor(1.0f);
249 GURL url = FindBestMatchingIcon(icons);
250 EXPECT_EQ("http://foo.com/icon_x1.png", url.spec());
251
252 SetDisplayDeviceScaleFactor(2.0f);
253 url = FindBestMatchingIcon(icons);
254 EXPECT_EQ("http://foo.com/icon_x1.png", url.spec());
255
256 SetDisplayDeviceScaleFactor(3.0f);
257 url = FindBestMatchingIcon(icons);
258 EXPECT_EQ("http://foo.com/icon_x1.png", url.spec());
259 }
260
261 TEST_F(ShortcutHelperTest, DeviceDensityFirst) {
262 // If there is no perfect icon but an icon of the current device density is
263 // present, it will be picked.
264 // This test has three icons each are marked with sizes set to the preferred
265 // icon size for the associated density.
266 std::vector<gfx::Size> sizes;
267 sizes.push_back(gfx::Size(2, 2));
268
269 std::vector<content::Manifest::Icon> icons;
270 icons.push_back(CreateIcon("http://foo.com/icon_x1.png", "", 1.0, sizes));
271 icons.push_back(CreateIcon("http://foo.com/icon_x2.png", "", 2.0, sizes));
272 icons.push_back(CreateIcon("http://foo.com/icon_x3.png", "", 3.0, sizes));
273
274 SetDisplayDeviceScaleFactor(1.0f);
275 GURL url = FindBestMatchingIcon(icons);
276 EXPECT_EQ("http://foo.com/icon_x1.png", url.spec());
277
278 SetDisplayDeviceScaleFactor(2.0f);
279 url = FindBestMatchingIcon(icons);
280 EXPECT_EQ("http://foo.com/icon_x2.png", url.spec());
281
282 SetDisplayDeviceScaleFactor(3.0f);
283 url = FindBestMatchingIcon(icons);
284 EXPECT_EQ("http://foo.com/icon_x3.png", url.spec());
285 }
286
287 TEST_F(ShortcutHelperTest, DeviceDensityFallback) {
288 // If there is no perfect icon but and no icon of the current display density,
289 // an icon of density 1.0 will be used.
290 std::vector<gfx::Size> sizes;
291 sizes.push_back(gfx::Size(2, 2));
292
293 std::vector<content::Manifest::Icon> icons;
294 icons.push_back(CreateIcon("http://foo.com/icon_x1.png", "", 1.0, sizes));
295 icons.push_back(CreateIcon("http://foo.com/icon_x2.png", "", 2.0, sizes));
296
297 SetDisplayDeviceScaleFactor(3.0f);
298 GURL url = FindBestMatchingIcon(icons);
299 EXPECT_EQ("http://foo.com/icon_x1.png", url.spec());
300 }
301
302 TEST_F(ShortcutHelperTest, DoNotUseOtherDensities) {
303 // If there are only icons of densities that are not the current display
304 // density or the default density, they are ignored.
305 std::vector<gfx::Size> sizes;
306 sizes.push_back(gfx::Size(2, 2));
307
308 std::vector<content::Manifest::Icon> icons;
309 icons.push_back(CreateIcon("http://foo.com/icon_x2.png", "", 2.0, sizes));
310
311 SetDisplayDeviceScaleFactor(3.0f);
312 GURL url = FindBestMatchingIcon(icons);
313 EXPECT_TRUE(url.is_empty());
314 }
315
316 TEST_F(ShortcutHelperTest, NotSquareIconsAreIgnored) {
317 std::vector<gfx::Size> sizes;
318 sizes.push_back(gfx::Size(20, 2));
319
320 std::vector<content::Manifest::Icon> icons;
321 icons.push_back(CreateIcon("http://foo.com/icon.png", "", 1.0, sizes));
322
323 GURL url = FindBestMatchingIcon(icons);
324 EXPECT_TRUE(url.is_empty());
325 }
326
327 TEST_F(ShortcutHelperTest, ClosestIconToPreferred) {
328 // This test verifies ShortcutHelper::FindBestMatchingIcon by passing
329 // different icon sizes and checking which one is picked.
330 // The Device Scale Factor is 1.0 and the preferred icon size is returned by
331 // GetPreferredIconSizeInDp().
332 int very_small = GetPreferredIconSizeInDp() / 4;
333 int small = GetPreferredIconSizeInDp() / 2;
334 int bit_small = GetPreferredIconSizeInDp() - 1;
335 int bit_big = GetPreferredIconSizeInDp() + 1;
336 int big = GetPreferredIconSizeInDp() * 2;
337 int very_big = GetPreferredIconSizeInDp() * 4;
338
339 // (very_small, bit_small) => bit_small
340 {
341 std::vector<gfx::Size> sizes_1;
342 sizes_1.push_back(gfx::Size(very_small, very_small));
343
344 std::vector<gfx::Size> sizes_2;
345 sizes_2.push_back(gfx::Size(bit_small, bit_small));
346
347 std::vector<content::Manifest::Icon> icons;
348 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", 1.0, sizes_1));
349 icons.push_back(CreateIcon("http://foo.com/icon.png", "", 1.0, sizes_2));
350
351 GURL url = FindBestMatchingIcon(icons);
352 EXPECT_EQ("http://foo.com/icon.png", url.spec());
353 }
354
355 // (very_small, bit_small, smaller) => bit_small
356 {
357 std::vector<gfx::Size> sizes_1;
358 sizes_1.push_back(gfx::Size(very_small, very_small));
359
360 std::vector<gfx::Size> sizes_2;
361 sizes_2.push_back(gfx::Size(bit_small, bit_small));
362
363 std::vector<gfx::Size> sizes_3;
364 sizes_3.push_back(gfx::Size(small, small));
365
366 std::vector<content::Manifest::Icon> icons;
367 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", 1.0, sizes_1));
368 icons.push_back(CreateIcon("http://foo.com/icon.png", "", 1.0, sizes_2));
369 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", 1.0, sizes_3));
370
371 GURL url = FindBestMatchingIcon(icons);
372 EXPECT_EQ("http://foo.com/icon.png", url.spec());
373 }
374
375 // (very_big, big) => big
376 {
377 std::vector<gfx::Size> sizes_1;
378 sizes_1.push_back(gfx::Size(very_big, very_big));
379
380 std::vector<gfx::Size> sizes_2;
381 sizes_2.push_back(gfx::Size(big, big));
382
383 std::vector<content::Manifest::Icon> icons;
384 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", 1.0, sizes_1));
385 icons.push_back(CreateIcon("http://foo.com/icon.png", "", 1.0, sizes_2));
386
387 GURL url = FindBestMatchingIcon(icons);
388 EXPECT_EQ("http://foo.com/icon.png", url.spec());
389 }
390
391 // (very_big, big, bit_big) => bit_big
392 {
393 std::vector<gfx::Size> sizes_1;
394 sizes_1.push_back(gfx::Size(very_big, very_big));
395
396 std::vector<gfx::Size> sizes_2;
397 sizes_2.push_back(gfx::Size(big, big));
398
399 std::vector<gfx::Size> sizes_3;
400 sizes_3.push_back(gfx::Size(bit_big, bit_big));
401
402 std::vector<content::Manifest::Icon> icons;
403 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", 1.0, sizes_1));
404 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", 1.0, sizes_2));
405 icons.push_back(CreateIcon("http://foo.com/icon.png", "", 1.0, sizes_3));
406
407 GURL url = FindBestMatchingIcon(icons);
408 EXPECT_EQ("http://foo.com/icon.png", url.spec());
409 }
410
411 // (bit_small, very_big) => very_big
412 {
413 std::vector<gfx::Size> sizes_1;
414 sizes_1.push_back(gfx::Size(bit_small, bit_small));
415
416 std::vector<gfx::Size> sizes_2;
417 sizes_2.push_back(gfx::Size(very_big, very_big));
418
419 std::vector<content::Manifest::Icon> icons;
420 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", 1.0, sizes_1));
421 icons.push_back(CreateIcon("http://foo.com/icon.png", "", 1.0, sizes_2));
422
423 GURL url = FindBestMatchingIcon(icons);
424 EXPECT_EQ("http://foo.com/icon.png", url.spec());
425 }
426
427 // (bit_small, bit_big) => bit_big
428 {
429 std::vector<gfx::Size> sizes_1;
430 sizes_1.push_back(gfx::Size(bit_small, bit_small));
431
432 std::vector<gfx::Size> sizes_2;
433 sizes_2.push_back(gfx::Size(bit_big, bit_big));
434
435 std::vector<content::Manifest::Icon> icons;
436 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", 1.0, sizes_1));
437 icons.push_back(CreateIcon("http://foo.com/icon.png", "", 1.0, sizes_2));
438
439 GURL url = FindBestMatchingIcon(icons);
440 EXPECT_EQ("http://foo.com/icon.png", url.spec());
441 }
442 }
443
444 TEST_F(ShortcutHelperTest, UseAnyIfNoPreferredSize) {
445 // 'any' (ie. gfx::Size(0,0)) should be used if there is no icon of a
446 // preferred size. An icon with the current device scale factor is preferred
447 // over one with the default density.
448
449 // 'any' with preferred size => preferred size
450 {
451 std::vector<gfx::Size> sizes_1;
452 sizes_1.push_back(gfx::Size(GetPreferredIconSizeInDp(),
453 GetPreferredIconSizeInDp()));
454 std::vector<gfx::Size> sizes_2;
455 sizes_2.push_back(gfx::Size(0,0));
456
457 std::vector<content::Manifest::Icon> icons;
458 icons.push_back(CreateIcon("http://foo.com/icon.png", "", 1.0, sizes_1));
459 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", 1.0, sizes_2));
460
461 GURL url = FindBestMatchingIcon(icons);
462 EXPECT_EQ("http://foo.com/icon.png", url.spec());
463 }
464
465 // 'any' with nearly preferred size => any
466 {
467 std::vector<gfx::Size> sizes_1;
468 sizes_1.push_back(gfx::Size(GetPreferredIconSizeInDp() + 1,
469 GetPreferredIconSizeInDp() + 1));
470 std::vector<gfx::Size> sizes_2;
471 sizes_2.push_back(gfx::Size(0,0));
472
473 std::vector<content::Manifest::Icon> icons;
474 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", 1.0, sizes_1));
475 icons.push_back(CreateIcon("http://foo.com/icon.png", "", 1.0, sizes_2));
476
477 GURL url = FindBestMatchingIcon(icons);
478 EXPECT_EQ("http://foo.com/icon.png", url.spec());
479 }
480
481 // 'any' on default density and current density => current density.
482 {
483 std::vector<gfx::Size> sizes;
484 sizes.push_back(gfx::Size(0,0));
485
486 std::vector<content::Manifest::Icon> icons;
487 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", 1.0, sizes));
488 icons.push_back(CreateIcon("http://foo.com/icon.png", "", 3.0, sizes));
489
490 SetDisplayDeviceScaleFactor(3.0f);
491 GURL url = FindBestMatchingIcon(icons);
492 EXPECT_EQ("http://foo.com/icon.png", url.spec());
493 }
494 }
OLDNEW
« no previous file with comments | « chrome/browser/android/shortcut_helper.cc ('k') | chrome/chrome_browser.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698