summaryrefslogtreecommitdiffstats
path: root/tests/camera2/TestForkerEventListener.cpp
blob: 9416db2656b765f99f15a530ceb036fc5d456ed1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>

#include <gtest/gtest.h>

#include "TestForkerEventListener.h"
#include "TestExtensions.h"

#define DEBUG_TEST_FORKER_EVENT_LISTENER 0

#define RETURN_CODE_PASSED 0
#define RETURN_CODE_FAILED 1

namespace android {
namespace camera2 {
namespace tests {

bool TestForkerEventListener::mIsForked         = false;

TestForkerEventListener::TestForkerEventListener() {
    mIsForked = false;
    mHasSucceeded = true;
    mTermSignal = 0;
}

// Called before a test starts.
void TestForkerEventListener::OnTestStart(const ::testing::TestInfo&) {

    if (!TEST_EXTENSION_FORKING_ENABLED) {
        return;
    }

    pid_t childPid = fork();
    if (childPid != 0) {
        int status;
        waitpid(childPid, &status, /*options*/0);

        // terminated normally?
        mHasSucceeded = WIFEXITED(status);
        // terminate with return code 0 = test passed, 1 = test failed
        if (mHasSucceeded) {
          mHasSucceeded = WEXITSTATUS(status) == RETURN_CODE_PASSED;
        } else if (WIFSIGNALED(status)) {
          mTermSignal = WTERMSIG(status);
        }

        /* the test is then skipped by inserting the various
        TEST_EXTENSION_ macros in TestExtensions.h */

    } else {
        mIsForked = true;
    }
}

// Called after a failed assertion or a SUCCEED() invocation.
void TestForkerEventListener::OnTestPartResult(
    const ::testing::TestPartResult& test_part_result) {

    if (DEBUG_TEST_FORKER_EVENT_LISTENER) {
        printf("%s in %s:%d\n%s\n",
             test_part_result.failed() ? "*** Failure" : "Success",
             test_part_result.file_name(),
             test_part_result.line_number(),
             test_part_result.summary());
    }
}

// Called after a test ends.
void TestForkerEventListener::OnTestEnd(const ::testing::TestInfo& test_info) {

    if (!TEST_EXTENSION_FORKING_ENABLED) {
        return;
    }

    if (mIsForked) {
        exit(test_info.result()->Passed()
            ? RETURN_CODE_PASSED : RETURN_CODE_FAILED);
    } else if (!mHasSucceeded && mTermSignal != 0) {

      printf("*** Test %s.%s crashed with signal = %s\n",
             test_info.test_case_name(), test_info.name(),
             strsignal(mTermSignal));
    }

    //TODO: overload the default event listener to suppress this message
    // dynamically (e.g. by skipping OnTestPartResult after OnTestEnd )

    // trigger a test failure if the child has failed
    if (!mHasSucceeded) {
        ADD_FAILURE();
    }
    mTermSignal = 0;
}


}
}
}