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" |
| 18 | #include "test/Test.h" |
| 19 | #include "util/Util.h" |
| 20 | |
| 21 | #include <sstream> |
| 22 | #include <string> |
| 23 | #include <vector> |
| 24 | |
| 25 | namespace aapt { |
| 26 | |
| 27 | namespace { |
| 28 | |
| 29 | class PrettyPrinter : public DominatorTree::Visitor { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 30 | public: |
| 31 | explicit PrettyPrinter(const int indent = 2) : mIndent(indent) {} |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 32 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 33 | void visitTree(const std::string& product, |
| 34 | DominatorTree::Node* root) override { |
| 35 | for (auto& child : root->children()) { |
| 36 | visitNode(child.get(), 0); |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 37 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 38 | } |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 39 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 40 | 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 Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 59 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 60 | } |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 61 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 62 | std::stringstream mBuffer; |
| 63 | const int mIndent = 2; |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 64 | }; |
| 65 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 66 | } // namespace |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 67 | |
| 68 | TEST(DominatorTreeTest, DefaultDominatesEverything) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 69 | const ConfigDescription defaultConfig = {}; |
| 70 | const ConfigDescription landConfig = test::parseConfigOrDie("land"); |
| 71 | const ConfigDescription sw600dpLandConfig = |
| 72 | 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; |
| 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 Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 79 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 80 | DominatorTree tree(configs); |
| 81 | PrettyPrinter printer; |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 82 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 83 | std::string expected = |
| 84 | "<default>\n" |
| 85 | " land\n" |
| 86 | " sw600dp-land-v13\n"; |
| 87 | EXPECT_EQ(expected, printer.toString(&tree)); |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | TEST(DominatorTreeTest, ProductsAreDominatedSeparately) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 91 | const ConfigDescription defaultConfig = {}; |
| 92 | const ConfigDescription landConfig = test::parseConfigOrDie("land"); |
| 93 | const ConfigDescription sw600dpLandConfig = |
| 94 | 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; |
| 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 Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 103 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 104 | DominatorTree tree(configs); |
| 105 | PrettyPrinter printer; |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 106 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 107 | 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 Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | TEST(DominatorTreeTest, MoreSpecificConfigurationsAreDominated) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 116 | 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 Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 127 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 128 | 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 Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 140 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 141 | DominatorTree tree(configs); |
| 142 | PrettyPrinter printer; |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 143 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 144 | 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 Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 158 | } // namespace aapt |