blob: e89c6beb0c57b66ef581b888687294e76d1bbf1a [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 Lesinskice5e56e22016-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 Lesinskice5e56e22016-10-21 17:56:45 -070032 explicit PrettyPrinter(const int indent = 2) : indent_(indent) {}
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070033
Adam Lesinskice5e56e22016-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 Lesinskice5e56e22016-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 Lesinskice5e56e22016-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 Lesinskice5e56e22016-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 Lesinskice5e56e22016-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 Lesinskice5e56e22016-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 Lesinskice5e56e22016-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 Lesinskice5e56e22016-10-21 17:56:45 -070070 const ConfigDescription default_config = {};
71 const ConfigDescription land_config = test::ParseConfigOrDie("land");
72 const ConfigDescription sw600dp_land_config =
73 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 Lesinskice5e56e22016-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 Lesinskicacb28f2016-10-19 12:18:14 -070078 configs.push_back(
Adam Lesinskice5e56e22016-10-21 17:56:45 -070079 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 Lesinskice5e56e22016-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 Lesinskice5e56e22016-10-21 17:56:45 -070092 const ConfigDescription default_config = {};
93 const ConfigDescription land_config = test::ParseConfigOrDie("land");
94 const ConfigDescription sw600dp_land_config =
95 test::ParseConfigOrDie("sw600dp-land-v13");
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070096
Adam Lesinskicacb28f2016-10-19 12:18:14 -070097 std::vector<std::unique_ptr<ResourceConfigValue>> configs;
Adam Lesinskice5e56e22016-10-21 17:56:45 -070098 configs.push_back(util::make_unique<ResourceConfigValue>(default_config, ""));
99 configs.push_back(util::make_unique<ResourceConfigValue>(land_config, ""));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700100 configs.push_back(
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700101 util::make_unique<ResourceConfigValue>(default_config, "phablet"));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700102 configs.push_back(
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700103 util::make_unique<ResourceConfigValue>(sw600dp_land_config, "phablet"));
Alexandria Cornwall77788eb2016-09-06 15:16:49 -0700104
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700105 DominatorTree tree(configs);
106 PrettyPrinter printer;
Alexandria Cornwall77788eb2016-09-06 15:16:49 -0700107
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700108 std::string expected =
109 "<default>\n"
110 " land\n"
111 "<default>\n"
112 " sw600dp-land-v13\n";
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700113 EXPECT_EQ(expected, printer.ToString(&tree));
Alexandria Cornwall77788eb2016-09-06 15:16:49 -0700114}
115
116TEST(DominatorTreeTest, MoreSpecificConfigurationsAreDominated) {
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700117 const ConfigDescription default_config = {};
118 const ConfigDescription en_config = test::ParseConfigOrDie("en");
119 const ConfigDescription en_v21_config = test::ParseConfigOrDie("en-v21");
120 const ConfigDescription ldrtl_config = test::ParseConfigOrDie("ldrtl-v4");
121 const ConfigDescription ldrtl_xhdpi_config =
122 test::ParseConfigOrDie("ldrtl-xhdpi-v4");
123 const ConfigDescription sw300dp_config =
124 test::ParseConfigOrDie("sw300dp-v13");
125 const ConfigDescription sw540dp_config =
126 test::ParseConfigOrDie("sw540dp-v14");
127 const ConfigDescription sw600dp_config =
128 test::ParseConfigOrDie("sw600dp-v14");
129 const ConfigDescription sw720dp_config =
130 test::ParseConfigOrDie("sw720dp-v13");
131 const ConfigDescription v20_config = test::ParseConfigOrDie("v20");
Alexandria Cornwall77788eb2016-09-06 15:16:49 -0700132
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700133 std::vector<std::unique_ptr<ResourceConfigValue>> configs;
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700134 configs.push_back(util::make_unique<ResourceConfigValue>(default_config, ""));
135 configs.push_back(util::make_unique<ResourceConfigValue>(en_config, ""));
136 configs.push_back(util::make_unique<ResourceConfigValue>(en_v21_config, ""));
137 configs.push_back(util::make_unique<ResourceConfigValue>(ldrtl_config, ""));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700138 configs.push_back(
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700139 util::make_unique<ResourceConfigValue>(ldrtl_xhdpi_config, ""));
140 configs.push_back(util::make_unique<ResourceConfigValue>(sw300dp_config, ""));
141 configs.push_back(util::make_unique<ResourceConfigValue>(sw540dp_config, ""));
142 configs.push_back(util::make_unique<ResourceConfigValue>(sw600dp_config, ""));
143 configs.push_back(util::make_unique<ResourceConfigValue>(sw720dp_config, ""));
144 configs.push_back(util::make_unique<ResourceConfigValue>(v20_config, ""));
Alexandria Cornwall77788eb2016-09-06 15:16:49 -0700145
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700146 DominatorTree tree(configs);
147 PrettyPrinter printer;
Alexandria Cornwall77788eb2016-09-06 15:16:49 -0700148
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700149 std::string expected =
150 "<default>\n"
151 " en\n"
152 " en-v21\n"
153 " ldrtl-v4\n"
154 " ldrtl-xhdpi-v4\n"
155 " sw300dp-v13\n"
156 " sw540dp-v14\n"
157 " sw600dp-v14\n"
158 " sw720dp-v13\n"
159 " v20\n";
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700160 EXPECT_EQ(expected, printer.ToString(&tree));
Alexandria Cornwall77788eb2016-09-06 15:16:49 -0700161}
162
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700163} // namespace aapt