android_frameworks_base/tools/split-select/RuleGenerator_test.cpp
Adam Lesinski 40e8eefbed First commit of split-select tool
This tool emits a set of rules as JSON for when a Split APK
should match a target device.

Change-Id: I8bfbdfbdb51efcfc645889dd03e1961f16e39645
2014-10-23 10:39:31 -07:00

156 lines
5.9 KiB
C++

/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "RuleGenerator.h"
#include <algorithm>
#include <gtest/gtest.h>
#include <utils/String8.h>
using namespace android;
namespace split {
static void expectDensityRule(const Vector<int>& densities, int density, int greaterThan, int lessThan);
static void expectAbiRule(const Vector<abi::Variant>& abis, abi::Variant variant,
std::initializer_list<const char*> matches);
TEST(RuleGeneratorTest, testAbiRules) {
Vector<abi::Variant> abis;
abis.add(abi::Variant::armeabi);
abis.add(abi::Variant::armeabi_v7a);
abis.add(abi::Variant::x86);
std::sort(abis.begin(), abis.end());
expectAbiRule(abis, abi::Variant::armeabi, {"armeabi"});
expectAbiRule(abis, abi::Variant::armeabi_v7a, {"armeabi-v7a", "arm64-v8a"});
expectAbiRule(abis, abi::Variant::x86, {"x86", "x86_64"});
}
TEST(RuleGeneratorTest, testDensityRules) {
Vector<int> densities;
densities.add(ConfigDescription::DENSITY_HIGH);
densities.add(ConfigDescription::DENSITY_XHIGH);
densities.add(ConfigDescription::DENSITY_XXHIGH);
densities.add(ConfigDescription::DENSITY_ANY);
ASSERT_LT(263, ConfigDescription::DENSITY_XHIGH);
ASSERT_GT(262, ConfigDescription::DENSITY_HIGH);
ASSERT_LT(363, ConfigDescription::DENSITY_XXHIGH);
ASSERT_GT(362, ConfigDescription::DENSITY_XHIGH);
expectDensityRule(densities, ConfigDescription::DENSITY_HIGH, 0, 263);
expectDensityRule(densities, ConfigDescription::DENSITY_XHIGH, 262, 363);
expectDensityRule(densities, ConfigDescription::DENSITY_XXHIGH, 362, 0);
expectDensityRule(densities, ConfigDescription::DENSITY_ANY, 0, 0);
}
//
// Helper methods.
//
static void expectDensityRule(const Vector<int>& densities, int density, int greaterThan, int lessThan) {
const int* iter = std::find(densities.begin(), densities.end(), density);
if (densities.end() == iter) {
ADD_FAILURE() << density << "dpi was not in the density list.";
return;
}
sp<Rule> rule = RuleGenerator::generateDensity(densities, iter - densities.begin());
if (rule->op != Rule::AND_SUBRULES) {
ADD_FAILURE() << "Op in rule for " << density << "dpi is not Rule::AND_SUBRULES.";
return;
}
size_t index = 0;
bool isAnyDpi = density == ConfigDescription::DENSITY_ANY;
sp<Rule> anyDpiRule = rule->subrules[index++];
EXPECT_EQ(Rule::EQUALS, anyDpiRule->op)
<< "for " << density << "dpi ANY DPI rule";
EXPECT_EQ(Rule::SCREEN_DENSITY, anyDpiRule->key)
<< "for " << density << "dpi ANY DPI rule";
EXPECT_EQ(isAnyDpi == false, anyDpiRule->negate)
<< "for " << density << "dpi ANY DPI rule";
if (anyDpiRule->longArgs.size() == 1) {
EXPECT_EQ(ConfigDescription::DENSITY_ANY, anyDpiRule->longArgs[0])
<< "for " << density << "dpi ANY DPI rule";
} else {
EXPECT_EQ(1u, anyDpiRule->longArgs.size())
<< "for " << density << "dpi ANY DPI rule";
}
if (greaterThan != 0) {
sp<Rule> greaterThanRule = rule->subrules[index++];
EXPECT_EQ(Rule::GREATER_THAN, greaterThanRule->op)
<< "for " << density << "dpi GREATER_THAN rule";
EXPECT_EQ(Rule::SCREEN_DENSITY, greaterThanRule->key)
<< "for " << density << "dpi GREATER_THAN rule";
if (greaterThanRule->longArgs.size() == 1) {
EXPECT_EQ(greaterThan, greaterThanRule->longArgs[0])
<< "for " << density << "dpi GREATER_THAN rule";
} else {
EXPECT_EQ(1u, greaterThanRule->longArgs.size())
<< "for " << density << "dpi GREATER_THAN rule";
}
}
if (lessThan != 0) {
sp<Rule> lessThanRule = rule->subrules[index++];
EXPECT_EQ(Rule::LESS_THAN, lessThanRule->op)
<< "for " << density << "dpi LESS_THAN rule";
EXPECT_EQ(Rule::SCREEN_DENSITY, lessThanRule->key)
<< "for " << density << "dpi LESS_THAN rule";
if (lessThanRule->longArgs.size() == 1) {
EXPECT_EQ(lessThan, lessThanRule->longArgs[0])
<< "for " << density << "dpi LESS_THAN rule";
} else {
EXPECT_EQ(1u, lessThanRule->longArgs.size())
<< "for " << density << "dpi LESS_THAN rule";
}
}
}
static void expectAbiRule(const Vector<abi::Variant>& abis, abi::Variant variant,
std::initializer_list<const char*> matches) {
const abi::Variant* iter = std::find(abis.begin(), abis.end(), variant);
if (abis.end() == iter) {
ADD_FAILURE() << abi::toString(variant) << " was not in the abi list.";
return;
}
sp<Rule> rule = RuleGenerator::generateAbi(abis, iter - abis.begin());
EXPECT_EQ(Rule::CONTAINS_ANY, rule->op)
<< "for " << abi::toString(variant) << " rule";
EXPECT_EQ(Rule::NATIVE_PLATFORM, rule->key)
<< " for " << abi::toString(variant) << " rule";
EXPECT_EQ(matches.size(), rule->stringArgs.size())
<< " for " << abi::toString(variant) << " rule";
for (const char* match : matches) {
if (rule->stringArgs.end() ==
std::find(rule->stringArgs.begin(), rule->stringArgs.end(), String8(match))) {
ADD_FAILURE() << "Rule for abi " << abi::toString(variant)
<< " does not contain match for expected abi " << match;
}
}
}
} // namespace split