blob: b00c3d52874ce41844924c35e61b9c273201e399 [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{
36 "res-testdata/res-mdpi/images/", "res-testdata/res-hdpi/images/",
37 "res-testdata/res-xhdpi/images/", "res-testdata/res-xxhdpi/images/",
38 "res-testdata/res-xxxhdpi/images/",
39};
Tao Bao6b28f052018-04-13 13:41:58 -070040
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}
73
74class ResourcesTest : public testing::TestWithParam<std::string> {
75 public:
76 static std::vector<std::string> png_list;
77
78 protected:
79 void SetUp() override {
80 png_ = std::make_unique<PngHandler>(GetParam());
81 ASSERT_TRUE(png_);
82
83 ASSERT_EQ(PNG_COLOR_TYPE_GRAY, png_->color_type()) << "Recovery expects grayscale PNG file.";
84 ASSERT_LT(static_cast<png_uint_32>(5), png_->width());
85 ASSERT_LT(static_cast<png_uint_32>(0), png_->height());
86 ASSERT_EQ(1, png_->channels()) << "Recovery background text images expects 1-channel PNG file.";
87 }
88
89 std::unique_ptr<PngHandler> png_{ nullptr };
90};
91
92// Parses a png file and tests if it's qualified for the background text image under recovery.
93TEST_P(ResourcesTest, ValidateLocale) {
94 std::vector<unsigned char> row(png_->width());
95 for (png_uint_32 y = 0; y < png_->height(); ++y) {
96 png_read_row(png_->png_ptr(), row.data(), nullptr);
97 int w = (row[1] << 8) | row[0];
98 int h = (row[3] << 8) | row[2];
99 int len = row[4];
100 EXPECT_LT(0, w);
101 EXPECT_LT(0, h);
102 EXPECT_LT(0, len) << "Locale string should be non-empty.";
103 EXPECT_NE(0, row[5]) << "Locale string is missing.";
104
105 ASSERT_GT(png_->height(), y + 1 + h) << "Locale: " << kLocale << " is not found in the file.";
106 char* loc = reinterpret_cast<char*>(&row[5]);
107 if (matches_locale(loc, kLocale.c_str())) {
108 EXPECT_TRUE(android::base::StartsWith(loc, kLocale));
109 break;
110 }
111 for (int i = 0; i < h; ++i, ++y) {
112 png_read_row(png_->png_ptr(), row.data(), nullptr);
113 }
114 }
115}
116
117std::vector<std::string> ResourcesTest::png_list = add_files();
118
119INSTANTIATE_TEST_CASE_P(BackgroundTextValidation, ResourcesTest,
120 ::testing::ValuesIn(ResourcesTest::png_list.cbegin(),
121 ResourcesTest::png_list.cend()));