| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/manifest/manifest_icon_selector.h" | 5 #include "chrome/browser/manifest/manifest_icon_selector.h" |
| 6 | 6 |
| 7 #include <string> |
| 8 #include <vector> |
| 9 |
| 7 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 9 #include "ui/gfx/screen.h" | 12 #include "ui/gfx/screen.h" |
| 10 #include "ui/gfx/screen_type_delegate.h" | 13 #include "ui/gfx/screen_type_delegate.h" |
| 14 #include "ui/gfx/test/test_screen.h" |
| 11 | 15 |
| 12 namespace { | 16 namespace { |
| 13 | 17 |
| 14 const int kPreferredIconSize = 48; | 18 const int kPreferredIconSize = 48; |
| 15 | 19 |
| 16 } | 20 } |
| 17 | 21 |
| 18 // A dummy implementation of gfx::Screen, since ManifestIconSelector needs | 22 class ManifestIconSelectorTest : public testing::Test { |
| 19 // access to a gfx::Display's device scale factor. | 23 protected: |
| 20 // This is inspired by web_contents_video_capture_device_unittest.cc | 24 ManifestIconSelectorTest() { |
| 21 // A bug has been opened to merge all those mocks: http://crbug.com/417227 | 25 test_screen_.display()->set_id(0x1337); |
| 22 class FakeScreen : public gfx::Screen { | 26 test_screen_.display()->set_bounds(gfx::Rect(0, 0, 2560, 1440)); |
| 23 public: | |
| 24 FakeScreen() : display_(0x1337, gfx::Rect(0, 0, 2560, 1440)) { | |
| 25 } | |
| 26 ~FakeScreen() override {} | |
| 27 | |
| 28 void SetDisplayDeviceScaleFactor(float device_scale_factor) { | |
| 29 display_.set_device_scale_factor(device_scale_factor); | |
| 30 } | 27 } |
| 31 | 28 |
| 32 // gfx::Screen implementation (only what's needed for testing). | |
| 33 gfx::Point GetCursorScreenPoint() override { return gfx::Point(); } | |
| 34 gfx::NativeWindow GetWindowUnderCursor() override { return nullptr; } | |
| 35 gfx::NativeWindow GetWindowAtScreenPoint( | |
| 36 const gfx::Point& point) override { return nullptr; } | |
| 37 int GetNumDisplays() const override { return 1; } | |
| 38 std::vector<gfx::Display> GetAllDisplays() const override { | |
| 39 return std::vector<gfx::Display>(1, display_); | |
| 40 } | |
| 41 gfx::Display GetDisplayNearestWindow( | |
| 42 gfx::NativeView view) const override { | |
| 43 return display_; | |
| 44 } | |
| 45 gfx::Display GetDisplayNearestPoint( | |
| 46 const gfx::Point& point) const override { | |
| 47 return display_; | |
| 48 } | |
| 49 gfx::Display GetDisplayMatching( | |
| 50 const gfx::Rect& match_rect) const override { | |
| 51 return display_; | |
| 52 } | |
| 53 gfx::Display GetPrimaryDisplay() const override { | |
| 54 return display_; | |
| 55 } | |
| 56 void AddObserver(gfx::DisplayObserver* observer) override {} | |
| 57 void RemoveObserver(gfx::DisplayObserver* observer) override {} | |
| 58 | |
| 59 private: | |
| 60 gfx::Display display_; | |
| 61 | |
| 62 DISALLOW_COPY_AND_ASSIGN(FakeScreen); | |
| 63 }; | |
| 64 | |
| 65 class ManifestIconSelectorTest : public testing::Test { | |
| 66 protected: | |
| 67 ManifestIconSelectorTest() {} | |
| 68 ~ManifestIconSelectorTest() override {} | 29 ~ManifestIconSelectorTest() override {} |
| 69 | 30 |
| 70 GURL FindBestMatchingIcon(const std::vector<content::Manifest::Icon>& icons) { | 31 GURL FindBestMatchingIcon(const std::vector<content::Manifest::Icon>& icons) { |
| 71 return ManifestIconSelector::FindBestMatchingIcon( | 32 return ManifestIconSelector::FindBestMatchingIcon( |
| 72 icons, | 33 icons, GetPreferredIconSizeInDp(), &test_screen_); |
| 73 GetPreferredIconSizeInDp(), | |
| 74 &fake_screen_); | |
| 75 } | 34 } |
| 76 | 35 |
| 77 void SetDisplayDeviceScaleFactor(float device_scale_factor) { | 36 void SetDisplayDeviceScaleFactor(float device_scale_factor) { |
| 78 fake_screen_.SetDisplayDeviceScaleFactor(device_scale_factor); | 37 test_screen_.display()->set_device_scale_factor(device_scale_factor); |
| 79 } | 38 } |
| 80 | 39 |
| 81 static int GetPreferredIconSizeInDp() { | 40 static int GetPreferredIconSizeInDp() { |
| 82 return kPreferredIconSize; | 41 return kPreferredIconSize; |
| 83 } | 42 } |
| 84 | 43 |
| 85 static content::Manifest::Icon CreateIcon( | 44 static content::Manifest::Icon CreateIcon( |
| 86 const std::string& url, | 45 const std::string& url, |
| 87 const std::string& type, | 46 const std::string& type, |
| 88 double density, | 47 double density, |
| 89 const std::vector<gfx::Size> sizes) { | 48 const std::vector<gfx::Size> sizes) { |
| 90 content::Manifest::Icon icon; | 49 content::Manifest::Icon icon; |
| 91 icon.src = GURL(url); | 50 icon.src = GURL(url); |
| 92 if (!type.empty()) | 51 if (!type.empty()) |
| 93 icon.type = base::NullableString16(base::UTF8ToUTF16(type), false); | 52 icon.type = base::NullableString16(base::UTF8ToUTF16(type), false); |
| 94 icon.density = density; | 53 icon.density = density; |
| 95 icon.sizes = sizes; | 54 icon.sizes = sizes; |
| 96 | 55 |
| 97 return icon; | 56 return icon; |
| 98 } | 57 } |
| 99 | 58 |
| 100 private: | 59 private: |
| 101 FakeScreen fake_screen_; | 60 gfx::test::TestScreen test_screen_; |
| 102 | 61 |
| 103 DISALLOW_COPY_AND_ASSIGN(ManifestIconSelectorTest); | 62 DISALLOW_COPY_AND_ASSIGN(ManifestIconSelectorTest); |
| 104 }; | 63 }; |
| 105 | 64 |
| 106 TEST_F(ManifestIconSelectorTest, NoIcons) { | 65 TEST_F(ManifestIconSelectorTest, NoIcons) { |
| 107 // No icons should return the empty URL. | 66 // No icons should return the empty URL. |
| 108 std::vector<content::Manifest::Icon> icons; | 67 std::vector<content::Manifest::Icon> icons; |
| 109 GURL url = FindBestMatchingIcon(icons); | 68 GURL url = FindBestMatchingIcon(icons); |
| 110 EXPECT_TRUE(url.is_empty()); | 69 EXPECT_TRUE(url.is_empty()); |
| 111 } | 70 } |
| (...skipping 302 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 414 // 'any' (ie. gfx::Size(0,0)) should be used if there is no icon of a | 373 // 'any' (ie. gfx::Size(0,0)) should be used if there is no icon of a |
| 415 // preferred size. An icon with the current device scale factor is preferred | 374 // preferred size. An icon with the current device scale factor is preferred |
| 416 // over one with the default density. | 375 // over one with the default density. |
| 417 | 376 |
| 418 // 'any' with preferred size => preferred size | 377 // 'any' with preferred size => preferred size |
| 419 { | 378 { |
| 420 std::vector<gfx::Size> sizes_1; | 379 std::vector<gfx::Size> sizes_1; |
| 421 sizes_1.push_back(gfx::Size(GetPreferredIconSizeInDp(), | 380 sizes_1.push_back(gfx::Size(GetPreferredIconSizeInDp(), |
| 422 GetPreferredIconSizeInDp())); | 381 GetPreferredIconSizeInDp())); |
| 423 std::vector<gfx::Size> sizes_2; | 382 std::vector<gfx::Size> sizes_2; |
| 424 sizes_2.push_back(gfx::Size(0,0)); | 383 sizes_2.push_back(gfx::Size(0, 0)); |
| 425 | 384 |
| 426 std::vector<content::Manifest::Icon> icons; | 385 std::vector<content::Manifest::Icon> icons; |
| 427 icons.push_back(CreateIcon("http://foo.com/icon.png", "", 1.0, sizes_1)); | 386 icons.push_back(CreateIcon("http://foo.com/icon.png", "", 1.0, sizes_1)); |
| 428 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", 1.0, sizes_2)); | 387 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", 1.0, sizes_2)); |
| 429 | 388 |
| 430 GURL url = FindBestMatchingIcon(icons); | 389 GURL url = FindBestMatchingIcon(icons); |
| 431 EXPECT_EQ("http://foo.com/icon.png", url.spec()); | 390 EXPECT_EQ("http://foo.com/icon.png", url.spec()); |
| 432 } | 391 } |
| 433 | 392 |
| 434 // 'any' with nearly preferred size => any | 393 // 'any' with nearly preferred size => any |
| 435 { | 394 { |
| 436 std::vector<gfx::Size> sizes_1; | 395 std::vector<gfx::Size> sizes_1; |
| 437 sizes_1.push_back(gfx::Size(GetPreferredIconSizeInDp() + 1, | 396 sizes_1.push_back(gfx::Size(GetPreferredIconSizeInDp() + 1, |
| 438 GetPreferredIconSizeInDp() + 1)); | 397 GetPreferredIconSizeInDp() + 1)); |
| 439 std::vector<gfx::Size> sizes_2; | 398 std::vector<gfx::Size> sizes_2; |
| 440 sizes_2.push_back(gfx::Size(0,0)); | 399 sizes_2.push_back(gfx::Size(0, 0)); |
| 441 | 400 |
| 442 std::vector<content::Manifest::Icon> icons; | 401 std::vector<content::Manifest::Icon> icons; |
| 443 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", 1.0, sizes_1)); | 402 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", 1.0, sizes_1)); |
| 444 icons.push_back(CreateIcon("http://foo.com/icon.png", "", 1.0, sizes_2)); | 403 icons.push_back(CreateIcon("http://foo.com/icon.png", "", 1.0, sizes_2)); |
| 445 | 404 |
| 446 GURL url = FindBestMatchingIcon(icons); | 405 GURL url = FindBestMatchingIcon(icons); |
| 447 EXPECT_EQ("http://foo.com/icon.png", url.spec()); | 406 EXPECT_EQ("http://foo.com/icon.png", url.spec()); |
| 448 } | 407 } |
| 449 | 408 |
| 450 // 'any' on default density and current density => current density. | 409 // 'any' on default density and current density => current density. |
| 451 { | 410 { |
| 452 std::vector<gfx::Size> sizes; | 411 std::vector<gfx::Size> sizes; |
| 453 sizes.push_back(gfx::Size(0,0)); | 412 sizes.push_back(gfx::Size(0, 0)); |
| 454 | 413 |
| 455 std::vector<content::Manifest::Icon> icons; | 414 std::vector<content::Manifest::Icon> icons; |
| 456 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", 1.0, sizes)); | 415 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", 1.0, sizes)); |
| 457 icons.push_back(CreateIcon("http://foo.com/icon.png", "", 3.0, sizes)); | 416 icons.push_back(CreateIcon("http://foo.com/icon.png", "", 3.0, sizes)); |
| 458 | 417 |
| 459 SetDisplayDeviceScaleFactor(3.0f); | 418 SetDisplayDeviceScaleFactor(3.0f); |
| 460 GURL url = FindBestMatchingIcon(icons); | 419 GURL url = FindBestMatchingIcon(icons); |
| 461 EXPECT_EQ("http://foo.com/icon.png", url.spec()); | 420 EXPECT_EQ("http://foo.com/icon.png", url.spec()); |
| 462 } | 421 } |
| 463 } | 422 } |
| OLD | NEW |