Adam Lesinski | 40e8eef | 2014-09-16 14:43:29 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 "RuleGenerator.h" |
| 18 | |
| 19 | #include <algorithm> |
| 20 | #include <gtest/gtest.h> |
| 21 | #include <utils/String8.h> |
| 22 | |
| 23 | using namespace android; |
| 24 | |
| 25 | namespace split { |
| 26 | |
| 27 | static void expectDensityRule(const Vector<int>& densities, int density, int greaterThan, int lessThan); |
| 28 | static void expectAbiRule(const Vector<abi::Variant>& abis, abi::Variant variant, |
| 29 | std::initializer_list<const char*> matches); |
| 30 | |
| 31 | TEST(RuleGeneratorTest, testAbiRules) { |
| 32 | Vector<abi::Variant> abis; |
| 33 | abis.add(abi::Variant::armeabi); |
| 34 | abis.add(abi::Variant::armeabi_v7a); |
| 35 | abis.add(abi::Variant::x86); |
| 36 | std::sort(abis.begin(), abis.end()); |
| 37 | |
| 38 | expectAbiRule(abis, abi::Variant::armeabi, {"armeabi"}); |
| 39 | expectAbiRule(abis, abi::Variant::armeabi_v7a, {"armeabi-v7a", "arm64-v8a"}); |
| 40 | expectAbiRule(abis, abi::Variant::x86, {"x86", "x86_64"}); |
| 41 | } |
| 42 | |
| 43 | TEST(RuleGeneratorTest, testDensityRules) { |
| 44 | Vector<int> densities; |
| 45 | densities.add(ConfigDescription::DENSITY_HIGH); |
| 46 | densities.add(ConfigDescription::DENSITY_XHIGH); |
| 47 | densities.add(ConfigDescription::DENSITY_XXHIGH); |
| 48 | densities.add(ConfigDescription::DENSITY_ANY); |
| 49 | |
| 50 | ASSERT_LT(263, ConfigDescription::DENSITY_XHIGH); |
| 51 | ASSERT_GT(262, ConfigDescription::DENSITY_HIGH); |
| 52 | ASSERT_LT(363, ConfigDescription::DENSITY_XXHIGH); |
| 53 | ASSERT_GT(362, ConfigDescription::DENSITY_XHIGH); |
| 54 | |
| 55 | expectDensityRule(densities, ConfigDescription::DENSITY_HIGH, 0, 263); |
| 56 | expectDensityRule(densities, ConfigDescription::DENSITY_XHIGH, 262, 363); |
| 57 | expectDensityRule(densities, ConfigDescription::DENSITY_XXHIGH, 362, 0); |
| 58 | expectDensityRule(densities, ConfigDescription::DENSITY_ANY, 0, 0); |
| 59 | } |
| 60 | |
| 61 | // |
| 62 | // Helper methods. |
| 63 | // |
| 64 | |
| 65 | static void expectDensityRule(const Vector<int>& densities, int density, int greaterThan, int lessThan) { |
| 66 | const int* iter = std::find(densities.begin(), densities.end(), density); |
| 67 | if (densities.end() == iter) { |
| 68 | ADD_FAILURE() << density << "dpi was not in the density list."; |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | sp<Rule> rule = RuleGenerator::generateDensity(densities, iter - densities.begin()); |
| 73 | if (rule->op != Rule::AND_SUBRULES) { |
| 74 | ADD_FAILURE() << "Op in rule for " << density << "dpi is not Rule::AND_SUBRULES."; |
| 75 | return; |
| 76 | } |
| 77 | |
| 78 | size_t index = 0; |
| 79 | |
| 80 | bool isAnyDpi = density == ConfigDescription::DENSITY_ANY; |
| 81 | |
| 82 | sp<Rule> anyDpiRule = rule->subrules[index++]; |
| 83 | EXPECT_EQ(Rule::EQUALS, anyDpiRule->op) |
| 84 | << "for " << density << "dpi ANY DPI rule"; |
| 85 | EXPECT_EQ(Rule::SCREEN_DENSITY, anyDpiRule->key) |
| 86 | << "for " << density << "dpi ANY DPI rule"; |
| 87 | EXPECT_EQ(isAnyDpi == false, anyDpiRule->negate) |
| 88 | << "for " << density << "dpi ANY DPI rule"; |
| 89 | if (anyDpiRule->longArgs.size() == 1) { |
| 90 | EXPECT_EQ(ConfigDescription::DENSITY_ANY, anyDpiRule->longArgs[0]) |
| 91 | << "for " << density << "dpi ANY DPI rule"; |
| 92 | } else { |
| 93 | EXPECT_EQ(1u, anyDpiRule->longArgs.size()) |
| 94 | << "for " << density << "dpi ANY DPI rule"; |
| 95 | } |
| 96 | |
| 97 | |
| 98 | if (greaterThan != 0) { |
| 99 | sp<Rule> greaterThanRule = rule->subrules[index++]; |
| 100 | EXPECT_EQ(Rule::GREATER_THAN, greaterThanRule->op) |
| 101 | << "for " << density << "dpi GREATER_THAN rule"; |
| 102 | EXPECT_EQ(Rule::SCREEN_DENSITY, greaterThanRule->key) |
| 103 | << "for " << density << "dpi GREATER_THAN rule"; |
| 104 | if (greaterThanRule->longArgs.size() == 1) { |
| 105 | EXPECT_EQ(greaterThan, greaterThanRule->longArgs[0]) |
| 106 | << "for " << density << "dpi GREATER_THAN rule"; |
| 107 | } else { |
| 108 | EXPECT_EQ(1u, greaterThanRule->longArgs.size()) |
| 109 | << "for " << density << "dpi GREATER_THAN rule"; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | if (lessThan != 0) { |
| 114 | sp<Rule> lessThanRule = rule->subrules[index++]; |
| 115 | EXPECT_EQ(Rule::LESS_THAN, lessThanRule->op) |
| 116 | << "for " << density << "dpi LESS_THAN rule"; |
| 117 | EXPECT_EQ(Rule::SCREEN_DENSITY, lessThanRule->key) |
| 118 | << "for " << density << "dpi LESS_THAN rule"; |
| 119 | if (lessThanRule->longArgs.size() == 1) { |
| 120 | EXPECT_EQ(lessThan, lessThanRule->longArgs[0]) |
| 121 | << "for " << density << "dpi LESS_THAN rule"; |
| 122 | } else { |
| 123 | EXPECT_EQ(1u, lessThanRule->longArgs.size()) |
| 124 | << "for " << density << "dpi LESS_THAN rule"; |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | static void expectAbiRule(const Vector<abi::Variant>& abis, abi::Variant variant, |
| 130 | std::initializer_list<const char*> matches) { |
| 131 | const abi::Variant* iter = std::find(abis.begin(), abis.end(), variant); |
| 132 | if (abis.end() == iter) { |
| 133 | ADD_FAILURE() << abi::toString(variant) << " was not in the abi list."; |
| 134 | return; |
| 135 | } |
| 136 | |
| 137 | sp<Rule> rule = RuleGenerator::generateAbi(abis, iter - abis.begin()); |
| 138 | |
| 139 | EXPECT_EQ(Rule::CONTAINS_ANY, rule->op) |
| 140 | << "for " << abi::toString(variant) << " rule"; |
| 141 | EXPECT_EQ(Rule::NATIVE_PLATFORM, rule->key) |
| 142 | << " for " << abi::toString(variant) << " rule"; |
| 143 | EXPECT_EQ(matches.size(), rule->stringArgs.size()) |
| 144 | << " for " << abi::toString(variant) << " rule"; |
| 145 | |
| 146 | for (const char* match : matches) { |
| 147 | if (rule->stringArgs.end() == |
| 148 | std::find(rule->stringArgs.begin(), rule->stringArgs.end(), String8(match))) { |
| 149 | ADD_FAILURE() << "Rule for abi " << abi::toString(variant) |
| 150 | << " does not contain match for expected abi " << match; |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | } // namespace split |