Upstream version 8.37.186.0
[platform/framework/web/crosswalk.git] / src / xwalk / application / common / manifest_unittest.cc
1 // Copyright (c) 2012 The Chromium Authors. 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.h"
6
7 #include <algorithm>
8 #include <set>
9 #include <string>
10 #include <vector>
11
12 #include "base/memory/scoped_ptr.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/values.h"
15 #include "xwalk/application/common/application_manifest_constants.h"
16 #include "xwalk/application/common/install_warning.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18
19 namespace errors = xwalk::application_manifest_errors;
20 namespace keys = xwalk::application_manifest_keys;
21
22 namespace xwalk {
23 namespace application {
24
25 class ManifestTest : public testing::Test {
26  public:
27   ManifestTest() : default_value_("test") {}
28
29  protected:
30   void AssertType(Manifest* manifest, Manifest::Type type) {
31     EXPECT_EQ(type, manifest->GetType());
32     EXPECT_EQ(type == Manifest::TYPE_PACKAGED_APP,
33               manifest->IsPackaged());
34     EXPECT_EQ(type == Manifest::TYPE_HOSTED_APP, manifest->IsHosted());
35   }
36
37   // Helper function that replaces the Manifest held by |manifest| with a copy
38   // with its |key| changed to |value|. If |value| is NULL, then |key| will
39   // instead be deleted.
40   void MutateManifest(scoped_ptr<Manifest>* manifest,
41                       const std::string& key,
42                       base::Value* value) {
43     scoped_ptr<base::DictionaryValue> manifest_value(
44         manifest->get()->value()->DeepCopy());
45     if (value)
46       manifest_value->Set(key, value);
47     else
48       manifest_value->Remove(key, NULL);
49     manifest->reset(new Manifest(Manifest::COMMAND_LINE,
50             manifest_value.Pass()));
51   }
52
53   std::string default_value_;
54 };
55
56 // Verifies that application can access the correct keys.
57 TEST_F(ManifestTest, ApplicationData) {
58   scoped_ptr<base::DictionaryValue> manifest_value(new base::DictionaryValue());
59   manifest_value->SetString(keys::kNameKey, "extension");
60   manifest_value->SetString(keys::kXWalkVersionKey, "1");
61   manifest_value->SetString("unknown_key", "foo");
62
63   scoped_ptr<Manifest> manifest(
64       new Manifest(Manifest::COMMAND_LINE, manifest_value.Pass()));
65   std::string error;
66   std::vector<InstallWarning> warnings;
67   EXPECT_TRUE(manifest->ValidateManifest(&error, &warnings));
68   EXPECT_TRUE(error.empty());
69   // TODO(xiang): warnings will not be empty after enable manifest features
70   ASSERT_EQ(0u, warnings.size());
71   // AssertType(manifest.get(), Manifest::TYPE_HOSTED_AP);
72
73   // The unknown key 'unknown_key' should be accesible.
74   std::string value;
75   EXPECT_TRUE(manifest->GetString("unknown_key", &value));
76   EXPECT_EQ("foo", value);
77
78   // Test DeepCopy and Equals.
79   scoped_ptr<Manifest> manifest2(manifest->DeepCopy());
80   EXPECT_TRUE(manifest->Equals(manifest2.get()));
81   EXPECT_TRUE(manifest2->Equals(manifest.get()));
82   MutateManifest(
83       &manifest, "foo", new base::StringValue("blah"));
84   EXPECT_FALSE(manifest->Equals(manifest2.get()));
85 }
86
87 // Verifies that key restriction based on type works.
88 TEST_F(ManifestTest, ApplicationTypes) {
89   scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
90   value->SetString(keys::kNameKey, "extension");
91   value->SetString(keys::kXWalkVersionKey, "1");
92
93   scoped_ptr<Manifest> manifest(
94       new Manifest(Manifest::COMMAND_LINE, value.Pass()));
95   std::string error;
96   std::vector<InstallWarning> warnings;
97   EXPECT_TRUE(manifest->ValidateManifest(&error, &warnings));
98   EXPECT_TRUE(error.empty());
99   EXPECT_TRUE(warnings.empty());
100
101   // Platform app.
102   MutateManifest(
103       &manifest, keys::kStartURLKey,
104       new base::StringValue("main.html"));
105   AssertType(manifest.get(), Manifest::TYPE_PACKAGED_APP);
106   MutateManifest(
107       &manifest, keys::kStartURLKey, NULL);
108
109   // Hosted app.
110   MutateManifest(
111       &manifest, keys::kLaunchWebURLKey, new base::StringValue("foo"));
112   AssertType(manifest.get(), Manifest::TYPE_HOSTED_APP);
113   MutateManifest(
114       &manifest, keys::kLaunchWebURLKey, NULL);
115 }
116
117 }  // namespace application
118 }  // namespace xwalk
119
120