blob: 54329db229e89240adb8d6cc07fa0e6ec70f2c79 [file] [log] [blame]
Tao Bao6b28f052018-04-13 13:41:58 -07001/*
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 "minui/minui.h"
31#include "private/resources.h"
32
33static const std::string kLocale = "zu";
34
Tao Bao13f98162018-07-24 14:58:05 -070035static const std::vector<std::string> kResourceImagesDirs{
Tao Baoef5e38f2018-07-24 15:34:39 -070036 "res-mdpi/images/", "res-hdpi/images/", "res-xhdpi/images/",
37 "res-xxhdpi/images/", "res-xxxhdpi/images/",
Tao Bao13f98162018-07-24 14:58:05 -070038};
Tao Bao6b28f052018-04-13 13:41:58 -070039
40static int png_filter(const dirent* de) {
41 if (de->d_type != DT_REG || !android::base::EndsWith(de->d_name, "_text.png")) {
42 return 0;
43 }
44 return 1;
45}
46
47// Finds out all the PNG files to test, which stay under the same dir with the executabl..
48static std::vector<std::string> add_files() {
49 std::vector<std::string> files;
50 for (const std::string& images_dir : kResourceImagesDirs) {
51 static std::string exec_dir = android::base::GetExecutableDirectory();
52 std::string dir_path = exec_dir + "/" + images_dir;
53 dirent** namelist;
54 int n = scandir(dir_path.c_str(), &namelist, png_filter, alphasort);
55 if (n == -1) {
56 printf("Failed to scandir %s: %s\n", dir_path.c_str(), strerror(errno));
57 continue;
58 }
59 if (n == 0) {
60 printf("No file is added for test in %s\n", dir_path.c_str());
61 }
62
63 while (n--) {
64 std::string file_path = dir_path + namelist[n]->d_name;
65 files.push_back(file_path);
66 free(namelist[n]);
67 }
68 free(namelist);
69 }
70 return files;
71}
72
73class ResourcesTest : public testing::TestWithParam<std::string> {
74 public:
75 static std::vector<std::string> png_list;
76
77 protected:
78 void SetUp() override {
79 png_ = std::make_unique<PngHandler>(GetParam());
80 ASSERT_TRUE(png_);
81
82 ASSERT_EQ(PNG_COLOR_TYPE_GRAY, png_->color_type()) << "Recovery expects grayscale PNG file.";
83 ASSERT_LT(static_cast<png_uint_32>(5), png_->width());
84 ASSERT_LT(static_cast<png_uint_32>(0), png_->height());
85 ASSERT_EQ(1, png_->channels()) << "Recovery background text images expects 1-channel PNG file.";
86 }
87
88 std::unique_ptr<PngHandler> png_{ nullptr };
89};
90
91// Parses a png file and tests if it's qualified for the background text image under recovery.
92TEST_P(ResourcesTest, ValidateLocale) {
93 std::vector<unsigned char> row(png_->width());
94 for (png_uint_32 y = 0; y < png_->height(); ++y) {
95 png_read_row(png_->png_ptr(), row.data(), nullptr);
96 int w = (row[1] << 8) | row[0];
97 int h = (row[3] << 8) | row[2];
98 int len = row[4];
99 EXPECT_LT(0, w);
100 EXPECT_LT(0, h);
101 EXPECT_LT(0, len) << "Locale string should be non-empty.";
102 EXPECT_NE(0, row[5]) << "Locale string is missing.";
103
104 ASSERT_GT(png_->height(), y + 1 + h) << "Locale: " << kLocale << " is not found in the file.";
105 char* loc = reinterpret_cast<char*>(&row[5]);
106 if (matches_locale(loc, kLocale.c_str())) {
107 EXPECT_TRUE(android::base::StartsWith(loc, kLocale));
108 break;
109 }
110 for (int i = 0; i < h; ++i, ++y) {
111 png_read_row(png_->png_ptr(), row.data(), nullptr);
112 }
113 }
114}
115
116std::vector<std::string> ResourcesTest::png_list = add_files();
117
118INSTANTIATE_TEST_CASE_P(BackgroundTextValidation, ResourcesTest,
119 ::testing::ValuesIn(ResourcesTest::png_list.cbegin(),
120 ResourcesTest::png_list.cend()));