| 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) { | |
| 66 base::Time::Exploded exploded = {0}; | |
| 67 exploded.year = year; | |
| 68 exploded.month = month; | |
| 69 exploded.day_of_month = day; | |
| 70 return base::Time::FromLocalExploded(exploded); | |
| 71 } | |
| 72 | |
| 73 } // namespace | |
| 74 | |
| 75 | |
| 76 TEST(ExtensionFromWebApp, GenerateVersion) { | |
| 77 // If the month is less than two digits it isn't zero padded. | |
| 78 EXPECT_EQ("2010.101.19261.43904", | |
| 79 ConvertTimeToExtensionVersion(GetTestTime(2010, 1, 1))); | |
| 80 } | |
| 81 | |
| 82 TEST(ExtensionFromWebApp, Basic) { | |
| 83 webkit_glue::WebApplicationInfo web_app; | |
| 84 web_app.manifest_url = GURL("http://aaronboodman.com/gearpad/manifest.json"); | |
| 85 web_app.title = ASCIIToUTF16("Gearpad"); | |
| 86 web_app.description = ASCIIToUTF16("The best text editor in the universe!"); | |
| 87 web_app.app_url = GURL("http://aaronboodman.com/gearpad/"); | |
| 88 web_app.permissions.push_back("geolocation"); | |
| 89 web_app.permissions.push_back("notifications"); | |
| 90 web_app.urls.push_back(GURL("http://aaronboodman.com/gearpad/")); | |
| 91 | |
| 92 const int sizes[] = {16, 48, 128}; | |
| 93 for (size_t i = 0; i < arraysize(sizes); ++i) { | |
| 94 GURL icon_url(web_app.app_url.Resolve(StringPrintf("%i.png", sizes[i]))); | |
| 95 web_app.icons.push_back(GetIconInfo(icon_url, sizes[i])); | |
| 96 } | |
| 97 | |
| 98 scoped_refptr<Extension> extension = ConvertWebAppToExtension( | |
| 99 web_app, GetTestTime(1978, 12, 11)); | |
| 100 ASSERT_TRUE(extension.get()); | |
| 101 | |
| 102 ScopedTempDir extension_dir; | |
| 103 extension_dir.Set(extension->path()); | |
| 104 | |
| 105 EXPECT_TRUE(extension->is_app()); | |
| 106 EXPECT_TRUE(extension->is_hosted_app()); | |
| 107 EXPECT_FALSE(extension->is_packaged_app()); | |
| 108 | |
| 109 EXPECT_EQ("lJqm1+jncOHClAuwif1QxNJKfeV9Fbl9IBZx7FkNwkA=", | |
| 110 extension->public_key()); | |
| 111 EXPECT_EQ("ncnbaadanljoanockmphfdkimpdedemj", extension->id()); | |
| 112 EXPECT_EQ("1978.1211.4306.13184", extension->version()->GetString()); | |
| 113 EXPECT_EQ(UTF16ToUTF8(web_app.title), extension->name()); | |
| 114 EXPECT_EQ(UTF16ToUTF8(web_app.description), extension->description()); | |
| 115 EXPECT_EQ(web_app.app_url, extension->GetFullLaunchURL()); | |
| 116 EXPECT_EQ(2u, extension->api_permissions().size()); | |
| 117 EXPECT_TRUE(extension->HasApiPermission("geolocation")); | |
| 118 EXPECT_TRUE(extension->HasApiPermission("notifications")); | |
| 119 ASSERT_EQ(1u, extension->web_extent().patterns().size()); | |
| 120 EXPECT_EQ("http://aaronboodman.com/gearpad/*", | |
| 121 extension->web_extent().patterns()[0].GetAsString()); | |
| 122 | |
| 123 EXPECT_EQ(web_app.icons.size(), extension->icons().map().size()); | |
| 124 for (size_t i = 0; i < web_app.icons.size(); ++i) { | |
| 125 EXPECT_EQ(StringPrintf("_icons/%i.png", web_app.icons[i].width), | |
| 126 extension->icons().Get(web_app.icons[i].width, | |
| 127 ExtensionIconSet::MATCH_EXACTLY)); | |
| 128 ExtensionResource resource = extension->GetIconResource( | |
| 129 web_app.icons[i].width, ExtensionIconSet::MATCH_EXACTLY); | |
| 130 ASSERT_TRUE(!resource.empty()); | |
| 131 EXPECT_TRUE(file_util::PathExists(resource.GetFilePath())); | |
| 132 } | |
| 133 } | |
| 134 | |
| 135 TEST(ExtensionFromWebApp, Minimal) { | |
| 136 webkit_glue::WebApplicationInfo web_app; | |
| 137 web_app.manifest_url = GURL("http://aaronboodman.com/gearpad/manifest.json"); | |
| 138 web_app.title = ASCIIToUTF16("Gearpad"); | |
| 139 web_app.app_url = GURL("http://aaronboodman.com/gearpad/"); | |
| 140 | |
| 141 scoped_refptr<Extension> extension = ConvertWebAppToExtension( | |
| 142 web_app, GetTestTime(1978, 12, 11)); | |
| 143 ASSERT_TRUE(extension.get()); | |
| 144 | |
| 145 ScopedTempDir extension_dir; | |
| 146 extension_dir.Set(extension->path()); | |
| 147 | |
| 148 EXPECT_TRUE(extension->is_app()); | |
| 149 EXPECT_TRUE(extension->is_hosted_app()); | |
| 150 EXPECT_FALSE(extension->is_packaged_app()); | |
| 151 | |
| 152 EXPECT_EQ("lJqm1+jncOHClAuwif1QxNJKfeV9Fbl9IBZx7FkNwkA=", | |
| 153 extension->public_key()); | |
| 154 EXPECT_EQ("ncnbaadanljoanockmphfdkimpdedemj", extension->id()); | |
| 155 EXPECT_EQ("1978.1211.4306.13184", extension->version()->GetString()); | |
| 156 EXPECT_EQ(UTF16ToUTF8(web_app.title), extension->name()); | |
| 157 EXPECT_EQ("", extension->description()); | |
| 158 EXPECT_EQ(web_app.app_url, extension->GetFullLaunchURL()); | |
| 159 EXPECT_EQ(0u, extension->icons().map().size()); | |
| 160 EXPECT_EQ(0u, extension->api_permissions().size()); | |
| 161 ASSERT_EQ(1u, extension->web_extent().patterns().size()); | |
| 162 EXPECT_EQ("*://aaronboodman.com/*", | |
| 163 extension->web_extent().patterns()[0].GetAsString()); | |
| 164 } | |
| OLD | NEW |