1 /*
2 * Copyright (C) 2018 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 <dirent.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20
21 #include <memory>
22 #include <string>
23 #include <vector>
24
25 #include <android-base/file.h>
26 #include <android-base/strings.h>
27 #include <gtest/gtest.h>
28 #include <png.h>
29
30 #include "common/test_constants.h"
31 #include "minui/minui.h"
32 #include "private/resources.h"
33
34 static const std::string kLocale = "zu";
35
36 static const std::vector<std::string> kResourceImagesDirs{
37 "res-mdpi/images/", "res-hdpi/images/", "res-xhdpi/images/",
38 "res-xxhdpi/images/", "res-xxxhdpi/images/",
39 };
40
png_filter(const dirent * de)41 static int png_filter(const dirent* de) {
42 if (de->d_type != DT_REG || !android::base::EndsWith(de->d_name, "_text.png")) {
43 return 0;
44 }
45 return 1;
46 }
47
48 // Finds out all the PNG files to test, which stay under the same dir with the executabl..
add_files()49 static std::vector<std::string> add_files() {
50 std::vector<std::string> files;
51 for (const std::string& images_dir : kResourceImagesDirs) {
52 static std::string exec_dir = android::base::GetExecutableDirectory();
53 std::string dir_path = exec_dir + "/" + images_dir;
54 dirent** namelist;
55 int n = scandir(dir_path.c_str(), &namelist, png_filter, alphasort);
56 if (n == -1) {
57 printf("Failed to scandir %s: %s\n", dir_path.c_str(), strerror(errno));
58 continue;
59 }
60 if (n == 0) {
61 printf("No file is added for test in %s\n", dir_path.c_str());
62 }
63
64 while (n--) {
65 std::string file_path = dir_path + namelist[n]->d_name;
66 files.push_back(file_path);
67 free(namelist[n]);
68 }
69 free(namelist);
70 }
71 return files;
72 }
73
TEST(ResourcesTest,res_create_multi_display_surface)74 TEST(ResourcesTest, res_create_multi_display_surface) {
75 GRSurface** frames;
76 int frame_count;
77 int fps;
78 ASSERT_EQ(0, res_create_multi_display_surface(from_testdata_base("battery_scale.png").c_str(),
79 &frame_count, &fps, &frames));
80 ASSERT_EQ(6, frame_count);
81 ASSERT_EQ(20, fps);
82
83 for (auto i = 0; i < frame_count; i++) {
84 free(frames[i]);
85 }
86 free(frames);
87 }
88
89 class ResourcesTest : public testing::TestWithParam<std::string> {
90 public:
91 static std::vector<std::string> png_list;
92
93 protected:
SetUp()94 void SetUp() override {
95 png_ = std::make_unique<PngHandler>(GetParam());
96 ASSERT_TRUE(png_);
97
98 ASSERT_EQ(PNG_COLOR_TYPE_GRAY, png_->color_type()) << "Recovery expects grayscale PNG file.";
99 ASSERT_LT(static_cast<png_uint_32>(5), png_->width());
100 ASSERT_LT(static_cast<png_uint_32>(0), png_->height());
101 ASSERT_EQ(1, png_->channels()) << "Recovery background text images expects 1-channel PNG file.";
102 }
103
104 std::unique_ptr<PngHandler> png_{ nullptr };
105 };
106
107 // Parses a png file and tests if it's qualified for the background text image under recovery.
TEST_P(ResourcesTest,ValidateLocale)108 TEST_P(ResourcesTest, ValidateLocale) {
109 std::vector<unsigned char> row(png_->width());
110 for (png_uint_32 y = 0; y < png_->height(); ++y) {
111 png_read_row(png_->png_ptr(), row.data(), nullptr);
112 int w = (row[1] << 8) | row[0];
113 int h = (row[3] << 8) | row[2];
114 int len = row[4];
115 EXPECT_LT(0, w);
116 EXPECT_LT(0, h);
117 EXPECT_LT(0, len) << "Locale string should be non-empty.";
118 EXPECT_NE(0, row[5]) << "Locale string is missing.";
119
120 ASSERT_GE(png_->height(), y + 1 + h) << "Locale: " << kLocale << " is not found in the file.";
121 char* loc = reinterpret_cast<char*>(&row[5]);
122 if (matches_locale(loc, kLocale.c_str())) {
123 EXPECT_TRUE(android::base::StartsWith(loc, kLocale));
124 break;
125 }
126 for (int i = 0; i < h; ++i, ++y) {
127 png_read_row(png_->png_ptr(), row.data(), nullptr);
128 }
129 }
130 }
131
132 std::vector<std::string> ResourcesTest::png_list = add_files();
133
134 INSTANTIATE_TEST_CASE_P(BackgroundTextValidation, ResourcesTest,
135 ::testing::ValuesIn(ResourcesTest::png_list.cbegin(),
136 ResourcesTest::png_list.cend()));
137