EME test page application.
[chromium-blink-merge.git] / chrome / browser / extensions / bookmark_app_helper_unittest.cc
blob66454a05a102ed52af11172fab554984cd8df125
1 // Copyright 2014 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/extensions/bookmark_app_helper.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/browser/extensions/extension_service_unittest.h"
9 #include "chrome/common/extensions/manifest_handlers/app_launch_info.h"
10 #include "chrome/test/base/testing_profile.h"
11 #include "extensions/browser/extension_registry.h"
12 #include "extensions/common/extension_icon_set.h"
13 #include "extensions/common/manifest_handlers/icons_handler.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "third_party/skia/include/core/SkBitmap.h"
16 #include "ui/gfx/skia_util.h"
18 namespace {
20 #if !defined(OS_ANDROID)
21 const char kAppUrl[] = "http://www.chromium.org";
22 const char kAppTitle[] = "Test title";
23 const char kAlternativeAppTitle[] = "Different test title";
24 const char kAppDescription[] = "Test description";
26 const int kIconSizeSmall = extension_misc::EXTENSION_ICON_SMALL;
27 const int kIconSizeLarge = extension_misc::EXTENSION_ICON_LARGE;
28 #endif
30 class BookmarkAppHelperTest : public testing::Test {
31 public:
32 BookmarkAppHelperTest() {}
33 virtual ~BookmarkAppHelperTest() {}
35 private:
36 DISALLOW_COPY_AND_ASSIGN(BookmarkAppHelperTest);
39 class BookmarkAppHelperExtensionServiceTest : public ExtensionServiceTestBase {
40 public:
41 BookmarkAppHelperExtensionServiceTest() {}
42 virtual ~BookmarkAppHelperExtensionServiceTest() {}
44 virtual void SetUp() OVERRIDE {
45 ExtensionServiceTestBase::SetUp();
46 InitializeEmptyExtensionService();
47 service_->Init();
48 EXPECT_EQ(0u, service_->extensions()->size());
51 private:
52 DISALLOW_COPY_AND_ASSIGN(BookmarkAppHelperExtensionServiceTest);
55 SkBitmap CreateSquareBitmapWithColor(int size, SkColor color) {
56 SkBitmap bitmap;
57 bitmap.setConfig(SkBitmap::kARGB_8888_Config, size, size);
58 bitmap.allocPixels();
59 bitmap.eraseColor(color);
60 return bitmap;
63 void ValidateBitmapSizeAndColor(SkBitmap bitmap, int size, SkColor color) {
64 // Obtain pixel lock to access pixels.
65 SkAutoLockPixels lock(bitmap);
66 EXPECT_EQ(color, bitmap.getColor(0, 0));
67 EXPECT_EQ(size, bitmap.width());
68 EXPECT_EQ(size, bitmap.height());
71 #if !defined(OS_ANDROID)
72 WebApplicationInfo::IconInfo CreateIconInfoWithBitmap(int size, SkColor color) {
73 WebApplicationInfo::IconInfo icon_info;
74 icon_info.width = size;
75 icon_info.height = size;
76 icon_info.data = CreateSquareBitmapWithColor(size, color);
77 return icon_info;
80 void ValidateWebApplicationInfo(base::Closure callback,
81 const WebApplicationInfo& expected,
82 const WebApplicationInfo& actual) {
83 EXPECT_EQ(expected.title, actual.title);
84 EXPECT_EQ(expected.description, actual.description);
85 EXPECT_EQ(expected.app_url, actual.app_url);
86 EXPECT_EQ(expected.icons.size(), actual.icons.size());
87 for (size_t i = 0; i < expected.icons.size(); ++i) {
88 EXPECT_EQ(expected.icons[i].width, actual.icons[i].width);
89 EXPECT_EQ(expected.icons[i].height, actual.icons[i].height);
90 EXPECT_EQ(expected.icons[i].url, actual.icons[i].url);
91 EXPECT_TRUE(
92 gfx::BitmapsAreEqual(expected.icons[i].data, actual.icons[i].data));
94 callback.Run();
96 #endif
98 } // namespace
100 namespace extensions {
102 class TestBookmarkAppHelper : public BookmarkAppHelper {
103 public:
104 TestBookmarkAppHelper(ExtensionService* service,
105 WebApplicationInfo web_app_info,
106 content::WebContents* contents)
107 : BookmarkAppHelper(service, web_app_info, contents) {}
109 virtual ~TestBookmarkAppHelper() {}
111 void CreationComplete(const extensions::Extension* extension,
112 const WebApplicationInfo& web_app_info) {
113 extension_ = extension;
116 void CompleteIconDownload(
117 bool success,
118 const std::map<GURL, std::vector<SkBitmap> >& bitmaps) {
119 BookmarkAppHelper::OnIconsDownloaded(success, bitmaps);
122 const Extension* extension() { return extension_; }
124 private:
125 const Extension* extension_;
127 DISALLOW_COPY_AND_ASSIGN(TestBookmarkAppHelper);
130 // Android doesn't support extensions.
131 #if !defined(OS_ANDROID)
132 TEST_F(BookmarkAppHelperExtensionServiceTest, CreateBookmarkApp) {
133 WebApplicationInfo web_app_info;
134 web_app_info.app_url = GURL(kAppUrl);
135 web_app_info.title = base::UTF8ToUTF16(kAppTitle);
136 web_app_info.description = base::UTF8ToUTF16(kAppDescription);
138 TestBookmarkAppHelper helper(service_, web_app_info, NULL);
139 helper.Create(base::Bind(&TestBookmarkAppHelper::CreationComplete,
140 base::Unretained(&helper)));
142 std::map<GURL, std::vector<SkBitmap> > icon_map;
143 icon_map[GURL(kAppUrl)].push_back(
144 CreateSquareBitmapWithColor(kIconSizeSmall, SK_ColorRED));
145 helper.CompleteIconDownload(true, icon_map);
147 base::RunLoop().RunUntilIdle();
148 EXPECT_TRUE(helper.extension());
149 const Extension* extension =
150 service_->GetInstalledExtension(helper.extension()->id());
151 EXPECT_TRUE(extension);
152 EXPECT_EQ(1u, service_->extensions()->size());
153 EXPECT_TRUE(extension->from_bookmark());
154 EXPECT_EQ(kAppTitle, extension->name());
155 EXPECT_EQ(kAppDescription, extension->description());
156 EXPECT_EQ(GURL(kAppUrl), AppLaunchInfo::GetLaunchWebURL(extension));
157 EXPECT_FALSE(
158 IconsInfo::GetIconResource(
159 extension, kIconSizeSmall, ExtensionIconSet::MATCH_EXACTLY).empty());
162 TEST_F(BookmarkAppHelperExtensionServiceTest, CreateAndUpdateBookmarkApp) {
163 EXPECT_EQ(0u, registry_->enabled_extensions().size());
164 WebApplicationInfo web_app_info;
165 web_app_info.app_url = GURL(kAppUrl);
166 web_app_info.title = base::UTF8ToUTF16(kAppTitle);
167 web_app_info.description = base::UTF8ToUTF16(kAppDescription);
168 web_app_info.icons.push_back(
169 CreateIconInfoWithBitmap(kIconSizeSmall, SK_ColorRED));
171 extensions::CreateOrUpdateBookmarkApp(service_, web_app_info);
172 base::RunLoop().RunUntilIdle();
175 EXPECT_EQ(1u, registry_->enabled_extensions().size());
176 const Extension* extension = service_->extensions()->begin()->get();
177 EXPECT_TRUE(extension->from_bookmark());
178 EXPECT_EQ(kAppTitle, extension->name());
179 EXPECT_EQ(kAppDescription, extension->description());
180 EXPECT_EQ(GURL(kAppUrl), AppLaunchInfo::GetLaunchWebURL(extension));
181 EXPECT_FALSE(extensions::IconsInfo::GetIconResource(
182 extension, kIconSizeSmall, ExtensionIconSet::MATCH_EXACTLY)
183 .empty());
186 web_app_info.title = base::UTF8ToUTF16(kAlternativeAppTitle);
187 web_app_info.icons[0] = CreateIconInfoWithBitmap(kIconSizeLarge, SK_ColorRED);
189 extensions::CreateOrUpdateBookmarkApp(service_, web_app_info);
190 base::RunLoop().RunUntilIdle();
193 EXPECT_EQ(1u, registry_->enabled_extensions().size());
194 const Extension* extension = service_->extensions()->begin()->get();
195 EXPECT_TRUE(extension->from_bookmark());
196 EXPECT_EQ(kAlternativeAppTitle, extension->name());
197 EXPECT_EQ(kAppDescription, extension->description());
198 EXPECT_EQ(GURL(kAppUrl), AppLaunchInfo::GetLaunchWebURL(extension));
199 EXPECT_TRUE(extensions::IconsInfo::GetIconResource(
200 extension, kIconSizeSmall, ExtensionIconSet::MATCH_EXACTLY)
201 .empty());
202 EXPECT_FALSE(extensions::IconsInfo::GetIconResource(
203 extension, kIconSizeLarge, ExtensionIconSet::MATCH_EXACTLY)
204 .empty());
208 TEST_F(BookmarkAppHelperExtensionServiceTest, GetWebApplicationInfo) {
209 WebApplicationInfo web_app_info;
210 web_app_info.app_url = GURL(kAppUrl);
211 web_app_info.title = base::UTF8ToUTF16(kAppTitle);
212 web_app_info.description = base::UTF8ToUTF16(kAppDescription);
214 web_app_info.icons.push_back(
215 CreateIconInfoWithBitmap(kIconSizeSmall, SK_ColorRED));
216 web_app_info.icons.push_back(
217 CreateIconInfoWithBitmap(kIconSizeLarge, SK_ColorRED));
219 extensions::CreateOrUpdateBookmarkApp(service_, web_app_info);
220 base::RunLoop().RunUntilIdle();
222 EXPECT_EQ(1u, registry_->enabled_extensions().size());
223 base::RunLoop run_loop;
224 extensions::GetWebApplicationInfoFromApp(
225 profile_.get(),
226 service_->extensions()->begin()->get(),
227 base::Bind(
228 &ValidateWebApplicationInfo, run_loop.QuitClosure(), web_app_info));
229 run_loop.Run();
231 #endif
233 TEST_F(BookmarkAppHelperTest, ConstrainBitmapsToSizes) {
234 std::set<int> desired_sizes;
235 desired_sizes.insert(16);
236 desired_sizes.insert(32);
237 desired_sizes.insert(128);
238 desired_sizes.insert(256);
241 std::vector<SkBitmap> bitmaps;
242 bitmaps.push_back(CreateSquareBitmapWithColor(16, SK_ColorRED));
243 bitmaps.push_back(CreateSquareBitmapWithColor(32, SK_ColorGREEN));
244 bitmaps.push_back(CreateSquareBitmapWithColor(48, SK_ColorBLUE));
245 bitmaps.push_back(CreateSquareBitmapWithColor(144, SK_ColorYELLOW));
247 std::map<int, SkBitmap> results(
248 BookmarkAppHelper::ConstrainBitmapsToSizes(bitmaps, desired_sizes));
250 EXPECT_EQ(3u, results.size());
251 ValidateBitmapSizeAndColor(results[16], 16, SK_ColorRED);
252 ValidateBitmapSizeAndColor(results[32], 32, SK_ColorGREEN);
253 ValidateBitmapSizeAndColor(results[128], 128, SK_ColorYELLOW);
256 std::vector<SkBitmap> bitmaps;
257 bitmaps.push_back(CreateSquareBitmapWithColor(512, SK_ColorRED));
258 bitmaps.push_back(CreateSquareBitmapWithColor(18, SK_ColorGREEN));
259 bitmaps.push_back(CreateSquareBitmapWithColor(33, SK_ColorBLUE));
260 bitmaps.push_back(CreateSquareBitmapWithColor(17, SK_ColorYELLOW));
262 std::map<int, SkBitmap> results(
263 BookmarkAppHelper::ConstrainBitmapsToSizes(bitmaps, desired_sizes));
265 EXPECT_EQ(3u, results.size());
266 ValidateBitmapSizeAndColor(results[16], 16, SK_ColorYELLOW);
267 ValidateBitmapSizeAndColor(results[32], 32, SK_ColorBLUE);
268 ValidateBitmapSizeAndColor(results[256], 256, SK_ColorRED);
272 TEST_F(BookmarkAppHelperTest, IsValidBookmarkAppUrl) {
273 EXPECT_TRUE(IsValidBookmarkAppUrl(GURL("https://www.chromium.org")));
274 EXPECT_TRUE(IsValidBookmarkAppUrl(GURL("http://www.chromium.org/path")));
275 EXPECT_FALSE(IsValidBookmarkAppUrl(GURL("ftp://www.chromium.org")));
276 EXPECT_FALSE(IsValidBookmarkAppUrl(GURL("chrome://flags")));
279 } // namespace extensions