blob: 3027443087ac077d4c6d3a1e0538250fb4494619 [file] [log] [blame]
Tao Bao2201d082018-11-26 15:46:05 -08001/*
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
Tao Bao4a01f362019-04-26 23:41:49 -070017#include <dirent.h>
18#include <stdio.h>
19#include <stdlib.h>
Tao Bao2201d082018-11-26 15:46:05 -080020
Tao Bao4a01f362019-04-26 23:41:49 -070021#include <memory>
22#include <string>
23#include <vector>
24
25#include <android-base/file.h>
26#include <android-base/strings.h>
Tao Bao2201d082018-11-26 15:46:05 -080027#include <gtest/gtest.h>
Tao Bao4a01f362019-04-26 23:41:49 -070028#include <png.h>
Tao Bao2201d082018-11-26 15:46:05 -080029
30#include "common/test_constants.h"
31#include "minui/minui.h"
Tao Bao4a01f362019-04-26 23:41:49 -070032#include "private/resources.h"
33
34static const std::string kLocale = "zu";
35
36static 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
41static 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..
49static 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}
Tao Bao2201d082018-11-26 15:46:05 -080073
74TEST(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}
Tao Bao4a01f362019-04-26 23:41:49 -070088
89class ResourcesTest : public testing::TestWithParam<std::string> {
90 public:
91 static std::vector<std::string> png_list;
92
93 protected:
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.
108TEST_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
132std::vector<std::string> ResourcesTest::png_list = add_files();
133
134INSTANTIATE_TEST_CASE_P(BackgroundTextValidation, ResourcesTest,
135 ::testing::ValuesIn(ResourcesTest::png_list.cbegin(),
136 ResourcesTest::png_list.cend()));