a2752c27d26cc99a12338cf1514b9a99850433b3
[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(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);
106   }
107
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);
121   }
122 };
123
124 TEST_F(WidgetHandlerTest, ParseManifestWithOnlyNameAndVersion) {
125   base::DictionaryValue manifest;
126   manifest.SetString(keys::kNameKey, "no name");
127   manifest.SetString(keys::kVersionKey, "0");
128
129   scoped_refptr<ApplicationData> application = CreateApplication(manifest);
130   EXPECT_TRUE(application.get());
131
132   WidgetInfo* info = GetWidgetInfo(application);
133   int size = info->GetWidgetInfo()->size();
134
135   // Only name and version ,others are empty string "",but exist.
136   // And widget have 10 items.
137   EXPECT_EQ(size, 10);
138
139   base::DictionaryValue* widget = info->GetWidgetInfo();
140   base::DictionaryValue::Iterator it(*widget);
141
142   std::string tmpStr;
143   // Check value
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");
150     } else {
151       EXPECT_EQ(tmpStr, "");
152     }
153     it.Advance();
154   }
155 }
156
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);
170   EXPECT_TRUE(info);
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);
175
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));
180
181   // Compare the widget parsed from manifest with
182   // the widget create manually.
183   EXPECT_TRUE(widget->Equals(widget_parsed_from_manifest));
184 }
185
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));
194   }
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);
202   EXPECT_TRUE(info);
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);
207
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));
214   }
215
216   // Compare the widget parsed from manifest with
217   // the widget create manually.
218   EXPECT_TRUE(widget->Equals(widget_parsed_from_manifest));
219 }
220
221 }  // namespace application
222 }  // namespace xwalk