blob: a0679a65b9fd27445e0aa5f122f2e5aa46c0300c [file] [log] [blame]
Alexandria Cornwall77788eb2016-09-06 15:16:49 -07001/*
2 * Copyright (C) 2016 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 "DominatorTree.h"
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070018
19#include <sstream>
20#include <string>
21#include <vector>
22
Adam Lesinskice5e56e2016-10-21 17:56:45 -070023#include "test/Test.h"
24#include "util/Util.h"
25
MÃ¥rten Kongstad5c541f62018-06-20 08:46:41 +020026using ::android::ConfigDescription;
27
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070028namespace aapt {
29
30namespace {
31
32class PrettyPrinter : public DominatorTree::Visitor {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070033 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070034 explicit PrettyPrinter(const int indent = 2) : indent_(indent) {}
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070035
Adam Lesinskice5e56e2016-10-21 17:56:45 -070036 void VisitTree(const std::string& product,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070037 DominatorTree::Node* root) override {
38 for (auto& child : root->children()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070039 VisitNode(child.get(), 0);
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070040 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070041 }
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070042
Adam Lesinskice5e56e2016-10-21 17:56:45 -070043 std::string ToString(DominatorTree* tree) {
44 buffer_.str("");
45 buffer_.clear();
46 tree->Accept(this);
47 return buffer_.str();
Adam Lesinskicacb28f2016-10-19 12:18:14 -070048 }
49
50 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070051 void VisitConfig(const DominatorTree::Node* node, const int indent) {
52 auto config_string = node->value()->config.toString();
Tomasz Wasilczyk7e22cab2023-08-24 19:02:33 +000053 buffer_ << std::string(indent, ' ') << (config_string.empty() ? "<default>" : config_string)
Adam Lesinskicacb28f2016-10-19 12:18:14 -070054 << std::endl;
55 }
56
Adam Lesinskice5e56e2016-10-21 17:56:45 -070057 void VisitNode(const DominatorTree::Node* node, const int indent) {
58 VisitConfig(node, indent);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070059 for (const auto& child : node->children()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070060 VisitNode(child.get(), indent + indent_);
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070061 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070062 }
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070063
Adam Lesinskice5e56e2016-10-21 17:56:45 -070064 std::stringstream buffer_;
65 const int indent_ = 2;
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070066};
67
Adam Lesinskicacb28f2016-10-19 12:18:14 -070068} // namespace
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070069
70TEST(DominatorTreeTest, DefaultDominatesEverything) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070071 const ConfigDescription default_config = {};
72 const ConfigDescription land_config = test::ParseConfigOrDie("land");
Adam Lesinski90739912017-06-12 14:55:58 -070073 const ConfigDescription sw600dp_land_config = test::ParseConfigOrDie("sw600dp-land-v13");
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070074
Adam Lesinskicacb28f2016-10-19 12:18:14 -070075 std::vector<std::unique_ptr<ResourceConfigValue>> configs;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070076 configs.push_back(util::make_unique<ResourceConfigValue>(default_config, ""));
77 configs.push_back(util::make_unique<ResourceConfigValue>(land_config, ""));
Adam Lesinski90739912017-06-12 14:55:58 -070078 configs.push_back(util::make_unique<ResourceConfigValue>(sw600dp_land_config, ""));
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070079
Adam Lesinskicacb28f2016-10-19 12:18:14 -070080 DominatorTree tree(configs);
81 PrettyPrinter printer;
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070082
Adam Lesinskicacb28f2016-10-19 12:18:14 -070083 std::string expected =
84 "<default>\n"
85 " land\n"
86 " sw600dp-land-v13\n";
Adam Lesinskice5e56e2016-10-21 17:56:45 -070087 EXPECT_EQ(expected, printer.ToString(&tree));
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070088}
89
90TEST(DominatorTreeTest, ProductsAreDominatedSeparately) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070091 const ConfigDescription default_config = {};
92 const ConfigDescription land_config = test::ParseConfigOrDie("land");
Adam Lesinski90739912017-06-12 14:55:58 -070093 const ConfigDescription sw600dp_land_config = test::ParseConfigOrDie("sw600dp-land-v13");
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070094
Adam Lesinskicacb28f2016-10-19 12:18:14 -070095 std::vector<std::unique_ptr<ResourceConfigValue>> configs;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070096 configs.push_back(util::make_unique<ResourceConfigValue>(default_config, ""));
97 configs.push_back(util::make_unique<ResourceConfigValue>(land_config, ""));
Adam Lesinski90739912017-06-12 14:55:58 -070098 configs.push_back(util::make_unique<ResourceConfigValue>(default_config, "phablet"));
99 configs.push_back(util::make_unique<ResourceConfigValue>(sw600dp_land_config, "phablet"));
Alexandria Cornwall77788eb2016-09-06 15:16:49 -0700100
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700101 DominatorTree tree(configs);
102 PrettyPrinter printer;
Alexandria Cornwall77788eb2016-09-06 15:16:49 -0700103
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700104 std::string expected =
105 "<default>\n"
106 " land\n"
107 "<default>\n"
108 " sw600dp-land-v13\n";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700109 EXPECT_EQ(expected, printer.ToString(&tree));
Alexandria Cornwall77788eb2016-09-06 15:16:49 -0700110}
111
112TEST(DominatorTreeTest, MoreSpecificConfigurationsAreDominated) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700113 const ConfigDescription default_config = {};
114 const ConfigDescription en_config = test::ParseConfigOrDie("en");
115 const ConfigDescription en_v21_config = test::ParseConfigOrDie("en-v21");
116 const ConfigDescription ldrtl_config = test::ParseConfigOrDie("ldrtl-v4");
Adam Lesinski90739912017-06-12 14:55:58 -0700117 const ConfigDescription ldrtl_xhdpi_config = test::ParseConfigOrDie("ldrtl-xhdpi-v4");
118 const ConfigDescription sw300dp_config = test::ParseConfigOrDie("sw300dp-v13");
119 const ConfigDescription sw540dp_config = test::ParseConfigOrDie("sw540dp-v14");
120 const ConfigDescription sw600dp_config = test::ParseConfigOrDie("sw600dp-v14");
121 const ConfigDescription sw720dp_config = test::ParseConfigOrDie("sw720dp-v13");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700122 const ConfigDescription v20_config = test::ParseConfigOrDie("v20");
Alexandria Cornwall77788eb2016-09-06 15:16:49 -0700123
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700124 std::vector<std::unique_ptr<ResourceConfigValue>> configs;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700125 configs.push_back(util::make_unique<ResourceConfigValue>(default_config, ""));
126 configs.push_back(util::make_unique<ResourceConfigValue>(en_config, ""));
127 configs.push_back(util::make_unique<ResourceConfigValue>(en_v21_config, ""));
128 configs.push_back(util::make_unique<ResourceConfigValue>(ldrtl_config, ""));
Adam Lesinski90739912017-06-12 14:55:58 -0700129 configs.push_back(util::make_unique<ResourceConfigValue>(ldrtl_xhdpi_config, ""));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700130 configs.push_back(util::make_unique<ResourceConfigValue>(sw300dp_config, ""));
131 configs.push_back(util::make_unique<ResourceConfigValue>(sw540dp_config, ""));
132 configs.push_back(util::make_unique<ResourceConfigValue>(sw600dp_config, ""));
133 configs.push_back(util::make_unique<ResourceConfigValue>(sw720dp_config, ""));
134 configs.push_back(util::make_unique<ResourceConfigValue>(v20_config, ""));
Alexandria Cornwall77788eb2016-09-06 15:16:49 -0700135
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700136 DominatorTree tree(configs);
137 PrettyPrinter printer;
Alexandria Cornwall77788eb2016-09-06 15:16:49 -0700138
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700139 std::string expected =
140 "<default>\n"
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700141 " ldrtl-v4\n"
142 " ldrtl-xhdpi-v4\n"
143 " sw300dp-v13\n"
144 " sw540dp-v14\n"
145 " sw600dp-v14\n"
146 " sw720dp-v13\n"
Adam Lesinski90739912017-06-12 14:55:58 -0700147 " v20\n"
148 "en\n"
149 " en-v21\n";
150 EXPECT_EQ(expected, printer.ToString(&tree));
151}
152
153TEST(DominatorTreeTest, LocalesAreNeverDominated) {
154 const ConfigDescription fr_config = test::ParseConfigOrDie("fr");
155 const ConfigDescription fr_rCA_config = test::ParseConfigOrDie("fr-rCA");
156 const ConfigDescription fr_rFR_config = test::ParseConfigOrDie("fr-rFR");
157
158 std::vector<std::unique_ptr<ResourceConfigValue>> configs;
159 configs.push_back(util::make_unique<ResourceConfigValue>(ConfigDescription::DefaultConfig(), ""));
160 configs.push_back(util::make_unique<ResourceConfigValue>(fr_config, ""));
161 configs.push_back(util::make_unique<ResourceConfigValue>(fr_rCA_config, ""));
162 configs.push_back(util::make_unique<ResourceConfigValue>(fr_rFR_config, ""));
163
164 DominatorTree tree(configs);
165 PrettyPrinter printer;
166
167 std::string expected =
168 "<default>\n"
169 "fr\n"
170 "fr-rCA\n"
171 "fr-rFR\n";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700172 EXPECT_EQ(expected, printer.ToString(&tree));
Alexandria Cornwall77788eb2016-09-06 15:16:49 -0700173}
174
Ryan Mitchell0a0bf362020-09-14 13:11:22 -0700175TEST(DominatorTreeTest, NonZeroDensitiesMatch) {
176 const ConfigDescription sw600_config = test::ParseConfigOrDie("sw600dp");
177 const ConfigDescription sw600_hdpi_config = test::ParseConfigOrDie("sw600dp-hdpi");
178 const ConfigDescription sw800_hdpi_config = test::ParseConfigOrDie("sw800dp-hdpi");
179 const ConfigDescription sw800_xxhdpi_config = test::ParseConfigOrDie("sw800dp-xxhdpi");
180
181 std::vector<std::unique_ptr<ResourceConfigValue>> configs;
182 configs.push_back(util::make_unique<ResourceConfigValue>(ConfigDescription::DefaultConfig(), ""));
183 configs.push_back(util::make_unique<ResourceConfigValue>(sw600_config, ""));
184 configs.push_back(util::make_unique<ResourceConfigValue>(sw600_hdpi_config, ""));
185 configs.push_back(util::make_unique<ResourceConfigValue>(sw800_hdpi_config, ""));
186 configs.push_back(util::make_unique<ResourceConfigValue>(sw800_xxhdpi_config, ""));
187
188 DominatorTree tree(configs);
189 PrettyPrinter printer;
190
191 std::string expected =
192 "<default>\n"
193 " sw600dp-v13\n"
194 " sw600dp-hdpi-v13\n"
195 " sw800dp-hdpi-v13\n"
196 " sw800dp-xxhdpi-v13\n";
197 EXPECT_EQ(expected, printer.ToString(&tree));
198}
199
Ryan Mitchell527ebba2020-10-30 12:32:47 -0700200TEST(DominatorTreeTest, MccMncIsPeertoLocale) {
201 const ConfigDescription default_config = {};
202 const ConfigDescription de_config = test::ParseConfigOrDie("de");
203 const ConfigDescription fr_config = test::ParseConfigOrDie("fr");
204 const ConfigDescription mcc_config = test::ParseConfigOrDie("mcc262");
205 const ConfigDescription mcc_fr_config = test::ParseConfigOrDie("mcc262-fr");
206 const ConfigDescription mnc_config = test::ParseConfigOrDie("mnc2");
207 const ConfigDescription mnc_fr_config = test::ParseConfigOrDie("mnc2-fr");
208 std::vector<std::unique_ptr<ResourceConfigValue>> configs;
209 configs.push_back(util::make_unique<ResourceConfigValue>(default_config, ""));
210 configs.push_back(util::make_unique<ResourceConfigValue>(de_config, ""));
211 configs.push_back(util::make_unique<ResourceConfigValue>(fr_config, ""));
212 configs.push_back(util::make_unique<ResourceConfigValue>(mcc_config, ""));
213 configs.push_back(util::make_unique<ResourceConfigValue>(mcc_fr_config, ""));
214 configs.push_back(util::make_unique<ResourceConfigValue>(mnc_config, ""));
215 configs.push_back(util::make_unique<ResourceConfigValue>(mnc_fr_config, ""));
216 DominatorTree tree(configs);
217 PrettyPrinter printer;
218 std::string expected =
219 "<default>\n"
220 "de\n"
221 "fr\n"
222 "mcc262\n"
223 "mcc262-fr\n"
224 "mnc2\n"
225 "mnc2-fr\n";
226 EXPECT_EQ(expected, printer.ToString(&tree));
227}
Ryan Mitchell0a0bf362020-09-14 13:11:22 -0700228
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700229} // namespace aapt