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