1/*
2 * Copyright (C) 2016 The Android Open Source Project
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#include "gtest/gtest.h"
18#include "gmock/gmock.h"
19
20#include "Caches.h"
21#include "debug/GlesDriver.h"
22#include "debug/NullGlesDriver.h"
23#include "hwui/Typeface.h"
24#include "thread/TaskManager.h"
25#include "tests/common/LeakChecker.h"
26
27#include <signal.h>
28
29using namespace std;
30using namespace android;
31using namespace android::uirenderer;
32
33static auto CRASH_SIGNALS = {
34        SIGABRT,
35        SIGSEGV,
36        SIGBUS,
37};
38
39static map<int, struct sigaction> gSigChain;
40
41static void gtestSigHandler(int sig, siginfo_t* siginfo, void* context) {
42    auto testinfo = ::testing::UnitTest::GetInstance()->current_test_info();
43    printf("[  FAILED  ] %s.%s\n", testinfo->test_case_name(),
44            testinfo->name());
45    printf("[  FATAL!  ] Process crashed, aborting tests!\n");
46    fflush(stdout);
47
48    // restore the default sighandler and re-raise
49    struct sigaction sa = gSigChain[sig];
50    sigaction(sig, &sa, nullptr);
51    raise(sig);
52}
53
54class TypefaceEnvironment : public testing::Environment {
55public:
56    virtual void SetUp() {
57        Typeface::setRobotoTypefaceForTest();
58    }
59};
60
61int main(int argc, char* argv[]) {
62    // Register a crash handler
63    struct sigaction sa;
64    memset(&sa, 0, sizeof(sa));
65    sa.sa_sigaction = &gtestSigHandler;
66    sa.sa_flags = SA_SIGINFO;
67    for (auto sig : CRASH_SIGNALS) {
68        struct sigaction old_sa;
69        sigaction(sig, &sa, &old_sa);
70        gSigChain.insert(pair<int, struct sigaction>(sig, old_sa));
71    }
72
73    // Replace the default GLES driver
74    debug::GlesDriver::replace(std::make_unique<debug::NullGlesDriver>());
75
76    // Run the tests
77    testing::InitGoogleTest(&argc, argv);
78    testing::InitGoogleMock(&argc, argv);
79
80    testing::AddGlobalTestEnvironment(new TypefaceEnvironment());
81
82    int ret = RUN_ALL_TESTS();
83    test::LeakChecker::checkForLeaks();
84    return ret;
85}
86
87