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