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::kAuthorKey, author);
97 manifest->SetString(keys::kDescriptionKey, decription);
98 manifest->SetString(keys::kNameKey, name);
99 manifest->SetString(keys::kShortNameKey, shortName);
100 manifest->SetString(keys::kVersionKey, version);
101 manifest->SetString(keys::kIDKey, ID);
102 manifest->SetString(keys::kAuthorEmailKey, authorEmail);
103 manifest->SetString(keys::kAuthorHrefKey, authorHref);
104 manifest->SetString(keys::kHeightKey, height);
105 manifest->SetString(keys::kWidthKey, width);
108 // No Preferences and full other information
109 void SetAllInfoToWidget(base::DictionaryValue* widget) {
110 // Insert some key-value pairs into widget use widget key;
111 widget->SetString(kWidgetAuthor, author);
112 widget->SetString(kWidgetDecription, decription);
113 widget->SetString(kWidgetName, name);
114 widget->SetString(kWidgetShortName, shortName);
115 widget->SetString(kWidgetVersion, version);
116 widget->SetString(kWidgetID, ID);
117 widget->SetString(kWidgetAuthorEmail, authorEmail);
118 widget->SetString(kWidgetAuthorHref, authorHref);
119 widget->SetString(kWidgetHeight, height);
120 widget->SetString(kWidgetWidth, width);
124 TEST_F(WidgetHandlerTest, ParseManifestWithOnlyNameAndVersion) {
125 base::DictionaryValue manifest;
126 manifest.SetString(keys::kNameKey, "no name");
127 manifest.SetString(keys::kVersionKey, "0");
129 scoped_refptr<ApplicationData> application = CreateApplication(manifest);
130 EXPECT_TRUE(application.get());
132 WidgetInfo* info = GetWidgetInfo(application);
133 int size = info->GetWidgetInfo()->size();
135 // Only name and version ,others are empty string "",but exist.
136 // And widget have 10 items.
139 base::DictionaryValue* widget = info->GetWidgetInfo();
140 base::DictionaryValue::Iterator it(*widget);
144 while (!it.IsAtEnd()) {
145 it.value().GetAsString(&tmpStr);
146 if (it.key() == kWidgetName) {
147 EXPECT_EQ(tmpStr, "no name");
148 } else if (it.key() == kWidgetVersion) {
149 EXPECT_EQ(tmpStr, "0");
151 EXPECT_EQ(tmpStr, "");
157 TEST_F(WidgetHandlerTest,
158 ParseManifestWithAllOfOtherItemsAndOnePreferenceItem) {
159 // Create a manifest with one preference item.
160 scoped_ptr<base::DictionaryValue> manifest(new base::DictionaryValue);
161 SetAllInfoToManifest(manifest.get());
162 manifest->Set(keys::kPreferencesKey, GetPreferencesItem(0, true));
163 // Create an application use this manifest.
164 scoped_refptr<ApplicationData> application;
165 application = CreateApplication(*(manifest.get()));
166 EXPECT_TRUE(application.get());
167 EXPECT_EQ(application->manifest_type(), Manifest::TYPE_WIDGET);
168 // Get widget info from this application.
169 WidgetInfo* info = GetWidgetInfo(application);
171 scoped_ptr<base::DictionaryValue> Copy(info->GetWidgetInfo()->DeepCopy());
172 base::DictionaryValue* widget_parsed_from_manifest;
173 Copy->GetAsDictionary(&widget_parsed_from_manifest);
174 EXPECT_TRUE(widget_parsed_from_manifest);
176 // Create a widget with one preference item manually.
177 scoped_ptr<base::DictionaryValue> widget(new base::DictionaryValue);
178 SetAllInfoToWidget(widget.get());
179 widget->Set(kWidgetPreferences, GetPreferencesItem(0, false));
181 // Compare the widget parsed from manifest with
182 // the widget create manually.
183 EXPECT_TRUE(widget->Equals(widget_parsed_from_manifest));
186 TEST_F(WidgetHandlerTest,
187 ParseManifestWithAllOfOtherItemsAndThreePreferenceItemsList) {
188 // Create a manifest with three preference items.
189 scoped_ptr<base::DictionaryValue> manifest(new base::DictionaryValue);
190 SetAllInfoToManifest(manifest.get());
191 base::ListValue* manifestPreferences = new base::ListValue;
192 for (int i = 0; i < 3; i++) {
193 manifestPreferences->Append(GetPreferencesItem(i, true));
195 // Create an application use this manifest,
196 scoped_refptr<ApplicationData> application;
197 application = CreateApplication(*(manifest.get()));
198 EXPECT_TRUE(application.get());
199 EXPECT_EQ(application->manifest_type(), Manifest::TYPE_WIDGET);
200 // Get widget info from this application.
201 WidgetInfo* info = GetWidgetInfo(application);
203 scoped_ptr<base::DictionaryValue> Copy(info->GetWidgetInfo()->DeepCopy());
204 base::DictionaryValue* widget_parsed_from_manifest;
205 Copy->GetAsDictionary(&widget_parsed_from_manifest);
206 EXPECT_TRUE(widget_parsed_from_manifest);
208 // Create a widget with three preference items manually.
209 scoped_ptr<base::DictionaryValue> widget(new base::DictionaryValue);
210 SetAllInfoToWidget(widget.get());
211 base::ListValue* widgetPreferences = new base::ListValue;
212 for (int i = 0; i < 3; i++) {
213 widgetPreferences->Append(GetPreferencesItem(i, false));
216 // Compare the widget parsed from manifest with
217 // the widget create manually.
218 EXPECT_TRUE(widget->Equals(widget_parsed_from_manifest));
221 } // namespace application