| 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/banners/app_banner_data_fetcher.h" | 5 #include "chrome/browser/banners/webapp_manifest_validator.h" |
| 6 | 6 |
| 7 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
| 8 #include "content/public/common/manifest.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 9 #include "third_party/WebKit/public/platform/WebDisplayMode.h" | 10 #include "third_party/WebKit/public/platform/WebDisplayMode.h" |
| 10 | 11 |
| 11 namespace banners { | 12 namespace banners { |
| 12 | 13 |
| 13 class AppBannerDataFetcherUnitTest : public testing::Test { | 14 class WebappManifestValidatorUnitTest : public testing::Test { |
| 14 public: | 15 public: |
| 15 AppBannerDataFetcherUnitTest() { } | 16 WebappManifestValidatorUnitTest() { } |
| 16 | 17 |
| 17 protected: | 18 protected: |
| 18 static base::NullableString16 ToNullableUTF16(const std::string& str) { | 19 static base::NullableString16 ToNullableUTF16(const std::string& str) { |
| 19 return base::NullableString16(base::UTF8ToUTF16(str), false); | 20 return base::NullableString16(base::UTF8ToUTF16(str), false); |
| 20 } | 21 } |
| 21 | 22 |
| 22 static content::Manifest GetValidManifest() { | 23 static content::Manifest GetValidManifest() { |
| 23 content::Manifest manifest; | 24 content::Manifest manifest; |
| 24 manifest.name = ToNullableUTF16("foo"); | 25 manifest.name = ToNullableUTF16("foo"); |
| 25 manifest.short_name = ToNullableUTF16("bar"); | 26 manifest.short_name = ToNullableUTF16("bar"); |
| 26 manifest.start_url = GURL("http://example.com"); | 27 manifest.start_url = GURL("http://example.com"); |
| 27 manifest.display = blink::WebDisplayModeStandalone; | 28 manifest.display = blink::WebDisplayModeStandalone; |
| 28 | 29 |
| 29 content::Manifest::Icon icon; | 30 content::Manifest::Icon icon; |
| 30 icon.type = ToNullableUTF16("image/png"); | 31 icon.type = ToNullableUTF16("image/png"); |
| 31 icon.sizes.push_back(gfx::Size(144, 144)); | 32 icon.sizes.push_back(gfx::Size(144, 144)); |
| 32 manifest.icons.push_back(icon); | 33 manifest.icons.push_back(icon); |
| 33 | 34 |
| 34 return manifest; | 35 return manifest; |
| 35 } | 36 } |
| 36 | 37 |
| 37 static bool IsManifestValid(const content::Manifest& manifest) { | 38 static bool IsManifestGood(const content::Manifest& manifest) { |
| 38 // The second argument is the web_contents pointer, which is used for | 39 // The second argument is the web_contents pointer, which is used for |
| 39 // developer debug logging to the console. The logging is skipped inside the | 40 // developer debug logging to the console. The logging is skipped inside the |
| 40 // method if a null web_contents pointer is provided, so this is safe. | 41 // method if a null web_contents pointer is provided, so this is safe. |
| 41 // The third argument is the is_debug_mode boolean value, which is true only | 42 // The third argument is the is_debug_mode boolean value, which is true only |
| 42 // when it is triggered by the developer's action in DevTools. | 43 // when it is triggered by the developer's action in DevTools. |
| 43 return AppBannerDataFetcher::IsManifestValidForWebApp(manifest, nullptr, | 44 OutputDeveloperMessageCode code = OutputDeveloperMessageCode::kNone; |
| 44 false); | 45 return webapp_manifest_validator::IsManifestGoodForWebapp(manifest, &code); |
| 45 } | 46 } |
| 46 }; | 47 }; |
| 47 | 48 |
| 48 TEST_F(AppBannerDataFetcherUnitTest, EmptyManifestIsInvalid) { | 49 TEST_F(WebappManifestValidatorUnitTest, EmptyManifestIsInvalid) { |
| 49 content::Manifest manifest; | 50 content::Manifest manifest; |
| 50 EXPECT_FALSE(IsManifestValid(manifest)); | 51 EXPECT_FALSE(IsManifestGood(manifest)); |
| 51 } | 52 } |
| 52 | 53 |
| 53 TEST_F(AppBannerDataFetcherUnitTest, CheckMinimalValidManifest) { | 54 TEST_F(WebappManifestValidatorUnitTest, CheckMinimalValidManifest) { |
| 54 content::Manifest manifest = GetValidManifest(); | 55 content::Manifest manifest = GetValidManifest(); |
| 55 EXPECT_TRUE(IsManifestValid(manifest)); | 56 EXPECT_TRUE(IsManifestGood(manifest)); |
| 56 } | 57 } |
| 57 | 58 |
| 58 TEST_F(AppBannerDataFetcherUnitTest, ManifestRequiresNameORShortName) { | 59 TEST_F(WebappManifestValidatorUnitTest, ManifestRequiresNameORShortName) { |
| 59 content::Manifest manifest = GetValidManifest(); | 60 content::Manifest manifest = GetValidManifest(); |
| 60 | 61 |
| 61 manifest.name = base::NullableString16(); | 62 manifest.name = base::NullableString16(); |
| 62 EXPECT_TRUE(IsManifestValid(manifest)); | 63 EXPECT_TRUE(IsManifestGood(manifest)); |
| 63 | 64 |
| 64 manifest.name = ToNullableUTF16("foo"); | 65 manifest.name = ToNullableUTF16("foo"); |
| 65 manifest.short_name = base::NullableString16(); | 66 manifest.short_name = base::NullableString16(); |
| 66 EXPECT_TRUE(IsManifestValid(manifest)); | 67 EXPECT_TRUE(IsManifestGood(manifest)); |
| 67 | 68 |
| 68 manifest.name = base::NullableString16(); | 69 manifest.name = base::NullableString16(); |
| 69 EXPECT_FALSE(IsManifestValid(manifest)); | 70 EXPECT_FALSE(IsManifestGood(manifest)); |
| 70 } | 71 } |
| 71 | 72 |
| 72 TEST_F(AppBannerDataFetcherUnitTest, ManifestRequiresNonEmptyNameORShortName) { | 73 TEST_F(WebappManifestValidatorUnitTest, |
| 74 ManifestRequiresNonEmptyNameORShortName) { |
| 73 content::Manifest manifest = GetValidManifest(); | 75 content::Manifest manifest = GetValidManifest(); |
| 74 | 76 |
| 75 manifest.name = ToNullableUTF16(""); | 77 manifest.name = ToNullableUTF16(""); |
| 76 EXPECT_TRUE(IsManifestValid(manifest)); | 78 EXPECT_TRUE(IsManifestGood(manifest)); |
| 77 | 79 |
| 78 manifest.name = ToNullableUTF16("foo"); | 80 manifest.name = ToNullableUTF16("foo"); |
| 79 manifest.short_name = ToNullableUTF16(""); | 81 manifest.short_name = ToNullableUTF16(""); |
| 80 EXPECT_TRUE(IsManifestValid(manifest)); | 82 EXPECT_TRUE(IsManifestGood(manifest)); |
| 81 | 83 |
| 82 manifest.name = ToNullableUTF16(""); | 84 manifest.name = ToNullableUTF16(""); |
| 83 EXPECT_FALSE(IsManifestValid(manifest)); | 85 EXPECT_FALSE(IsManifestGood(manifest)); |
| 84 } | 86 } |
| 85 | 87 |
| 86 TEST_F(AppBannerDataFetcherUnitTest, ManifestRequiresValidStartURL) { | 88 TEST_F(WebappManifestValidatorUnitTest, ManifestRequiresValidStartURL) { |
| 87 content::Manifest manifest = GetValidManifest(); | 89 content::Manifest manifest = GetValidManifest(); |
| 88 | 90 |
| 89 manifest.start_url = GURL(); | 91 manifest.start_url = GURL(); |
| 90 EXPECT_FALSE(IsManifestValid(manifest)); | 92 EXPECT_FALSE(IsManifestGood(manifest)); |
| 91 | 93 |
| 92 manifest.start_url = GURL("/"); | 94 manifest.start_url = GURL("/"); |
| 93 EXPECT_FALSE(IsManifestValid(manifest)); | 95 EXPECT_FALSE(IsManifestGood(manifest)); |
| 94 } | 96 } |
| 95 | 97 |
| 96 TEST_F(AppBannerDataFetcherUnitTest, ManifestRequiresImagePNG) { | 98 TEST_F(WebappManifestValidatorUnitTest, ManifestRequiresImagePNG) { |
| 97 content::Manifest manifest = GetValidManifest(); | 99 content::Manifest manifest = GetValidManifest(); |
| 98 | 100 |
| 99 manifest.icons[0].type = ToNullableUTF16("image/gif"); | 101 manifest.icons[0].type = ToNullableUTF16("image/gif"); |
| 100 EXPECT_FALSE(IsManifestValid(manifest)); | 102 EXPECT_FALSE(IsManifestGood(manifest)); |
| 101 manifest.icons[0].type = base::NullableString16(); | 103 manifest.icons[0].type = base::NullableString16(); |
| 102 EXPECT_FALSE(IsManifestValid(manifest)); | 104 EXPECT_FALSE(IsManifestGood(manifest)); |
| 103 } | 105 } |
| 104 | 106 |
| 105 TEST_F(AppBannerDataFetcherUnitTest, ManifestRequiresMinimalSize) { | 107 TEST_F(WebappManifestValidatorUnitTest, ManifestRequiresMinimalSize) { |
| 106 content::Manifest manifest = GetValidManifest(); | 108 content::Manifest manifest = GetValidManifest(); |
| 107 | 109 |
| 108 // The icon MUST be 144x144 size at least. | 110 // The icon MUST be 144x144 size at least. |
| 109 manifest.icons[0].sizes[0] = gfx::Size(1, 1); | 111 manifest.icons[0].sizes[0] = gfx::Size(1, 1); |
| 110 EXPECT_FALSE(IsManifestValid(manifest)); | 112 EXPECT_FALSE(IsManifestGood(manifest)); |
| 111 | 113 |
| 112 // If one of the sizes match the requirement, it should be accepted. | 114 // If one of the sizes match the requirement, it should be accepted. |
| 113 manifest.icons[0].sizes.push_back(gfx::Size(144, 144)); | 115 manifest.icons[0].sizes.push_back(gfx::Size(144, 144)); |
| 114 EXPECT_TRUE(IsManifestValid(manifest)); | 116 EXPECT_TRUE(IsManifestGood(manifest)); |
| 115 | 117 |
| 116 // Higher than the required size is okay. | 118 // Higher than the required size is okay. |
| 117 manifest.icons[0].sizes[1] = gfx::Size(200, 200); | 119 manifest.icons[0].sizes[1] = gfx::Size(200, 200); |
| 118 EXPECT_TRUE(IsManifestValid(manifest)); | 120 EXPECT_TRUE(IsManifestGood(manifest)); |
| 119 | 121 |
| 120 // Non-square is okay. | 122 // Non-square is okay. |
| 121 manifest.icons[0].sizes[1] = gfx::Size(144, 200); | 123 manifest.icons[0].sizes[1] = gfx::Size(144, 200); |
| 122 EXPECT_TRUE(IsManifestValid(manifest)); | 124 EXPECT_TRUE(IsManifestGood(manifest)); |
| 123 | 125 |
| 124 // The representation of the keyword 'any' should be recognized. | 126 // The representation of the keyword 'any' should be recognized. |
| 125 manifest.icons[0].sizes[1] = gfx::Size(0, 0); | 127 manifest.icons[0].sizes[1] = gfx::Size(0, 0); |
| 126 EXPECT_TRUE(IsManifestValid(manifest)); | 128 EXPECT_TRUE(IsManifestGood(manifest)); |
| 127 } | 129 } |
| 128 | 130 |
| 129 TEST_F(AppBannerDataFetcherUnitTest, ManifestDisplayStandaloneFullscreen) { | 131 TEST_F(WebappManifestValidatorUnitTest, ManifestDisplayStandaloneFullscreen) { |
| 130 content::Manifest manifest = GetValidManifest(); | 132 content::Manifest manifest = GetValidManifest(); |
| 131 | 133 |
| 132 manifest.display = blink::WebDisplayModeUndefined; | 134 manifest.display = blink::WebDisplayModeUndefined; |
| 133 EXPECT_FALSE(IsManifestValid(manifest)); | 135 EXPECT_FALSE(IsManifestGood(manifest)); |
| 134 | 136 |
| 135 manifest.display = blink::WebDisplayModeBrowser; | 137 manifest.display = blink::WebDisplayModeBrowser; |
| 136 EXPECT_FALSE(IsManifestValid(manifest)); | 138 EXPECT_FALSE(IsManifestGood(manifest)); |
| 137 | 139 |
| 138 manifest.display = blink::WebDisplayModeMinimalUi; | 140 manifest.display = blink::WebDisplayModeMinimalUi; |
| 139 EXPECT_FALSE(IsManifestValid(manifest)); | 141 EXPECT_FALSE(IsManifestGood(manifest)); |
| 140 | 142 |
| 141 manifest.display = blink::WebDisplayModeStandalone; | 143 manifest.display = blink::WebDisplayModeStandalone; |
| 142 EXPECT_TRUE(IsManifestValid(manifest)); | 144 EXPECT_TRUE(IsManifestGood(manifest)); |
| 143 | 145 |
| 144 manifest.display = blink::WebDisplayModeFullscreen; | 146 manifest.display = blink::WebDisplayModeFullscreen; |
| 145 EXPECT_TRUE(IsManifestValid(manifest)); | 147 EXPECT_TRUE(IsManifestGood(manifest)); |
| 146 } | 148 } |
| 147 | 149 |
| 148 } // namespace banners | 150 } // namespace banners |
| OLD | NEW |