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