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 <getopt.h>
18 #include <signal.h>
19
20 #include "Properties.h"
21 #include "gmock/gmock.h"
22 #include "gtest/gtest.h"
23 #include "hwui/Typeface.h"
24 #include "tests/common/LeakChecker.h"
25
26 using namespace std;
27 using namespace android;
28 using namespace android::uirenderer;
29
30 static auto CRASH_SIGNALS = {
31 SIGABRT, SIGSEGV, SIGBUS,
32 };
33
34 static map<int, struct sigaction> gSigChain;
35
gtestSigHandler(int sig,siginfo_t * siginfo,void * context)36 static void gtestSigHandler(int sig, siginfo_t* siginfo, void* context) {
37 auto testinfo = ::testing::UnitTest::GetInstance()->current_test_info();
38 printf("[ FAILED ] %s.%s\n", testinfo->test_case_name(), testinfo->name());
39 printf("[ FATAL! ] Process crashed, aborting tests!\n");
40 fflush(stdout);
41
42 // restore the default sighandler and re-raise
43 struct sigaction sa = gSigChain[sig];
44 sigaction(sig, &sa, nullptr);
45 raise(sig);
46 }
47
48 // For options that only exist in long-form. Anything in the
49 // 0-255 range is reserved for short options (which just use their ASCII value)
50 namespace LongOpts {
51 enum {
52 Reserved = 255,
53 Renderer,
54 };
55 }
56
57 static const struct option LONG_OPTIONS[] = {
58 {"renderer", required_argument, nullptr, LongOpts::Renderer}, {0, 0, 0, 0}};
59
parseRenderer(const char * renderer)60 static RenderPipelineType parseRenderer(const char* renderer) {
61 // Anything that's not skiavk is skiagl
62 if (!strcmp(renderer, "skiavk")) {
63 return RenderPipelineType::SkiaVulkan;
64 }
65 return RenderPipelineType::SkiaGL;
66 }
67
68 struct Options {
69 RenderPipelineType renderer = RenderPipelineType::SkiaGL;
70 };
71
parseOptions(int argc,char * argv[])72 Options parseOptions(int argc, char* argv[]) {
73 int c;
74 opterr = 0;
75 Options opts;
76
77 while (true) {
78 /* getopt_long stores the option index here. */
79 int option_index = 0;
80
81 c = getopt_long(argc, argv, "", LONG_OPTIONS, &option_index);
82
83 if (c == -1) break;
84
85 switch (c) {
86 case 0:
87 // Option set a flag, don't need to do anything
88 // (although none of the current LONG_OPTIONS do this...)
89 break;
90
91 case LongOpts::Renderer:
92 opts.renderer = parseRenderer(optarg);
93 break;
94 }
95 }
96 return opts;
97 }
98
99 class TypefaceEnvironment : public testing::Environment {
100 public:
SetUp()101 virtual void SetUp() { Typeface::setRobotoTypefaceForTest(); }
102 };
103
main(int argc,char * argv[])104 int main(int argc, char* argv[]) {
105 // Register a crash handler
106 struct sigaction sa;
107 memset(&sa, 0, sizeof(sa));
108 sa.sa_sigaction = >estSigHandler;
109 sa.sa_flags = SA_SIGINFO;
110 for (auto sig : CRASH_SIGNALS) {
111 struct sigaction old_sa;
112 sigaction(sig, &sa, &old_sa);
113 gSigChain.insert(pair<int, struct sigaction>(sig, old_sa));
114 }
115
116 // Avoid talking to SF
117 Properties::isolatedProcess = true;
118
119 auto opts = parseOptions(argc, argv);
120 Properties::overrideRenderPipelineType(opts.renderer);
121
122 // Run the tests
123 testing::InitGoogleTest(&argc, argv);
124 testing::InitGoogleMock(&argc, argv);
125
126 testing::AddGlobalTestEnvironment(new TypefaceEnvironment());
127
128 int ret = RUN_ALL_TESTS();
129 test::LeakChecker::checkForLeaks();
130 return ret;
131 }
132