• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/files/file_path.h"
11 #include "base/files/file_util.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/manifest_handlers/app_launch_info.h"
20 #include "chrome/common/web_application_info.h"
21 #include "extensions/common/extension.h"
22 #include "extensions/common/extension_icon_set.h"
23 #include "extensions/common/extension_resource.h"
24 #include "extensions/common/manifest_handlers/icons_handler.h"
25 #include "extensions/common/permissions/permission_set.h"
26 #include "extensions/common/permissions/permissions_data.h"
27 #include "extensions/common/url_pattern.h"
28 #include "testing/gtest/include/gtest/gtest.h"
29 #include "ui/gfx/codec/png_codec.h"
30 #include "url/gurl.h"
31 
32 namespace extensions {
33 
34 namespace {
35 
36 // Returns an icon info corresponding to a canned icon.
GetIconInfo(const GURL & url,int size)37 WebApplicationInfo::IconInfo GetIconInfo(const GURL& url, int size) {
38   WebApplicationInfo::IconInfo result;
39 
40   base::FilePath icon_file;
41   if (!PathService::Get(chrome::DIR_TEST_DATA, &icon_file)) {
42     ADD_FAILURE() << "Could not get test data directory.";
43     return result;
44   }
45 
46   icon_file = icon_file.AppendASCII("extensions")
47                        .AppendASCII("convert_web_app")
48                        .AppendASCII(base::StringPrintf("%i.png", size));
49 
50   result.url = url;
51   result.width = size;
52   result.height = size;
53 
54   std::string icon_data;
55   if (!base::ReadFileToString(icon_file, &icon_data)) {
56     ADD_FAILURE() << "Could not read test icon.";
57     return result;
58   }
59 
60   if (!gfx::PNGCodec::Decode(
61         reinterpret_cast<const unsigned char*>(icon_data.c_str()),
62         icon_data.size(), &result.data)) {
63     ADD_FAILURE() << "Could not decode test icon.";
64     return result;
65   }
66 
67   return result;
68 }
69 
GetTestTime(int year,int month,int day,int hour,int minute,int second,int millisecond)70 base::Time GetTestTime(int year, int month, int day, int hour, int minute,
71                        int second, int millisecond) {
72   base::Time::Exploded exploded = {0};
73   exploded.year = year;
74   exploded.month = month;
75   exploded.day_of_month = day;
76   exploded.hour = hour;
77   exploded.minute = minute;
78   exploded.second = second;
79   exploded.millisecond = millisecond;
80   return base::Time::FromUTCExploded(exploded);
81 }
82 
83 }  // namespace
84 
TEST(ExtensionFromWebApp,GenerateVersion)85 TEST(ExtensionFromWebApp, GenerateVersion) {
86   EXPECT_EQ("2010.1.1.0",
87             ConvertTimeToExtensionVersion(
88                 GetTestTime(2010, 1, 1, 0, 0, 0, 0)));
89   EXPECT_EQ("2010.12.31.22111",
90             ConvertTimeToExtensionVersion(
91                 GetTestTime(2010, 12, 31, 8, 5, 50, 500)));
92   EXPECT_EQ("2010.10.1.65535",
93             ConvertTimeToExtensionVersion(
94                 GetTestTime(2010, 10, 1, 23, 59, 59, 999)));
95 }
96 
TEST(ExtensionFromWebApp,Basic)97 TEST(ExtensionFromWebApp, Basic) {
98   base::ScopedTempDir extensions_dir;
99   ASSERT_TRUE(extensions_dir.CreateUniqueTempDir());
100 
101   WebApplicationInfo web_app;
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 
107   const int sizes[] = {16, 48, 128};
108   for (size_t i = 0; i < arraysize(sizes); ++i) {
109     GURL icon_url(
110         web_app.app_url.Resolve(base::StringPrintf("%i.png", sizes[i])));
111     web_app.icons.push_back(GetIconInfo(icon_url, sizes[i]));
112   }
113 
114   scoped_refptr<Extension> extension = ConvertWebAppToExtension(
115       web_app, GetTestTime(1978, 12, 11, 0, 0, 0, 0),
116       extensions_dir.path());
117   ASSERT_TRUE(extension.get());
118 
119   base::ScopedTempDir extension_dir;
120   EXPECT_TRUE(extension_dir.Set(extension->path()));
121 
122   EXPECT_TRUE(extension->is_app());
123   EXPECT_TRUE(extension->is_hosted_app());
124   EXPECT_FALSE(extension->is_legacy_packaged_app());
125 
126   EXPECT_EQ("zVvdNZy3Mp7CFU8JVSyXNlDuHdVLbP7fDO3TGVzj/0w=",
127             extension->public_key());
128   EXPECT_EQ("oplhagaaipaimkjlbekcdjkffijdockj", extension->id());
129   EXPECT_EQ("1978.12.11.0", extension->version()->GetString());
130   EXPECT_EQ(base::UTF16ToUTF8(web_app.title), extension->name());
131   EXPECT_EQ(base::UTF16ToUTF8(web_app.description), extension->description());
132   EXPECT_EQ(web_app.app_url, AppLaunchInfo::GetFullLaunchURL(extension.get()));
133   EXPECT_EQ(0u,
134             extension->permissions_data()->active_permissions()->apis().size());
135   ASSERT_EQ(0u, extension->web_extent().patterns().size());
136 
137   EXPECT_EQ(web_app.icons.size(),
138             IconsInfo::GetIcons(extension.get()).map().size());
139   for (size_t i = 0; i < web_app.icons.size(); ++i) {
140     EXPECT_EQ(base::StringPrintf("icons/%i.png", web_app.icons[i].width),
141               IconsInfo::GetIcons(extension.get()).Get(
142                   web_app.icons[i].width, ExtensionIconSet::MATCH_EXACTLY));
143     ExtensionResource resource =
144         IconsInfo::GetIconResource(extension.get(),
145                                    web_app.icons[i].width,
146                                    ExtensionIconSet::MATCH_EXACTLY);
147     ASSERT_TRUE(!resource.empty());
148     EXPECT_TRUE(base::PathExists(resource.GetFilePath()));
149   }
150 }
151 
TEST(ExtensionFromWebApp,Minimal)152 TEST(ExtensionFromWebApp, Minimal) {
153   base::ScopedTempDir extensions_dir;
154   ASSERT_TRUE(extensions_dir.CreateUniqueTempDir());
155 
156   WebApplicationInfo web_app;
157   web_app.title = base::ASCIIToUTF16("Gearpad");
158   web_app.app_url = GURL("http://aaronboodman.com/gearpad/");
159 
160   scoped_refptr<Extension> extension = ConvertWebAppToExtension(
161       web_app, GetTestTime(1978, 12, 11, 0, 0, 0, 0),
162       extensions_dir.path());
163   ASSERT_TRUE(extension.get());
164 
165   base::ScopedTempDir extension_dir;
166   EXPECT_TRUE(extension_dir.Set(extension->path()));
167 
168   EXPECT_TRUE(extension->is_app());
169   EXPECT_TRUE(extension->is_hosted_app());
170   EXPECT_FALSE(extension->is_legacy_packaged_app());
171 
172   EXPECT_EQ("zVvdNZy3Mp7CFU8JVSyXNlDuHdVLbP7fDO3TGVzj/0w=",
173             extension->public_key());
174   EXPECT_EQ("oplhagaaipaimkjlbekcdjkffijdockj", extension->id());
175   EXPECT_EQ("1978.12.11.0", extension->version()->GetString());
176   EXPECT_EQ(base::UTF16ToUTF8(web_app.title), extension->name());
177   EXPECT_EQ("", extension->description());
178   EXPECT_EQ(web_app.app_url, AppLaunchInfo::GetFullLaunchURL(extension.get()));
179   EXPECT_EQ(0u, IconsInfo::GetIcons(extension.get()).map().size());
180   EXPECT_EQ(0u,
181             extension->permissions_data()->active_permissions()->apis().size());
182   ASSERT_EQ(0u, extension->web_extent().patterns().size());
183 }
184 
185 }  // namespace extensions
186