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 <gtest/gtest.h>
18 #include <utils/String8.h>
19 #include <utils/Vector.h>
20 
21 #include "SplitDescription.h"
22 #include "SplitSelector.h"
23 #include "TestRules.h"
24 
25 namespace split {
26 
27 using namespace android;
28 
addSplit(Vector<SplitDescription> & splits,const char * str)29 static ::testing::AssertionResult addSplit(Vector<SplitDescription>& splits, const char* str) {
30     SplitDescription split;
31     if (!SplitDescription::parse(String8(str), &split)) {
32         return ::testing::AssertionFailure() << str << " is not a valid configuration.";
33     }
34     splits.add(split);
35     return ::testing::AssertionSuccess();
36 }
37 
TEST(SplitSelectorTest,rulesShouldMatchSelection)38 TEST(SplitSelectorTest, rulesShouldMatchSelection) {
39     Vector<SplitDescription> splits;
40     ASSERT_TRUE(addSplit(splits, "hdpi"));
41     ASSERT_TRUE(addSplit(splits, "xhdpi"));
42     ASSERT_TRUE(addSplit(splits, "xxhdpi"));
43     ASSERT_TRUE(addSplit(splits, "mdpi"));
44 
45     SplitDescription targetSplit;
46     ASSERT_TRUE(SplitDescription::parse(String8("hdpi"), &targetSplit));
47 
48     SplitSelector selector(splits);
49     SortedVector<SplitDescription> bestSplits;
50     bestSplits.merge(selector.getBestSplits(targetSplit));
51 
52     SplitDescription expected;
53     ASSERT_TRUE(SplitDescription::parse(String8("hdpi"), &expected));
54     EXPECT_GE(bestSplits.indexOf(expected), 0);
55 
56     KeyedVector<SplitDescription, sp<Rule> > rules = selector.getRules();
57     ssize_t idx = rules.indexOfKey(expected);
58     ASSERT_GE(idx, 0);
59     sp<Rule> rule = rules[idx];
60     ASSERT_TRUE(rule != NULL);
61 
62     ASSERT_GT(ResTable_config::DENSITY_HIGH, 180);
63     ASSERT_LT(ResTable_config::DENSITY_HIGH, 263);
64 
65     Rule expectedRule(test::AndRule()
66             .add(test::GtRule(Rule::SDK_VERSION, 3))
67             .add(test::GtRule(Rule::SCREEN_DENSITY, 180))
68             .add(test::LtRule(Rule::SCREEN_DENSITY, 263)));
69     EXPECT_RULES_EQ(rule, expectedRule);
70 }
71 
72 } // namespace split
73