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 "aapt/SdkConstants.h"
20 #include "TestRules.h"
21 
22 #include <gtest/gtest.h>
23 #include <utils/Vector.h>
24 
25 using namespace android;
26 using namespace split::test;
27 
28 namespace split {
29 
TEST(RuleGeneratorTest,testAbiRules)30 TEST(RuleGeneratorTest, testAbiRules) {
31     Vector<abi::Variant> abis;
32     const ssize_t armeabiIndex = abis.add(abi::Variant_armeabi);
33     const ssize_t armeabi_v7aIndex = abis.add(abi::Variant_armeabi_v7a);
34     const ssize_t x86Index = abis.add(abi::Variant_x86);
35 
36     EXPECT_RULES_EQ(RuleGenerator::generateAbi(abis, armeabiIndex),
37             ContainsAnyRule(Rule::NATIVE_PLATFORM, "armeabi")
38     );
39 
40     EXPECT_RULES_EQ(RuleGenerator::generateAbi(abis, armeabi_v7aIndex),
41             ContainsAnyRule(Rule::NATIVE_PLATFORM, "armeabi-v7a", "arm64-v8a")
42     );
43 
44     EXPECT_RULES_EQ(RuleGenerator::generateAbi(abis, x86Index),
45             ContainsAnyRule(Rule::NATIVE_PLATFORM, "x86", "x86_64")
46     );
47 }
48 
TEST(RuleGeneratorTest,densityConstantsAreSane)49 TEST(RuleGeneratorTest, densityConstantsAreSane) {
50     EXPECT_LT(263, (int) ConfigDescription::DENSITY_XHIGH);
51     EXPECT_GT(262, (int) ConfigDescription::DENSITY_HIGH);
52     EXPECT_LT(363, (int) ConfigDescription::DENSITY_XXHIGH);
53     EXPECT_GT(362, (int) ConfigDescription::DENSITY_XHIGH);
54 }
55 
TEST(RuleGeneratorTest,testDensityRules)56 TEST(RuleGeneratorTest, testDensityRules) {
57     Vector<int> densities;
58     const ssize_t highIndex = densities.add(ConfigDescription::DENSITY_HIGH);
59     const ssize_t xhighIndex = densities.add(ConfigDescription::DENSITY_XHIGH);
60     const ssize_t xxhighIndex = densities.add(ConfigDescription::DENSITY_XXHIGH);
61 
62     EXPECT_RULES_EQ(RuleGenerator::generateDensity(densities, highIndex),
63             AndRule()
64             .add(LtRule(Rule::SCREEN_DENSITY, 263))
65     );
66 
67     EXPECT_RULES_EQ(RuleGenerator::generateDensity(densities, xhighIndex),
68             AndRule()
69             .add(GtRule(Rule::SCREEN_DENSITY, 262))
70             .add(LtRule(Rule::SCREEN_DENSITY, 363))
71     );
72 
73     EXPECT_RULES_EQ(RuleGenerator::generateDensity(densities, xxhighIndex),
74             AndRule()
75             .add(GtRule(Rule::SCREEN_DENSITY, 362))
76     );
77 }
78 
TEST(RuleGeneratorTest,testDensityRulesWithAnyDpi)79 TEST(RuleGeneratorTest, testDensityRulesWithAnyDpi) {
80     Vector<int> densities;
81     const ssize_t highIndex = densities.add(ConfigDescription::DENSITY_HIGH);
82     const ssize_t xhighIndex = densities.add(ConfigDescription::DENSITY_XHIGH);
83     const ssize_t xxhighIndex = densities.add(ConfigDescription::DENSITY_XXHIGH);
84     const ssize_t anyIndex = densities.add(ConfigDescription::DENSITY_ANY);
85 
86     EXPECT_RULES_EQ(RuleGenerator::generateDensity(densities, highIndex),
87             AndRule()
88             .add(LtRule(Rule::SDK_VERSION, SDK_LOLLIPOP))
89             .add(LtRule(Rule::SCREEN_DENSITY, 263))
90     );
91 
92     EXPECT_RULES_EQ(RuleGenerator::generateDensity(densities, xhighIndex),
93             AndRule()
94             .add(LtRule(Rule::SDK_VERSION, SDK_LOLLIPOP))
95             .add(GtRule(Rule::SCREEN_DENSITY, 262))
96             .add(LtRule(Rule::SCREEN_DENSITY, 363))
97     );
98 
99     EXPECT_RULES_EQ(RuleGenerator::generateDensity(densities, xxhighIndex),
100             AndRule()
101             .add(LtRule(Rule::SDK_VERSION, SDK_LOLLIPOP))
102             .add(GtRule(Rule::SCREEN_DENSITY, 362))
103     );
104 
105     // We expect AlwaysTrue because anydpi always has attached v21 to the configuration
106     // and the rest of the rule generation code generates the sdk version checks.
107     EXPECT_RULES_EQ(RuleGenerator::generateDensity(densities, anyIndex), AlwaysTrue());
108 }
109 
110 } // namespace split
111