blob: efc523fcf59bbce0792bf0fa1b370702cb9f19e0 [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
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070026namespace aapt {
27
28namespace {
29
30class PrettyPrinter : public DominatorTree::Visitor {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070031 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070032 explicit PrettyPrinter(const int indent = 2) : indent_(indent) {}
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070033
Adam Lesinskice5e56e2016-10-21 17:56:45 -070034 void VisitTree(const std::string& product,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070035 DominatorTree::Node* root) override {
36 for (auto& child : root->children()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070037 VisitNode(child.get(), 0);
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070038 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070039 }
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070040
Adam Lesinskice5e56e2016-10-21 17:56:45 -070041 std::string ToString(DominatorTree* tree) {
42 buffer_.str("");
43 buffer_.clear();
44 tree->Accept(this);
45 return buffer_.str();
Adam Lesinskicacb28f2016-10-19 12:18:14 -070046 }
47
48 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070049 void VisitConfig(const DominatorTree::Node* node, const int indent) {
50 auto config_string = node->value()->config.toString();
51 buffer_ << std::string(indent, ' ')
52 << (config_string.isEmpty() ? "<default>" : config_string)
Adam Lesinskicacb28f2016-10-19 12:18:14 -070053 << std::endl;
54 }
55
Adam Lesinskice5e56e2016-10-21 17:56:45 -070056 void VisitNode(const DominatorTree::Node* node, const int indent) {
57 VisitConfig(node, indent);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070058 for (const auto& child : node->children()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070059 VisitNode(child.get(), indent + indent_);
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070060 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070061 }
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070062
Adam Lesinskice5e56e2016-10-21 17:56:45 -070063 std::stringstream buffer_;
64 const int indent_ = 2;
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070065};
66
Adam Lesinskicacb28f2016-10-19 12:18:14 -070067} // namespace
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070068
69TEST(DominatorTreeTest, DefaultDominatesEverything) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070070 const ConfigDescription default_config = {};
71 const ConfigDescription land_config = test::ParseConfigOrDie("land");
Adam Lesinskie3856742017-06-12 14:55:58 -070072 const ConfigDescription sw600dp_land_config = test::ParseConfigOrDie("sw600dp-land-v13");
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070073
Adam Lesinskicacb28f2016-10-19 12:18:14 -070074 std::vector<std::unique_ptr<ResourceConfigValue>> configs;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070075 configs.push_back(util::make_unique<ResourceConfigValue>(default_config, ""));
76 configs.push_back(util::make_unique<ResourceConfigValue>(land_config, ""));
Adam Lesinskie3856742017-06-12 14:55:58 -070077 configs.push_back(util::make_unique<ResourceConfigValue>(sw600dp_land_config, ""));
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070078
Adam Lesinskicacb28f2016-10-19 12:18:14 -070079 DominatorTree tree(configs);
80 PrettyPrinter printer;
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070081
Adam Lesinskicacb28f2016-10-19 12:18:14 -070082 std::string expected =
83 "<default>\n"
84 " land\n"
85 " sw600dp-land-v13\n";
Adam Lesinskice5e56e2016-10-21 17:56:45 -070086 EXPECT_EQ(expected, printer.ToString(&tree));
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070087}
88
89TEST(DominatorTreeTest, ProductsAreDominatedSeparately) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070090 const ConfigDescription default_config = {};
91 const ConfigDescription land_config = test::ParseConfigOrDie("land");
Adam Lesinskie3856742017-06-12 14:55:58 -070092 const ConfigDescription sw600dp_land_config = test::ParseConfigOrDie("sw600dp-land-v13");
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070093
Adam Lesinskicacb28f2016-10-19 12:18:14 -070094 std::vector<std::unique_ptr<ResourceConfigValue>> configs;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070095 configs.push_back(util::make_unique<ResourceConfigValue>(default_config, ""));
96 configs.push_back(util::make_unique<ResourceConfigValue>(land_config, ""));
Adam Lesinskie3856742017-06-12 14:55:58 -070097 configs.push_back(util::make_unique<ResourceConfigValue>(default_config, "phablet"));
98 configs.push_back(util::make_unique<ResourceConfigValue>(sw600dp_land_config, "phablet"));
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070099
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700100 DominatorTree tree(configs);
101 PrettyPrinter printer;
Alexandria Cornwall77788eb2016-09-06 15:16:49 -0700102
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700103 std::string expected =
104 "<default>\n"
105 " land\n"
106 "<default>\n"
107 " sw600dp-land-v13\n";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700108 EXPECT_EQ(expected, printer.ToString(&tree));
Alexandria Cornwall77788eb2016-09-06 15:16:49 -0700109}
110
111TEST(DominatorTreeTest, MoreSpecificConfigurationsAreDominated) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700112 const ConfigDescription default_config = {};
113 const ConfigDescription en_config = test::ParseConfigOrDie("en");
114 const ConfigDescription en_v21_config = test::ParseConfigOrDie("en-v21");
115 const ConfigDescription ldrtl_config = test::ParseConfigOrDie("ldrtl-v4");
Adam Lesinskie3856742017-06-12 14:55:58 -0700116 const ConfigDescription ldrtl_xhdpi_config = test::ParseConfigOrDie("ldrtl-xhdpi-v4");
117 const ConfigDescription sw300dp_config = test::ParseConfigOrDie("sw300dp-v13");
118 const ConfigDescription sw540dp_config = test::ParseConfigOrDie("sw540dp-v14");
119 const ConfigDescription sw600dp_config = test::ParseConfigOrDie("sw600dp-v14");
120 const ConfigDescription sw720dp_config = test::ParseConfigOrDie("sw720dp-v13");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700121 const ConfigDescription v20_config = test::ParseConfigOrDie("v20");
Alexandria Cornwall77788eb2016-09-06 15:16:49 -0700122
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700123 std::vector<std::unique_ptr<ResourceConfigValue>> configs;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700124 configs.push_back(util::make_unique<ResourceConfigValue>(default_config, ""));
125 configs.push_back(util::make_unique<ResourceConfigValue>(en_config, ""));
126 configs.push_back(util::make_unique<ResourceConfigValue>(en_v21_config, ""));
127 configs.push_back(util::make_unique<ResourceConfigValue>(ldrtl_config, ""));
Adam Lesinskie3856742017-06-12 14:55:58 -0700128 configs.push_back(util::make_unique<ResourceConfigValue>(ldrtl_xhdpi_config, ""));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700129 configs.push_back(util::make_unique<ResourceConfigValue>(sw300dp_config, ""));
130 configs.push_back(util::make_unique<ResourceConfigValue>(sw540dp_config, ""));
131 configs.push_back(util::make_unique<ResourceConfigValue>(sw600dp_config, ""));
132 configs.push_back(util::make_unique<ResourceConfigValue>(sw720dp_config, ""));
133 configs.push_back(util::make_unique<ResourceConfigValue>(v20_config, ""));
Alexandria Cornwall77788eb2016-09-06 15:16:49 -0700134
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700135 DominatorTree tree(configs);
136 PrettyPrinter printer;
Alexandria Cornwall77788eb2016-09-06 15:16:49 -0700137
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700138 std::string expected =
139 "<default>\n"
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700140 " ldrtl-v4\n"
141 " ldrtl-xhdpi-v4\n"
142 " sw300dp-v13\n"
143 " sw540dp-v14\n"
144 " sw600dp-v14\n"
145 " sw720dp-v13\n"
Adam Lesinskie3856742017-06-12 14:55:58 -0700146 " v20\n"
147 "en\n"
148 " en-v21\n";
149 EXPECT_EQ(expected, printer.ToString(&tree));
150}
151
152TEST(DominatorTreeTest, LocalesAreNeverDominated) {
153 const ConfigDescription fr_config = test::ParseConfigOrDie("fr");
154 const ConfigDescription fr_rCA_config = test::ParseConfigOrDie("fr-rCA");
155 const ConfigDescription fr_rFR_config = test::ParseConfigOrDie("fr-rFR");
156
157 std::vector<std::unique_ptr<ResourceConfigValue>> configs;
158 configs.push_back(util::make_unique<ResourceConfigValue>(ConfigDescription::DefaultConfig(), ""));
159 configs.push_back(util::make_unique<ResourceConfigValue>(fr_config, ""));
160 configs.push_back(util::make_unique<ResourceConfigValue>(fr_rCA_config, ""));
161 configs.push_back(util::make_unique<ResourceConfigValue>(fr_rFR_config, ""));
162
163 DominatorTree tree(configs);
164 PrettyPrinter printer;
165
166 std::string expected =
167 "<default>\n"
168 "fr\n"
169 "fr-rCA\n"
170 "fr-rFR\n";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700171 EXPECT_EQ(expected, printer.ToString(&tree));
Alexandria Cornwall77788eb2016-09-06 15:16:49 -0700172}
173
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700174} // namespace aapt