Home | History | Annotate | Download | only in link
      1 /*
      2  * Copyright (C) 2018 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 "link/NoDefaultResourceRemover.h"
     18 
     19 #include "test/Test.h"
     20 
     21 namespace aapt {
     22 
     23 TEST(NoDefaultResourceRemoverTest, RemoveEntryWithNoDefaultAndOnlyLocales) {
     24   std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
     25   std::unique_ptr<ResourceTable> table =
     26       test::ResourceTableBuilder()
     27           .SetPackageId("android", 0x01)
     28           .AddSimple("android:string/foo")
     29           .AddSimple("android:string/foo", test::ParseConfigOrDie("en-rGB"))
     30           .AddSimple("android:string/foo", test::ParseConfigOrDie("fr-rFR"))
     31           .AddSimple("android:string/bar", test::ParseConfigOrDie("en-rGB"))
     32           .AddSimple("android:string/bar", test::ParseConfigOrDie("fr-rFR"))
     33           .AddSimple("android:string/bat", test::ParseConfigOrDie("en-rGB-xhdpi"))
     34           .AddSimple("android:string/bat", test::ParseConfigOrDie("fr-rFR-hdpi"))
     35           .AddSimple("android:string/baz", test::ParseConfigOrDie("en-rGB"))
     36           .AddSimple("android:string/baz", test::ParseConfigOrDie("fr-rFR"))
     37           .SetSymbolState("android:string/baz", ResourceId(0x01020002), Visibility::Level::kPublic)
     38           .Build();
     39 
     40   NoDefaultResourceRemover remover;
     41   ASSERT_TRUE(remover.Consume(context.get(), table.get()));
     42 
     43   EXPECT_TRUE(table->FindResource(test::ParseNameOrDie("android:string/foo")));
     44   EXPECT_FALSE(table->FindResource(test::ParseNameOrDie("android:string/bar")));
     45   EXPECT_TRUE(table->FindResource(test::ParseNameOrDie("android:string/bat")));
     46   EXPECT_TRUE(table->FindResource(test::ParseNameOrDie("android:string/baz")));
     47 }
     48 
     49 TEST(NoDefaultResourceRemoverTest, KeepEntryWithLocalesAndDensities) {
     50   std::unique_ptr<IAaptContext> context = test::ContextBuilder().SetMinSdkVersion(26).Build();
     51   std::unique_ptr<ResourceTable> table =
     52       test::ResourceTableBuilder()
     53           .SetPackageId("android", 0x01)
     54           .AddSimple("android:drawable/keep1", test::ParseConfigOrDie("mdpi")) // v4
     55           .AddSimple("android:drawable/keep1", test::ParseConfigOrDie("en-rGB"))
     56           .AddSimple("android:drawable/keep1", test::ParseConfigOrDie("fr-rFR"))
     57           .AddSimple("android:drawable/keep2", test::ParseConfigOrDie("fr-rFR"))
     58           .AddSimple("android:drawable/keep2", test::ParseConfigOrDie("en-rGB"))
     59           .AddSimple("android:drawable/keep2", test::ParseConfigOrDie("xxxhdpi")) // v4
     60           .AddSimple("android:drawable/remove1", test::ParseConfigOrDie("fr-rFR"))
     61           .AddSimple("android:drawable/remove1", test::ParseConfigOrDie("en-rGB"))
     62           .AddSimple("android:drawable/remove1", test::ParseConfigOrDie("w600dp-xhdpi")) // v13
     63           .Build();
     64 
     65   NoDefaultResourceRemover remover;
     66   ASSERT_TRUE(remover.Consume(context.get(), table.get()));
     67 
     68   EXPECT_TRUE(table->FindResource(test::ParseNameOrDie("android:drawable/keep1")));
     69   EXPECT_TRUE(table->FindResource(test::ParseNameOrDie("android:drawable/keep2")));
     70   EXPECT_FALSE(table->FindResource(test::ParseNameOrDie("android:drawable/remove1")));
     71 }
     72 
     73 TEST(NoDefaultResourceRemoverTest, RemoveEntryWithLocalesAndDensitiesLowVersion) {
     74   std::unique_ptr<IAaptContext> context = test::ContextBuilder().SetMinSdkVersion(3).Build();
     75   std::unique_ptr<ResourceTable> table =
     76       test::ResourceTableBuilder()
     77           .SetPackageId("android", 0x01)
     78           .AddSimple("android:drawable/remove1", test::ParseConfigOrDie("mdpi")) // v4
     79           .AddSimple("android:drawable/remove1", test::ParseConfigOrDie("en-rGB"))
     80           .AddSimple("android:drawable/remove1", test::ParseConfigOrDie("fr-rFR"))
     81           .Build();
     82 
     83   NoDefaultResourceRemover remover;
     84   ASSERT_TRUE(remover.Consume(context.get(), table.get()));
     85 
     86   EXPECT_FALSE(table->FindResource(test::ParseNameOrDie("android:drawable/remove1")));
     87 }
     88 
     89 TEST(NoDefaultResourceRemoverTest, KeepEntryWithVersion) {
     90   std::unique_ptr<IAaptContext> context = test::ContextBuilder().SetMinSdkVersion(8).Build();
     91   std::unique_ptr<ResourceTable> table =
     92       test::ResourceTableBuilder()
     93           .SetPackageId("android", 0x01)
     94           .AddSimple("android:drawable/keep1", test::ParseConfigOrDie("v8"))
     95           .AddSimple("android:drawable/keep1", test::ParseConfigOrDie("en-rGB"))
     96           .AddSimple("android:drawable/keep1", test::ParseConfigOrDie("fr-rFR"))
     97           .AddSimple("android:drawable/remove1", test::ParseConfigOrDie("v9"))
     98           .AddSimple("android:drawable/remove1", test::ParseConfigOrDie("en-rGB"))
     99           .AddSimple("android:drawable/remove1", test::ParseConfigOrDie("fr-rFR"))
    100           .Build();
    101 
    102   NoDefaultResourceRemover remover;
    103   ASSERT_TRUE(remover.Consume(context.get(), table.get()));
    104 
    105   EXPECT_TRUE(table->FindResource(test::ParseNameOrDie("android:drawable/keep1")));
    106   EXPECT_FALSE(table->FindResource(test::ParseNameOrDie("android:drawable/remove1")));
    107 }
    108 
    109 }  // namespace aapt
    110