Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / extensions / convert_web_app_unittest.cc
blobfcf8f9fdc0c418f2f22d75699d6e30c6e8ea20ae
1 // Copyright (c) 2012 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/convert_web_app.h"
7 #include <string>
8 #include <vector>
10 #include "base/file_util.h"
11 #include "base/files/file_path.h"
12 #include "base/files/scoped_temp_dir.h"
13 #include "base/path_service.h"
14 #include "base/strings/stringprintf.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "base/time/time.h"
17 #include "base/version.h"
18 #include "chrome/common/chrome_paths.h"
19 #include "chrome/common/extensions/extension_icon_set.h"
20 #include "chrome/common/extensions/manifest_handlers/app_launch_info.h"
21 #include "chrome/common/extensions/manifest_handlers/icons_handler.h"
22 #include "chrome/common/web_application_info.h"
23 #include "extensions/common/extension.h"
24 #include "extensions/common/extension_resource.h"
25 #include "extensions/common/permissions/permission_set.h"
26 #include "extensions/common/url_pattern.h"
27 #include "testing/gtest/include/gtest/gtest.h"
28 #include "ui/gfx/codec/png_codec.h"
29 #include "url/gurl.h"
31 namespace extensions {
33 namespace {
35 // Returns an icon info corresponding to a canned icon.
36 WebApplicationInfo::IconInfo GetIconInfo(const GURL& url, int size) {
37 WebApplicationInfo::IconInfo result;
39 base::FilePath icon_file;
40 if (!PathService::Get(chrome::DIR_TEST_DATA, &icon_file)) {
41 ADD_FAILURE() << "Could not get test data directory.";
42 return result;
45 icon_file = icon_file.AppendASCII("extensions")
46 .AppendASCII("convert_web_app")
47 .AppendASCII(base::StringPrintf("%i.png", size));
49 result.url = url;
50 result.width = size;
51 result.height = size;
53 std::string icon_data;
54 if (!base::ReadFileToString(icon_file, &icon_data)) {
55 ADD_FAILURE() << "Could not read test icon.";
56 return result;
59 if (!gfx::PNGCodec::Decode(
60 reinterpret_cast<const unsigned char*>(icon_data.c_str()),
61 icon_data.size(), &result.data)) {
62 ADD_FAILURE() << "Could not decode test icon.";
63 return result;
66 return result;
69 base::Time GetTestTime(int year, int month, int day, int hour, int minute,
70 int second, int millisecond) {
71 base::Time::Exploded exploded = {0};
72 exploded.year = year;
73 exploded.month = month;
74 exploded.day_of_month = day;
75 exploded.hour = hour;
76 exploded.minute = minute;
77 exploded.second = second;
78 exploded.millisecond = millisecond;
79 return base::Time::FromUTCExploded(exploded);
82 } // namespace
84 TEST(ExtensionFromWebApp, GenerateVersion) {
85 EXPECT_EQ("2010.1.1.0",
86 ConvertTimeToExtensionVersion(
87 GetTestTime(2010, 1, 1, 0, 0, 0, 0)));
88 EXPECT_EQ("2010.12.31.22111",
89 ConvertTimeToExtensionVersion(
90 GetTestTime(2010, 12, 31, 8, 5, 50, 500)));
91 EXPECT_EQ("2010.10.1.65535",
92 ConvertTimeToExtensionVersion(
93 GetTestTime(2010, 10, 1, 23, 59, 59, 999)));
96 TEST(ExtensionFromWebApp, Basic) {
97 base::ScopedTempDir extensions_dir;
98 ASSERT_TRUE(extensions_dir.CreateUniqueTempDir());
100 WebApplicationInfo web_app;
101 web_app.manifest_url = GURL("http://aaronboodman.com/gearpad/manifest.json");
102 web_app.title = base::ASCIIToUTF16("Gearpad");
103 web_app.description =
104 base::ASCIIToUTF16("The best text editor in the universe!");
105 web_app.app_url = GURL("http://aaronboodman.com/gearpad/");
106 web_app.permissions.push_back("geolocation");
107 web_app.permissions.push_back("notifications");
108 web_app.urls.push_back(GURL("http://aaronboodman.com/gearpad/"));
110 const int sizes[] = {16, 48, 128};
111 for (size_t i = 0; i < arraysize(sizes); ++i) {
112 GURL icon_url(
113 web_app.app_url.Resolve(base::StringPrintf("%i.png", sizes[i])));
114 web_app.icons.push_back(GetIconInfo(icon_url, sizes[i]));
117 scoped_refptr<Extension> extension = ConvertWebAppToExtension(
118 web_app, GetTestTime(1978, 12, 11, 0, 0, 0, 0),
119 extensions_dir.path());
120 ASSERT_TRUE(extension.get());
122 base::ScopedTempDir extension_dir;
123 EXPECT_TRUE(extension_dir.Set(extension->path()));
125 EXPECT_TRUE(extension->is_app());
126 EXPECT_TRUE(extension->is_hosted_app());
127 EXPECT_FALSE(extension->is_legacy_packaged_app());
129 EXPECT_EQ("lJqm1+jncOHClAuwif1QxNJKfeV9Fbl9IBZx7FkNwkA=",
130 extension->public_key());
131 EXPECT_EQ("ncnbaadanljoanockmphfdkimpdedemj", extension->id());
132 EXPECT_EQ("1978.12.11.0", extension->version()->GetString());
133 EXPECT_EQ(base::UTF16ToUTF8(web_app.title), extension->name());
134 EXPECT_EQ(base::UTF16ToUTF8(web_app.description), extension->description());
135 EXPECT_EQ(web_app.app_url, AppLaunchInfo::GetFullLaunchURL(extension.get()));
136 EXPECT_EQ(2u, extension->GetActivePermissions()->apis().size());
137 EXPECT_TRUE(extension->HasAPIPermission("geolocation"));
138 EXPECT_TRUE(extension->HasAPIPermission("notifications"));
139 ASSERT_EQ(1u, extension->web_extent().patterns().size());
140 EXPECT_EQ("http://aaronboodman.com/gearpad/*",
141 extension->web_extent().patterns().begin()->GetAsString());
143 EXPECT_EQ(web_app.icons.size(),
144 IconsInfo::GetIcons(extension.get()).map().size());
145 for (size_t i = 0; i < web_app.icons.size(); ++i) {
146 EXPECT_EQ(base::StringPrintf("icons/%i.png", web_app.icons[i].width),
147 IconsInfo::GetIcons(extension.get()).Get(
148 web_app.icons[i].width, ExtensionIconSet::MATCH_EXACTLY));
149 ExtensionResource resource =
150 IconsInfo::GetIconResource(extension.get(),
151 web_app.icons[i].width,
152 ExtensionIconSet::MATCH_EXACTLY);
153 ASSERT_TRUE(!resource.empty());
154 EXPECT_TRUE(base::PathExists(resource.GetFilePath()));
158 TEST(ExtensionFromWebApp, Minimal) {
159 base::ScopedTempDir extensions_dir;
160 ASSERT_TRUE(extensions_dir.CreateUniqueTempDir());
162 WebApplicationInfo web_app;
163 web_app.manifest_url = GURL("http://aaronboodman.com/gearpad/manifest.json");
164 web_app.title = base::ASCIIToUTF16("Gearpad");
165 web_app.app_url = GURL("http://aaronboodman.com/gearpad/");
167 scoped_refptr<Extension> extension = ConvertWebAppToExtension(
168 web_app, GetTestTime(1978, 12, 11, 0, 0, 0, 0),
169 extensions_dir.path());
170 ASSERT_TRUE(extension.get());
172 base::ScopedTempDir extension_dir;
173 EXPECT_TRUE(extension_dir.Set(extension->path()));
175 EXPECT_TRUE(extension->is_app());
176 EXPECT_TRUE(extension->is_hosted_app());
177 EXPECT_FALSE(extension->is_legacy_packaged_app());
179 EXPECT_EQ("lJqm1+jncOHClAuwif1QxNJKfeV9Fbl9IBZx7FkNwkA=",
180 extension->public_key());
181 EXPECT_EQ("ncnbaadanljoanockmphfdkimpdedemj", extension->id());
182 EXPECT_EQ("1978.12.11.0", extension->version()->GetString());
183 EXPECT_EQ(base::UTF16ToUTF8(web_app.title), extension->name());
184 EXPECT_EQ("", extension->description());
185 EXPECT_EQ(web_app.app_url, AppLaunchInfo::GetFullLaunchURL(extension.get()));
186 EXPECT_EQ(0u, IconsInfo::GetIcons(extension.get()).map().size());
187 EXPECT_EQ(0u, extension->GetActivePermissions()->apis().size());
188 ASSERT_EQ(1u, extension->web_extent().patterns().size());
189 EXPECT_EQ("*://aaronboodman.com/*",
190 extension->web_extent().patterns().begin()->GetAsString());
193 } // namespace extensions