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(), ApplicationData::LOCAL_DIRECTORY,
59 manifest, "", &error);
63 WidgetInfo* GetWidgetInfo(scoped_refptr<ApplicationData> application) {
64 WidgetInfo* info = static_cast<WidgetInfo*>(
65 application->GetManifestData(keys::kWidgetKey));
69 base::DictionaryValue* GetPreferencesItem(int id,
70 bool is_parsed_manifest_key) {
71 base::DictionaryValue* preferences = new base::DictionaryValue;
72 if (is_parsed_manifest_key) {
73 preferences->SetString(keys::kPreferencesNameKey,
75 preferences->SetString(keys::kPreferencesValueKey,
76 preferencesValue[id]);
77 // PreferencesReadonly is string on manifest and bool on widgetInfo
78 preferences->SetString(keys::kPreferencesReadonlyKey,
79 preferencesReadonly[id]);
81 preferences->SetString(kWidgetPreferencesName,
83 preferences->SetString(kWidgetPreferencesValue,
84 preferencesValue[id]);
85 preferences->SetBoolean(kWidgetPreferencesReadonly,
86 strncmp(preferencesReadonly[id], "true", 4) == 0);
91 // No Preferences and full other information
92 void SetAllInfoToManifest(base::DictionaryValue* manifest) {
93 // Insert some key-value pairs into manifest use full key
94 manifest->SetString(keys::kAuthorKey, author);
95 manifest->SetString(keys::kDescriptionKey, decription);
96 manifest->SetString(keys::kNameKey, name);
97 manifest->SetString(keys::kShortNameKey, shortName);
98 manifest->SetString(keys::kVersionKey, version);
99 manifest->SetString(keys::kIDKey, ID);
100 manifest->SetString(keys::kAuthorEmailKey, authorEmail);
101 manifest->SetString(keys::kAuthorHrefKey, authorHref);
102 manifest->SetString(keys::kHeightKey, height);
103 manifest->SetString(keys::kWidthKey, width);
106 // No Preferences and full other information
107 void SetAllInfoToWidget(base::DictionaryValue* widget) {
108 // Insert some key-value pairs into widget use widget key;
109 widget->SetString(kWidgetAuthor, author);
110 widget->SetString(kWidgetDecription, decription);
111 widget->SetString(kWidgetName, name);
112 widget->SetString(kWidgetShortName, shortName);
113 widget->SetString(kWidgetVersion, version);
114 widget->SetString(kWidgetID, ID);
115 widget->SetString(kWidgetAuthorEmail, authorEmail);
116 widget->SetString(kWidgetAuthorHref, authorHref);
117 widget->SetString(kWidgetHeight, height);
118 widget->SetString(kWidgetWidth, width);
122 TEST_F(WidgetHandlerTest, ParseManifestWithOnlyNameAndVersion) {
123 base::DictionaryValue manifest;
124 manifest.SetString(keys::kNameKey, "no name");
125 manifest.SetString(keys::kVersionKey, "0");
127 scoped_refptr<ApplicationData> application = CreateApplication(manifest);
128 EXPECT_TRUE(application);
130 WidgetInfo* info = GetWidgetInfo(application);
131 int size = info->GetWidgetInfo()->size();
133 // Only name and version ,others are empty string "",but exist.
134 // And widget have 10 items.
137 base::DictionaryValue* widget = info->GetWidgetInfo();
138 base::DictionaryValue::Iterator it(*widget);
142 while (!it.IsAtEnd()) {
143 it.value().GetAsString(&tmpStr);
144 if (it.key() == kWidgetName) {
145 EXPECT_EQ(tmpStr, "no name");
146 } else if (it.key() == kWidgetVersion) {
147 EXPECT_EQ(tmpStr, "0");
149 EXPECT_EQ(tmpStr, "");
155 TEST_F(WidgetHandlerTest,
156 ParseManifestWithAllOfOtherItemsAndOnePreferenceItem) {
157 // Create a manifest with one preference item.
158 scoped_ptr<base::DictionaryValue> manifest(new base::DictionaryValue);
159 SetAllInfoToManifest(manifest.get());
160 manifest->Set(keys::kPreferencesKey, GetPreferencesItem(0, true));
161 // Create an application use this manifest.
162 scoped_refptr<ApplicationData> application;
163 application = CreateApplication(*(manifest.get()));
164 EXPECT_TRUE(application);
165 EXPECT_EQ(application->GetPackageType(), Package::WGT);
166 // Get widget info from this application.
167 WidgetInfo* info = GetWidgetInfo(application);
169 scoped_ptr<base::DictionaryValue> Copy(info->GetWidgetInfo()->DeepCopy());
170 base::DictionaryValue* widget_parsed_from_manifest;
171 Copy->GetAsDictionary(&widget_parsed_from_manifest);
172 EXPECT_TRUE(widget_parsed_from_manifest);
174 // Create a widget with one preference item manually.
175 scoped_ptr<base::DictionaryValue> widget(new base::DictionaryValue);
176 SetAllInfoToWidget(widget.get());
177 widget->Set(kWidgetPreferences, GetPreferencesItem(0, false));
179 // Compare the widget parsed from manifest with
180 // the widget create manually.
181 EXPECT_TRUE(widget->Equals(widget_parsed_from_manifest));
184 TEST_F(WidgetHandlerTest,
185 ParseManifestWithAllOfOtherItemsAndThreePreferenceItemsList) {
186 // Create a manifest with three preference items.
187 scoped_ptr<base::DictionaryValue> manifest(new base::DictionaryValue);
188 SetAllInfoToManifest(manifest.get());
189 base::ListValue* manifestPreferences = new base::ListValue;
190 for (int i = 0; i < 3; i++) {
191 manifestPreferences->Append(GetPreferencesItem(i, true));
193 // Create an application use this manifest,
194 scoped_refptr<ApplicationData> application;
195 application = CreateApplication(*(manifest.get()));
196 EXPECT_TRUE(application);
197 EXPECT_EQ(application->GetPackageType(), Package::WGT);
198 // Get widget info from this application.
199 WidgetInfo* info = GetWidgetInfo(application);
201 scoped_ptr<base::DictionaryValue> Copy(info->GetWidgetInfo()->DeepCopy());
202 base::DictionaryValue* widget_parsed_from_manifest;
203 Copy->GetAsDictionary(&widget_parsed_from_manifest);
204 EXPECT_TRUE(widget_parsed_from_manifest);
206 // Create a widget with three preference items manually.
207 scoped_ptr<base::DictionaryValue> widget(new base::DictionaryValue);
208 SetAllInfoToWidget(widget.get());
209 base::ListValue* widgetPreferences = new base::ListValue;
210 for (int i = 0; i < 3; i++) {
211 widgetPreferences->Append(GetPreferencesItem(i, false));
214 // Compare the widget parsed from manifest with
215 // the widget create manually.
216 EXPECT_TRUE(widget->Equals(widget_parsed_from_manifest));
219 } // namespace application