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.
4
5 #include "chrome/browser/extensions/convert_web_app.h"
6
7 #include <string>
8 #include <vector>
9
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"
30
31 namespace extensions {
32
33 namespace {
34
35 // Returns an icon info corresponding to a canned icon.
GetIconInfo(const GURL & url,int size)36 WebApplicationInfo::IconInfo GetIconInfo(const GURL& url, int size) {
37 WebApplicationInfo::IconInfo result;
38
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;
43 }
44
45 icon_file = icon_file.AppendASCII("extensions")
46 .AppendASCII("convert_web_app")
47 .AppendASCII(base::StringPrintf("%i.png", size));
48
49 result.url = url;
50 result.width = size;
51 result.height = size;
52
53 std::string icon_data;
54 if (!base::ReadFileToString(icon_file, &icon_data)) {
55 ADD_FAILURE() << "Could not read test icon.";
56 return result;
57 }
58
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;
64 }
65
66 return result;
67 }
68
GetTestTime(int year,int month,int day,int hour,int minute,int second,int millisecond)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);
80 }
81
82 } // namespace
83
TEST(ExtensionFromWebApp,GenerateVersion)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)));
94 }
95
TEST(ExtensionFromWebApp,Basic)96 TEST(ExtensionFromWebApp, Basic) {
97 base::ScopedTempDir extensions_dir;
98 ASSERT_TRUE(extensions_dir.CreateUniqueTempDir());
99
100 WebApplicationInfo web_app;
101 web_app.manifest_url = GURL("http://aaronboodman.com/gearpad/manifest.json");
102 web_app.title = ASCIIToUTF16("Gearpad");
103 web_app.description = ASCIIToUTF16("The best text editor in the universe!");
104 web_app.app_url = GURL("http://aaronboodman.com/gearpad/");
105 web_app.permissions.push_back("geolocation");
106 web_app.permissions.push_back("notifications");
107 web_app.urls.push_back(GURL("http://aaronboodman.com/gearpad/"));
108
109 const int sizes[] = {16, 48, 128};
110 for (size_t i = 0; i < arraysize(sizes); ++i) {
111 GURL icon_url(
112 web_app.app_url.Resolve(base::StringPrintf("%i.png", sizes[i])));
113 web_app.icons.push_back(GetIconInfo(icon_url, sizes[i]));
114 }
115
116 scoped_refptr<Extension> extension = ConvertWebAppToExtension(
117 web_app, GetTestTime(1978, 12, 11, 0, 0, 0, 0),
118 extensions_dir.path());
119 ASSERT_TRUE(extension.get());
120
121 base::ScopedTempDir extension_dir;
122 EXPECT_TRUE(extension_dir.Set(extension->path()));
123
124 EXPECT_TRUE(extension->is_app());
125 EXPECT_TRUE(extension->is_hosted_app());
126 EXPECT_FALSE(extension->is_legacy_packaged_app());
127
128 EXPECT_EQ("lJqm1+jncOHClAuwif1QxNJKfeV9Fbl9IBZx7FkNwkA=",
129 extension->public_key());
130 EXPECT_EQ("ncnbaadanljoanockmphfdkimpdedemj", extension->id());
131 EXPECT_EQ("1978.12.11.0", extension->version()->GetString());
132 EXPECT_EQ(UTF16ToUTF8(web_app.title), extension->name());
133 EXPECT_EQ(UTF16ToUTF8(web_app.description), extension->description());
134 EXPECT_EQ(web_app.app_url, AppLaunchInfo::GetFullLaunchURL(extension.get()));
135 EXPECT_EQ(2u, extension->GetActivePermissions()->apis().size());
136 EXPECT_TRUE(extension->HasAPIPermission("geolocation"));
137 EXPECT_TRUE(extension->HasAPIPermission("notifications"));
138 ASSERT_EQ(1u, extension->web_extent().patterns().size());
139 EXPECT_EQ("http://aaronboodman.com/gearpad/*",
140 extension->web_extent().patterns().begin()->GetAsString());
141
142 EXPECT_EQ(web_app.icons.size(),
143 IconsInfo::GetIcons(extension.get()).map().size());
144 for (size_t i = 0; i < web_app.icons.size(); ++i) {
145 EXPECT_EQ(base::StringPrintf("icons/%i.png", web_app.icons[i].width),
146 IconsInfo::GetIcons(extension.get()).Get(
147 web_app.icons[i].width, ExtensionIconSet::MATCH_EXACTLY));
148 ExtensionResource resource =
149 IconsInfo::GetIconResource(extension.get(),
150 web_app.icons[i].width,
151 ExtensionIconSet::MATCH_EXACTLY);
152 ASSERT_TRUE(!resource.empty());
153 EXPECT_TRUE(base::PathExists(resource.GetFilePath()));
154 }
155 }
156
TEST(ExtensionFromWebApp,Minimal)157 TEST(ExtensionFromWebApp, Minimal) {
158 base::ScopedTempDir extensions_dir;
159 ASSERT_TRUE(extensions_dir.CreateUniqueTempDir());
160
161 WebApplicationInfo web_app;
162 web_app.manifest_url = GURL("http://aaronboodman.com/gearpad/manifest.json");
163 web_app.title = ASCIIToUTF16("Gearpad");
164 web_app.app_url = GURL("http://aaronboodman.com/gearpad/");
165
166 scoped_refptr<Extension> extension = ConvertWebAppToExtension(
167 web_app, GetTestTime(1978, 12, 11, 0, 0, 0, 0),
168 extensions_dir.path());
169 ASSERT_TRUE(extension.get());
170
171 base::ScopedTempDir extension_dir;
172 EXPECT_TRUE(extension_dir.Set(extension->path()));
173
174 EXPECT_TRUE(extension->is_app());
175 EXPECT_TRUE(extension->is_hosted_app());
176 EXPECT_FALSE(extension->is_legacy_packaged_app());
177
178 EXPECT_EQ("lJqm1+jncOHClAuwif1QxNJKfeV9Fbl9IBZx7FkNwkA=",
179 extension->public_key());
180 EXPECT_EQ("ncnbaadanljoanockmphfdkimpdedemj", extension->id());
181 EXPECT_EQ("1978.12.11.0", extension->version()->GetString());
182 EXPECT_EQ(UTF16ToUTF8(web_app.title), extension->name());
183 EXPECT_EQ("", extension->description());
184 EXPECT_EQ(web_app.app_url, AppLaunchInfo::GetFullLaunchURL(extension.get()));
185 EXPECT_EQ(0u, IconsInfo::GetIcons(extension.get()).map().size());
186 EXPECT_EQ(0u, extension->GetActivePermissions()->apis().size());
187 ASSERT_EQ(1u, extension->web_extent().patterns().size());
188 EXPECT_EQ("*://aaronboodman.com/*",
189 extension->web_extent().patterns().begin()->GetAsString());
190 }
191
192 } // namespace extensions
193