1 // Copyright (c) 2014 Intel Corporation. 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.
5 #include "xwalk/application/common/manifest_handlers/widget_handler.h"
7 #include "xwalk/application/common/application_manifest_constants.h"
8 #include "testing/gtest/include/gtest/gtest.h"
12 namespace keys = application_widget_keys;
14 namespace application {
17 // Below key names are readable from Javascript widget interface.
18 const char kWidgetAuthor[] = "author";
19 const char kWidgetDecription[] = "description";
20 const char kWidgetName[] = "name";
21 const char kWidgetShortName[] = "shortName";
22 const char kWidgetVersion[] = "version";
23 const char kWidgetID[] = "id";
24 const char kWidgetAuthorEmail[] = "authorEmail";
25 const char kWidgetAuthorHref[] = "authorHref";
26 const char kWidgetHeight[] = "height";
27 const char kWidgetWidth[] = "width";
28 const char kWidgetPreferences[] = "preferences";
30 // Child keys inside 'preferences' key.
31 const char kWidgetPreferencesName[] = "name";
32 const char kWidgetPreferencesValue[] = "value";
33 const char kWidgetPreferencesReadonly[] = "readonly";
36 const char author[] = "Some one";
37 const char decription[] = "This is a test";
38 const char name[] = "widget handler unittest";
39 const char shortName[] = "whu";
40 const char version[] = "0.0.0.1";
41 const char ID[] = "iiiiiiiiiid";
42 const char authorEmail[] = "aaa@bbb.com";
43 const char authorHref[] = "http://www.ssss.com";
44 const char height[] = "800";
45 const char width[] = "480";
47 const char* preferencesName[] = {"pname0", "pname1", "pname2"};
48 const char* preferencesValue[] = {"pvalue0", "pvalue1", "pvalue2"};
49 const char* preferencesReadonly[] = {"true", "false", "false"};
52 class WidgetHandlerTest: public testing::Test {
54 scoped_refptr<ApplicationData> CreateApplication(
55 const base::DictionaryValue& manifest) {
57 scoped_refptr<ApplicationData> application = ApplicationData::Create(
58 base::FilePath(), std::string(), ApplicationData::LOCAL_DIRECTORY,
59 make_scoped_ptr(new Manifest(make_scoped_ptr(manifest.DeepCopy()),
60 Manifest::TYPE_WIDGET)),
65 WidgetInfo* GetWidgetInfo(scoped_refptr<ApplicationData> application) {
66 WidgetInfo* info = static_cast<WidgetInfo*>(
67 application->GetManifestData(keys::kWidgetKey));
71 base::DictionaryValue* GetPreferencesItem(int id,
72 bool is_parsed_manifest_key) {
73 base::DictionaryValue* preferences = new base::DictionaryValue;
74 if (is_parsed_manifest_key) {
75 preferences->SetString(keys::kPreferencesNameKey,
77 preferences->SetString(keys::kPreferencesValueKey,
78 preferencesValue[id]);
79 // PreferencesReadonly is string on manifest and bool on widgetInfo
80 preferences->SetString(keys::kPreferencesReadonlyKey,
81 preferencesReadonly[id]);
83 preferences->SetString(kWidgetPreferencesName,
85 preferences->SetString(kWidgetPreferencesValue,
86 preferencesValue[id]);
87 preferences->SetBoolean(kWidgetPreferencesReadonly,
88 strncmp(preferencesReadonly[id], "true", 4) == 0);
93 // No Preferences and full other information
94 void SetAllInfoToManifest(base::DictionaryValue* manifest) {
95 // Insert some key-value pairs into manifest use full key
96 manifest->SetString(keys::kWidgetNamespaceKey,
97 keys::kWidgetNamespacePrefix);
98 manifest->SetString(keys::kAuthorKey, author);
99 manifest->SetString(keys::kDescriptionKey, decription);
100 manifest->SetString(keys::kNameKey, name);
101 manifest->SetString(keys::kShortNameKey, shortName);
102 manifest->SetString(keys::kVersionKey, version);
103 manifest->SetString(keys::kIDKey, ID);
104 manifest->SetString(keys::kAuthorEmailKey, authorEmail);
105 manifest->SetString(keys::kAuthorHrefKey, authorHref);
106 manifest->SetString(keys::kHeightKey, height);
107 manifest->SetString(keys::kWidthKey, width);
110 // No Preferences and full other information
111 void SetAllInfoToWidget(base::DictionaryValue* widget) {
112 // Insert some key-value pairs into widget use widget key;
113 widget->SetString(kWidgetAuthor, author);
114 widget->SetString(kWidgetDecription, decription);
115 widget->SetString(kWidgetName, name);
116 widget->SetString(kWidgetShortName, shortName);
117 widget->SetString(kWidgetVersion, version);
118 widget->SetString(kWidgetID, ID);
119 widget->SetString(kWidgetAuthorEmail, authorEmail);
120 widget->SetString(kWidgetAuthorHref, authorHref);
121 widget->SetString(kWidgetHeight, height);
122 widget->SetString(kWidgetWidth, width);
126 TEST_F(WidgetHandlerTest, ParseManifestWithOnlyNameAndVersion) {
127 base::DictionaryValue manifest;
128 manifest.SetString(keys::kWidgetNamespaceKey, keys::kWidgetNamespacePrefix);
129 manifest.SetString(keys::kNameKey, "no name");
130 manifest.SetString(keys::kVersionKey, "0");
132 scoped_refptr<ApplicationData> application = CreateApplication(manifest);
133 EXPECT_TRUE(application.get());
135 WidgetInfo* info = GetWidgetInfo(application);
136 int size = info->GetWidgetInfo()->size();
138 // Only name and version ,others are empty string "",but exist.
139 // And widget have 10 items.
142 base::DictionaryValue* widget = info->GetWidgetInfo();
143 base::DictionaryValue::Iterator it(*widget);
147 while (!it.IsAtEnd()) {
148 it.value().GetAsString(&tmpStr);
149 if (it.key() == kWidgetName) {
150 EXPECT_EQ(tmpStr, "no name");
151 } else if (it.key() == kWidgetVersion) {
152 EXPECT_EQ(tmpStr, "0");
154 EXPECT_EQ(tmpStr, "");
160 TEST_F(WidgetHandlerTest,
161 ParseManifestWithAllOfOtherItemsAndOnePreferenceItem) {
162 // Create a manifest with one preference item.
163 scoped_ptr<base::DictionaryValue> manifest(new base::DictionaryValue);
164 SetAllInfoToManifest(manifest.get());
165 manifest->Set(keys::kPreferencesKey, GetPreferencesItem(0, true));
166 // Create an application use this manifest.
167 scoped_refptr<ApplicationData> application;
168 application = CreateApplication(*(manifest.get()));
169 EXPECT_TRUE(application.get());
170 EXPECT_EQ(application->manifest_type(), Manifest::TYPE_WIDGET);
171 // Get widget info from this application.
172 WidgetInfo* info = GetWidgetInfo(application);
174 scoped_ptr<base::DictionaryValue> Copy(info->GetWidgetInfo()->DeepCopy());
175 base::DictionaryValue* widget_parsed_from_manifest;
176 Copy->GetAsDictionary(&widget_parsed_from_manifest);
177 EXPECT_TRUE(widget_parsed_from_manifest);
179 // Create a widget with one preference item manually.
180 scoped_ptr<base::DictionaryValue> widget(new base::DictionaryValue);
181 SetAllInfoToWidget(widget.get());
182 widget->Set(kWidgetPreferences, GetPreferencesItem(0, false));
184 // Compare the widget parsed from manifest with
185 // the widget create manually.
186 EXPECT_TRUE(widget->Equals(widget_parsed_from_manifest));
189 TEST_F(WidgetHandlerTest,
190 ParseManifestWithAllOfOtherItemsAndThreePreferenceItemsList) {
191 // Create a manifest with three preference items.
192 scoped_ptr<base::DictionaryValue> manifest(new base::DictionaryValue);
193 SetAllInfoToManifest(manifest.get());
194 base::ListValue* manifestPreferences = new base::ListValue;
195 for (int i = 0; i < 3; i++) {
196 manifestPreferences->Append(GetPreferencesItem(i, true));
198 // Create an application use this manifest,
199 scoped_refptr<ApplicationData> application;
200 application = CreateApplication(*(manifest.get()));
201 EXPECT_TRUE(application.get());
202 EXPECT_EQ(application->manifest_type(), Manifest::TYPE_WIDGET);
203 // Get widget info from this application.
204 WidgetInfo* info = GetWidgetInfo(application);
206 scoped_ptr<base::DictionaryValue> Copy(info->GetWidgetInfo()->DeepCopy());
207 base::DictionaryValue* widget_parsed_from_manifest;
208 Copy->GetAsDictionary(&widget_parsed_from_manifest);
209 EXPECT_TRUE(widget_parsed_from_manifest);
211 // Create a widget with three preference items manually.
212 scoped_ptr<base::DictionaryValue> widget(new base::DictionaryValue);
213 SetAllInfoToWidget(widget.get());
214 base::ListValue* widgetPreferences = new base::ListValue;
215 for (int i = 0; i < 3; i++) {
216 widgetPreferences->Append(GetPreferencesItem(i, false));
219 // Compare the widget parsed from manifest with
220 // the widget create manually.
221 EXPECT_TRUE(widget->Equals(widget_parsed_from_manifest));
224 TEST_F(WidgetHandlerTest,
225 ParseManifestWithInvalidAuthorHrefValue) {
226 scoped_ptr<base::DictionaryValue> manifest(new base::DictionaryValue);
227 SetAllInfoToManifest(manifest.get());
228 manifest->SetString(keys::kAuthorHrefKey, "INVALID_HREF");
230 // Create an application use this manifest,
231 scoped_refptr<ApplicationData> application;
232 application = CreateApplication(*(manifest.get()));
233 EXPECT_TRUE(application.get());
234 EXPECT_EQ(application->manifest_type(), Manifest::TYPE_WIDGET);
235 // Get widget info from this application.
236 WidgetInfo* info = GetWidgetInfo(application);
238 std::string authorhref;
239 info->GetWidgetInfo()->GetString(keys::kAuthorHrefKey, &authorhref);
240 EXPECT_TRUE(authorhref.empty());
242 } // namespace application