Initialize Tizen 2.3
[framework/web/wrt-installer.git] / tests / general / ParsingMetadataTests.cpp
1 /*
2  * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /**
17  * @file    ParsingMetadataTests.cpp
18  * @author  Slawomir Pajak (s.pajak@partner.samsung.com)
19  * @version 1.0
20  * @brief   metadata element installation tests
21  */
22
23 #include <string>
24 #include <dpl/test/test_runner.h>
25 #include <InstallerWrapper.h>
26 #include <ManifestFile.h>
27 #include <dpl/utils/wrt_utility.h>
28
29 #include <root_parser.h>
30 #include <widget_parser.h>
31 #include <parser_runner.h>
32
33 using namespace InstallerWrapper;
34
35 namespace{
36
37 template<typename Exception, typename Function>
38 bool checkException(Function fun)
39 {
40     Try
41     {
42         fun();
43     }
44     Catch(Exception){
45         return true;
46     }
47     return false;
48 }
49
50 } // namespace
51
52 #define RUNNER_ASSERT_EXCEPTION(exceptionType, function)             \
53     {                                                                \
54         RUNNER_ASSERT(checkException<exceptionType>([&](){function})); \
55     }
56
57
58
59 ////////////////////////////////////////////////////////////////////////////////
60
61 RUNNER_TEST_GROUP_INIT(ParsingMetadata)
62
63 /*
64 Name: InstallWidgetWithMetadata
65 Description: Tests if widget with metadata element is installed correctly
66 Expected: widget should be installed correctly and metadata info should be stored in manifest file
67 */
68 RUNNER_TEST(InstallWidgetWithMetadata)
69 {
70     std::string tizenId;
71     std::string manifestPath = "/opt/share/packages/";
72     RUNNER_ASSERT(install(miscWidgetsStuff + "widgets/metadata.wgt", tizenId) == InstallerWrapper::Success);
73
74     RUNNER_ASSERT(WrtUtilFileExists(manifestPath.append(tizenId.substr(0, 10)).append(".xml")));
75     ManifestFile mf(manifestPath);
76
77     RUNNER_ASSERT(mf.getValueByXpath("/p:manifest/p:ui-application/p:metadata[1]/@key") == "key1");
78     RUNNER_ASSERT(mf.getValueByXpath("/p:manifest/p:ui-application/p:metadata[2]/@key") == "key2");
79     RUNNER_ASSERT(mf.getValueByXpath("/p:manifest/p:ui-application/p:metadata[2]/@value") == "value2");
80     RUNNER_ASSERT(mf.getValueByXpath("/p:manifest/p:ui-application/p:metadata[3]/@key") == "key3");
81     RUNNER_ASSERT(mf.getValueByXpath("/p:manifest/p:ui-application/p:metadata[3]/@value") == "value3");
82     uninstall(tizenId);
83 }
84
85
86 /*
87 Name: NoMetadata
88 Description: Tests parsing configuration file without metadata element
89 Expected: Element should be parsed correctly.
90 */
91 RUNNER_TEST(NoMetadata)
92 {
93     ParserRunner parser;
94     WrtDB::ConfigParserData widgetConfig;
95
96     parser.Parse(miscWidgetsStuff + "configs/NoMetadata.xml",
97             ElementParserPtr(
98                 new RootParser<WidgetParser>(widgetConfig,
99                         L"widget")));
100
101     RUNNER_ASSERT(widgetConfig.metadataList.empty());
102 }
103
104 /*
105 Name: MetadataEmpty
106 Description: Tests parsing configuration file with empty metadata element
107 Expected: Exception should be thrown
108 */
109 //TODO: Fix in parser needed
110 //RUNNER_TEST(MetadataEmpty)
111 //{
112 //    ParserRunner parser;
113 //    WrtDB::ConfigParserData widgetConfig;
114 //
115 //    RUNNER_ASSERT_EXCEPTION(ElementParser::Exception::ParseError,
116 //        parser.Parse(miscWidgetsStuff + "configs/MetadataEmpty.xml",
117 //                ElementParserPtr( new RootParser<WidgetParser>(widgetConfig, L"widget")));
118 //    );
119 //}
120
121 /*
122 Name: MultipleMetadata
123 Description: Tests parsing configuration file with multiple metadata element
124 Expected: Element should be parsed correctly. All values should be stored
125 */
126 RUNNER_TEST(MultipleMetadata)
127 {
128     ParserRunner parser;
129     WrtDB::ConfigParserData widgetConfig;
130
131     parser.Parse(miscWidgetsStuff + "configs/MultipleMetadata.xml",
132             ElementParserPtr(
133                 new RootParser<WidgetParser>(widgetConfig,
134                         L"widget")));
135
136     RUNNER_ASSERT(3 == widgetConfig.metadataList.size());
137
138     RUNNER_ASSERT(1 == std::count(widgetConfig.metadataList.begin(), widgetConfig.metadataList.end(),
139             WrtDB::ConfigParserData::Metadata(DPL::OptionalString(L"key1"), DPL::OptionalString())));
140     RUNNER_ASSERT(1 == std::count(widgetConfig.metadataList.begin(), widgetConfig.metadataList.end(),
141             WrtDB::ConfigParserData::Metadata(DPL::OptionalString(L"key2"), DPL::OptionalString(L"value2"))));
142     RUNNER_ASSERT(1 == std::count(widgetConfig.metadataList.begin(), widgetConfig.metadataList.end(),
143             WrtDB::ConfigParserData::Metadata(DPL::OptionalString(L"key3"), DPL::OptionalString(L"value3"))));
144
145 }
146
147 /*
148 Name: MetadataDuplicatedKey
149 Description: Tests parsing configuration file with duplicated key attribute value in metadata element
150 Expected: Exception should be thrown
151 */
152 //TODO: Fix in parser needed
153 //RUNNER_TEST(MetadataDuplicatedKey)
154 //{
155 //    ParserRunner parser;
156 //    WrtDB::ConfigParserData widgetConfig;
157 //
158 //    RUNNER_ASSERT_EXCEPTION(ElementParser::Exception::ParseError,
159 //        parser.Parse(miscWidgetsStuff + "configs/MetadataDuplicatedKey.xml",
160 //                ElementParserPtr( new RootParser<WidgetParser>(widgetConfig, L"widget")));
161 //    );
162 //}