Always select the next higher density bucket when picking resources

Test: ran and updated Config_test.cpp
Fix: 183136881
Change-Id: Ife1575caedb2cf3fbc2d3659c34a52e1207575c6
diff --git a/libs/androidfw/tests/Config_test.cpp b/libs/androidfw/tests/Config_test.cpp
index b54915f..f5fd0f2 100644
--- a/libs/androidfw/tests/Config_test.cpp
+++ b/libs/androidfw/tests/Config_test.cpp
@@ -27,15 +27,19 @@
 
 static ResTable_config selectBest(const ResTable_config& target,
                                   const Vector<ResTable_config>& configs) {
-  ResTable_config bestConfig;
-  memset(&bestConfig, 0, sizeof(bestConfig));
+  Vector<ResTable_config> matchedConfigs;
   const size_t configCount = configs.size();
   for (size_t i = 0; i < configCount; i++) {
     const ResTable_config& thisConfig = configs[i];
-    if (!thisConfig.match(target)) {
-      continue;
+    if (thisConfig.match(target)) {
+      matchedConfigs.add(thisConfig);
     }
+  }
 
+  ResTable_config bestConfig = matchedConfigs[0];
+  const size_t matchingConfigCount = matchedConfigs.size();
+  for (size_t i = 1; i < matchingConfigCount; i++) {
+    const ResTable_config& thisConfig = configs[i];
     if (thisConfig.isBetterThan(bestConfig, &target)) {
       bestConfig = thisConfig;
     }
@@ -75,6 +79,9 @@
   configs.add(buildDensityConfig(int(ResTable_config::DENSITY_HIGH) + 20));
   ASSERT_EQ(expectedBest, selectBest(deviceConfig, configs));
 
+  configs.add(buildDensityConfig(int(ResTable_config::DENSITY_XHIGH) - 1));
+  ASSERT_EQ(expectedBest, selectBest(deviceConfig, configs));
+
   expectedBest = buildDensityConfig(ResTable_config::DENSITY_XHIGH);
   configs.add(expectedBest);
   ASSERT_EQ(expectedBest, selectBest(deviceConfig, configs));