Grouper_test.cpp 6.0 KB

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