tizen 2.4 release
[framework/web/wrt-commons.git] / tests / test / runner_multiprocess.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    runner_multiprocess.cpp
18  * @author  Marcin Niesluchowski (m.niesluchow@samsung.com)
19  * @version 1.0
20  * @brief   Implementation file for test cases for engine internal tests
21  */
22
23 #include <dpl/test/test_runner_multiprocess.h>
24 #include <dpl/test/test_runner.h>
25 #include <list>
26
27 namespace {
28 std::list<std::string> split_string(std::string str, std::string delimiter)
29 {
30     size_t pos = 0;
31     std::string token;
32     std::list<std::string> stringList;
33     while ((pos = str.find(delimiter)) != std::string::npos) {
34         token = str.substr(0, pos);
35         stringList.push_back(token);
36         str.erase(0, pos + delimiter.length());
37     }
38     if(str.length() != 0){
39         stringList.push_back(token);
40     }
41     return stringList;
42 }
43 }
44
45 #define RUNNER_MULTIPROCESS_TEST_EXPECT(name, messages)                        \
46     static void testExpectFunction##name();                                    \
47     RUNNER_TEST(name)                                                          \
48     {                                                                          \
49         Try                                                                    \
50         {                                                                      \
51             DPL::Test::RunMultiProc(&testExpectFunction##name);                \
52         }                                                                      \
53         Catch(DPL::Test::TestRunner::TestFailed)                               \
54         {                                                                      \
55             std::string eMsg = messages;                                       \
56             std::list<std::string> eMessages = split_string(eMsg, "|");        \
57             std::string rMessage = _rethrown_exception.GetMessage();           \
58             if(eMsg.length() == 0 && rMessage.length() != 0) {                 \
59                 RUNNER_ASSERT_MSG(false, rMessage);                            \
60             }                                                                  \
61             bool failedFound = false;                                          \
62             for(std::list<std::string>::iterator it = eMessages.begin();       \
63                 it != eMessages.end();                                         \
64                 ++it)                                                          \
65             {                                                                  \
66                 if (!(*it).compare("TEST_FAILED")) {                           \
67                     failedFound = true;                                        \
68                     continue;                                                  \
69                 }                                                              \
70                 RUNNER_ASSERT_MSG(rMessage.find(*it)!=std::string::npos,       \
71                     "Key word " << *it << " not found in " << rMessage);       \
72             }                                                                  \
73             RUNNER_ASSERT_MSG(                                                 \
74                 rMessage.find("Reading pipe error")==std::string::npos,        \
75                 "Reading pipe error");                                         \
76             RUNNER_ASSERT_MSG(                                                 \
77                 rMessage.find("Timeout error")==std::string::npos,             \
78                 "Timeout error");                                              \
79             RUNNER_ASSERT_MSG(failedFound, "No TEST_FAILED found");            \
80         }                                                                      \
81         Catch(DPL::Test::TestRunner::Ignored)                                  \
82         {                                                                      \
83             std::string eMsg = messages;                                       \
84             std::list<std::string> eMessages = split_string(eMsg, "|");        \
85             std::string rMessage = _rethrown_exception.GetMessage();           \
86             if(eMsg.length() == 0 && rMessage.length() != 0) {                 \
87                 RUNNER_ASSERT_MSG(false, rMessage);                            \
88             }                                                                  \
89             bool ignoredFound = false;                                         \
90             for(std::list<std::string>::iterator it = eMessages.begin();       \
91                 it != eMessages.end();                                         \
92                 ++it)                                                          \
93             {                                                                  \
94                 if (!(*it).compare("TEST_IGNORED")) {                           \
95                     ignoredFound = true;                                       \
96                     continue;                                                  \
97                 }                                                              \
98                 RUNNER_ASSERT_MSG(rMessage.find(*it)!=std::string::npos,       \
99                     "Key word " << *it << " not found in " << rMessage);       \
100             }                                                                  \
101             RUNNER_ASSERT_MSG(                                                 \
102                 rMessage.find("Reading pipe error")==std::string::npos,        \
103                 "Reading pipe error");                                         \
104             RUNNER_ASSERT_MSG(                                                 \
105                 rMessage.find("Timeout error")==std::string::npos,             \
106                 "Timeout error");                                              \
107             RUNNER_ASSERT_MSG(ignoredFound, "No TEST_IGNORED found");          \
108         }                                                                      \
109     }                                                                          \
110     void testExpectFunction##name()                                            \
111
112 RUNNER_TEST_GROUP_INIT(DPL_TESTS_TEST_MULTIPROCESS)
113
114 RUNNER_MULTIPROCESS_TEST_EXPECT(tm00_pass, "")
115 {
116     RUNNER_ASSERT_MSG(1, "This test should pass");
117 }
118
119 RUNNER_MULTIPROCESS_TEST_EXPECT(tm01_pass, "")
120 {
121     pid_t pid = fork();
122     if(pid){
123         sleep(2);
124         RUNNER_ASSERT_MSG(1, "This test should pass");
125     } else {
126         RUNNER_ASSERT_MSG(1, "This test should pass");
127     }
128     RUNNER_ASSERT_MSG(1, "This test should pass");
129 }
130
131 RUNNER_MULTIPROCESS_TEST_EXPECT(tm02_pass, "")
132 {
133     pid_t pid = fork();
134     if(pid){
135         RUNNER_ASSERT_MSG(1, "This test should pass");
136     } else {
137         sleep(2);
138         RUNNER_ASSERT_MSG(1, "This test should pass");
139     }
140     RUNNER_ASSERT_MSG(1, "This test should pass");
141 }
142
143 RUNNER_MULTIPROCESS_TEST_EXPECT(tm03_pass, "")
144 {
145     pid_t pid = fork();
146     if(pid){
147         pid = fork();
148         if(pid){
149             sleep(1);
150         } else {
151             sleep(2);
152         }
153     } else {
154         if(pid){
155             sleep(2);
156         } else {
157             sleep(1);
158         }
159     }
160 }
161
162 RUNNER_MULTIPROCESS_TEST_EXPECT(tm04_fail, "TEST_FAILED|"
163                                            "This test should fail")
164 {
165     RUNNER_ASSERT_MSG(0, "This test should fail");
166 }
167
168 RUNNER_MULTIPROCESS_TEST_EXPECT(tm05_fail,"TEST_FAILED|"
169                                           "Test failed 1|"
170                                           "Test failed 2|"
171                                           "Test failed 3|"
172                                           "Test failed 4")
173 {
174     pid_t pid = fork();
175     if(pid){
176         pid = fork();
177         if(pid){
178             RUNNER_ASSERT_MSG(0, "Test failed 1");
179         } else {
180             RUNNER_ASSERT_MSG(0, "Test failed 2");
181         }
182     } else {
183         pid = fork();
184         if(pid){
185             RUNNER_ASSERT_MSG(0, "Test failed 3");
186         } else {
187             RUNNER_ASSERT_MSG(0, "Test failed 4");
188         }
189     }
190 }
191
192 RUNNER_MULTIPROCESS_TEST_EXPECT(tm06_fail, "TEST_FAILED|"
193                                           "Test failed 1|"
194                                           "Test failed 2|"
195                                           "Test failed 3|"
196                                           "Test failed 4")
197 {
198     pid_t pid = fork();
199     if(pid){
200         pid = fork();
201         if(pid){
202             sleep(2);
203             RUNNER_ASSERT_MSG(0, "Test failed 1");
204         } else {
205             RUNNER_ASSERT_MSG(0, "Test failed 2");
206         }
207     } else {
208         pid = fork();
209         if(pid){
210             RUNNER_ASSERT_MSG(0, "Test failed 3");
211         } else {
212             RUNNER_ASSERT_MSG(0, "Test failed 4");
213         }
214     }
215 }
216
217 RUNNER_MULTIPROCESS_TEST_EXPECT(tm07_fail, "TEST_FAILED|"
218                                           "Test failed 1|"
219                                           "Test failed 2|"
220                                           "Test failed 3|"
221                                           "Test failed 4")
222 {
223     pid_t pid = fork();
224     if(pid){
225         pid = fork();
226         if(pid){
227             RUNNER_ASSERT_MSG(0, "Test failed 1");
228         } else {
229             RUNNER_ASSERT_MSG(0, "Test failed 2");
230         }
231     } else {
232         pid = fork();
233         if(pid){
234             sleep(2);
235             RUNNER_ASSERT_MSG(0, "Test failed 3");
236         } else {
237             RUNNER_ASSERT_MSG(0, "Test failed 4");
238         }
239     }
240 }
241
242 RUNNER_MULTIPROCESS_TEST_EXPECT(tm08_fail_unknown_exception, "TEST_FAILED|"
243                                                              "unknown exception")
244 {
245     throw("hello");
246 }
247
248 RUNNER_MULTIPROCESS_TEST_EXPECT(tm09_fail_unknown_exception, "TEST_FAILED|"
249                                                             "unknown exception")
250 {
251     throw(1);
252 }
253
254 RUNNER_MULTIPROCESS_TEST_EXPECT(tm10_ignore, "TEST_IGNORED|"
255                                              "Test ignored")
256 {
257     RUNNER_IGNORED_MSG("Test ignored");
258 }
259
260 RUNNER_MULTIPROCESS_TEST_EXPECT(tm11_ignore, "TEST_IGNORED|"
261                                             "Test ignored 1|"
262                                             "Test ignored 2|"
263                                             "Test ignored 3|"
264                                             "Test ignored 4")
265 {
266     pid_t pid = fork();
267     if(pid){
268         pid = fork();
269         if(pid){
270             RUNNER_IGNORED_MSG("Test ignored 1");
271         } else {
272             RUNNER_IGNORED_MSG("Test ignored 2");
273         }
274     } else {
275         pid = fork();
276         if(pid){
277             sleep(2);
278             RUNNER_IGNORED_MSG("Test ignored 3");
279         } else {
280             RUNNER_IGNORED_MSG("Test ignored 4");
281         }
282     }
283 }
284
285 RUNNER_MULTIPROCESS_TEST_EXPECT(tm12_fail, "TEST_FAILED|"
286                                           "Test failed 1|"
287                                           "Test ignored 2|"
288                                           "Test ignored 3|"
289                                           "Test ignored 4")
290 {
291     pid_t pid = fork();
292     if(pid){
293         pid = fork();
294         if(pid){
295             RUNNER_ASSERT_MSG(0, "Test failed 1");
296         } else {
297             RUNNER_IGNORED_MSG("Test ignored 2");
298         }
299     } else {
300         pid = fork();
301         if(pid){
302             sleep(2);
303             RUNNER_IGNORED_MSG("Test ignored 3");
304         } else {
305             RUNNER_IGNORED_MSG("Test ignored 4");
306         }
307     }
308 }