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 | |
MÃ¥rten Kongstad | 5c541f6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 26 | using ::android::ConfigDescription; |
| 27 | |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 28 | namespace aapt { |
| 29 | |
| 30 | namespace { |
| 31 | |
| 32 | class PrettyPrinter : public DominatorTree::Visitor { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 33 | public: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 34 | explicit PrettyPrinter(const int indent = 2) : indent_(indent) {} |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 35 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 36 | void VisitTree(const std::string& product, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 37 | DominatorTree::Node* root) override { |
| 38 | for (auto& child : root->children()) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 39 | VisitNode(child.get(), 0); |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 40 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 41 | } |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 42 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 43 | std::string ToString(DominatorTree* tree) { |
| 44 | buffer_.str(""); |
| 45 | buffer_.clear(); |
| 46 | tree->Accept(this); |
| 47 | return buffer_.str(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | private: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 51 | 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 Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 55 | << std::endl; |
| 56 | } |
| 57 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 58 | void VisitNode(const DominatorTree::Node* node, const int indent) { |
| 59 | VisitConfig(node, indent); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 60 | for (const auto& child : node->children()) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 61 | VisitNode(child.get(), indent + indent_); |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 62 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 63 | } |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 64 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 65 | std::stringstream buffer_; |
| 66 | const int indent_ = 2; |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 67 | }; |
| 68 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 69 | } // namespace |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 70 | |
| 71 | TEST(DominatorTreeTest, DefaultDominatesEverything) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 72 | const ConfigDescription default_config = {}; |
| 73 | const ConfigDescription land_config = test::ParseConfigOrDie("land"); |
Adam Lesinski | 9073991 | 2017-06-12 14:55:58 -0700 | [diff] [blame] | 74 | const ConfigDescription sw600dp_land_config = test::ParseConfigOrDie("sw600dp-land-v13"); |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 75 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 76 | std::vector<std::unique_ptr<ResourceConfigValue>> configs; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 77 | configs.push_back(util::make_unique<ResourceConfigValue>(default_config, "")); |
| 78 | configs.push_back(util::make_unique<ResourceConfigValue>(land_config, "")); |
Adam Lesinski | 9073991 | 2017-06-12 14:55:58 -0700 | [diff] [blame] | 79 | configs.push_back(util::make_unique<ResourceConfigValue>(sw600dp_land_config, "")); |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 80 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 81 | DominatorTree tree(configs); |
| 82 | PrettyPrinter printer; |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 83 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 84 | std::string expected = |
| 85 | "<default>\n" |
| 86 | " land\n" |
| 87 | " sw600dp-land-v13\n"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 88 | EXPECT_EQ(expected, printer.ToString(&tree)); |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | TEST(DominatorTreeTest, ProductsAreDominatedSeparately) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 92 | const ConfigDescription default_config = {}; |
| 93 | const ConfigDescription land_config = test::ParseConfigOrDie("land"); |
Adam Lesinski | 9073991 | 2017-06-12 14:55:58 -0700 | [diff] [blame] | 94 | const ConfigDescription sw600dp_land_config = test::ParseConfigOrDie("sw600dp-land-v13"); |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 95 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 96 | std::vector<std::unique_ptr<ResourceConfigValue>> configs; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 97 | configs.push_back(util::make_unique<ResourceConfigValue>(default_config, "")); |
| 98 | configs.push_back(util::make_unique<ResourceConfigValue>(land_config, "")); |
Adam Lesinski | 9073991 | 2017-06-12 14:55:58 -0700 | [diff] [blame] | 99 | configs.push_back(util::make_unique<ResourceConfigValue>(default_config, "phablet")); |
| 100 | configs.push_back(util::make_unique<ResourceConfigValue>(sw600dp_land_config, "phablet")); |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 101 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 102 | DominatorTree tree(configs); |
| 103 | PrettyPrinter printer; |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 104 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 105 | std::string expected = |
| 106 | "<default>\n" |
| 107 | " land\n" |
| 108 | "<default>\n" |
| 109 | " sw600dp-land-v13\n"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 110 | EXPECT_EQ(expected, printer.ToString(&tree)); |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | TEST(DominatorTreeTest, MoreSpecificConfigurationsAreDominated) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 114 | 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 Lesinski | 9073991 | 2017-06-12 14:55:58 -0700 | [diff] [blame] | 118 | 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 Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 123 | const ConfigDescription v20_config = test::ParseConfigOrDie("v20"); |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 124 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 125 | std::vector<std::unique_ptr<ResourceConfigValue>> configs; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 126 | 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 Lesinski | 9073991 | 2017-06-12 14:55:58 -0700 | [diff] [blame] | 130 | configs.push_back(util::make_unique<ResourceConfigValue>(ldrtl_xhdpi_config, "")); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 131 | 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 Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 136 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 137 | DominatorTree tree(configs); |
| 138 | PrettyPrinter printer; |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 139 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 140 | std::string expected = |
| 141 | "<default>\n" |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 142 | " 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 Lesinski | 9073991 | 2017-06-12 14:55:58 -0700 | [diff] [blame] | 148 | " v20\n" |
| 149 | "en\n" |
| 150 | " en-v21\n"; |
| 151 | EXPECT_EQ(expected, printer.ToString(&tree)); |
| 152 | } |
| 153 | |
| 154 | TEST(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 Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 173 | EXPECT_EQ(expected, printer.ToString(&tree)); |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Ryan Mitchell | 0a0bf36 | 2020-09-14 13:11:22 -0700 | [diff] [blame] | 176 | TEST(DominatorTreeTest, NonZeroDensitiesMatch) { |
| 177 | const ConfigDescription sw600_config = test::ParseConfigOrDie("sw600dp"); |
| 178 | const ConfigDescription sw600_hdpi_config = test::ParseConfigOrDie("sw600dp-hdpi"); |
| 179 | const ConfigDescription sw800_hdpi_config = test::ParseConfigOrDie("sw800dp-hdpi"); |
| 180 | const ConfigDescription sw800_xxhdpi_config = test::ParseConfigOrDie("sw800dp-xxhdpi"); |
| 181 | |
| 182 | std::vector<std::unique_ptr<ResourceConfigValue>> configs; |
| 183 | configs.push_back(util::make_unique<ResourceConfigValue>(ConfigDescription::DefaultConfig(), "")); |
| 184 | configs.push_back(util::make_unique<ResourceConfigValue>(sw600_config, "")); |
| 185 | configs.push_back(util::make_unique<ResourceConfigValue>(sw600_hdpi_config, "")); |
| 186 | configs.push_back(util::make_unique<ResourceConfigValue>(sw800_hdpi_config, "")); |
| 187 | configs.push_back(util::make_unique<ResourceConfigValue>(sw800_xxhdpi_config, "")); |
| 188 | |
| 189 | DominatorTree tree(configs); |
| 190 | PrettyPrinter printer; |
| 191 | |
| 192 | std::string expected = |
| 193 | "<default>\n" |
| 194 | " sw600dp-v13\n" |
| 195 | " sw600dp-hdpi-v13\n" |
| 196 | " sw800dp-hdpi-v13\n" |
| 197 | " sw800dp-xxhdpi-v13\n"; |
| 198 | EXPECT_EQ(expected, printer.ToString(&tree)); |
| 199 | } |
| 200 | |
Ryan Mitchell | 527ebba | 2020-10-30 12:32:47 -0700 | [diff] [blame] | 201 | TEST(DominatorTreeTest, MccMncIsPeertoLocale) { |
| 202 | const ConfigDescription default_config = {}; |
| 203 | const ConfigDescription de_config = test::ParseConfigOrDie("de"); |
| 204 | const ConfigDescription fr_config = test::ParseConfigOrDie("fr"); |
| 205 | const ConfigDescription mcc_config = test::ParseConfigOrDie("mcc262"); |
| 206 | const ConfigDescription mcc_fr_config = test::ParseConfigOrDie("mcc262-fr"); |
| 207 | const ConfigDescription mnc_config = test::ParseConfigOrDie("mnc2"); |
| 208 | const ConfigDescription mnc_fr_config = test::ParseConfigOrDie("mnc2-fr"); |
| 209 | std::vector<std::unique_ptr<ResourceConfigValue>> configs; |
| 210 | configs.push_back(util::make_unique<ResourceConfigValue>(default_config, "")); |
| 211 | configs.push_back(util::make_unique<ResourceConfigValue>(de_config, "")); |
| 212 | configs.push_back(util::make_unique<ResourceConfigValue>(fr_config, "")); |
| 213 | configs.push_back(util::make_unique<ResourceConfigValue>(mcc_config, "")); |
| 214 | configs.push_back(util::make_unique<ResourceConfigValue>(mcc_fr_config, "")); |
| 215 | configs.push_back(util::make_unique<ResourceConfigValue>(mnc_config, "")); |
| 216 | configs.push_back(util::make_unique<ResourceConfigValue>(mnc_fr_config, "")); |
| 217 | DominatorTree tree(configs); |
| 218 | PrettyPrinter printer; |
| 219 | std::string expected = |
| 220 | "<default>\n" |
| 221 | "de\n" |
| 222 | "fr\n" |
| 223 | "mcc262\n" |
| 224 | "mcc262-fr\n" |
| 225 | "mnc2\n" |
| 226 | "mnc2-fr\n"; |
| 227 | EXPECT_EQ(expected, printer.ToString(&tree)); |
| 228 | } |
Ryan Mitchell | 0a0bf36 | 2020-09-14 13:11:22 -0700 | [diff] [blame] | 229 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 230 | } // namespace aapt |