1 // Copyright 2020 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "android/opengl/EmuglBackendList.h"
16 
17 #include "android/base/files/PathUtils.h"
18 #include "android/base/testing/TestSystem.h"
19 #include "android/base/testing/TestTempDir.h"
20 
21 #include <gtest/gtest.h>
22 
23 namespace android {
24 namespace opengl {
25 
26 #define ARRAYLEN(x)  (sizeof(x)/sizeof(x[0]))
27 
28 using android::base::pj;
29 using android::base::System;
30 using android::base::TestTempDir;
31 using android::base::TestSystem;
32 
makeLibSubPath(const char * name)33 static std::string makeLibSubPath(const char* name) {
34     return pj("foo", System::kLibSubDir, name);
35 }
36 
makeLibSubDir(TestTempDir * dir,const char * name)37 static void makeLibSubDir(TestTempDir* dir, const char* name) {
38     dir->makeSubDir(makeLibSubPath(name).c_str());
39 }
40 
makeLibSubFile(TestTempDir * dir,const char * name)41 static void makeLibSubFile(TestTempDir* dir, const char* name) {
42     dir->makeSubFile(makeLibSubPath(name).c_str());
43 }
44 
TEST(EmuglBackendList,init)45 TEST(EmuglBackendList, init) {
46     TestSystem testSys("foo", System::kProgramBitness, "/");
47     TestTempDir* myDir = testSys.getTempRoot();
48 
49     myDir->makeSubDir("foo");
50 
51     makeLibSubDir(myDir, "");
52     makeLibSubDir(myDir, "gles_first");
53     makeLibSubFile(myDir, "gles_second");  // should be ignored (file).
54     makeLibSubDir(myDir, "gles_");  // should be ignored (no name).
55     makeLibSubDir(myDir, "gles_fourth");
56     makeLibSubDir(myDir, "gles_fifth");
57 
58     EmuglBackendList list("foo", System::kProgramBitness);
59 
60     // NOTE: Must appear in alphabetical order
61     const char* const kExpected[] = {
62         "fifth", "first", "fourth",
63     };
64     const size_t kExpectedLen = ARRAYLEN(kExpected);
65 
66     const std::vector<std::string>& names = list.names();
67     EXPECT_EQ(kExpectedLen, names.size());
68     for (size_t n = 0; n < kExpectedLen; ++n) {
69         EXPECT_STREQ(kExpected[n], names[n].c_str()) << "#" << n;
70         EXPECT_TRUE(list.contains(kExpected[n]));
71     }
72 }
73 
TEST(EmuglBackendList,getBackendLibPath)74 TEST(EmuglBackendList, getBackendLibPath) {
75     TestSystem testSys("foo", System::kProgramBitness, "/");
76     TestTempDir* myDir = testSys.getTempRoot();
77 
78     myDir->makeSubDir("foo");
79 
80     makeLibSubDir(myDir, "");
81     makeLibSubDir(myDir, "gles_bar");
82 
83     static const struct {
84         EmuglBackendList::Library library;
85         const char* libName;
86     } kData[] = {
87 #ifdef _WIN32
88         { EmuglBackendList::LIBRARY_EGL, "libEGL.dll" },
89         { EmuglBackendList::LIBRARY_GLESv1, "libGLES_CM.dll" },
90         { EmuglBackendList::LIBRARY_GLESv2, "libGLESv2.dll" },
91 #elif defined(__APPLE__)
92         { EmuglBackendList::LIBRARY_EGL, "libEGL.dylib" },
93         { EmuglBackendList::LIBRARY_GLESv1, "libGLES_CM.dylib" },
94         { EmuglBackendList::LIBRARY_GLESv2, "libGLESv2.dylib" },
95 #else
96         { EmuglBackendList::LIBRARY_EGL, "libEGL.so" },
97         { EmuglBackendList::LIBRARY_GLESv1, "libGLES_CM.so" },
98         { EmuglBackendList::LIBRARY_GLESv2, "libGLESv2.so" },
99 #endif
100     };
101     const size_t kDataLen = ARRAYLEN(kData);
102 
103     for (size_t n = 0; n < kDataLen; ++n) {
104         std::string file =
105             pj("gles_bar", kData[n].libName);
106         makeLibSubFile(myDir, file.c_str());
107     }
108 
109     auto sysdir = pj("/", "foo");
110     EmuglBackendList list(sysdir.c_str(), System::kProgramBitness);
111     const std::vector<std::string>& names = list.names();
112 
113     EXPECT_EQ(1U, names.size());
114     EXPECT_STREQ("bar", names[0].c_str());
115 
116     for (size_t n = 0; n < kDataLen; ++n) {
117         std::string expected =
118             pj("/", "foo", System::kLibSubDir, "gles_bar", kData[n].libName);
119         std::string libdir;
120         EXPECT_TRUE(list.getBackendLibPath("bar", kData[n].library, &libdir));
121         EXPECT_TRUE(list.contains("bar"));
122         EXPECT_FALSE(list.contains("foo"));
123         EXPECT_STREQ(expected.c_str(), libdir.c_str());
124     }
125 }
126 
TEST(EmuglBackend,defaultName)127 TEST(EmuglBackend, defaultName) {
128     EmuglBackendList list("foo", 0);
129     EXPECT_STREQ("auto", list.defaultName().c_str());
130 }
131 
132 }  // namespace opengl
133 }  // namespace android
134 
135 
136