Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 1 | /* |
| 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 Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 18 | |
| 19 | #include <sstream> |
| 20 | #include <string> |
| 21 | #include <vector> |
| 22 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 23 | #include "test/Test.h" |
| 24 | #include "util/Util.h" |
| 25 | |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 26 | namespace aapt { |
| 27 | |
| 28 | namespace { |
| 29 | |
| 30 | class PrettyPrinter : public DominatorTree::Visitor { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 31 | public: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 32 | explicit PrettyPrinter(const int indent = 2) : indent_(indent) {} |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 33 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 34 | void VisitTree(const std::string& product, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 35 | DominatorTree::Node* root) override { |
| 36 | for (auto& child : root->children()) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 37 | VisitNode(child.get(), 0); |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 38 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 39 | } |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 40 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 41 | std::string ToString(DominatorTree* tree) { |
| 42 | buffer_.str(""); |
| 43 | buffer_.clear(); |
| 44 | tree->Accept(this); |
| 45 | return buffer_.str(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | private: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 49 | 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 Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 53 | << std::endl; |
| 54 | } |
| 55 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 56 | void VisitNode(const DominatorTree::Node* node, const int indent) { |
| 57 | VisitConfig(node, indent); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 58 | for (const auto& child : node->children()) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 59 | VisitNode(child.get(), indent + indent_); |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 60 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 61 | } |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 62 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 63 | std::stringstream buffer_; |
| 64 | const int indent_ = 2; |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 65 | }; |
| 66 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 67 | } // namespace |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 68 | |
| 69 | TEST(DominatorTreeTest, DefaultDominatesEverything) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 70 | const ConfigDescription default_config = {}; |
| 71 | const ConfigDescription land_config = test::ParseConfigOrDie("land"); |
Adam Lesinski | e385674 | 2017-06-12 14:55:58 -0700 | [diff] [blame] | 72 | const ConfigDescription sw600dp_land_config = test::ParseConfigOrDie("sw600dp-land-v13"); |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 73 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 74 | std::vector<std::unique_ptr<ResourceConfigValue>> configs; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 75 | configs.push_back(util::make_unique<ResourceConfigValue>(default_config, "")); |
| 76 | configs.push_back(util::make_unique<ResourceConfigValue>(land_config, "")); |
Adam Lesinski | e385674 | 2017-06-12 14:55:58 -0700 | [diff] [blame] | 77 | configs.push_back(util::make_unique<ResourceConfigValue>(sw600dp_land_config, "")); |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 78 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 79 | DominatorTree tree(configs); |
| 80 | PrettyPrinter printer; |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 81 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 82 | std::string expected = |
| 83 | "<default>\n" |
| 84 | " land\n" |
| 85 | " sw600dp-land-v13\n"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 86 | EXPECT_EQ(expected, printer.ToString(&tree)); |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | TEST(DominatorTreeTest, ProductsAreDominatedSeparately) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 90 | const ConfigDescription default_config = {}; |
| 91 | const ConfigDescription land_config = test::ParseConfigOrDie("land"); |
Adam Lesinski | e385674 | 2017-06-12 14:55:58 -0700 | [diff] [blame] | 92 | const ConfigDescription sw600dp_land_config = test::ParseConfigOrDie("sw600dp-land-v13"); |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 93 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 94 | std::vector<std::unique_ptr<ResourceConfigValue>> configs; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 95 | configs.push_back(util::make_unique<ResourceConfigValue>(default_config, "")); |
| 96 | configs.push_back(util::make_unique<ResourceConfigValue>(land_config, "")); |
Adam Lesinski | e385674 | 2017-06-12 14:55:58 -0700 | [diff] [blame] | 97 | configs.push_back(util::make_unique<ResourceConfigValue>(default_config, "phablet")); |
| 98 | configs.push_back(util::make_unique<ResourceConfigValue>(sw600dp_land_config, "phablet")); |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 99 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 100 | DominatorTree tree(configs); |
| 101 | PrettyPrinter printer; |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 102 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 103 | std::string expected = |
| 104 | "<default>\n" |
| 105 | " land\n" |
| 106 | "<default>\n" |
| 107 | " sw600dp-land-v13\n"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 108 | EXPECT_EQ(expected, printer.ToString(&tree)); |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | TEST(DominatorTreeTest, MoreSpecificConfigurationsAreDominated) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 112 | 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 Lesinski | e385674 | 2017-06-12 14:55:58 -0700 | [diff] [blame] | 116 | 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 Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 121 | const ConfigDescription v20_config = test::ParseConfigOrDie("v20"); |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 122 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 123 | std::vector<std::unique_ptr<ResourceConfigValue>> configs; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 124 | 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 Lesinski | e385674 | 2017-06-12 14:55:58 -0700 | [diff] [blame] | 128 | configs.push_back(util::make_unique<ResourceConfigValue>(ldrtl_xhdpi_config, "")); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 129 | 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 Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 134 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 135 | DominatorTree tree(configs); |
| 136 | PrettyPrinter printer; |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 137 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 138 | std::string expected = |
| 139 | "<default>\n" |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 140 | " 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 Lesinski | e385674 | 2017-06-12 14:55:58 -0700 | [diff] [blame] | 146 | " v20\n" |
| 147 | "en\n" |
| 148 | " en-v21\n"; |
| 149 | EXPECT_EQ(expected, printer.ToString(&tree)); |
| 150 | } |
| 151 | |
| 152 | TEST(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 Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 171 | EXPECT_EQ(expected, printer.ToString(&tree)); |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 174 | } // namespace aapt |