OLD | NEW |
1 // Copyright 2015 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/banners/app_banner_data_fetcher.h" | 5 #include "chrome/browser/installable/installable_checker.h" |
6 | 6 |
7 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
9 #include "third_party/WebKit/public/platform/WebDisplayMode.h" | 9 #include "third_party/WebKit/public/platform/WebDisplayMode.h" |
10 | 10 |
11 namespace banners { | 11 namespace installable { |
12 | 12 |
13 class AppBannerDataFetcherUnitTest : public testing::Test { | 13 class InstallableCheckerUnitTest : public testing::Test { |
14 public: | 14 public: |
15 AppBannerDataFetcherUnitTest() { } | 15 InstallableCheckerUnitTest() : checker_(new InstallableChecker(nullptr)) { } |
16 | 16 |
17 protected: | 17 protected: |
18 static base::NullableString16 ToNullableUTF16(const std::string& str) { | 18 static base::NullableString16 ToNullableUTF16(const std::string& str) { |
19 return base::NullableString16(base::UTF8ToUTF16(str), false); | 19 return base::NullableString16(base::UTF8ToUTF16(str), false); |
20 } | 20 } |
21 | 21 |
22 static content::Manifest GetValidManifest() { | 22 static content::Manifest GetValidManifest() { |
23 content::Manifest manifest; | 23 content::Manifest manifest; |
24 manifest.name = ToNullableUTF16("foo"); | 24 manifest.name = ToNullableUTF16("foo"); |
25 manifest.short_name = ToNullableUTF16("bar"); | 25 manifest.short_name = ToNullableUTF16("bar"); |
26 manifest.start_url = GURL("http://example.com"); | 26 manifest.start_url = GURL("http://example.com"); |
27 manifest.display = blink::WebDisplayModeStandalone; | 27 manifest.display = blink::WebDisplayModeStandalone; |
28 | 28 |
29 content::Manifest::Icon icon; | 29 content::Manifest::Icon icon; |
30 icon.type = ToNullableUTF16("image/png"); | 30 icon.type = ToNullableUTF16("image/png"); |
31 icon.sizes.push_back(gfx::Size(144, 144)); | 31 icon.sizes.push_back(gfx::Size(144, 144)); |
32 manifest.icons.push_back(icon); | 32 manifest.icons.push_back(icon); |
33 | 33 |
34 return manifest; | 34 return manifest; |
35 } | 35 } |
36 | 36 |
37 static bool IsManifestValid(const content::Manifest& manifest) { | 37 bool IsManifestValid(const content::Manifest& manifest) { |
38 // The second argument is the web_contents pointer, which is used for | 38 // Explicitly reset the error code before running the method. |
39 // developer debug logging to the console. The logging is skipped inside the | 39 checker_->valid_webapp_manifest_error_ = NoErrorDetected; |
40 // method if a null web_contents pointer is provided, so this is safe. | 40 return checker_->IsManifestValidForWebApp(manifest); |
41 // 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 return AppBannerDataFetcher::IsManifestValidForWebApp(manifest, nullptr, | |
44 false); | |
45 } | 41 } |
| 42 |
| 43 ErrorCode GetErrorCode() { |
| 44 return checker_->valid_webapp_manifest_error_; |
| 45 } |
| 46 |
| 47 private: |
| 48 std::unique_ptr<InstallableChecker> checker_; |
46 }; | 49 }; |
47 | 50 |
48 TEST_F(AppBannerDataFetcherUnitTest, EmptyManifestIsInvalid) { | 51 TEST_F(InstallableCheckerUnitTest, EmptyManifestIsInvalid) { |
49 content::Manifest manifest; | 52 content::Manifest manifest; |
50 EXPECT_FALSE(IsManifestValid(manifest)); | 53 EXPECT_FALSE(IsManifestValid(manifest)); |
| 54 EXPECT_EQ(ManifestEmpty, GetErrorCode()); |
51 } | 55 } |
52 | 56 |
53 TEST_F(AppBannerDataFetcherUnitTest, CheckMinimalValidManifest) { | 57 TEST_F(InstallableCheckerUnitTest, CheckMinimalValidManifest) { |
54 content::Manifest manifest = GetValidManifest(); | 58 content::Manifest manifest = GetValidManifest(); |
55 EXPECT_TRUE(IsManifestValid(manifest)); | 59 EXPECT_TRUE(IsManifestValid(manifest)); |
| 60 EXPECT_EQ(NoErrorDetected, GetErrorCode()); |
56 } | 61 } |
57 | 62 |
58 TEST_F(AppBannerDataFetcherUnitTest, ManifestRequiresNameORShortName) { | 63 TEST_F(InstallableCheckerUnitTest, ManifestRequiresNameOrShortName) { |
59 content::Manifest manifest = GetValidManifest(); | 64 content::Manifest manifest = GetValidManifest(); |
60 | 65 |
61 manifest.name = base::NullableString16(); | 66 manifest.name = base::NullableString16(); |
62 EXPECT_TRUE(IsManifestValid(manifest)); | 67 EXPECT_TRUE(IsManifestValid(manifest)); |
| 68 EXPECT_EQ(NoErrorDetected, GetErrorCode()); |
63 | 69 |
64 manifest.name = ToNullableUTF16("foo"); | 70 manifest.name = ToNullableUTF16("foo"); |
65 manifest.short_name = base::NullableString16(); | 71 manifest.short_name = base::NullableString16(); |
66 EXPECT_TRUE(IsManifestValid(manifest)); | 72 EXPECT_TRUE(IsManifestValid(manifest)); |
| 73 EXPECT_EQ(NoErrorDetected, GetErrorCode()); |
67 | 74 |
68 manifest.name = base::NullableString16(); | 75 manifest.name = base::NullableString16(); |
69 EXPECT_FALSE(IsManifestValid(manifest)); | 76 EXPECT_FALSE(IsManifestValid(manifest)); |
| 77 EXPECT_EQ(ManifestMissingNameOrShortName, GetErrorCode()); |
70 } | 78 } |
71 | 79 |
72 TEST_F(AppBannerDataFetcherUnitTest, ManifestRequiresNonEmptyNameORShortName) { | 80 TEST_F(InstallableCheckerUnitTest, ManifestRequiresNonEmptyNameORShortName) { |
73 content::Manifest manifest = GetValidManifest(); | 81 content::Manifest manifest = GetValidManifest(); |
74 | 82 |
75 manifest.name = ToNullableUTF16(""); | 83 manifest.name = ToNullableUTF16(""); |
76 EXPECT_TRUE(IsManifestValid(manifest)); | 84 EXPECT_TRUE(IsManifestValid(manifest)); |
| 85 EXPECT_EQ(NoErrorDetected, GetErrorCode()); |
77 | 86 |
78 manifest.name = ToNullableUTF16("foo"); | 87 manifest.name = ToNullableUTF16("foo"); |
79 manifest.short_name = ToNullableUTF16(""); | 88 manifest.short_name = ToNullableUTF16(""); |
80 EXPECT_TRUE(IsManifestValid(manifest)); | 89 EXPECT_TRUE(IsManifestValid(manifest)); |
| 90 EXPECT_EQ(NoErrorDetected, GetErrorCode()); |
81 | 91 |
82 manifest.name = ToNullableUTF16(""); | 92 manifest.name = ToNullableUTF16(""); |
83 EXPECT_FALSE(IsManifestValid(manifest)); | 93 EXPECT_FALSE(IsManifestValid(manifest)); |
| 94 EXPECT_EQ(ManifestMissingNameOrShortName, GetErrorCode()); |
84 } | 95 } |
85 | 96 |
86 TEST_F(AppBannerDataFetcherUnitTest, ManifestRequiresValidStartURL) { | 97 TEST_F(InstallableCheckerUnitTest, ManifestRequiresValidStartURL) { |
87 content::Manifest manifest = GetValidManifest(); | 98 content::Manifest manifest = GetValidManifest(); |
88 | 99 |
89 manifest.start_url = GURL(); | 100 manifest.start_url = GURL(); |
90 EXPECT_FALSE(IsManifestValid(manifest)); | 101 EXPECT_FALSE(IsManifestValid(manifest)); |
| 102 EXPECT_EQ(StartUrlNotValid, GetErrorCode()); |
91 | 103 |
92 manifest.start_url = GURL("/"); | 104 manifest.start_url = GURL("/"); |
93 EXPECT_FALSE(IsManifestValid(manifest)); | 105 EXPECT_FALSE(IsManifestValid(manifest)); |
| 106 EXPECT_EQ(StartUrlNotValid, GetErrorCode()); |
94 } | 107 } |
95 | 108 |
96 TEST_F(AppBannerDataFetcherUnitTest, ManifestRequiresImagePNG) { | 109 TEST_F(InstallableCheckerUnitTest, ManifestRequiresImagePNG) { |
97 content::Manifest manifest = GetValidManifest(); | 110 content::Manifest manifest = GetValidManifest(); |
98 | 111 |
99 manifest.icons[0].type = ToNullableUTF16("image/gif"); | 112 manifest.icons[0].type = ToNullableUTF16("image/gif"); |
100 EXPECT_FALSE(IsManifestValid(manifest)); | 113 EXPECT_FALSE(IsManifestValid(manifest)); |
| 114 EXPECT_EQ(ManifestMissingSuitableIcon, GetErrorCode()); |
| 115 |
101 manifest.icons[0].type = base::NullableString16(); | 116 manifest.icons[0].type = base::NullableString16(); |
102 EXPECT_FALSE(IsManifestValid(manifest)); | 117 EXPECT_FALSE(IsManifestValid(manifest)); |
| 118 EXPECT_EQ(ManifestMissingSuitableIcon, GetErrorCode()); |
| 119 |
| 120 // If the type is null, the icon src will be checked instead. |
| 121 manifest.icons[0].src = GURL("http://example.com/icon.png"); |
| 122 EXPECT_TRUE(IsManifestValid(manifest)); |
| 123 EXPECT_EQ(NoErrorDetected, GetErrorCode()); |
| 124 |
| 125 // Capital file extension is also permissible. |
| 126 manifest.icons[0].src = GURL("http://example.com/icon.PNG"); |
| 127 EXPECT_TRUE(IsManifestValid(manifest)); |
| 128 EXPECT_EQ(NoErrorDetected, GetErrorCode()); |
| 129 |
| 130 // Non-png extensions are rejected. |
| 131 manifest.icons[0].src = GURL("http://example.com/icon.gif"); |
| 132 EXPECT_FALSE(IsManifestValid(manifest)); |
| 133 EXPECT_EQ(ManifestMissingSuitableIcon, GetErrorCode()); |
103 } | 134 } |
104 | 135 |
105 TEST_F(AppBannerDataFetcherUnitTest, ManifestRequiresMinimalSize) { | 136 TEST_F(InstallableCheckerUnitTest, ManifestRequiresMinimalSize) { |
106 content::Manifest manifest = GetValidManifest(); | 137 content::Manifest manifest = GetValidManifest(); |
107 | 138 |
108 // The icon MUST be 144x144 size at least. | 139 // The icon MUST be 144x144 size at least. |
109 manifest.icons[0].sizes[0] = gfx::Size(1, 1); | 140 manifest.icons[0].sizes[0] = gfx::Size(1, 1); |
110 EXPECT_FALSE(IsManifestValid(manifest)); | 141 EXPECT_FALSE(IsManifestValid(manifest)); |
| 142 EXPECT_EQ(ManifestMissingSuitableIcon, GetErrorCode()); |
| 143 |
| 144 manifest.icons[0].sizes[0] = gfx::Size(143, 143); |
| 145 EXPECT_FALSE(IsManifestValid(manifest)); |
| 146 EXPECT_EQ(ManifestMissingSuitableIcon, GetErrorCode()); |
111 | 147 |
112 // If one of the sizes match the requirement, it should be accepted. | 148 // If one of the sizes match the requirement, it should be accepted. |
113 manifest.icons[0].sizes.push_back(gfx::Size(144, 144)); | 149 manifest.icons[0].sizes.push_back(gfx::Size(144, 144)); |
114 EXPECT_TRUE(IsManifestValid(manifest)); | 150 EXPECT_TRUE(IsManifestValid(manifest)); |
| 151 EXPECT_EQ(NoErrorDetected, GetErrorCode()); |
115 | 152 |
116 // Higher than the required size is okay. | 153 // Higher than the required size is okay. |
117 manifest.icons[0].sizes[1] = gfx::Size(200, 200); | 154 manifest.icons[0].sizes[1] = gfx::Size(200, 200); |
118 EXPECT_TRUE(IsManifestValid(manifest)); | 155 EXPECT_TRUE(IsManifestValid(manifest)); |
| 156 EXPECT_EQ(NoErrorDetected, GetErrorCode()); |
119 | 157 |
120 // Non-square is okay. | 158 // Non-square is okay. |
121 manifest.icons[0].sizes[1] = gfx::Size(144, 200); | 159 manifest.icons[0].sizes[1] = gfx::Size(144, 200); |
122 EXPECT_TRUE(IsManifestValid(manifest)); | 160 EXPECT_TRUE(IsManifestValid(manifest)); |
| 161 EXPECT_EQ(NoErrorDetected, GetErrorCode()); |
123 | 162 |
124 // The representation of the keyword 'any' should be recognized. | 163 // The representation of the keyword 'any' should be recognized. |
125 manifest.icons[0].sizes[1] = gfx::Size(0, 0); | 164 manifest.icons[0].sizes[1] = gfx::Size(0, 0); |
126 EXPECT_TRUE(IsManifestValid(manifest)); | 165 EXPECT_TRUE(IsManifestValid(manifest)); |
| 166 EXPECT_EQ(NoErrorDetected, GetErrorCode()); |
127 } | 167 } |
128 | 168 |
129 TEST_F(AppBannerDataFetcherUnitTest, ManifestDisplayStandaloneFullscreen) { | 169 TEST_F(InstallableCheckerUnitTest, ManifestDisplayStandaloneFullscreen) { |
130 content::Manifest manifest = GetValidManifest(); | 170 content::Manifest manifest = GetValidManifest(); |
131 | 171 |
132 manifest.display = blink::WebDisplayModeUndefined; | 172 manifest.display = blink::WebDisplayModeUndefined; |
133 EXPECT_FALSE(IsManifestValid(manifest)); | 173 EXPECT_FALSE(IsManifestValid(manifest)); |
| 174 EXPECT_EQ(ManifestDisplayIsNotStandaloneOrFullscreen, GetErrorCode()); |
134 | 175 |
135 manifest.display = blink::WebDisplayModeBrowser; | 176 manifest.display = blink::WebDisplayModeBrowser; |
136 EXPECT_FALSE(IsManifestValid(manifest)); | 177 EXPECT_FALSE(IsManifestValid(manifest)); |
| 178 EXPECT_EQ(ManifestDisplayIsNotStandaloneOrFullscreen, GetErrorCode()); |
137 | 179 |
138 manifest.display = blink::WebDisplayModeMinimalUi; | 180 manifest.display = blink::WebDisplayModeMinimalUi; |
139 EXPECT_FALSE(IsManifestValid(manifest)); | 181 EXPECT_FALSE(IsManifestValid(manifest)); |
| 182 EXPECT_EQ(ManifestDisplayIsNotStandaloneOrFullscreen, GetErrorCode()); |
140 | 183 |
141 manifest.display = blink::WebDisplayModeStandalone; | 184 manifest.display = blink::WebDisplayModeStandalone; |
142 EXPECT_TRUE(IsManifestValid(manifest)); | 185 EXPECT_TRUE(IsManifestValid(manifest)); |
| 186 EXPECT_EQ(NoErrorDetected, GetErrorCode()); |
143 | 187 |
144 manifest.display = blink::WebDisplayModeFullscreen; | 188 manifest.display = blink::WebDisplayModeFullscreen; |
145 EXPECT_TRUE(IsManifestValid(manifest)); | 189 EXPECT_TRUE(IsManifestValid(manifest)); |
| 190 EXPECT_EQ(NoErrorDetected, GetErrorCode()); |
146 } | 191 } |
147 | 192 |
148 } // namespace banners | 193 } // namespace installable |
OLD | NEW |