1 /*
2 * Copyright 2017 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include <sys/stat.h>
9
10 #include "gm_knowledge.h"
11 #include "gm_runner.h"
12
13 #ifdef __clang__
14 #pragma clang diagnostic push
15 #pragma clang diagnostic ignored "-Wused-but-marked-unused"
16 #endif
17
18 #include "gtest/gtest.h"
19
20 #ifdef __clang__
21 #pragma clang diagnostic pop
22 #endif
23
24 #include "Resources.h"
25 #include "SkStream.h"
26 #include "SkString.h"
27
28 ////////////////////////////////////////////////////////////////////////////////
29
30 static std::string gReportDirectoryPath;
31 static std::unique_ptr<skqp::AssetManager> gAssetMgr;
32
33 ////////////////////////////////////////////////////////////////////////////////
34
35 struct GMTestCase {
36 gm_runner::GMFactory fGMFactory;
37 gm_runner::SkiaBackend fBackend;
38 };
39
40 struct GMTest : public testing::Test {
41 GMTestCase fTest;
GMTestGMTest42 GMTest(GMTestCase t) : fTest(t) {}
TestBodyGMTest43 void TestBody() override {
44 float result;
45 gm_runner::Error error;
46 std::tie(result, error) =
47 gm_runner::EvaluateGM(fTest.fBackend, fTest.fGMFactory,
48 gAssetMgr.get(), gReportDirectoryPath.c_str());
49 EXPECT_EQ(error, gm_runner::Error::None);
50 if (gm_runner::Error::None == error) {
51 EXPECT_EQ(result, 0);
52 }
53 }
54 };
55
56 struct GMTestFactory : public testing::internal::TestFactoryBase {
57 GMTestCase fTest;
GMTestFactoryGMTestFactory58 GMTestFactory(GMTestCase t) : fTest(t) {}
CreateTestGMTestFactory59 testing::Test* CreateTest() override { return new GMTest(fTest); }
60 };
61
62 ////////////////////////////////////////////////////////////////////////////////
63
64 struct UnitTestTest : public testing::Test {
65 gm_runner::UnitTest fTest;
UnitTestTestUnitTestTest66 UnitTestTest(gm_runner::UnitTest test) : fTest(test) {}
TestBodyUnitTestTest67 void TestBody() override {
68 std::vector<std::string> errors = gm_runner::ExecuteTest(fTest);
69 for (const std::string& error : errors) {
70 GTEST_NONFATAL_FAILURE_(error.c_str());
71 }
72 }
73 };
74
75 struct UnitTestFactory : public testing::internal::TestFactoryBase {
76 gm_runner::UnitTest fTest;
UnitTestFactoryUnitTestFactory77 UnitTestFactory(gm_runner::UnitTest test) : fTest(test) {}
CreateTestUnitTestFactory78 testing::Test* CreateTest() override { return new UnitTestTest(fTest); }
79 };
80
81 ////////////////////////////////////////////////////////////////////////////////
82
reg_test(const char * test,const char * testCase,testing::internal::TestFactoryBase * fact)83 static void reg_test(const char* test, const char* testCase,
84 testing::internal::TestFactoryBase* fact) {
85 testing::internal::MakeAndRegisterTestInfo(test,
86 testCase,
87 nullptr,
88 nullptr,
89 testing::internal::CodeLocation(__FILE__, __LINE__),
90 testing::internal::GetTestTypeId(),
91 testing::Test::SetUpTestCase,
92 testing::Test::TearDownTestCase,
93 fact);
94 }
95
96
register_skia_tests()97 void register_skia_tests() {
98 gm_runner::InitSkia(gm_runner::Mode::kCompatibilityTestMode, gAssetMgr.get());
99
100 // Rendering Tests
101 std::vector<gm_runner::SkiaBackend> backends = gm_runner::GetSupportedBackends();
102 std::vector<gm_runner::GMFactory> gms = gm_runner::GetGMFactories(gAssetMgr.get());
103 for (auto backend : backends) {
104 const char* backendName = GetBackendName(backend);
105 std::string test = std::string("SkiaGM_") + backendName;
106 for (auto gmFactory : gms) {
107 std::string gmName = gm_runner::GetGMName(gmFactory);
108 reg_test(test.c_str(), gmName.c_str(),
109 new GMTestFactory(GMTestCase{gmFactory, backend}));
110 }
111 }
112
113 for (gm_runner::UnitTest test : gm_runner::GetUnitTests()) {
114 reg_test("Skia_Unit_Tests", gm_runner::GetUnitTestName(test), new UnitTestFactory{test});
115 }
116 }
117
118 namespace {
119 struct StdAssetManager : public skqp::AssetManager {
120 SkString fPrefix;
StdAssetManager__anon019e8b830111::StdAssetManager121 StdAssetManager(const char* p) : fPrefix(p) {}
open__anon019e8b830111::StdAssetManager122 std::unique_ptr<SkStreamAsset> open(const char* path) override {
123 SkString fullPath = fPrefix.isEmpty()
124 ? SkString(path)
125 : SkStringPrintf("%s/%s", fPrefix.c_str(), path);
126 return SkStream::MakeFromFile(fullPath.c_str());
127 }
128 };
129 }
130
main(int argc,char ** argv)131 int main(int argc, char** argv) {
132 testing::InitGoogleTest(&argc, argv);
133 if (argc < 2) {
134 std::cerr << "Usage:\n " << argv[0]
135 << " [GTEST_ARGUMENTS] GMKB_DIRECTORY_PATH GMKB_REPORT_PATH\n\n";
136 return 1;
137 }
138 SetResourcePath((std::string(argv[1]) + "/resources").c_str());
139 gAssetMgr.reset(new StdAssetManager(argv[1]));
140 if (argc > 2) {
141 gReportDirectoryPath = argv[2];
142 (void)mkdir(gReportDirectoryPath.c_str(), 0777);
143 }
144 register_skia_tests();
145 int ret = RUN_ALL_TESTS();
146 (void)gmkb::MakeReport(gReportDirectoryPath.c_str());
147 return ret;
148 }
149