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