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

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: cleanup 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 PathService::Get(chrome::DIR_TEST_DATA, &path);
Paweł Hajdan Jr. 2010/11/15 08:05:31 Check return value of this.
Aaron Boodman 2010/11/16 03:44:45 Done.
23 path = path.AppendASCII("web_app_info").AppendASCII(name.c_str());
24 if (!file_util::PathExists(path)) {
25 ADD_FAILURE() << L"Path does not exist: " << path.ToWStringHack();
Erik does not do reviews 2010/11/15 19:45:36 I think you can just use value() instead of ToWStr
Aaron Boodman 2010/11/16 03:44:45 Done.
26 return NULL;
27 }
28
29 std::string error;
30 JSONFileValueSerializer serializer(path);
31 DictionaryValue* result = static_cast<DictionaryValue*>(
32 serializer.Deserialize(NULL, &error));
33 if (!result) {
34 ADD_FAILURE() << "Error parsing " << name << ": " << error;
35 return NULL;
36 }
37
38 return result;
39 }
40
41 WebApplicationInfo* ParseFromDefinitionAndExpectSuccess(
42 const std::string& name) {
43 scoped_ptr<DictionaryValue> defintion(LoadDefinitionFile(name));
44 if (!defintion.get())
45 return NULL;
46
47 scoped_ptr<WebApplicationInfo> web_app(new WebApplicationInfo());
48 web_app->manifest_url = GURL("http://example.com/");
49
50 string16 error;
51 if (!ParseWebAppFromDefinitionFile(*defintion, web_app.get(), &error)) {
52 ADD_FAILURE() << "Error parsing " << name << ": " << UTF16ToUTF8(error);
53 return NULL;
54 }
55
56 return web_app.release();
57 }
58
59 void ParseFromDefinitionAndExpectFailure(const std::string& name,
60 const string16& expected_error) {
61 scoped_ptr<DictionaryValue> definition(LoadDefinitionFile(name));
62 if (!definition.get())
63 return;
64
65 WebApplicationInfo web_app;
66 web_app.manifest_url = GURL("http://example.com/");
67
68 string16 error;
69 if (ParseWebAppFromDefinitionFile(*definition, &web_app, &error)) {
70 ADD_FAILURE() << "Expected error parsing " << name
71 << " but parse succeeded.";
72 return;
73 }
74
75 EXPECT_EQ(UTF16ToUTF8(expected_error), UTF16ToUTF8(error)) << name;
76 }
77
78 }
79
80 TEST(WebAppInfo, ParseFromDefinitionFileErrors) {
81 // Test one definition file with a JSON schema error, just to make sure we're
82 // correctly propagating those. We don't extensively test all the properties
83 // covered by the schema, since we assume JSON schema is working correctly.
84 ParseFromDefinitionAndExpectFailure(
85 "missing_name.json",
86 UTF8ToUTF16(std::string("name: ") +
87 JSONSchemaValidator::kObjectPropertyIsRequired));
88
89 ParseFromDefinitionAndExpectFailure(
90 "invalid_launch_url.json",
91 UTF8ToUTF16(WebApplicationInfo::kInvalidLaunchURL));
92
93 ParseFromDefinitionAndExpectFailure(
94 "invalid_urls.json",
95 UTF8ToUTF16(
96 JSONSchemaValidator::FormatErrorMessage(
97 WebApplicationInfo::kInvalidURL, "2")));
98 }
99
100 TEST(WebAppInfo, Minimal) {
101 scoped_ptr<WebApplicationInfo> web_app(
102 ParseFromDefinitionAndExpectSuccess("minimal.json"));
103
104 EXPECT_EQ(UTF8ToUTF16("hello"), web_app->title);
105 EXPECT_EQ(UTF8ToUTF16(""), web_app->description);
106 EXPECT_EQ(GURL("http://example.com/launch_url"), web_app->app_url);
107 EXPECT_EQ(0u, web_app->icons.size());
108 EXPECT_EQ(0u, web_app->urls.size());
109 EXPECT_EQ(0u, web_app->privileges.size());
110 EXPECT_EQ("", web_app->launch_container);
111 }
112
113 TEST(WebAppInfo, Full) {
114 scoped_ptr<WebApplicationInfo> web_app(
115 ParseFromDefinitionAndExpectSuccess("full.json"));
116
117 EXPECT_EQ(UTF8ToUTF16("hello"), web_app->title);
118 EXPECT_EQ(UTF8ToUTF16("This app is super awesome"), web_app->description);
119 EXPECT_EQ(GURL("http://example.com/launch_url"), web_app->app_url);
120 ASSERT_EQ(1u, web_app->icons.size());
121 EXPECT_EQ("http://example.com/16.png", web_app->icons[0].url.spec());
122 EXPECT_EQ(16, web_app->icons[0].width);
123 EXPECT_EQ(16, web_app->icons[0].height);
124 ASSERT_EQ(2u, web_app->urls.size());
125 EXPECT_EQ("http://example.com/foobar", web_app->urls[0].spec());
126 EXPECT_EQ("http://example.com/baz", web_app->urls[1].spec());
127 ASSERT_EQ(2u, web_app->privileges.size());
128 EXPECT_EQ("geolocation", web_app->privileges[0]);
129 EXPECT_EQ("notifications", web_app->privileges[1]);
130 EXPECT_EQ("panel", web_app->launch_container);
131 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698