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 using ::aapt::test::HasValue;
     23 using ::testing::Not;
     24 
     25 namespace aapt {
     26 
     27 TEST(ResourceDeduperTest, SameValuesAreDeduped) {
     28   std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
     29   const ConfigDescription default_config = {};
     30   const ConfigDescription ldrtl_config = test::ParseConfigOrDie("ldrtl");
     31   const ConfigDescription ldrtl_v21_config = test::ParseConfigOrDie("ldrtl-v21");
     32   const ConfigDescription en_config = test::ParseConfigOrDie("en");
     33   const ConfigDescription en_v21_config = test::ParseConfigOrDie("en-v21");
     34   // Chosen because this configuration is compatible with ldrtl/en.
     35   const ConfigDescription land_config = test::ParseConfigOrDie("land");
     36 
     37   std::unique_ptr<ResourceTable> table =
     38       test::ResourceTableBuilder()
     39           .AddString("android:string/dedupe", ResourceId{}, default_config, "dedupe")
     40           .AddString("android:string/dedupe", ResourceId{}, ldrtl_config, "dedupe")
     41           .AddString("android:string/dedupe", ResourceId{}, land_config, "dedupe")
     42 
     43           .AddString("android:string/dedupe2", ResourceId{}, default_config, "dedupe")
     44           .AddString("android:string/dedupe2", ResourceId{}, ldrtl_config, "dedupe")
     45           .AddString("android:string/dedupe2", ResourceId{}, ldrtl_v21_config, "keep")
     46           .AddString("android:string/dedupe2", ResourceId{}, land_config, "dedupe")
     47 
     48           .AddString("android:string/dedupe3", ResourceId{}, default_config, "dedupe")
     49           .AddString("android:string/dedupe3", ResourceId{}, en_config, "dedupe")
     50           .AddString("android:string/dedupe3", ResourceId{}, en_v21_config, "dedupe")
     51           .Build();
     52 
     53   ASSERT_TRUE(ResourceDeduper().Consume(context.get(), table.get()));
     54   EXPECT_THAT(table, Not(HasValue("android:string/dedupe", ldrtl_config)));
     55   EXPECT_THAT(table, Not(HasValue("android:string/dedupe", land_config)));
     56 
     57   EXPECT_THAT(table, HasValue("android:string/dedupe2", ldrtl_v21_config));
     58   EXPECT_THAT(table, Not(HasValue("android:string/dedupe2", ldrtl_config)));
     59 
     60   EXPECT_THAT(table, HasValue("android:string/dedupe3", default_config));
     61   EXPECT_THAT(table, HasValue("android:string/dedupe3", en_config));
     62   EXPECT_THAT(table, Not(HasValue("android:string/dedupe3", en_v21_config)));
     63 }
     64 
     65 TEST(ResourceDeduperTest, DifferentValuesAreKept) {
     66   std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
     67   const ConfigDescription default_config = {};
     68   const ConfigDescription ldrtl_config = test::ParseConfigOrDie("ldrtl");
     69   const ConfigDescription ldrtl_v21_config = test::ParseConfigOrDie("ldrtl-v21");
     70   // Chosen because this configuration is compatible with ldrtl.
     71   const ConfigDescription land_config = test::ParseConfigOrDie("land");
     72 
     73   std::unique_ptr<ResourceTable> table =
     74       test::ResourceTableBuilder()
     75           .AddString("android:string/keep", ResourceId{}, default_config, "keep")
     76           .AddString("android:string/keep", ResourceId{}, ldrtl_config, "keep")
     77           .AddString("android:string/keep", ResourceId{}, ldrtl_v21_config, "keep2")
     78           .AddString("android:string/keep", ResourceId{}, land_config, "keep2")
     79           .Build();
     80 
     81   ASSERT_TRUE(ResourceDeduper().Consume(context.get(), table.get()));
     82   EXPECT_THAT(table, HasValue("android:string/keep", ldrtl_config));
     83   EXPECT_THAT(table, HasValue("android:string/keep", ldrtl_v21_config));
     84   EXPECT_THAT(table, HasValue("android:string/keep", land_config));
     85 }
     86 
     87 TEST(ResourceDeduperTest, LocalesValuesAreKept) {
     88   std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
     89   const ConfigDescription default_config = {};
     90   const ConfigDescription fr_config = test::ParseConfigOrDie("fr");
     91   const ConfigDescription fr_rCA_config = test::ParseConfigOrDie("fr-rCA");
     92 
     93   std::unique_ptr<ResourceTable> table =
     94       test::ResourceTableBuilder()
     95           .AddString("android:string/keep", ResourceId{}, default_config, "keep")
     96           .AddString("android:string/keep", ResourceId{}, fr_config, "keep")
     97           .AddString("android:string/keep", ResourceId{}, fr_rCA_config, "keep")
     98           .Build();
     99 
    100   ASSERT_TRUE(ResourceDeduper().Consume(context.get(), table.get()));
    101   EXPECT_THAT(table, HasValue("android:string/keep", default_config));
    102   EXPECT_THAT(table, HasValue("android:string/keep", fr_config));
    103   EXPECT_THAT(table, HasValue("android:string/keep", fr_rCA_config));
    104 }
    105 
    106 }  // namespace aapt
    107