[email protected] | bebe1d0 | 2012-08-02 20:17:09 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 5ba5dab | 2010-11-18 02:31:04 | [diff] [blame] | 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 | |
[email protected] | 5ba5dab | 2010-11-18 02:31:04 | [diff] [blame] | 10 | #include "base/file_util.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 11 | #include "base/files/file_path.h" |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 12 | #include "base/files/scoped_temp_dir.h" |
[email protected] | 5ba5dab | 2010-11-18 02:31:04 | [diff] [blame] | 13 | #include "base/path_service.h" |
[email protected] | 5ba5dab | 2010-11-18 02:31:04 | [diff] [blame] | 14 | #include "base/stringprintf.h" |
[email protected] | 112158af | 2013-06-07 23:46:18 | [diff] [blame^] | 15 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 5ba5dab | 2010-11-18 02:31:04 | [diff] [blame] | 16 | #include "base/time.h" |
[email protected] | 5ba5dab | 2010-11-18 02:31:04 | [diff] [blame] | 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" |
[email protected] | 6b414c23 | 2013-06-05 07:53:34 | [diff] [blame] | 21 | #include "chrome/common/extensions/manifest_handlers/app_launch_info.h" |
[email protected] | abe720c0 | 2013-04-12 04:00:10 | [diff] [blame] | 22 | #include "chrome/common/extensions/manifest_handlers/icons_handler.h" |
[email protected] | bebe1d0 | 2012-08-02 20:17:09 | [diff] [blame] | 23 | #include "chrome/common/extensions/permissions/permission_set.h" |
[email protected] | 93f5046 | 2013-05-10 04:40:40 | [diff] [blame] | 24 | #include "chrome/common/web_application_info.h" |
[email protected] | 993da5e | 2013-03-23 21:25:16 | [diff] [blame] | 25 | #include "extensions/common/extension_resource.h" |
[email protected] | 885c0e9 | 2012-11-13 20:27:42 | [diff] [blame] | 26 | #include "extensions/common/url_pattern.h" |
[email protected] | 5ba5dab | 2010-11-18 02:31:04 | [diff] [blame] | 27 | #include "googleurl/src/gurl.h" |
| 28 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 08397d5 | 2011-02-05 01:53:38 | [diff] [blame] | 29 | #include "ui/gfx/codec/png_codec.h" |
[email protected] | 5ba5dab | 2010-11-18 02:31:04 | [diff] [blame] | 30 | |
[email protected] | b1912d59 | 2012-08-17 22:29:38 | [diff] [blame] | 31 | namespace extensions { |
| 32 | |
[email protected] | 5ba5dab | 2010-11-18 02:31:04 | [diff] [blame] | 33 | namespace { |
| 34 | |
| 35 | // Returns an icon info corresponding to a canned icon. |
| 36 | WebApplicationInfo::IconInfo GetIconInfo(const GURL& url, int size) { |
| 37 | WebApplicationInfo::IconInfo result; |
| 38 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 39 | base::FilePath icon_file; |
[email protected] | 5ba5dab | 2010-11-18 02:31:04 | [diff] [blame] | 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") |
[email protected] | 7d3cbc9 | 2013-03-18 22:33:04 | [diff] [blame] | 47 | .AppendASCII(base::StringPrintf("%i.png", size)); |
[email protected] | 5ba5dab | 2010-11-18 02:31:04 | [diff] [blame] | 48 | |
| 49 | result.url = url; |
| 50 | result.width = size; |
| 51 | result.height = size; |
| 52 | |
| 53 | std::string icon_data; |
| 54 | if (!file_util::ReadFileToString(icon_file, &icon_data)) { |
| 55 | ADD_FAILURE() << "Could not read test icon."; |
| 56 | return result; |
| 57 | } |
| 58 | |
[email protected] | da87eec2 | 2013-05-14 09:25:28 | [diff] [blame] | 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 | } |
[email protected] | 5ba5dab | 2010-11-18 02:31:04 | [diff] [blame] | 65 | |
| 66 | return result; |
| 67 | } |
| 68 | |
| 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 | |
[email protected] | d592b1bd | 2013-05-06 06:40:47 | [diff] [blame] | 84 | TEST(ExtensionFromWebApp, GenerateVersion) { |
[email protected] | 5ba5dab | 2010-11-18 02:31:04 | [diff] [blame] | 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 | |
[email protected] | d592b1bd | 2013-05-06 06:40:47 | [diff] [blame] | 96 | TEST(ExtensionFromWebApp, Basic) { |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 97 | base::ScopedTempDir extensions_dir; |
[email protected] | 171ab92d | 2012-10-19 01:16:34 | [diff] [blame] | 98 | ASSERT_TRUE(extensions_dir.CreateUniqueTempDir()); |
| 99 | |
[email protected] | 5ba5dab | 2010-11-18 02:31:04 | [diff] [blame] | 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) { |
[email protected] | 7d3cbc9 | 2013-03-18 22:33:04 | [diff] [blame] | 111 | GURL icon_url( |
| 112 | web_app.app_url.Resolve(base::StringPrintf("%i.png", sizes[i]))); |
[email protected] | 5ba5dab | 2010-11-18 02:31:04 | [diff] [blame] | 113 | web_app.icons.push_back(GetIconInfo(icon_url, sizes[i])); |
| 114 | } |
| 115 | |
[email protected] | b1912d59 | 2012-08-17 22:29:38 | [diff] [blame] | 116 | scoped_refptr<Extension> extension = ConvertWebAppToExtension( |
[email protected] | 171ab92d | 2012-10-19 01:16:34 | [diff] [blame] | 117 | web_app, GetTestTime(1978, 12, 11, 0, 0, 0, 0), |
| 118 | extensions_dir.path()); |
[email protected] | 5ba5dab | 2010-11-18 02:31:04 | [diff] [blame] | 119 | ASSERT_TRUE(extension.get()); |
| 120 | |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 121 | base::ScopedTempDir extension_dir; |
[email protected] | 2d57f5d | 2011-01-13 14:20:12 | [diff] [blame] | 122 | EXPECT_TRUE(extension_dir.Set(extension->path())); |
[email protected] | 5ba5dab | 2010-11-18 02:31:04 | [diff] [blame] | 123 | |
| 124 | EXPECT_TRUE(extension->is_app()); |
| 125 | EXPECT_TRUE(extension->is_hosted_app()); |
[email protected] | c4f459d | 2012-09-28 04:40:10 | [diff] [blame] | 126 | EXPECT_FALSE(extension->is_legacy_packaged_app()); |
[email protected] | 5ba5dab | 2010-11-18 02:31:04 | [diff] [blame] | 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()); |
[email protected] | 6b414c23 | 2013-06-05 07:53:34 | [diff] [blame] | 134 | EXPECT_EQ(web_app.app_url, AppLaunchInfo::GetFullLaunchURL(extension)); |
[email protected] | 902fd7b | 2011-07-27 18:42:31 | [diff] [blame] | 135 | EXPECT_EQ(2u, extension->GetActivePermissions()->apis().size()); |
[email protected] | 0d3e4a2 | 2011-06-23 19:02:52 | [diff] [blame] | 136 | EXPECT_TRUE(extension->HasAPIPermission("geolocation")); |
| 137 | EXPECT_TRUE(extension->HasAPIPermission("notifications")); |
[email protected] | 5ba5dab | 2010-11-18 02:31:04 | [diff] [blame] | 138 | ASSERT_EQ(1u, extension->web_extent().patterns().size()); |
| 139 | EXPECT_EQ("http://aaronboodman.com/gearpad/*", |
[email protected] | 06e8b8ff | 2011-07-13 15:03:47 | [diff] [blame] | 140 | extension->web_extent().patterns().begin()->GetAsString()); |
[email protected] | 5ba5dab | 2010-11-18 02:31:04 | [diff] [blame] | 141 | |
[email protected] | dc24976f | 2013-06-02 21:15:09 | [diff] [blame] | 142 | EXPECT_EQ(web_app.icons.size(), |
| 143 | IconsInfo::GetIcons(extension.get()).map().size()); |
[email protected] | 5ba5dab | 2010-11-18 02:31:04 | [diff] [blame] | 144 | for (size_t i = 0; i < web_app.icons.size(); ++i) { |
[email protected] | 7d3cbc9 | 2013-03-18 22:33:04 | [diff] [blame] | 145 | EXPECT_EQ(base::StringPrintf("icons/%i.png", web_app.icons[i].width), |
[email protected] | dc24976f | 2013-06-02 21:15:09 | [diff] [blame] | 146 | IconsInfo::GetIcons(extension.get()).Get( |
[email protected] | 702d8b4 | 2013-02-27 20:55:50 | [diff] [blame] | 147 | web_app.icons[i].width, ExtensionIconSet::MATCH_EXACTLY)); |
[email protected] | dc24976f | 2013-06-02 21:15:09 | [diff] [blame] | 148 | ExtensionResource resource = |
| 149 | IconsInfo::GetIconResource(extension.get(), |
| 150 | web_app.icons[i].width, |
| 151 | ExtensionIconSet::MATCH_EXACTLY); |
[email protected] | 5ba5dab | 2010-11-18 02:31:04 | [diff] [blame] | 152 | ASSERT_TRUE(!resource.empty()); |
| 153 | EXPECT_TRUE(file_util::PathExists(resource.GetFilePath())); |
| 154 | } |
| 155 | } |
| 156 | |
[email protected] | d592b1bd | 2013-05-06 06:40:47 | [diff] [blame] | 157 | TEST(ExtensionFromWebApp, Minimal) { |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 158 | base::ScopedTempDir extensions_dir; |
[email protected] | 171ab92d | 2012-10-19 01:16:34 | [diff] [blame] | 159 | ASSERT_TRUE(extensions_dir.CreateUniqueTempDir()); |
| 160 | |
[email protected] | 5ba5dab | 2010-11-18 02:31:04 | [diff] [blame] | 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 | |
[email protected] | b1912d59 | 2012-08-17 22:29:38 | [diff] [blame] | 166 | scoped_refptr<Extension> extension = ConvertWebAppToExtension( |
[email protected] | 171ab92d | 2012-10-19 01:16:34 | [diff] [blame] | 167 | web_app, GetTestTime(1978, 12, 11, 0, 0, 0, 0), |
| 168 | extensions_dir.path()); |
[email protected] | 5ba5dab | 2010-11-18 02:31:04 | [diff] [blame] | 169 | ASSERT_TRUE(extension.get()); |
| 170 | |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 171 | base::ScopedTempDir extension_dir; |
[email protected] | 2d57f5d | 2011-01-13 14:20:12 | [diff] [blame] | 172 | EXPECT_TRUE(extension_dir.Set(extension->path())); |
[email protected] | 5ba5dab | 2010-11-18 02:31:04 | [diff] [blame] | 173 | |
| 174 | EXPECT_TRUE(extension->is_app()); |
| 175 | EXPECT_TRUE(extension->is_hosted_app()); |
[email protected] | c4f459d | 2012-09-28 04:40:10 | [diff] [blame] | 176 | EXPECT_FALSE(extension->is_legacy_packaged_app()); |
[email protected] | 5ba5dab | 2010-11-18 02:31:04 | [diff] [blame] | 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()); |
[email protected] | 6b414c23 | 2013-06-05 07:53:34 | [diff] [blame] | 184 | EXPECT_EQ(web_app.app_url, AppLaunchInfo::GetFullLaunchURL(extension)); |
[email protected] | dc24976f | 2013-06-02 21:15:09 | [diff] [blame] | 185 | EXPECT_EQ(0u, IconsInfo::GetIcons(extension.get()).map().size()); |
[email protected] | 902fd7b | 2011-07-27 18:42:31 | [diff] [blame] | 186 | EXPECT_EQ(0u, extension->GetActivePermissions()->apis().size()); |
[email protected] | 5ba5dab | 2010-11-18 02:31:04 | [diff] [blame] | 187 | ASSERT_EQ(1u, extension->web_extent().patterns().size()); |
| 188 | EXPECT_EQ("*://aaronboodman.com/*", |
[email protected] | 06e8b8ff | 2011-07-13 15:03:47 | [diff] [blame] | 189 | extension->web_extent().patterns().begin()->GetAsString()); |
[email protected] | 5ba5dab | 2010-11-18 02:31:04 | [diff] [blame] | 190 | } |
[email protected] | b1912d59 | 2012-08-17 22:29:38 | [diff] [blame] | 191 | |
| 192 | } // namespace extensions |