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