John Reck | dc87c52 | 2016-02-29 13:31:18 -0800 | [diff] [blame] | 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 | |
John Reck | 1efef7b | 2023-07-17 23:29:30 -0400 | [diff] [blame] | 17 | #include <getopt.h> |
Nolan Scobie | ba46cae3 | 2024-07-25 15:56:46 -0400 | [diff] [blame] | 18 | #include <log/log.h> |
John Reck | 1efef7b | 2023-07-17 23:29:30 -0400 | [diff] [blame] | 19 | #include <signal.h> |
John Reck | dc87c52 | 2016-02-29 13:31:18 -0800 | [diff] [blame] | 20 | |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 21 | #include "Properties.h" |
John Reck | 1efef7b | 2023-07-17 23:29:30 -0400 | [diff] [blame] | 22 | #include "gmock/gmock.h" |
| 23 | #include "gtest/gtest.h" |
Seigo Nonaka | b6e2013 | 2016-11-14 14:07:41 +0900 | [diff] [blame] | 24 | #include "hwui/Typeface.h" |
sergeyv | 7dc370b | 2016-06-17 11:21:11 -0700 | [diff] [blame] | 25 | #include "tests/common/LeakChecker.h" |
John Reck | dc87c52 | 2016-02-29 13:31:18 -0800 | [diff] [blame] | 26 | |
John Reck | dc87c52 | 2016-02-29 13:31:18 -0800 | [diff] [blame] | 27 | using namespace std; |
| 28 | using namespace android; |
| 29 | using namespace android::uirenderer; |
| 30 | |
| 31 | static auto CRASH_SIGNALS = { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 32 | SIGABRT, SIGSEGV, SIGBUS, |
John Reck | dc87c52 | 2016-02-29 13:31:18 -0800 | [diff] [blame] | 33 | }; |
| 34 | |
| 35 | static map<int, struct sigaction> gSigChain; |
| 36 | |
| 37 | static void gtestSigHandler(int sig, siginfo_t* siginfo, void* context) { |
| 38 | auto testinfo = ::testing::UnitTest::GetInstance()->current_test_info(); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 39 | printf("[ FAILED ] %s.%s\n", testinfo->test_case_name(), testinfo->name()); |
John Reck | dc87c52 | 2016-02-29 13:31:18 -0800 | [diff] [blame] | 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 | |
John Reck | 1efef7b | 2023-07-17 23:29:30 -0400 | [diff] [blame] | 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 | |
| 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 | |
Nolan Scobie | ba46cae3 | 2024-07-25 15:56:46 -0400 | [diff] [blame] | 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 | |
John Reck | 1efef7b | 2023-07-17 23:29:30 -0400 | [diff] [blame] | 82 | struct Options { |
| 83 | RenderPipelineType renderer = RenderPipelineType::SkiaGL; |
| 84 | }; |
| 85 | |
| 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 | |
Seigo Nonaka | b6e2013 | 2016-11-14 14:07:41 +0900 | [diff] [blame] | 113 | class TypefaceEnvironment : public testing::Environment { |
| 114 | public: |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 115 | virtual void SetUp() { Typeface::setRobotoTypefaceForTest(); } |
Seigo Nonaka | b6e2013 | 2016-11-14 14:07:41 +0900 | [diff] [blame] | 116 | }; |
| 117 | |
John Reck | dc87c52 | 2016-02-29 13:31:18 -0800 | [diff] [blame] | 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 = >estSigHandler; |
| 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 Reck | faa1b0a | 2021-05-13 10:28:38 -0400 | [diff] [blame] | 130 | // Avoid talking to SF |
John Reck | 5642847 | 2018-03-16 17:27:17 -0700 | [diff] [blame] | 131 | Properties::isolatedProcess = true; |
John Reck | 1efef7b | 2023-07-17 23:29:30 -0400 | [diff] [blame] | 132 | |
| 133 | auto opts = parseOptions(argc, argv); |
| 134 | Properties::overrideRenderPipelineType(opts.renderer); |
Nolan Scobie | ba46cae3 | 2024-07-25 15:56:46 -0400 | [diff] [blame] | 135 | ALOGI("Starting HWUI unit tests with %s pipeline", renderPipelineTypeName(opts.renderer)); |
sergeyv | 8bd5edf | 2016-05-13 15:03:35 -0700 | [diff] [blame] | 136 | |
John Reck | dc87c52 | 2016-02-29 13:31:18 -0800 | [diff] [blame] | 137 | // Run the tests |
| 138 | testing::InitGoogleTest(&argc, argv); |
sergeyv | 8bd5edf | 2016-05-13 15:03:35 -0700 | [diff] [blame] | 139 | testing::InitGoogleMock(&argc, argv); |
| 140 | |
Seigo Nonaka | b6e2013 | 2016-11-14 14:07:41 +0900 | [diff] [blame] | 141 | testing::AddGlobalTestEnvironment(new TypefaceEnvironment()); |
| 142 | |
John Reck | dc87c52 | 2016-02-29 13:31:18 -0800 | [diff] [blame] | 143 | int ret = RUN_ALL_TESTS(); |
sergeyv | 7dc370b | 2016-06-17 11:21:11 -0700 | [diff] [blame] | 144 | test::LeakChecker::checkForLeaks(); |
John Reck | dc87c52 | 2016-02-29 13:31:18 -0800 | [diff] [blame] | 145 | return ret; |
| 146 | } |