blob: 48d262bab77861cae895cfac12fb790757b16ef8 [file] [log] [blame]
[email protected]bebe1d02012-08-02 20:17:091// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]5ba5dab2010-11-18 02:31:042// 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]5ba5dab2010-11-18 02:31:0410#include "base/file_util.h"
[email protected]57999812013-02-24 05:40:5211#include "base/files/file_path.h"
[email protected]ea1a3f62012-11-16 20:34:2312#include "base/files/scoped_temp_dir.h"
[email protected]5ba5dab2010-11-18 02:31:0413#include "base/path_service.h"
[email protected]00e7bef2013-06-10 20:35:1714#include "base/strings/stringprintf.h"
[email protected]112158af2013-06-07 23:46:1815#include "base/strings/utf_string_conversions.h"
[email protected]41a17c52013-06-28 00:27:5316#include "base/time/time.h"
[email protected]5ba5dab2010-11-18 02:31:0417#include "base/version.h"
18#include "chrome/common/chrome_paths.h"
[email protected]6b414c232013-06-05 07:53:3419#include "chrome/common/extensions/manifest_handlers/app_launch_info.h"
[email protected]93f50462013-05-10 04:40:4020#include "chrome/common/web_application_info.h"
[email protected]e4452d32013-11-15 23:07:4121#include "extensions/common/extension.h"
[email protected]4b7908842014-04-07 23:50:2222#include "extensions/common/extension_icon_set.h"
[email protected]993da5e2013-03-23 21:25:1623#include "extensions/common/extension_resource.h"
[email protected]0db486f2014-04-09 19:32:2224#include "extensions/common/manifest_handlers/icons_handler.h"
[email protected]5a55f3f2013-10-29 01:08:2925#include "extensions/common/permissions/permission_set.h"
[email protected]076ebeda2014-06-06 21:47:2626#include "extensions/common/permissions/permissions_data.h"
[email protected]885c0e92012-11-13 20:27:4227#include "extensions/common/url_pattern.h"
[email protected]5ba5dab2010-11-18 02:31:0428#include "testing/gtest/include/gtest/gtest.h"
[email protected]08397d52011-02-05 01:53:3829#include "ui/gfx/codec/png_codec.h"
[email protected]a6483d22013-07-03 22:11:0030#include "url/gurl.h"
[email protected]5ba5dab2010-11-18 02:31:0431
[email protected]b1912d592012-08-17 22:29:3832namespace extensions {
33
[email protected]5ba5dab2010-11-18 02:31:0434namespace {
35
36// Returns an icon info corresponding to a canned icon.
37WebApplicationInfo::IconInfo GetIconInfo(const GURL& url, int size) {
38 WebApplicationInfo::IconInfo result;
39
[email protected]650b2d52013-02-10 03:41:4540 base::FilePath icon_file;
[email protected]5ba5dab2010-11-18 02:31:0441 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]7d3cbc92013-03-18 22:33:0448 .AppendASCII(base::StringPrintf("%i.png", size));
[email protected]5ba5dab2010-11-18 02:31:0449
50 result.url = url;
51 result.width = size;
52 result.height = size;
53
54 std::string icon_data;
[email protected]82f84b92013-08-30 18:23:5055 if (!base::ReadFileToString(icon_file, &icon_data)) {
[email protected]5ba5dab2010-11-18 02:31:0456 ADD_FAILURE() << "Could not read test icon.";
57 return result;
58 }
59
[email protected]da87eec22013-05-14 09:25:2860 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]5ba5dab2010-11-18 02:31:0466
67 return result;
68}
69
70base::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]d592b1bd2013-05-06 06:40:4785TEST(ExtensionFromWebApp, GenerateVersion) {
[email protected]5ba5dab2010-11-18 02:31:0486 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]d592b1bd2013-05-06 06:40:4797TEST(ExtensionFromWebApp, Basic) {
[email protected]ea1a3f62012-11-16 20:34:2398 base::ScopedTempDir extensions_dir;
[email protected]171ab92d2012-10-19 01:16:3499 ASSERT_TRUE(extensions_dir.CreateUniqueTempDir());
100
[email protected]5ba5dab2010-11-18 02:31:04101 WebApplicationInfo web_app;
[email protected]04338722013-12-24 23:18:05102 web_app.title = base::ASCIIToUTF16("Gearpad");
103 web_app.description =
104 base::ASCIIToUTF16("The best text editor in the universe!");
[email protected]5ba5dab2010-11-18 02:31:04105 web_app.app_url = GURL("http://aaronboodman.com/gearpad/");
[email protected]5ba5dab2010-11-18 02:31:04106
107 const int sizes[] = {16, 48, 128};
108 for (size_t i = 0; i < arraysize(sizes); ++i) {
[email protected]7d3cbc92013-03-18 22:33:04109 GURL icon_url(
110 web_app.app_url.Resolve(base::StringPrintf("%i.png", sizes[i])));
[email protected]5ba5dab2010-11-18 02:31:04111 web_app.icons.push_back(GetIconInfo(icon_url, sizes[i]));
112 }
113
[email protected]b1912d592012-08-17 22:29:38114 scoped_refptr<Extension> extension = ConvertWebAppToExtension(
[email protected]171ab92d2012-10-19 01:16:34115 web_app, GetTestTime(1978, 12, 11, 0, 0, 0, 0),
116 extensions_dir.path());
[email protected]5ba5dab2010-11-18 02:31:04117 ASSERT_TRUE(extension.get());
118
[email protected]ea1a3f62012-11-16 20:34:23119 base::ScopedTempDir extension_dir;
[email protected]2d57f5d2011-01-13 14:20:12120 EXPECT_TRUE(extension_dir.Set(extension->path()));
[email protected]5ba5dab2010-11-18 02:31:04121
122 EXPECT_TRUE(extension->is_app());
123 EXPECT_TRUE(extension->is_hosted_app());
[email protected]c4f459d2012-09-28 04:40:10124 EXPECT_FALSE(extension->is_legacy_packaged_app());
[email protected]5ba5dab2010-11-18 02:31:04125
[email protected]f8bbf6b2014-01-30 07:23:27126 EXPECT_EQ("zVvdNZy3Mp7CFU8JVSyXNlDuHdVLbP7fDO3TGVzj/0w=",
[email protected]5ba5dab2010-11-18 02:31:04127 extension->public_key());
[email protected]f8bbf6b2014-01-30 07:23:27128 EXPECT_EQ("oplhagaaipaimkjlbekcdjkffijdockj", extension->id());
[email protected]5ba5dab2010-11-18 02:31:04129 EXPECT_EQ("1978.12.11.0", extension->version()->GetString());
[email protected]04338722013-12-24 23:18:05130 EXPECT_EQ(base::UTF16ToUTF8(web_app.title), extension->name());
131 EXPECT_EQ(base::UTF16ToUTF8(web_app.description), extension->description());
[email protected]cadac622013-06-11 16:46:36132 EXPECT_EQ(web_app.app_url, AppLaunchInfo::GetFullLaunchURL(extension.get()));
[email protected]076ebeda2014-06-06 21:47:26133 EXPECT_EQ(0u,
134 extension->permissions_data()->active_permissions()->apis().size());
[email protected]f8bbf6b2014-01-30 07:23:27135 ASSERT_EQ(0u, extension->web_extent().patterns().size());
[email protected]5ba5dab2010-11-18 02:31:04136
[email protected]dc24976f2013-06-02 21:15:09137 EXPECT_EQ(web_app.icons.size(),
138 IconsInfo::GetIcons(extension.get()).map().size());
[email protected]5ba5dab2010-11-18 02:31:04139 for (size_t i = 0; i < web_app.icons.size(); ++i) {
[email protected]7d3cbc92013-03-18 22:33:04140 EXPECT_EQ(base::StringPrintf("icons/%i.png", web_app.icons[i].width),
[email protected]dc24976f2013-06-02 21:15:09141 IconsInfo::GetIcons(extension.get()).Get(
[email protected]702d8b42013-02-27 20:55:50142 web_app.icons[i].width, ExtensionIconSet::MATCH_EXACTLY));
[email protected]dc24976f2013-06-02 21:15:09143 ExtensionResource resource =
144 IconsInfo::GetIconResource(extension.get(),
145 web_app.icons[i].width,
146 ExtensionIconSet::MATCH_EXACTLY);
[email protected]5ba5dab2010-11-18 02:31:04147 ASSERT_TRUE(!resource.empty());
[email protected]7567484142013-07-11 17:36:07148 EXPECT_TRUE(base::PathExists(resource.GetFilePath()));
[email protected]5ba5dab2010-11-18 02:31:04149 }
150}
151
[email protected]d592b1bd2013-05-06 06:40:47152TEST(ExtensionFromWebApp, Minimal) {
[email protected]ea1a3f62012-11-16 20:34:23153 base::ScopedTempDir extensions_dir;
[email protected]171ab92d2012-10-19 01:16:34154 ASSERT_TRUE(extensions_dir.CreateUniqueTempDir());
155
[email protected]5ba5dab2010-11-18 02:31:04156 WebApplicationInfo web_app;
[email protected]04338722013-12-24 23:18:05157 web_app.title = base::ASCIIToUTF16("Gearpad");
[email protected]5ba5dab2010-11-18 02:31:04158 web_app.app_url = GURL("http://aaronboodman.com/gearpad/");
159
[email protected]b1912d592012-08-17 22:29:38160 scoped_refptr<Extension> extension = ConvertWebAppToExtension(
[email protected]171ab92d2012-10-19 01:16:34161 web_app, GetTestTime(1978, 12, 11, 0, 0, 0, 0),
162 extensions_dir.path());
[email protected]5ba5dab2010-11-18 02:31:04163 ASSERT_TRUE(extension.get());
164
[email protected]ea1a3f62012-11-16 20:34:23165 base::ScopedTempDir extension_dir;
[email protected]2d57f5d2011-01-13 14:20:12166 EXPECT_TRUE(extension_dir.Set(extension->path()));
[email protected]5ba5dab2010-11-18 02:31:04167
168 EXPECT_TRUE(extension->is_app());
169 EXPECT_TRUE(extension->is_hosted_app());
[email protected]c4f459d2012-09-28 04:40:10170 EXPECT_FALSE(extension->is_legacy_packaged_app());
[email protected]5ba5dab2010-11-18 02:31:04171
[email protected]f8bbf6b2014-01-30 07:23:27172 EXPECT_EQ("zVvdNZy3Mp7CFU8JVSyXNlDuHdVLbP7fDO3TGVzj/0w=",
[email protected]5ba5dab2010-11-18 02:31:04173 extension->public_key());
[email protected]f8bbf6b2014-01-30 07:23:27174 EXPECT_EQ("oplhagaaipaimkjlbekcdjkffijdockj", extension->id());
[email protected]5ba5dab2010-11-18 02:31:04175 EXPECT_EQ("1978.12.11.0", extension->version()->GetString());
[email protected]04338722013-12-24 23:18:05176 EXPECT_EQ(base::UTF16ToUTF8(web_app.title), extension->name());
[email protected]5ba5dab2010-11-18 02:31:04177 EXPECT_EQ("", extension->description());
[email protected]cadac622013-06-11 16:46:36178 EXPECT_EQ(web_app.app_url, AppLaunchInfo::GetFullLaunchURL(extension.get()));
[email protected]dc24976f2013-06-02 21:15:09179 EXPECT_EQ(0u, IconsInfo::GetIcons(extension.get()).map().size());
[email protected]076ebeda2014-06-06 21:47:26180 EXPECT_EQ(0u,
181 extension->permissions_data()->active_permissions()->apis().size());
[email protected]f8bbf6b2014-01-30 07:23:27182 ASSERT_EQ(0u, extension->web_extent().patterns().size());
[email protected]5ba5dab2010-11-18 02:31:04183}
[email protected]b1912d592012-08-17 22:29:38184
185} // namespace extensions