2 * Copyright (C) 2014 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "SplitDescription.h"
21 #include <gtest/gtest.h>
22 #include <utils/String8.h>
23 #include <utils/Vector.h>
25 using namespace android;
29 class GrouperTest : public ::testing::Test {
31 virtual void SetUp() {
32 Vector<SplitDescription> splits;
33 addSplit(splits, "en-rUS-sw600dp-hdpi");
34 addSplit(splits, "fr-rFR-sw600dp-hdpi");
35 addSplit(splits, "fr-rFR-sw600dp-xhdpi");
36 addSplit(splits, ":armeabi");
37 addSplit(splits, "en-rUS-sw300dp-xhdpi");
38 addSplit(splits, "large");
39 addSplit(splits, "pl-rPL");
40 addSplit(splits, "fr-rCA");
41 addSplit(splits, "fr");
42 addSplit(splits, "xlarge");
43 addSplit(splits, "en-rUS-sw600dp-xhdpi");
44 addSplit(splits, "en-rUS-sw300dp-hdpi");
45 addSplit(splits, "xxhdpi");
46 addSplit(splits, "hdpi");
47 addSplit(splits, "de-rDE");
48 addSplit(splits, "xhdpi");
49 addSplit(splits, ":x86");
50 addSplit(splits, "anydpi");
51 addSplit(splits, "v7");
52 addSplit(splits, "v8");
53 addSplit(splits, "sw600dp");
54 addSplit(splits, "sw300dp");
55 mGroups = groupByMutualExclusivity(splits);
58 void addSplit(Vector<SplitDescription>& splits, const char* str);
59 void expectHasGroupWithSplits(const char* a);
60 void expectHasGroupWithSplits(const char* a, const char* b);
61 void expectHasGroupWithSplits(const char* a, const char* b, const char* c);
62 void expectHasGroupWithSplits(const char* a, const char* b, const char* c, const char* d);
63 void expectHasGroupWithSplits(const Vector<const char*>& expectedStrs);
65 Vector<SortedVector<SplitDescription> > mGroups;
68 TEST_F(GrouperTest, shouldHaveCorrectNumberOfGroups) {
69 EXPECT_EQ(15u, mGroups.size());
72 TEST_F(GrouperTest, shouldGroupDensities) {
73 expectHasGroupWithSplits("en-rUS-sw300dp-hdpi", "en-rUS-sw300dp-xhdpi");
74 expectHasGroupWithSplits("en-rUS-sw600dp-hdpi", "en-rUS-sw600dp-xhdpi");
75 expectHasGroupWithSplits("fr-rFR-sw600dp-hdpi", "fr-rFR-sw600dp-xhdpi");
76 expectHasGroupWithSplits("hdpi", "xhdpi", "xxhdpi", "anydpi");
79 TEST_F(GrouperTest, shouldGroupAbi) {
80 expectHasGroupWithSplits(":armeabi", ":x86");
83 TEST_F(GrouperTest, shouldGroupLocale) {
84 expectHasGroupWithSplits("pl-rPL");
85 expectHasGroupWithSplits("de-rDE");
86 expectHasGroupWithSplits("fr");
87 expectHasGroupWithSplits("fr-rCA");
90 TEST_F(GrouperTest, shouldGroupEachSplitIntoItsOwnGroup) {
91 expectHasGroupWithSplits("large");
92 expectHasGroupWithSplits("xlarge");
93 expectHasGroupWithSplits("v7");
94 expectHasGroupWithSplits("v8");
95 expectHasGroupWithSplits("sw600dp");
96 expectHasGroupWithSplits("sw300dp");
103 void GrouperTest::expectHasGroupWithSplits(const char* a) {
104 Vector<const char*> expected;
106 expectHasGroupWithSplits(expected);
109 void GrouperTest::expectHasGroupWithSplits(const char* a, const char* b) {
110 Vector<const char*> expected;
113 expectHasGroupWithSplits(expected);
116 void GrouperTest::expectHasGroupWithSplits(const char* a, const char* b, const char* c) {
117 Vector<const char*> expected;
121 expectHasGroupWithSplits(expected);
124 void GrouperTest::expectHasGroupWithSplits(const char* a, const char* b, const char* c, const char* d) {
125 Vector<const char*> expected;
130 expectHasGroupWithSplits(expected);
133 void GrouperTest::expectHasGroupWithSplits(const Vector<const char*>& expectedStrs) {
134 Vector<SplitDescription> splits;
135 const size_t expectedStrCount = expectedStrs.size();
136 for (size_t i = 0; i < expectedStrCount; i++) {
138 if (!SplitDescription::parse(String8(expectedStrs[i]), &splits.editTop())) {
139 ADD_FAILURE() << "Failed to parse SplitDescription " << expectedStrs[i];
143 const size_t splitCount = splits.size();
145 const size_t groupCount = mGroups.size();
146 for (size_t i = 0; i < groupCount; i++) {
147 const SortedVector<SplitDescription>& group = mGroups[i];
148 if (group.size() != splitCount) {
153 for (size_t j = 0; j < splitCount; j++) {
154 if (group.indexOf(splits[j]) >= 0) {
159 if (found == splitCount) {
164 String8 errorMessage("Failed to find expected group [");
165 for (size_t i = 0; i < splitCount; i++) {
167 errorMessage.append(", ");
169 errorMessage.append(splits[i].toString());
171 errorMessage.append("].\nActual:\n");
173 for (size_t i = 0; i < groupCount; i++) {
174 errorMessage.appendFormat("Group %d:\n", int(i + 1));
175 const SortedVector<SplitDescription>& group = mGroups[i];
176 for (size_t j = 0; j < group.size(); j++) {
177 errorMessage.append(" ");
178 errorMessage.append(group[j].toString());
179 errorMessage.append("\n");
182 ADD_FAILURE() << errorMessage.string();
185 void GrouperTest::addSplit(Vector<SplitDescription>& splits, const char* str) {
187 EXPECT_TRUE(SplitDescription::parse(String8(str), &splits.editTop()));