blob: 3fd15c4c9c51be670aa37c2208dfcb7ada844236 [file] [log] [blame]
John Reckdc87c522016-02-29 13:31:18 -08001/*
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
John Reck1efef7b2023-07-17 23:29:30 -040017#include <getopt.h>
Nolan Scobieba46cae32024-07-25 15:56:46 -040018#include <log/log.h>
John Reck1efef7b2023-07-17 23:29:30 -040019#include <signal.h>
John Reckdc87c522016-02-29 13:31:18 -080020
John Reck283bb462018-12-13 16:40:14 -080021#include "Properties.h"
John Reck1efef7b2023-07-17 23:29:30 -040022#include "gmock/gmock.h"
23#include "gtest/gtest.h"
Seigo Nonakab6e20132016-11-14 14:07:41 +090024#include "hwui/Typeface.h"
sergeyv7dc370b2016-06-17 11:21:11 -070025#include "tests/common/LeakChecker.h"
John Reckdc87c522016-02-29 13:31:18 -080026
John Reckdc87c522016-02-29 13:31:18 -080027using namespace std;
28using namespace android;
29using namespace android::uirenderer;
30
31static auto CRASH_SIGNALS = {
John Reck1bcacfd2017-11-03 10:12:19 -070032 SIGABRT, SIGSEGV, SIGBUS,
John Reckdc87c522016-02-29 13:31:18 -080033};
34
35static map<int, struct sigaction> gSigChain;
36
37static void gtestSigHandler(int sig, siginfo_t* siginfo, void* context) {
38 auto testinfo = ::testing::UnitTest::GetInstance()->current_test_info();
John Reck1bcacfd2017-11-03 10:12:19 -070039 printf("[ FAILED ] %s.%s\n", testinfo->test_case_name(), testinfo->name());
John Reckdc87c522016-02-29 13:31:18 -080040 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
John Reck1efef7b2023-07-17 23:29:30 -040049// 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)
51namespace LongOpts {
52enum {
53 Reserved = 255,
54 Renderer,
55};
56}
57
58static const struct option LONG_OPTIONS[] = {
59 {"renderer", required_argument, nullptr, LongOpts::Renderer}, {0, 0, 0, 0}};
60
61static 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
Nolan Scobieba46cae32024-07-25 15:56:46 -040069static 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
John Reck1efef7b2023-07-17 23:29:30 -040082struct Options {
83 RenderPipelineType renderer = RenderPipelineType::SkiaGL;
84};
85
86Options 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
Seigo Nonakab6e20132016-11-14 14:07:41 +0900113class TypefaceEnvironment : public testing::Environment {
114public:
John Reck1bcacfd2017-11-03 10:12:19 -0700115 virtual void SetUp() { Typeface::setRobotoTypefaceForTest(); }
Seigo Nonakab6e20132016-11-14 14:07:41 +0900116};
117
John Reckdc87c522016-02-29 13:31:18 -0800118int 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
John Reckfaa1b0a2021-05-13 10:28:38 -0400130 // Avoid talking to SF
John Reck56428472018-03-16 17:27:17 -0700131 Properties::isolatedProcess = true;
John Reck1efef7b2023-07-17 23:29:30 -0400132
133 auto opts = parseOptions(argc, argv);
134 Properties::overrideRenderPipelineType(opts.renderer);
Nolan Scobieba46cae32024-07-25 15:56:46 -0400135 ALOGI("Starting HWUI unit tests with %s pipeline", renderPipelineTypeName(opts.renderer));
sergeyv8bd5edf2016-05-13 15:03:35 -0700136
John Reckdc87c522016-02-29 13:31:18 -0800137 // Run the tests
138 testing::InitGoogleTest(&argc, argv);
sergeyv8bd5edf2016-05-13 15:03:35 -0700139 testing::InitGoogleMock(&argc, argv);
140
Seigo Nonakab6e20132016-11-14 14:07:41 +0900141 testing::AddGlobalTestEnvironment(new TypefaceEnvironment());
142
John Reckdc87c522016-02-29 13:31:18 -0800143 int ret = RUN_ALL_TESTS();
sergeyv7dc370b2016-06-17 11:21:11 -0700144 test::LeakChecker::checkForLeaks();
John Reckdc87c522016-02-29 13:31:18 -0800145 return ret;
146}