blob: a42d2f744ef37e5fb7e484d988ca97517d0c10cb [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"
18#include "test/Test.h"
19#include "util/Util.h"
20
21#include <sstream>
22#include <string>
23#include <vector>
24
25namespace aapt {
26
27namespace {
28
29class PrettyPrinter : public DominatorTree::Visitor {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070030 public:
31 explicit PrettyPrinter(const int indent = 2) : mIndent(indent) {}
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070032
Adam Lesinskicacb28f2016-10-19 12:18:14 -070033 void visitTree(const std::string& product,
34 DominatorTree::Node* root) override {
35 for (auto& child : root->children()) {
36 visitNode(child.get(), 0);
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070037 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070038 }
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070039
Adam Lesinskicacb28f2016-10-19 12:18:14 -070040 std::string toString(DominatorTree* tree) {
41 mBuffer.str("");
42 mBuffer.clear();
43 tree->accept(this);
44 return mBuffer.str();
45 }
46
47 private:
48 void visitConfig(const DominatorTree::Node* node, const int indent) {
49 auto configString = node->value()->config.toString();
50 mBuffer << std::string(indent, ' ')
51 << (configString.isEmpty() ? "<default>" : configString)
52 << std::endl;
53 }
54
55 void visitNode(const DominatorTree::Node* node, const int indent) {
56 visitConfig(node, indent);
57 for (const auto& child : node->children()) {
58 visitNode(child.get(), indent + mIndent);
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070059 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070060 }
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070061
Adam Lesinskicacb28f2016-10-19 12:18:14 -070062 std::stringstream mBuffer;
63 const int mIndent = 2;
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070064};
65
Adam Lesinskicacb28f2016-10-19 12:18:14 -070066} // namespace
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070067
68TEST(DominatorTreeTest, DefaultDominatesEverything) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070069 const ConfigDescription defaultConfig = {};
70 const ConfigDescription landConfig = test::parseConfigOrDie("land");
71 const ConfigDescription sw600dpLandConfig =
72 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;
75 configs.push_back(util::make_unique<ResourceConfigValue>(defaultConfig, ""));
76 configs.push_back(util::make_unique<ResourceConfigValue>(landConfig, ""));
77 configs.push_back(
78 util::make_unique<ResourceConfigValue>(sw600dpLandConfig, ""));
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";
87 EXPECT_EQ(expected, printer.toString(&tree));
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070088}
89
90TEST(DominatorTreeTest, ProductsAreDominatedSeparately) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070091 const ConfigDescription defaultConfig = {};
92 const ConfigDescription landConfig = test::parseConfigOrDie("land");
93 const ConfigDescription sw600dpLandConfig =
94 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;
97 configs.push_back(util::make_unique<ResourceConfigValue>(defaultConfig, ""));
98 configs.push_back(util::make_unique<ResourceConfigValue>(landConfig, ""));
99 configs.push_back(
100 util::make_unique<ResourceConfigValue>(defaultConfig, "phablet"));
101 configs.push_back(
102 util::make_unique<ResourceConfigValue>(sw600dpLandConfig, "phablet"));
Alexandria Cornwall77788eb2016-09-06 15:16:49 -0700103
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700104 DominatorTree tree(configs);
105 PrettyPrinter printer;
Alexandria Cornwall77788eb2016-09-06 15:16:49 -0700106
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700107 std::string expected =
108 "<default>\n"
109 " land\n"
110 "<default>\n"
111 " sw600dp-land-v13\n";
112 EXPECT_EQ(expected, printer.toString(&tree));
Alexandria Cornwall77788eb2016-09-06 15:16:49 -0700113}
114
115TEST(DominatorTreeTest, MoreSpecificConfigurationsAreDominated) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700116 const ConfigDescription defaultConfig = {};
117 const ConfigDescription enConfig = test::parseConfigOrDie("en");
118 const ConfigDescription enV21Config = test::parseConfigOrDie("en-v21");
119 const ConfigDescription ldrtlConfig = test::parseConfigOrDie("ldrtl-v4");
120 const ConfigDescription ldrtlXhdpiConfig =
121 test::parseConfigOrDie("ldrtl-xhdpi-v4");
122 const ConfigDescription sw300dpConfig = test::parseConfigOrDie("sw300dp-v13");
123 const ConfigDescription sw540dpConfig = test::parseConfigOrDie("sw540dp-v14");
124 const ConfigDescription sw600dpConfig = test::parseConfigOrDie("sw600dp-v14");
125 const ConfigDescription sw720dpConfig = test::parseConfigOrDie("sw720dp-v13");
126 const ConfigDescription v20Config = test::parseConfigOrDie("v20");
Alexandria Cornwall77788eb2016-09-06 15:16:49 -0700127
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700128 std::vector<std::unique_ptr<ResourceConfigValue>> configs;
129 configs.push_back(util::make_unique<ResourceConfigValue>(defaultConfig, ""));
130 configs.push_back(util::make_unique<ResourceConfigValue>(enConfig, ""));
131 configs.push_back(util::make_unique<ResourceConfigValue>(enV21Config, ""));
132 configs.push_back(util::make_unique<ResourceConfigValue>(ldrtlConfig, ""));
133 configs.push_back(
134 util::make_unique<ResourceConfigValue>(ldrtlXhdpiConfig, ""));
135 configs.push_back(util::make_unique<ResourceConfigValue>(sw300dpConfig, ""));
136 configs.push_back(util::make_unique<ResourceConfigValue>(sw540dpConfig, ""));
137 configs.push_back(util::make_unique<ResourceConfigValue>(sw600dpConfig, ""));
138 configs.push_back(util::make_unique<ResourceConfigValue>(sw720dpConfig, ""));
139 configs.push_back(util::make_unique<ResourceConfigValue>(v20Config, ""));
Alexandria Cornwall77788eb2016-09-06 15:16:49 -0700140
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700141 DominatorTree tree(configs);
142 PrettyPrinter printer;
Alexandria Cornwall77788eb2016-09-06 15:16:49 -0700143
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700144 std::string expected =
145 "<default>\n"
146 " en\n"
147 " en-v21\n"
148 " ldrtl-v4\n"
149 " ldrtl-xhdpi-v4\n"
150 " sw300dp-v13\n"
151 " sw540dp-v14\n"
152 " sw600dp-v14\n"
153 " sw720dp-v13\n"
154 " v20\n";
155 EXPECT_EQ(expected, printer.toString(&tree));
Alexandria Cornwall77788eb2016-09-06 15:16:49 -0700156}
157
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700158} // namespace aapt