1e734dff28d7be5acb8e3046692cb387e48264d9
[platform/framework/web/crosswalk.git] / src / xwalk / application / common / manifest_handlers / widget_handler_unittest.cc
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.
4
5 #include "xwalk/application/common/manifest_handlers/widget_handler.h"
6
7 #include "xwalk/application/common/application_manifest_constants.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9
10 namespace xwalk {
11
12 namespace keys = application_widget_keys;
13
14 namespace application {
15
16 namespace {
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";
29
30 // Child keys inside 'preferences' key.
31 const char kWidgetPreferencesName[] = "name";
32 const char kWidgetPreferencesValue[] = "value";
33 const char kWidgetPreferencesReadonly[] = "readonly";
34
35 // Value to test
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";
46
47 const char* preferencesName[] = {"pname0", "pname1", "pname2"};
48 const char* preferencesValue[] = {"pvalue0", "pvalue1", "pvalue2"};
49 const char* preferencesReadonly[] = {"true", "false", "false"};
50 }  // namespace
51
52 class WidgetHandlerTest: public testing::Test {
53  public:
54   scoped_refptr<ApplicationData> CreateApplication(
55     const base::DictionaryValue& manifest) {
56     std::string error;
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)),
61         &error);
62     return application;
63   }
64
65   WidgetInfo* GetWidgetInfo(scoped_refptr<ApplicationData> application) {
66     WidgetInfo* info = static_cast<WidgetInfo*>(
67       application->GetManifestData(keys::kWidgetKey));
68     return info;
69   }
70
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,
76                              preferencesName[id]);
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]);
82     } else {
83       preferences->SetString(kWidgetPreferencesName,
84                              preferencesName[id]);
85       preferences->SetString(kWidgetPreferencesValue,
86                              preferencesValue[id]);
87       preferences->SetBoolean(kWidgetPreferencesReadonly,
88                               strncmp(preferencesReadonly[id], "true", 4) == 0);
89     }
90     return preferences;
91   }
92
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(kW3CNamespaceKey,      kW3CNamespacePrefix);
97     manifest->SetString(keys::kAuthorKey,      author);
98     manifest->SetString(keys::kDescriptionKey, decription);
99     manifest->SetString(keys::kNameKey,        name);
100     manifest->SetString(keys::kShortNameKey,   shortName);
101     manifest->SetString(keys::kVersionKey,     version);
102     manifest->SetString(keys::kIDKey,          ID);
103     manifest->SetString(keys::kAuthorEmailKey, authorEmail);
104     manifest->SetString(keys::kAuthorHrefKey,  authorHref);
105     manifest->SetString(keys::kHeightKey,      height);
106     manifest->SetString(keys::kWidthKey,       width);
107   }
108
109   // No Preferences and full other information
110   void SetAllInfoToWidget(base::DictionaryValue* widget) {
111     // Insert some key-value pairs into widget use widget key;
112     widget->SetString(kWidgetAuthor,      author);
113     widget->SetString(kWidgetDecription,  decription);
114     widget->SetString(kWidgetName,        name);
115     widget->SetString(kWidgetShortName,   shortName);
116     widget->SetString(kWidgetVersion,     version);
117     widget->SetString(kWidgetID,          ID);
118     widget->SetString(kWidgetAuthorEmail, authorEmail);
119     widget->SetString(kWidgetAuthorHref,  authorHref);
120     widget->SetString(kWidgetHeight,      height);
121     widget->SetString(kWidgetWidth,       width);
122   }
123 };
124
125 TEST_F(WidgetHandlerTest, ParseManifestWithOnlyNameAndVersion) {
126   base::DictionaryValue manifest;
127   manifest.SetString(kW3CNamespaceKey, kW3CNamespacePrefix);
128   manifest.SetString(keys::kNameKey, "no name");
129   manifest.SetString(keys::kVersionKey, "0");
130
131   scoped_refptr<ApplicationData> application = CreateApplication(manifest);
132   EXPECT_TRUE(application.get());
133
134   WidgetInfo* info = GetWidgetInfo(application);
135   int size = info->GetWidgetInfo()->size();
136
137   // Only name and version ,others are empty string "",but exist.
138   // And widget have 10 items.
139   EXPECT_EQ(size, 10);
140
141   base::DictionaryValue* widget = info->GetWidgetInfo();
142   base::DictionaryValue::Iterator it(*widget);
143
144   std::string tmpStr;
145   // Check value
146   while (!it.IsAtEnd()) {
147     it.value().GetAsString(&tmpStr);
148     if (it.key() == kWidgetName) {
149       EXPECT_EQ(tmpStr, "no name");
150     } else if (it.key() == kWidgetVersion) {
151       EXPECT_EQ(tmpStr, "0");
152     } else {
153       EXPECT_EQ(tmpStr, "");
154     }
155     it.Advance();
156   }
157 }
158
159 TEST_F(WidgetHandlerTest,
160        ParseManifestWithAllOfOtherItemsAndOnePreferenceItem) {
161   // Create a manifest with one preference item.
162   scoped_ptr<base::DictionaryValue> manifest(new base::DictionaryValue);
163   SetAllInfoToManifest(manifest.get());
164   manifest->Set(keys::kPreferencesKey, GetPreferencesItem(0, true));
165   // Create an application use this manifest.
166   scoped_refptr<ApplicationData> application;
167   application = CreateApplication(*(manifest.get()));
168   EXPECT_TRUE(application.get());
169   EXPECT_EQ(application->manifest_type(), Manifest::TYPE_WIDGET);
170   // Get widget info from this application.
171   WidgetInfo* info = GetWidgetInfo(application);
172   EXPECT_TRUE(info);
173   scoped_ptr<base::DictionaryValue> Copy(info->GetWidgetInfo()->DeepCopy());
174   base::DictionaryValue* widget_parsed_from_manifest;
175   Copy->GetAsDictionary(&widget_parsed_from_manifest);
176   EXPECT_TRUE(widget_parsed_from_manifest);
177
178   // Create a widget with one preference item manually.
179   scoped_ptr<base::DictionaryValue> widget(new base::DictionaryValue);
180   SetAllInfoToWidget(widget.get());
181   widget->Set(kWidgetPreferences, GetPreferencesItem(0, false));
182
183   // Compare the widget parsed from manifest with
184   // the widget create manually.
185   EXPECT_TRUE(widget->Equals(widget_parsed_from_manifest));
186 }
187
188 TEST_F(WidgetHandlerTest,
189        ParseManifestWithAllOfOtherItemsAndThreePreferenceItemsList) {
190   // Create a manifest with three preference items.
191   scoped_ptr<base::DictionaryValue> manifest(new base::DictionaryValue);
192   SetAllInfoToManifest(manifest.get());
193   base::ListValue* manifestPreferences = new base::ListValue;
194   for (int i = 0; i < 3; i++) {
195     manifestPreferences->Append(GetPreferencesItem(i, true));
196   }
197   // Create an application use this manifest,
198   scoped_refptr<ApplicationData> application;
199   application = CreateApplication(*(manifest.get()));
200   EXPECT_TRUE(application.get());
201   EXPECT_EQ(application->manifest_type(), Manifest::TYPE_WIDGET);
202   // Get widget info from this application.
203   WidgetInfo* info = GetWidgetInfo(application);
204   EXPECT_TRUE(info);
205   scoped_ptr<base::DictionaryValue> Copy(info->GetWidgetInfo()->DeepCopy());
206   base::DictionaryValue* widget_parsed_from_manifest;
207   Copy->GetAsDictionary(&widget_parsed_from_manifest);
208   EXPECT_TRUE(widget_parsed_from_manifest);
209
210   // Create a widget with three preference items manually.
211   scoped_ptr<base::DictionaryValue> widget(new base::DictionaryValue);
212   SetAllInfoToWidget(widget.get());
213   base::ListValue* widgetPreferences   = new base::ListValue;
214   for (int i = 0; i < 3; i++) {
215     widgetPreferences->Append(GetPreferencesItem(i, false));
216   }
217
218   // Compare the widget parsed from manifest with
219   // the widget create manually.
220   EXPECT_TRUE(widget->Equals(widget_parsed_from_manifest));
221 }
222
223 }  // namespace application
224 }  // namespace xwalk