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 { |
| 30 | public: |
| 31 | explicit PrettyPrinter(const int indent = 2) : mIndent(indent) { |
| 32 | } |
| 33 | |
| 34 | void visitTree(const std::string& product, DominatorTree::Node* root) override { |
| 35 | for (auto& child : root->children()) { |
| 36 | visitNode(child.get(), 0); |
| 37 | } |
| 38 | } |
| 39 | |
| 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); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | std::stringstream mBuffer; |
| 63 | const int mIndent = 2; |
| 64 | }; |
| 65 | |
| 66 | } // namespace |
| 67 | |
| 68 | TEST(DominatorTreeTest, DefaultDominatesEverything) { |
| 69 | const ConfigDescription defaultConfig = {}; |
| 70 | const ConfigDescription landConfig = test::parseConfigOrDie("land"); |
| 71 | const ConfigDescription sw600dpLandConfig = test::parseConfigOrDie("sw600dp-land-v13"); |
| 72 | |
| 73 | std::vector<std::unique_ptr<ResourceConfigValue>> configs; |
| 74 | configs.push_back(util::make_unique<ResourceConfigValue>(defaultConfig, "")); |
| 75 | configs.push_back(util::make_unique<ResourceConfigValue>(landConfig, "")); |
| 76 | configs.push_back(util::make_unique<ResourceConfigValue>(sw600dpLandConfig, "")); |
| 77 | |
| 78 | DominatorTree tree(configs); |
| 79 | PrettyPrinter printer; |
| 80 | |
| 81 | std::string expected = |
| 82 | "<default>\n" |
| 83 | " land\n" |
| 84 | " sw600dp-land-v13\n"; |
| 85 | EXPECT_EQ(expected, printer.toString(&tree)); |
| 86 | } |
| 87 | |
| 88 | TEST(DominatorTreeTest, ProductsAreDominatedSeparately) { |
| 89 | const ConfigDescription defaultConfig = {}; |
| 90 | const ConfigDescription landConfig = test::parseConfigOrDie("land"); |
| 91 | const ConfigDescription sw600dpLandConfig = test::parseConfigOrDie("sw600dp-land-v13"); |
| 92 | |
| 93 | std::vector<std::unique_ptr<ResourceConfigValue>> configs; |
| 94 | configs.push_back(util::make_unique<ResourceConfigValue>(defaultConfig, "")); |
| 95 | configs.push_back(util::make_unique<ResourceConfigValue>(landConfig, "")); |
| 96 | configs.push_back(util::make_unique<ResourceConfigValue>(defaultConfig, "phablet")); |
| 97 | configs.push_back(util::make_unique<ResourceConfigValue>(sw600dpLandConfig, "phablet")); |
| 98 | |
| 99 | DominatorTree tree(configs); |
| 100 | PrettyPrinter printer; |
| 101 | |
| 102 | std::string expected = |
| 103 | "<default>\n" |
| 104 | " land\n" |
| 105 | "<default>\n" |
| 106 | " sw600dp-land-v13\n"; |
| 107 | EXPECT_EQ(expected, printer.toString(&tree)); |
| 108 | } |
| 109 | |
| 110 | TEST(DominatorTreeTest, MoreSpecificConfigurationsAreDominated) { |
| 111 | const ConfigDescription defaultConfig = {}; |
| 112 | const ConfigDescription enConfig = test::parseConfigOrDie("en"); |
| 113 | const ConfigDescription enV21Config = test::parseConfigOrDie("en-v21"); |
| 114 | const ConfigDescription ldrtlConfig = test::parseConfigOrDie("ldrtl-v4"); |
| 115 | const ConfigDescription ldrtlXhdpiConfig = test::parseConfigOrDie("ldrtl-xhdpi-v4"); |
| 116 | const ConfigDescription sw300dpConfig = test::parseConfigOrDie("sw300dp-v13"); |
| 117 | const ConfigDescription sw540dpConfig = test::parseConfigOrDie("sw540dp-v14"); |
| 118 | const ConfigDescription sw600dpConfig = test::parseConfigOrDie("sw600dp-v14"); |
| 119 | const ConfigDescription sw720dpConfig = test::parseConfigOrDie("sw720dp-v13"); |
| 120 | const ConfigDescription v20Config = test::parseConfigOrDie("v20"); |
| 121 | |
| 122 | std::vector<std::unique_ptr<ResourceConfigValue>> configs; |
| 123 | configs.push_back(util::make_unique<ResourceConfigValue>(defaultConfig, "")); |
| 124 | configs.push_back(util::make_unique<ResourceConfigValue>(enConfig, "")); |
| 125 | configs.push_back(util::make_unique<ResourceConfigValue>(enV21Config, "")); |
| 126 | configs.push_back(util::make_unique<ResourceConfigValue>(ldrtlConfig, "")); |
| 127 | configs.push_back(util::make_unique<ResourceConfigValue>(ldrtlXhdpiConfig, "")); |
| 128 | configs.push_back(util::make_unique<ResourceConfigValue>(sw300dpConfig, "")); |
| 129 | configs.push_back(util::make_unique<ResourceConfigValue>(sw540dpConfig, "")); |
| 130 | configs.push_back(util::make_unique<ResourceConfigValue>(sw600dpConfig, "")); |
| 131 | configs.push_back(util::make_unique<ResourceConfigValue>(sw720dpConfig, "")); |
| 132 | configs.push_back(util::make_unique<ResourceConfigValue>(v20Config, "")); |
| 133 | |
| 134 | DominatorTree tree(configs); |
| 135 | PrettyPrinter printer; |
| 136 | |
| 137 | std::string expected = |
| 138 | "<default>\n" |
| 139 | " en\n" |
| 140 | " en-v21\n" |
| 141 | " ldrtl-v4\n" |
| 142 | " ldrtl-xhdpi-v4\n" |
| 143 | " sw300dp-v13\n" |
| 144 | " sw540dp-v14\n" |
| 145 | " sw600dp-v14\n" |
| 146 | " sw720dp-v13\n" |
| 147 | " v20\n"; |
| 148 | EXPECT_EQ(expected, printer.toString(&tree)); |
| 149 | } |
| 150 | |
| 151 | } // namespace aapt |