[Release] wrt-commons_0.2.145
[framework/web/wrt-commons.git] / tests / test / runner_child.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    widget_version.cpp
18  * @author  Bartlomiej Grzelewski (b.grzelewski@samsung.com)
19  * @version 1.0
20  * @brief   Implementation file for test cases for engine internal tests
21  */
22 #include <dpl/test/test_runner_child.h>
23 #include <dpl/log/log.h>
24 #include <unistd.h>
25 #include <vector>
26 #include <sys/types.h>
27 #include <signal.h>
28
29
30 namespace {
31 enum class TestResult
32 {
33     PASS,
34     FAIL,
35     IGNORED,
36     TIMEOUT,
37     UNKNOWN
38 };
39 }
40
41 #define RUNNER_CHILD_TEST_EXPECT(name, result, message)                        \
42     static void testExpectFunction##name();                                    \
43     RUNNER_TEST(name)                                                          \
44     {                                                                          \
45         TestResult eResult = result;                                           \
46         TestResult rResult = TestResult::UNKNOWN;                              \
47         std::string eMessage = message;                                        \
48         Try                                                                    \
49         {                                                                      \
50             DPL::Test::RunChildProc(&testExpectFunction##name);                \
51         }                                                                      \
52         Catch(DPL::Test::TestRunner::TestFailed)                               \
53         {                                                                      \
54             std::string rMessage = _rethrown_exception.GetMessage();           \
55             size_t pos = rMessage.find(")");                                   \
56             if(pos != std::string::npos && pos+2 <= rMessage.length())         \
57             {                                                                  \
58                 rMessage = rMessage.substr(pos+2);                             \
59             }                                                                  \
60             if(rMessage == "Timeout")                                          \
61             {                                                                  \
62                 rResult = TestResult::TIMEOUT;                                 \
63             }                                                                  \
64             else if(rMessage == "Ignored")                                     \
65             {                                                                  \
66                 rResult = TestResult::IGNORED;                                 \
67             }                                                                  \
68             else if(rMessage == eMessage)                                      \
69             {                                                                  \
70                 rResult = TestResult::FAIL;                                    \
71             }                                                                  \
72             else                                                               \
73             {                                                                  \
74                 RUNNER_ASSERT_MSG(false, "Fail message do not matches");       \
75             }                                                                  \
76         }                                                                      \
77         if(rResult == TestResult::UNKNOWN)                                     \
78         {                                                                      \
79             rResult = TestResult::PASS;                                        \
80         }                                                                      \
81         RUNNER_ASSERT_MSG(eResult == rResult, "Expected other result");        \
82     }                                                                          \
83     void testExpectFunction##name()                                            \
84
85
86 RUNNER_TEST_GROUP_INIT(DPL_TESTS_TEST_CHILD)
87
88 RUNNER_CHILD_TEST_EXPECT(t00_pass, TestResult::PASS, "")
89 {
90     RUNNER_ASSERT_MSG(1, "This test should pass");
91 }
92
93 RUNNER_CHILD_TEST_EXPECT(t01_pass, TestResult::PASS, "")
94 {
95     RUNNER_ASSERT_MSG(1, "This test should pass");
96 }
97
98 RUNNER_CHILD_TEST_EXPECT(t02_fail, TestResult::FAIL, "This test should fail")
99 {
100     RUNNER_ASSERT_MSG(0, "This test should fail");
101 }
102
103 RUNNER_CHILD_TEST_EXPECT(t03_fail_timeout, TestResult::TIMEOUT, "")
104 {
105     sleep(20);
106     RUNNER_ASSERT_MSG(1, "This test should fail");
107 }
108
109 RUNNER_CHILD_TEST_EXPECT(t04_fail, TestResult::FAIL, "This test should fail")
110 {
111     RUNNER_ASSERT_MSG(1, "This test should fail");
112     RUNNER_ASSERT_MSG(1, "This test should fail");
113     RUNNER_ASSERT_MSG(1, "This test should fail");
114     RUNNER_ASSERT_MSG(1, "This test should fail");
115     RUNNER_ASSERT_MSG(0, "This test should fail");
116 }
117
118 RUNNER_CHILD_TEST_EXPECT(t05_fail_child_died, TestResult::FAIL, "Reading pipe error")
119 {
120     kill(getpid(), SIGKILL);
121     RUNNER_ASSERT_MSG(1, "This test should fail");
122 }
123
124 RUNNER_CHILD_TEST_EXPECT(t06_pass_8_second_test, TestResult::PASS, "")
125 {
126     sleep(8);
127     RUNNER_ASSERT_MSG(1, "This test should pass");
128 }
129
130 RUNNER_CHILD_TEST_EXPECT(t07_fail_unknown_exception, TestResult::FAIL, "unhandled exeception")
131 {
132     throw("hello");
133 }
134
135 RUNNER_CHILD_TEST_EXPECT(t08_fail_unknown_exception, TestResult::FAIL, "unhandled exeception")
136 {
137     throw(1);
138 }
139
140 RUNNER_CHILD_TEST_EXPECT(t09_fail_you_should_see_text_normal_assert, TestResult::FAIL, "Normal assert")
141 {
142     RUNNER_ASSERT_MSG(0, "Normal assert");
143 }
144
145 RUNNER_CHILD_TEST_EXPECT(t10_pass, TestResult::PASS, "")
146 {
147     RUNNER_ASSERT_MSG(1, "Normal assert");
148 }
149
150 RUNNER_CHILD_TEST_EXPECT(t11_ignore, TestResult::IGNORED, "Test ignored")
151 {
152     RUNNER_IGNORED_MSG("Test ignored");
153 }
154
155