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
3 // found in the LICENSE file.
5 #include "chrome/browser/banners/app_banner_data_fetcher.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "testing/gtest/include/gtest/gtest.h"
12 class AppBannerDataFetcherUnitTest
: public testing::Test
{
14 AppBannerDataFetcherUnitTest() { }
17 static base::NullableString16
ToNullableUTF16(const std::string
& str
) {
18 return base::NullableString16(base::UTF8ToUTF16(str
), false);
21 static content::Manifest
GetValidManifest() {
22 content::Manifest manifest
;
23 manifest
.name
= ToNullableUTF16("foo");
24 manifest
.short_name
= ToNullableUTF16("bar");
25 manifest
.start_url
= GURL("http://example.com");
27 content::Manifest::Icon icon
;
28 icon
.type
= ToNullableUTF16("image/png");
29 icon
.sizes
.push_back(gfx::Size(144, 144));
30 manifest
.icons
.push_back(icon
);
35 static bool IsManifestValid(const content::Manifest
& manifest
) {
36 // The second argument is the web_contents pointer, which is used for
37 // developer debug logging to the console. The logging is skipped inside the
38 // method if a null web_contents pointer is provided, so this is safe.
39 return AppBannerDataFetcher::IsManifestValidForWebApp(manifest
, nullptr);
43 TEST_F(AppBannerDataFetcherUnitTest
, EmptyManifestIsInvalid
) {
44 content::Manifest manifest
;
45 EXPECT_FALSE(IsManifestValid(manifest
));
48 TEST_F(AppBannerDataFetcherUnitTest
, CheckMinimalValidManifest
) {
49 content::Manifest manifest
= GetValidManifest();
50 EXPECT_TRUE(IsManifestValid(manifest
));
53 TEST_F(AppBannerDataFetcherUnitTest
, ManifestRequiresNameORShortName
) {
54 content::Manifest manifest
= GetValidManifest();
56 manifest
.name
= base::NullableString16();
57 EXPECT_TRUE(IsManifestValid(manifest
));
59 manifest
.name
= ToNullableUTF16("foo");
60 manifest
.short_name
= base::NullableString16();
61 EXPECT_TRUE(IsManifestValid(manifest
));
63 manifest
.name
= base::NullableString16();
64 EXPECT_FALSE(IsManifestValid(manifest
));
67 TEST_F(AppBannerDataFetcherUnitTest
, ManifestRequiresValidStartURL
) {
68 content::Manifest manifest
= GetValidManifest();
70 manifest
.start_url
= GURL();
71 EXPECT_FALSE(IsManifestValid(manifest
));
73 manifest
.start_url
= GURL("/");
74 EXPECT_FALSE(IsManifestValid(manifest
));
77 TEST_F(AppBannerDataFetcherUnitTest
, ManifestRequiresImagePNG
) {
78 content::Manifest manifest
= GetValidManifest();
80 manifest
.icons
[0].type
= ToNullableUTF16("image/gif");
81 EXPECT_FALSE(IsManifestValid(manifest
));
82 manifest
.icons
[0].type
= base::NullableString16();
83 EXPECT_FALSE(IsManifestValid(manifest
));
86 TEST_F(AppBannerDataFetcherUnitTest
, ManifestRequiresMinimalSize
) {
87 content::Manifest manifest
= GetValidManifest();
89 // The icon MUST be 144x144 size at least.
90 manifest
.icons
[0].sizes
[0] = gfx::Size(1, 1);
91 EXPECT_FALSE(IsManifestValid(manifest
));
93 // If one of the sizes match the requirement, it should be accepted.
94 manifest
.icons
[0].sizes
.push_back(gfx::Size(144, 144));
95 EXPECT_TRUE(IsManifestValid(manifest
));
97 // Higher than the required size is okay.
98 manifest
.icons
[0].sizes
[1] = gfx::Size(200, 200);
99 EXPECT_TRUE(IsManifestValid(manifest
));
101 // Non-square is okay.
102 manifest
.icons
[0].sizes
[1] = gfx::Size(144, 200);
103 EXPECT_TRUE(IsManifestValid(manifest
));
105 // The representation of the keyword 'any' should be recognized.
106 manifest
.icons
[0].sizes
[1] = gfx::Size(0, 0);
107 EXPECT_TRUE(IsManifestValid(manifest
));
110 } // namespace banners