summaryrefslogtreecommitdiffstats
path: root/chrome/browser/banners/app_banner_data_fetcher_unittest.cc
blob: 4be44feb74fac8636a73d5971473266ca8e8721a (plain)
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
// Copyright 2015 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/banners/app_banner_data_fetcher.h"

#include "base/strings/utf_string_conversions.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace banners {

class AppBannerDataFetcherUnitTest : public testing::Test {
 public:
  AppBannerDataFetcherUnitTest() { }

 protected:
  static base::NullableString16 ToNullableUTF16(const std::string& str) {
    return base::NullableString16(base::UTF8ToUTF16(str), false);
  }

  static content::Manifest GetValidManifest() {
    content::Manifest manifest;
    manifest.name = ToNullableUTF16("foo");
    manifest.short_name = ToNullableUTF16("bar");
    manifest.start_url = GURL("http://example.com");

    content::Manifest::Icon icon;
    icon.type = ToNullableUTF16("image/png");
    icon.sizes.push_back(gfx::Size(144, 144));
    manifest.icons.push_back(icon);

    return manifest;
  }

  static bool IsManifestValid(const content::Manifest& manifest) {
    // The second argument is the web_contents pointer, which is used for
    // developer debug logging to the console. The logging is skipped inside the
    // method if a null web_contents pointer is provided, so this is safe.
    // The third argument is the is_debug_mode boolean value, which is true only
    // when it is triggered by the developer's action in DevTools.
    return AppBannerDataFetcher::IsManifestValidForWebApp(manifest, nullptr,
                                                          false);
  }
};

TEST_F(AppBannerDataFetcherUnitTest, EmptyManifestIsInvalid) {
  content::Manifest manifest;
  EXPECT_FALSE(IsManifestValid(manifest));
}

TEST_F(AppBannerDataFetcherUnitTest, CheckMinimalValidManifest) {
  content::Manifest manifest = GetValidManifest();
  EXPECT_TRUE(IsManifestValid(manifest));
}

TEST_F(AppBannerDataFetcherUnitTest, ManifestRequiresNameORShortName) {
  content::Manifest manifest = GetValidManifest();

  manifest.name = base::NullableString16();
  EXPECT_TRUE(IsManifestValid(manifest));

  manifest.name = ToNullableUTF16("foo");
  manifest.short_name = base::NullableString16();
  EXPECT_TRUE(IsManifestValid(manifest));

  manifest.name = base::NullableString16();
  EXPECT_FALSE(IsManifestValid(manifest));
}

TEST_F(AppBannerDataFetcherUnitTest, ManifestRequiresValidStartURL) {
  content::Manifest manifest = GetValidManifest();

  manifest.start_url = GURL();
  EXPECT_FALSE(IsManifestValid(manifest));

  manifest.start_url = GURL("/");
  EXPECT_FALSE(IsManifestValid(manifest));
}

TEST_F(AppBannerDataFetcherUnitTest, ManifestRequiresImagePNG) {
  content::Manifest manifest = GetValidManifest();

  manifest.icons[0].type = ToNullableUTF16("image/gif");
  EXPECT_FALSE(IsManifestValid(manifest));
  manifest.icons[0].type = base::NullableString16();
  EXPECT_FALSE(IsManifestValid(manifest));
}

TEST_F(AppBannerDataFetcherUnitTest, ManifestRequiresMinimalSize) {
  content::Manifest manifest = GetValidManifest();

  // The icon MUST be 144x144 size at least.
  manifest.icons[0].sizes[0] = gfx::Size(1, 1);
  EXPECT_FALSE(IsManifestValid(manifest));

  // If one of the sizes match the requirement, it should be accepted.
  manifest.icons[0].sizes.push_back(gfx::Size(144, 144));
  EXPECT_TRUE(IsManifestValid(manifest));

  // Higher than the required size is okay.
  manifest.icons[0].sizes[1] = gfx::Size(200, 200);
  EXPECT_TRUE(IsManifestValid(manifest));

  // Non-square is okay.
  manifest.icons[0].sizes[1] = gfx::Size(144, 200);
  EXPECT_TRUE(IsManifestValid(manifest));

  // The representation of the keyword 'any' should be recognized.
  manifest.icons[0].sizes[1] = gfx::Size(0, 0);
  EXPECT_TRUE(IsManifestValid(manifest));
}

}  // namespace banners