Home | History | Annotate | Download | only in optimize
      1 /*
      2  * Copyright (C) 2016 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 "optimize/ResourceDeduper.h"
     18 
     19 #include "ResourceTable.h"
     20 #include "test/Test.h"
     21 
     22 namespace aapt {
     23 
     24 TEST(ResourceDeduperTest, SameValuesAreDeduped) {
     25   std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
     26   const ConfigDescription default_config = {};
     27   const ConfigDescription en_config = test::ParseConfigOrDie("en");
     28   const ConfigDescription en_v21_config = test::ParseConfigOrDie("en-v21");
     29   // Chosen because this configuration is compatible with en.
     30   const ConfigDescription land_config = test::ParseConfigOrDie("land");
     31 
     32   std::unique_ptr<ResourceTable> table =
     33       test::ResourceTableBuilder()
     34           .AddString("android:string/dedupe", ResourceId{}, default_config,
     35                      "dedupe")
     36           .AddString("android:string/dedupe", ResourceId{}, en_config, "dedupe")
     37           .AddString("android:string/dedupe", ResourceId{}, land_config,
     38                      "dedupe")
     39           .AddString("android:string/dedupe2", ResourceId{}, default_config,
     40                      "dedupe")
     41           .AddString("android:string/dedupe2", ResourceId{}, en_config,
     42                      "dedupe")
     43           .AddString("android:string/dedupe2", ResourceId{}, en_v21_config,
     44                      "keep")
     45           .AddString("android:string/dedupe2", ResourceId{}, land_config,
     46                      "dedupe")
     47           .Build();
     48 
     49   ASSERT_TRUE(ResourceDeduper().Consume(context.get(), table.get()));
     50   EXPECT_EQ(nullptr, test::GetValueForConfig<String>(
     51                          table.get(), "android:string/dedupe", en_config));
     52   EXPECT_EQ(nullptr, test::GetValueForConfig<String>(
     53                          table.get(), "android:string/dedupe", land_config));
     54   EXPECT_EQ(nullptr, test::GetValueForConfig<String>(
     55                          table.get(), "android:string/dedupe2", en_config));
     56   EXPECT_NE(nullptr, test::GetValueForConfig<String>(
     57                          table.get(), "android:string/dedupe2", en_v21_config));
     58 }
     59 
     60 TEST(ResourceDeduperTest, DifferentValuesAreKept) {
     61   std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
     62   const ConfigDescription default_config = {};
     63   const ConfigDescription en_config = test::ParseConfigOrDie("en");
     64   const ConfigDescription en_v21_config = test::ParseConfigOrDie("en-v21");
     65   // Chosen because this configuration is compatible with en.
     66   const ConfigDescription land_config = test::ParseConfigOrDie("land");
     67 
     68   std::unique_ptr<ResourceTable> table =
     69       test::ResourceTableBuilder()
     70           .AddString("android:string/keep", ResourceId{}, default_config,
     71                      "keep")
     72           .AddString("android:string/keep", ResourceId{}, en_config, "keep")
     73           .AddString("android:string/keep", ResourceId{}, en_v21_config,
     74                      "keep2")
     75           .AddString("android:string/keep", ResourceId{}, land_config, "keep2")
     76           .Build();
     77 
     78   ASSERT_TRUE(ResourceDeduper().Consume(context.get(), table.get()));
     79   EXPECT_NE(nullptr, test::GetValueForConfig<String>(
     80                          table.get(), "android:string/keep", en_config));
     81   EXPECT_NE(nullptr, test::GetValueForConfig<String>(
     82                          table.get(), "android:string/keep", en_v21_config));
     83   EXPECT_NE(nullptr, test::GetValueForConfig<String>(
     84                          table.get(), "android:string/keep", land_config));
     85 }
     86 
     87 }  // namespace aapt
     88