[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] | 00e7bef | 2013-06-10 20:35:17 | [diff] [blame] | 14 | #include "base/strings/stringprintf.h" |
[email protected] | 112158af | 2013-06-07 23:46:18 | [diff] [blame] | 15 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 41a17c5 | 2013-06-28 00:27:53 | [diff] [blame] | 16 | #include "base/time/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" |
[email protected] | 6b414c23 | 2013-06-05 07:53:34 | [diff] [blame] | 19 | #include "chrome/common/extensions/manifest_handlers/app_launch_info.h" |
[email protected] | 93f5046 | 2013-05-10 04:40:40 | [diff] [blame] | 20 | #include "chrome/common/web_application_info.h" |
[email protected] | e4452d3 | 2013-11-15 23:07:41 | [diff] [blame] | 21 | #include "extensions/common/extension.h" |
[email protected] | 4b790884 | 2014-04-07 23:50:22 | [diff] [blame] | 22 | #include "extensions/common/extension_icon_set.h" |
[email protected] | 993da5e | 2013-03-23 21:25:16 | [diff] [blame] | 23 | #include "extensions/common/extension_resource.h" |
[email protected] | 0db486f | 2014-04-09 19:32:22 | [diff] [blame] | 24 | #include "extensions/common/manifest_handlers/icons_handler.h" |
[email protected] | 5a55f3f | 2013-10-29 01:08:29 | [diff] [blame] | 25 | #include "extensions/common/permissions/permission_set.h" |
[email protected] | 076ebeda | 2014-06-06 21:47:26 | [diff] [blame^] | 26 | #include "extensions/common/permissions/permissions_data.h" |
[email protected] | 885c0e9 | 2012-11-13 20:27:42 | [diff] [blame] | 27 | #include "extensions/common/url_pattern.h" |
[email protected] | 5ba5dab | 2010-11-18 02:31:04 | [diff] [blame] | 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] | a6483d2 | 2013-07-03 22:11:00 | [diff] [blame] | 30 | #include "url/gurl.h" |
[email protected] | 5ba5dab | 2010-11-18 02:31:04 | [diff] [blame] | 31 | |
[email protected] | b1912d59 | 2012-08-17 22:29:38 | [diff] [blame] | 32 | namespace extensions { |
| 33 | |
[email protected] | 5ba5dab | 2010-11-18 02:31:04 | [diff] [blame] | 34 | namespace { |
| 35 | |
| 36 | // Returns an icon info corresponding to a canned icon. |
| 37 | WebApplicationInfo::IconInfo GetIconInfo(const GURL& url, int size) { |
| 38 | WebApplicationInfo::IconInfo result; |
| 39 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 40 | base::FilePath icon_file; |
[email protected] | 5ba5dab | 2010-11-18 02:31:04 | [diff] [blame] | 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") |
[email protected] | 7d3cbc9 | 2013-03-18 22:33:04 | [diff] [blame] | 48 | .AppendASCII(base::StringPrintf("%i.png", size)); |
[email protected] | 5ba5dab | 2010-11-18 02:31:04 | [diff] [blame] | 49 | |
| 50 | result.url = url; |
| 51 | result.width = size; |
| 52 | result.height = size; |
| 53 | |
| 54 | std::string icon_data; |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 55 | if (!base::ReadFileToString(icon_file, &icon_data)) { |
[email protected] | 5ba5dab | 2010-11-18 02:31:04 | [diff] [blame] | 56 | ADD_FAILURE() << "Could not read test icon."; |
| 57 | return result; |
| 58 | } |
| 59 | |
[email protected] | da87eec2 | 2013-05-14 09:25:28 | [diff] [blame] | 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 | } |
[email protected] | 5ba5dab | 2010-11-18 02:31:04 | [diff] [blame] | 66 | |
| 67 | return result; |
| 68 | } |
| 69 | |
| 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 | |
[email protected] | d592b1bd | 2013-05-06 06:40:47 | [diff] [blame] | 85 | TEST(ExtensionFromWebApp, GenerateVersion) { |
[email protected] | 5ba5dab | 2010-11-18 02:31:04 | [diff] [blame] | 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 | |
[email protected] | d592b1bd | 2013-05-06 06:40:47 | [diff] [blame] | 97 | TEST(ExtensionFromWebApp, Basic) { |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 98 | base::ScopedTempDir extensions_dir; |
[email protected] | 171ab92d | 2012-10-19 01:16:34 | [diff] [blame] | 99 | ASSERT_TRUE(extensions_dir.CreateUniqueTempDir()); |
| 100 | |
[email protected] | 5ba5dab | 2010-11-18 02:31:04 | [diff] [blame] | 101 | WebApplicationInfo web_app; |
[email protected] | 0433872 | 2013-12-24 23:18:05 | [diff] [blame] | 102 | web_app.title = base::ASCIIToUTF16("Gearpad"); |
| 103 | web_app.description = |
| 104 | base::ASCIIToUTF16("The best text editor in the universe!"); |
[email protected] | 5ba5dab | 2010-11-18 02:31:04 | [diff] [blame] | 105 | web_app.app_url = GURL("http://aaronboodman.com/gearpad/"); |
[email protected] | 5ba5dab | 2010-11-18 02:31:04 | [diff] [blame] | 106 | |
| 107 | const int sizes[] = {16, 48, 128}; |
| 108 | for (size_t i = 0; i < arraysize(sizes); ++i) { |
[email protected] | 7d3cbc9 | 2013-03-18 22:33:04 | [diff] [blame] | 109 | GURL icon_url( |
| 110 | web_app.app_url.Resolve(base::StringPrintf("%i.png", sizes[i]))); |
[email protected] | 5ba5dab | 2010-11-18 02:31:04 | [diff] [blame] | 111 | web_app.icons.push_back(GetIconInfo(icon_url, sizes[i])); |
| 112 | } |
| 113 | |
[email protected] | b1912d59 | 2012-08-17 22:29:38 | [diff] [blame] | 114 | scoped_refptr<Extension> extension = ConvertWebAppToExtension( |
[email protected] | 171ab92d | 2012-10-19 01:16:34 | [diff] [blame] | 115 | web_app, GetTestTime(1978, 12, 11, 0, 0, 0, 0), |
| 116 | extensions_dir.path()); |
[email protected] | 5ba5dab | 2010-11-18 02:31:04 | [diff] [blame] | 117 | ASSERT_TRUE(extension.get()); |
| 118 | |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 119 | base::ScopedTempDir extension_dir; |
[email protected] | 2d57f5d | 2011-01-13 14:20:12 | [diff] [blame] | 120 | EXPECT_TRUE(extension_dir.Set(extension->path())); |
[email protected] | 5ba5dab | 2010-11-18 02:31:04 | [diff] [blame] | 121 | |
| 122 | EXPECT_TRUE(extension->is_app()); |
| 123 | EXPECT_TRUE(extension->is_hosted_app()); |
[email protected] | c4f459d | 2012-09-28 04:40:10 | [diff] [blame] | 124 | EXPECT_FALSE(extension->is_legacy_packaged_app()); |
[email protected] | 5ba5dab | 2010-11-18 02:31:04 | [diff] [blame] | 125 | |
[email protected] | f8bbf6b | 2014-01-30 07:23:27 | [diff] [blame] | 126 | EXPECT_EQ("zVvdNZy3Mp7CFU8JVSyXNlDuHdVLbP7fDO3TGVzj/0w=", |
[email protected] | 5ba5dab | 2010-11-18 02:31:04 | [diff] [blame] | 127 | extension->public_key()); |
[email protected] | f8bbf6b | 2014-01-30 07:23:27 | [diff] [blame] | 128 | EXPECT_EQ("oplhagaaipaimkjlbekcdjkffijdockj", extension->id()); |
[email protected] | 5ba5dab | 2010-11-18 02:31:04 | [diff] [blame] | 129 | EXPECT_EQ("1978.12.11.0", extension->version()->GetString()); |
[email protected] | 0433872 | 2013-12-24 23:18:05 | [diff] [blame] | 130 | EXPECT_EQ(base::UTF16ToUTF8(web_app.title), extension->name()); |
| 131 | EXPECT_EQ(base::UTF16ToUTF8(web_app.description), extension->description()); |
[email protected] | cadac62 | 2013-06-11 16:46:36 | [diff] [blame] | 132 | EXPECT_EQ(web_app.app_url, AppLaunchInfo::GetFullLaunchURL(extension.get())); |
[email protected] | 076ebeda | 2014-06-06 21:47:26 | [diff] [blame^] | 133 | EXPECT_EQ(0u, |
| 134 | extension->permissions_data()->active_permissions()->apis().size()); |
[email protected] | f8bbf6b | 2014-01-30 07:23:27 | [diff] [blame] | 135 | ASSERT_EQ(0u, extension->web_extent().patterns().size()); |
[email protected] | 5ba5dab | 2010-11-18 02:31:04 | [diff] [blame] | 136 | |
[email protected] | dc24976f | 2013-06-02 21:15:09 | [diff] [blame] | 137 | EXPECT_EQ(web_app.icons.size(), |
| 138 | IconsInfo::GetIcons(extension.get()).map().size()); |
[email protected] | 5ba5dab | 2010-11-18 02:31:04 | [diff] [blame] | 139 | for (size_t i = 0; i < web_app.icons.size(); ++i) { |
[email protected] | 7d3cbc9 | 2013-03-18 22:33:04 | [diff] [blame] | 140 | EXPECT_EQ(base::StringPrintf("icons/%i.png", web_app.icons[i].width), |
[email protected] | dc24976f | 2013-06-02 21:15:09 | [diff] [blame] | 141 | IconsInfo::GetIcons(extension.get()).Get( |
[email protected] | 702d8b4 | 2013-02-27 20:55:50 | [diff] [blame] | 142 | web_app.icons[i].width, ExtensionIconSet::MATCH_EXACTLY)); |
[email protected] | dc24976f | 2013-06-02 21:15:09 | [diff] [blame] | 143 | ExtensionResource resource = |
| 144 | IconsInfo::GetIconResource(extension.get(), |
| 145 | web_app.icons[i].width, |
| 146 | ExtensionIconSet::MATCH_EXACTLY); |
[email protected] | 5ba5dab | 2010-11-18 02:31:04 | [diff] [blame] | 147 | ASSERT_TRUE(!resource.empty()); |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 148 | EXPECT_TRUE(base::PathExists(resource.GetFilePath())); |
[email protected] | 5ba5dab | 2010-11-18 02:31:04 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
[email protected] | d592b1bd | 2013-05-06 06:40:47 | [diff] [blame] | 152 | TEST(ExtensionFromWebApp, Minimal) { |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 153 | base::ScopedTempDir extensions_dir; |
[email protected] | 171ab92d | 2012-10-19 01:16:34 | [diff] [blame] | 154 | ASSERT_TRUE(extensions_dir.CreateUniqueTempDir()); |
| 155 | |
[email protected] | 5ba5dab | 2010-11-18 02:31:04 | [diff] [blame] | 156 | WebApplicationInfo web_app; |
[email protected] | 0433872 | 2013-12-24 23:18:05 | [diff] [blame] | 157 | web_app.title = base::ASCIIToUTF16("Gearpad"); |
[email protected] | 5ba5dab | 2010-11-18 02:31:04 | [diff] [blame] | 158 | web_app.app_url = GURL("http://aaronboodman.com/gearpad/"); |
| 159 | |
[email protected] | b1912d59 | 2012-08-17 22:29:38 | [diff] [blame] | 160 | scoped_refptr<Extension> extension = ConvertWebAppToExtension( |
[email protected] | 171ab92d | 2012-10-19 01:16:34 | [diff] [blame] | 161 | web_app, GetTestTime(1978, 12, 11, 0, 0, 0, 0), |
| 162 | extensions_dir.path()); |
[email protected] | 5ba5dab | 2010-11-18 02:31:04 | [diff] [blame] | 163 | ASSERT_TRUE(extension.get()); |
| 164 | |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 165 | base::ScopedTempDir extension_dir; |
[email protected] | 2d57f5d | 2011-01-13 14:20:12 | [diff] [blame] | 166 | EXPECT_TRUE(extension_dir.Set(extension->path())); |
[email protected] | 5ba5dab | 2010-11-18 02:31:04 | [diff] [blame] | 167 | |
| 168 | EXPECT_TRUE(extension->is_app()); |
| 169 | EXPECT_TRUE(extension->is_hosted_app()); |
[email protected] | c4f459d | 2012-09-28 04:40:10 | [diff] [blame] | 170 | EXPECT_FALSE(extension->is_legacy_packaged_app()); |
[email protected] | 5ba5dab | 2010-11-18 02:31:04 | [diff] [blame] | 171 | |
[email protected] | f8bbf6b | 2014-01-30 07:23:27 | [diff] [blame] | 172 | EXPECT_EQ("zVvdNZy3Mp7CFU8JVSyXNlDuHdVLbP7fDO3TGVzj/0w=", |
[email protected] | 5ba5dab | 2010-11-18 02:31:04 | [diff] [blame] | 173 | extension->public_key()); |
[email protected] | f8bbf6b | 2014-01-30 07:23:27 | [diff] [blame] | 174 | EXPECT_EQ("oplhagaaipaimkjlbekcdjkffijdockj", extension->id()); |
[email protected] | 5ba5dab | 2010-11-18 02:31:04 | [diff] [blame] | 175 | EXPECT_EQ("1978.12.11.0", extension->version()->GetString()); |
[email protected] | 0433872 | 2013-12-24 23:18:05 | [diff] [blame] | 176 | EXPECT_EQ(base::UTF16ToUTF8(web_app.title), extension->name()); |
[email protected] | 5ba5dab | 2010-11-18 02:31:04 | [diff] [blame] | 177 | EXPECT_EQ("", extension->description()); |
[email protected] | cadac62 | 2013-06-11 16:46:36 | [diff] [blame] | 178 | EXPECT_EQ(web_app.app_url, AppLaunchInfo::GetFullLaunchURL(extension.get())); |
[email protected] | dc24976f | 2013-06-02 21:15:09 | [diff] [blame] | 179 | EXPECT_EQ(0u, IconsInfo::GetIcons(extension.get()).map().size()); |
[email protected] | 076ebeda | 2014-06-06 21:47:26 | [diff] [blame^] | 180 | EXPECT_EQ(0u, |
| 181 | extension->permissions_data()->active_permissions()->apis().size()); |
[email protected] | f8bbf6b | 2014-01-30 07:23:27 | [diff] [blame] | 182 | ASSERT_EQ(0u, extension->web_extent().patterns().size()); |
[email protected] | 5ba5dab | 2010-11-18 02:31:04 | [diff] [blame] | 183 | } |
[email protected] | b1912d59 | 2012-08-17 22:29:38 | [diff] [blame] | 184 | |
| 185 | } // namespace extensions |