Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(42)

Side by Side Diff: chrome/common/web_apps_unittest.cc

Issue 4979003: Implement web app definition parsing. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix nacl64 build Created 10 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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/common/web_apps.h"
6
7 #include "base/file_path.h"
8 #include "base/file_util.h"
9 #include "base/path_service.h"
10 #include "base/scoped_ptr.h"
11 #include "base/utf_string_conversions.h"
12 #include "base/values.h"
13 #include "chrome/common/chrome_paths.h"
14 #include "chrome/common/json_schema_validator.h"
15 #include "chrome/common/json_value_serializer.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17
18 namespace {
19
20 DictionaryValue* LoadDefinitionFile(const std::string& name) {
21 FilePath path;
22 if (!PathService::Get(chrome::DIR_TEST_DATA, &path)) {
23 ADD_FAILURE() << "Could not get test data dir.";
24 return NULL;
25 }
26
27 path = path.AppendASCII("web_app_info").AppendASCII(name.c_str());
28 if (!file_util::PathExists(path)) {
29 ADD_FAILURE() << "Path does not exist: " << path.value();
30 return NULL;
31 }
32
33 std::string error;
34 JSONFileValueSerializer serializer(path);
35 DictionaryValue* result = static_cast<DictionaryValue*>(
36 serializer.Deserialize(NULL, &error));
37 if (!result) {
38 ADD_FAILURE() << "Error parsing " << name << ": " << error;
39 return NULL;
40 }
41
42 return result;
43 }
44
45 WebApplicationInfo* ParseFromDefinitionAndExpectSuccess(
46 const std::string& name) {
47 scoped_ptr<DictionaryValue> defintion(LoadDefinitionFile(name));
48 if (!defintion.get())
49 return NULL;
50
51 scoped_ptr<WebApplicationInfo> web_app(new WebApplicationInfo());
52 web_app->manifest_url = GURL("http://example.com/");
53
54 string16 error;
55 if (!ParseWebAppFromDefinitionFile(*defintion, web_app.get(), &error)) {
56 ADD_FAILURE() << "Error parsing " << name << ": " << UTF16ToUTF8(error);
57 return NULL;
58 }
59
60 return web_app.release();
61 }
62
63 void ParseFromDefinitionAndExpectFailure(const std::string& name,
64 const string16& expected_error) {
65 scoped_ptr<DictionaryValue> definition(LoadDefinitionFile(name));
66 if (!definition.get())
67 return;
68
69 WebApplicationInfo web_app;
70 web_app.manifest_url = GURL("http://example.com/");
71
72 string16 error;
73 if (ParseWebAppFromDefinitionFile(*definition, &web_app, &error)) {
74 ADD_FAILURE() << "Expected error parsing " << name
75 << " but parse succeeded.";
76 return;
77 }
78
79 EXPECT_EQ(UTF16ToUTF8(expected_error), UTF16ToUTF8(error)) << name;
80 }
81
82 }
83
84 TEST(WebAppInfo, ParseFromDefinitionFileErrors) {
85 // Test one definition file with a JSON schema error, just to make sure we're
86 // correctly propagating those. We don't extensively test all the properties
87 // covered by the schema, since we assume JSON schema is working correctly.
88 ParseFromDefinitionAndExpectFailure(
89 "missing_name.json",
90 UTF8ToUTF16(std::string("name: ") +
91 JSONSchemaValidator::kObjectPropertyIsRequired));
92
93 ParseFromDefinitionAndExpectFailure(
94 "invalid_launch_url.json",
95 UTF8ToUTF16(WebApplicationInfo::kInvalidLaunchURL));
96
97 ParseFromDefinitionAndExpectFailure(
98 "invalid_urls.json",
99 UTF8ToUTF16(
100 JSONSchemaValidator::FormatErrorMessage(
101 WebApplicationInfo::kInvalidURL, "2")));
102 }
103
104 TEST(WebAppInfo, Minimal) {
105 scoped_ptr<WebApplicationInfo> web_app(
106 ParseFromDefinitionAndExpectSuccess("minimal.json"));
107
108 EXPECT_EQ(UTF8ToUTF16("hello"), web_app->title);
109 EXPECT_EQ(UTF8ToUTF16(""), web_app->description);
110 EXPECT_EQ(GURL("http://example.com/launch_url"), web_app->app_url);
111 EXPECT_EQ(0u, web_app->icons.size());
112 EXPECT_EQ(0u, web_app->urls.size());
113 EXPECT_EQ(0u, web_app->permissions.size());
114 EXPECT_EQ("", web_app->launch_container);
115 }
116
117 TEST(WebAppInfo, Full) {
118 scoped_ptr<WebApplicationInfo> web_app(
119 ParseFromDefinitionAndExpectSuccess("full.json"));
120
121 EXPECT_EQ(UTF8ToUTF16("hello"), web_app->title);
122 EXPECT_EQ(UTF8ToUTF16("This app is super awesome"), web_app->description);
123 EXPECT_EQ(GURL("http://example.com/launch_url"), web_app->app_url);
124 ASSERT_EQ(1u, web_app->icons.size());
125 EXPECT_EQ("http://example.com/16.png", web_app->icons[0].url.spec());
126 EXPECT_EQ(16, web_app->icons[0].width);
127 EXPECT_EQ(16, web_app->icons[0].height);
128 ASSERT_EQ(2u, web_app->urls.size());
129 EXPECT_EQ("http://example.com/foobar", web_app->urls[0].spec());
130 EXPECT_EQ("http://example.com/baz", web_app->urls[1].spec());
131 ASSERT_EQ(2u, web_app->permissions.size());
132 EXPECT_EQ("geolocation", web_app->permissions[0]);
133 EXPECT_EQ("notifications", web_app->permissions[1]);
134 EXPECT_EQ("panel", web_app->launch_container);
135 }
136
137 // Tests ParseIconSizes with various input.
138 TEST(WebAppInfo, ParseIconSizes) {
139 struct TestData {
140 const char* input;
141 const bool expected_result;
142 const bool is_any;
143 const size_t expected_size_count;
144 const int width1;
145 const int height1;
146 const int width2;
147 const int height2;
148 } data[] = {
149 // Bogus input cases.
150 { "10", false, false, 0, 0, 0, 0, 0 },
151 { "10 10", false, false, 0, 0, 0, 0, 0 },
152 { "010", false, false, 0, 0, 0, 0, 0 },
153 { " 010 ", false, false, 0, 0, 0, 0, 0 },
154 { " 10x ", false, false, 0, 0, 0, 0, 0 },
155 { " x10 ", false, false, 0, 0, 0, 0, 0 },
156 { "any 10x10", false, false, 0, 0, 0, 0, 0 },
157 { "", false, false, 0, 0, 0, 0, 0 },
158 { "10ax11", false, false, 0, 0, 0, 0, 0 },
159
160 // Any.
161 { "any", true, true, 0, 0, 0, 0, 0 },
162 { " any", true, true, 0, 0, 0, 0, 0 },
163 { " any ", true, true, 0, 0, 0, 0, 0 },
164
165 // Sizes.
166 { "10x11", true, false, 1, 10, 11, 0, 0 },
167 { " 10x11 ", true, false, 1, 10, 11, 0, 0 },
168 { " 10x11 1x2", true, false, 2, 10, 11, 1, 2 },
169 };
170 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) {
171 bool is_any;
172 std::vector<gfx::Size> sizes;
173 bool result = ParseIconSizes(ASCIIToUTF16(data[i].input), &sizes, &is_any);
174 ASSERT_EQ(result, data[i].expected_result);
175 if (result) {
176 ASSERT_EQ(data[i].is_any, is_any);
177 ASSERT_EQ(data[i].expected_size_count, sizes.size());
178 if (sizes.size() > 0) {
179 ASSERT_EQ(data[i].width1, sizes[0].width());
180 ASSERT_EQ(data[i].height1, sizes[0].height());
181 }
182 if (sizes.size() > 1) {
183 ASSERT_EQ(data[i].width2, sizes[1].width());
184 ASSERT_EQ(data[i].height2, sizes[1].height());
185 }
186 }
187 }
188 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698