| 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 | ce5e56e2 | 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 | ce5e56e2 | 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 | ce5e56e2 | 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 | ce5e56e2 | 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 | ce5e56e2 | 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 | ce5e56e2 | 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 | ce5e56e2 | 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 | ce5e56e2 | 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 | ce5e56e2 | 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 | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 70 | 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 Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 74 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 75 | std::vector<std::unique_ptr<ResourceConfigValue>> configs; |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 76 | configs.push_back(util::make_unique<ResourceConfigValue>(default_config, "")); |
| 77 | configs.push_back(util::make_unique<ResourceConfigValue>(land_config, "")); |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 78 | configs.push_back( |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 79 | 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 | ce5e56e2 | 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 | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 92 | 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 Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 96 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 97 | std::vector<std::unique_ptr<ResourceConfigValue>> configs; |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 98 | configs.push_back(util::make_unique<ResourceConfigValue>(default_config, "")); |
| 99 | configs.push_back(util::make_unique<ResourceConfigValue>(land_config, "")); |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 100 | configs.push_back( |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 101 | util::make_unique<ResourceConfigValue>(default_config, "phablet")); |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 102 | configs.push_back( |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 103 | util::make_unique<ResourceConfigValue>(sw600dp_land_config, "phablet")); |
| 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 | DominatorTree tree(configs); |
| 106 | PrettyPrinter printer; |
| Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 107 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 108 | std::string expected = |
| 109 | "<default>\n" |
| 110 | " land\n" |
| 111 | "<default>\n" |
| 112 | " sw600dp-land-v13\n"; |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 113 | EXPECT_EQ(expected, printer.ToString(&tree)); |
| Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | TEST(DominatorTreeTest, MoreSpecificConfigurationsAreDominated) { |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 117 | 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 Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 132 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 133 | std::vector<std::unique_ptr<ResourceConfigValue>> configs; |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 134 | 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 Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 138 | configs.push_back( |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 139 | 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 Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 145 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 146 | DominatorTree tree(configs); |
| 147 | PrettyPrinter printer; |
| Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 148 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 149 | 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 Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 160 | EXPECT_EQ(expected, printer.ToString(&tree)); |
| Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 163 | } // namespace aapt |