blob: 4d146cdd8ed1d9076113b494c1c468c777400447 [file] [log] [blame]
Adam Lesinski40e8eef2014-09-16 14:43:29 -07001/*
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 "Grouper.h"
18
19#include "SplitDescription.h"
20
21#include <gtest/gtest.h>
22#include <initializer_list>
23#include <utils/String8.h>
24#include <utils/Vector.h>
25
26using namespace android;
27
28namespace split {
29
30class GrouperTest : public ::testing::Test {
31protected:
32 virtual void SetUp() {
33 Vector<SplitDescription> splits;
34 addSplit(splits, "en-rUS-sw600dp-hdpi");
35 addSplit(splits, "fr-rFR-sw600dp-hdpi");
36 addSplit(splits, "fr-rFR-sw600dp-xhdpi");
37 addSplit(splits, ":armeabi");
38 addSplit(splits, "en-rUS-sw300dp-xhdpi");
39 addSplit(splits, "large");
40 addSplit(splits, "pl-rPL");
41 addSplit(splits, "xlarge");
42 addSplit(splits, "en-rUS-sw600dp-xhdpi");
43 addSplit(splits, "en-rUS-sw300dp-hdpi");
44 addSplit(splits, "xxhdpi");
45 addSplit(splits, "hdpi");
46 addSplit(splits, "de-rDE");
47 addSplit(splits, "xhdpi");
48 addSplit(splits, ":x86");
49 addSplit(splits, "anydpi");
50 addSplit(splits, "v7");
51 addSplit(splits, "v8");
52 addSplit(splits, "sw600dp");
53 addSplit(splits, "sw300dp");
54 mGroups = groupByMutualExclusivity(splits);
55 }
56
57 void addSplit(Vector<SplitDescription>& splits, const char* str);
58 void expectHasGroupWithSplits(std::initializer_list<const char*> l);
59
60 Vector<SortedVector<SplitDescription> > mGroups;
61};
62
63TEST_F(GrouperTest, shouldHaveCorrectNumberOfGroups) {
64 EXPECT_EQ(12u, mGroups.size());
65}
66
67TEST_F(GrouperTest, shouldGroupDensities) {
68 expectHasGroupWithSplits({"en-rUS-sw300dp-hdpi", "en-rUS-sw300dp-xhdpi"});
69 expectHasGroupWithSplits({"en-rUS-sw600dp-hdpi", "en-rUS-sw600dp-xhdpi"});
70 expectHasGroupWithSplits({"fr-rFR-sw600dp-hdpi", "fr-rFR-sw600dp-xhdpi"});
71 expectHasGroupWithSplits({"hdpi", "xhdpi", "xxhdpi", "anydpi"});
72}
73
74TEST_F(GrouperTest, shouldGroupAbi) {
75 expectHasGroupWithSplits({":armeabi", ":x86"});
76}
77
78TEST_F(GrouperTest, shouldGroupLocale) {
79 expectHasGroupWithSplits({"pl-rPL", "de-rDE"});
80}
81
82TEST_F(GrouperTest, shouldGroupEachSplitIntoItsOwnGroup) {
83 expectHasGroupWithSplits({"large"});
84 expectHasGroupWithSplits({"xlarge"});
85 expectHasGroupWithSplits({"v7"});
86 expectHasGroupWithSplits({"v8"});
87 expectHasGroupWithSplits({"sw600dp"});
88 expectHasGroupWithSplits({"sw300dp"});
89}
90
91//
92// Helper methods
93//
94
95void GrouperTest::expectHasGroupWithSplits(std::initializer_list<const char*> l) {
96 Vector<SplitDescription> splits;
97 for (const char* str : l) {
98 splits.add();
99 if (!SplitDescription::parse(String8(str), &splits.editTop())) {
100 ADD_FAILURE() << "Failed to parse SplitDescription " << str;
101 return;
102 }
103 }
104 const size_t splitCount = splits.size();
105
106 const size_t groupCount = mGroups.size();
107 for (size_t i = 0; i < groupCount; i++) {
108 const SortedVector<SplitDescription>& group = mGroups[i];
109 if (group.size() != splitCount) {
110 continue;
111 }
112
113 size_t found = 0;
114 for (size_t j = 0; j < splitCount; j++) {
115 if (group.indexOf(splits[j]) >= 0) {
116 found++;
117 }
118 }
119
120 if (found == splitCount) {
121 return;
122 }
123 }
124
125 String8 errorMessage("Failed to find expected group [");
126 for (size_t i = 0; i < splitCount; i++) {
127 if (i != 0) {
128 errorMessage.append(", ");
129 }
130 errorMessage.append(splits[i].toString());
131 }
132 errorMessage.append("].\nActual:\n");
133
134 for (size_t i = 0; i < groupCount; i++) {
135 errorMessage.appendFormat("Group %d:\n", int(i + 1));
136 const SortedVector<SplitDescription>& group = mGroups[i];
137 for (size_t j = 0; j < group.size(); j++) {
138 errorMessage.append(" ");
139 errorMessage.append(group[j].toString());
140 errorMessage.append("\n");
141 }
142 }
143 ADD_FAILURE() << errorMessage.string();
144}
145
146void GrouperTest::addSplit(Vector<SplitDescription>& splits, const char* str) {
147 splits.add();
148 EXPECT_TRUE(SplitDescription::parse(String8(str), &splits.editTop()));
149}
150
151} // namespace split