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