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