blob: fe4f951a5cd0dc77307dec1476d0ff56c718daec [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 Kongstad24c9aa62018-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();
53 buffer_ << std::string(indent, ' ')
54 << (config_string.isEmpty() ? "<default>" : config_string)
Adam Lesinskicacb28f2016-10-19 12:18:14 -070055 << std::endl;
56 }
57
Adam Lesinskice5e56e2016-10-21 17:56:45 -070058 void VisitNode(const DominatorTree::Node* node, const int indent) {
59 VisitConfig(node, indent);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070060 for (const auto& child : node->children()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070061 VisitNode(child.get(), indent + indent_);
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070062 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070063 }
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070064
Adam Lesinskice5e56e2016-10-21 17:56:45 -070065 std::stringstream buffer_;
66 const int indent_ = 2;
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070067};
68
Adam Lesinskicacb28f2016-10-19 12:18:14 -070069} // namespace
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070070
71TEST(DominatorTreeTest, DefaultDominatesEverything) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070072 const ConfigDescription default_config = {};
73 const ConfigDescription land_config = test::ParseConfigOrDie("land");
Adam Lesinskie3856742017-06-12 14:55:58 -070074 const ConfigDescription sw600dp_land_config = test::ParseConfigOrDie("sw600dp-land-v13");
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070075
Adam Lesinskicacb28f2016-10-19 12:18:14 -070076 std::vector<std::unique_ptr<ResourceConfigValue>> configs;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070077 configs.push_back(util::make_unique<ResourceConfigValue>(default_config, ""));
78 configs.push_back(util::make_unique<ResourceConfigValue>(land_config, ""));
Adam Lesinskie3856742017-06-12 14:55:58 -070079 configs.push_back(util::make_unique<ResourceConfigValue>(sw600dp_land_config, ""));
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070080
Adam Lesinskicacb28f2016-10-19 12:18:14 -070081 DominatorTree tree(configs);
82 PrettyPrinter printer;
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070083
Adam Lesinskicacb28f2016-10-19 12:18:14 -070084 std::string expected =
85 "<default>\n"
86 " land\n"
87 " sw600dp-land-v13\n";
Adam Lesinskice5e56e2016-10-21 17:56:45 -070088 EXPECT_EQ(expected, printer.ToString(&tree));
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070089}
90
91TEST(DominatorTreeTest, ProductsAreDominatedSeparately) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070092 const ConfigDescription default_config = {};
93 const ConfigDescription land_config = test::ParseConfigOrDie("land");
Adam Lesinskie3856742017-06-12 14:55:58 -070094 const ConfigDescription sw600dp_land_config = test::ParseConfigOrDie("sw600dp-land-v13");
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070095
Adam Lesinskicacb28f2016-10-19 12:18:14 -070096 std::vector<std::unique_ptr<ResourceConfigValue>> configs;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070097 configs.push_back(util::make_unique<ResourceConfigValue>(default_config, ""));
98 configs.push_back(util::make_unique<ResourceConfigValue>(land_config, ""));
Adam Lesinskie3856742017-06-12 14:55:58 -070099 configs.push_back(util::make_unique<ResourceConfigValue>(default_config, "phablet"));
100 configs.push_back(util::make_unique<ResourceConfigValue>(sw600dp_land_config, "phablet"));
Alexandria Cornwall77788eb2016-09-06 15:16:49 -0700101
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700102 DominatorTree tree(configs);
103 PrettyPrinter printer;
Alexandria Cornwall77788eb2016-09-06 15:16:49 -0700104
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700105 std::string expected =
106 "<default>\n"
107 " land\n"
108 "<default>\n"
109 " sw600dp-land-v13\n";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700110 EXPECT_EQ(expected, printer.ToString(&tree));
Alexandria Cornwall77788eb2016-09-06 15:16:49 -0700111}
112
113TEST(DominatorTreeTest, MoreSpecificConfigurationsAreDominated) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700114 const ConfigDescription default_config = {};
115 const ConfigDescription en_config = test::ParseConfigOrDie("en");
116 const ConfigDescription en_v21_config = test::ParseConfigOrDie("en-v21");
117 const ConfigDescription ldrtl_config = test::ParseConfigOrDie("ldrtl-v4");
Adam Lesinskie3856742017-06-12 14:55:58 -0700118 const ConfigDescription ldrtl_xhdpi_config = test::ParseConfigOrDie("ldrtl-xhdpi-v4");
119 const ConfigDescription sw300dp_config = test::ParseConfigOrDie("sw300dp-v13");
120 const ConfigDescription sw540dp_config = test::ParseConfigOrDie("sw540dp-v14");
121 const ConfigDescription sw600dp_config = test::ParseConfigOrDie("sw600dp-v14");
122 const ConfigDescription sw720dp_config = test::ParseConfigOrDie("sw720dp-v13");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700123 const ConfigDescription v20_config = test::ParseConfigOrDie("v20");
Alexandria Cornwall77788eb2016-09-06 15:16:49 -0700124
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700125 std::vector<std::unique_ptr<ResourceConfigValue>> configs;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700126 configs.push_back(util::make_unique<ResourceConfigValue>(default_config, ""));
127 configs.push_back(util::make_unique<ResourceConfigValue>(en_config, ""));
128 configs.push_back(util::make_unique<ResourceConfigValue>(en_v21_config, ""));
129 configs.push_back(util::make_unique<ResourceConfigValue>(ldrtl_config, ""));
Adam Lesinskie3856742017-06-12 14:55:58 -0700130 configs.push_back(util::make_unique<ResourceConfigValue>(ldrtl_xhdpi_config, ""));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700131 configs.push_back(util::make_unique<ResourceConfigValue>(sw300dp_config, ""));
132 configs.push_back(util::make_unique<ResourceConfigValue>(sw540dp_config, ""));
133 configs.push_back(util::make_unique<ResourceConfigValue>(sw600dp_config, ""));
134 configs.push_back(util::make_unique<ResourceConfigValue>(sw720dp_config, ""));
135 configs.push_back(util::make_unique<ResourceConfigValue>(v20_config, ""));
Alexandria Cornwall77788eb2016-09-06 15:16:49 -0700136
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700137 DominatorTree tree(configs);
138 PrettyPrinter printer;
Alexandria Cornwall77788eb2016-09-06 15:16:49 -0700139
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700140 std::string expected =
141 "<default>\n"
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700142 " ldrtl-v4\n"
143 " ldrtl-xhdpi-v4\n"
144 " sw300dp-v13\n"
145 " sw540dp-v14\n"
146 " sw600dp-v14\n"
147 " sw720dp-v13\n"
Adam Lesinskie3856742017-06-12 14:55:58 -0700148 " v20\n"
149 "en\n"
150 " en-v21\n";
151 EXPECT_EQ(expected, printer.ToString(&tree));
152}
153
154TEST(DominatorTreeTest, LocalesAreNeverDominated) {
155 const ConfigDescription fr_config = test::ParseConfigOrDie("fr");
156 const ConfigDescription fr_rCA_config = test::ParseConfigOrDie("fr-rCA");
157 const ConfigDescription fr_rFR_config = test::ParseConfigOrDie("fr-rFR");
158
159 std::vector<std::unique_ptr<ResourceConfigValue>> configs;
160 configs.push_back(util::make_unique<ResourceConfigValue>(ConfigDescription::DefaultConfig(), ""));
161 configs.push_back(util::make_unique<ResourceConfigValue>(fr_config, ""));
162 configs.push_back(util::make_unique<ResourceConfigValue>(fr_rCA_config, ""));
163 configs.push_back(util::make_unique<ResourceConfigValue>(fr_rFR_config, ""));
164
165 DominatorTree tree(configs);
166 PrettyPrinter printer;
167
168 std::string expected =
169 "<default>\n"
170 "fr\n"
171 "fr-rCA\n"
172 "fr-rFR\n";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700173 EXPECT_EQ(expected, printer.ToString(&tree));
Alexandria Cornwall77788eb2016-09-06 15:16:49 -0700174}
175
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700176} // namespace aapt