Home | History | Annotate | Download | only in link
      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 "link/Linkers.h"
     18 
     19 #include "test/Test.h"
     20 
     21 namespace aapt {
     22 
     23 TEST(ProductFilterTest, SelectTwoProducts) {
     24   std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
     25 
     26   const ConfigDescription land = test::ParseConfigOrDie("land");
     27   const ConfigDescription port = test::ParseConfigOrDie("port");
     28 
     29   ResourceTable table;
     30   ASSERT_TRUE(table.AddResource(
     31       test::ParseNameOrDie("android:string/one"), land, "",
     32       test::ValueBuilder<Id>().SetSource(Source("land/default.xml")).Build(),
     33       context->GetDiagnostics()));
     34   ASSERT_TRUE(table.AddResource(
     35       test::ParseNameOrDie("android:string/one"), land, "tablet",
     36       test::ValueBuilder<Id>().SetSource(Source("land/tablet.xml")).Build(),
     37       context->GetDiagnostics()));
     38 
     39   ASSERT_TRUE(table.AddResource(
     40       test::ParseNameOrDie("android:string/one"), port, "",
     41       test::ValueBuilder<Id>().SetSource(Source("port/default.xml")).Build(),
     42       context->GetDiagnostics()));
     43   ASSERT_TRUE(table.AddResource(
     44       test::ParseNameOrDie("android:string/one"), port, "tablet",
     45       test::ValueBuilder<Id>().SetSource(Source("port/tablet.xml")).Build(),
     46       context->GetDiagnostics()));
     47 
     48   ProductFilter filter({"tablet"});
     49   ASSERT_TRUE(filter.Consume(context.get(), &table));
     50 
     51   EXPECT_EQ(nullptr, test::GetValueForConfigAndProduct<Id>(
     52                          &table, "android:string/one", land, ""));
     53   EXPECT_NE(nullptr, test::GetValueForConfigAndProduct<Id>(
     54                          &table, "android:string/one", land, "tablet"));
     55   EXPECT_EQ(nullptr, test::GetValueForConfigAndProduct<Id>(
     56                          &table, "android:string/one", port, ""));
     57   EXPECT_NE(nullptr, test::GetValueForConfigAndProduct<Id>(
     58                          &table, "android:string/one", port, "tablet"));
     59 }
     60 
     61 TEST(ProductFilterTest, SelectDefaultProduct) {
     62   std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
     63 
     64   ResourceTable table;
     65   ASSERT_TRUE(table.AddResource(
     66       test::ParseNameOrDie("android:string/one"),
     67       ConfigDescription::DefaultConfig(), "",
     68       test::ValueBuilder<Id>().SetSource(Source("default.xml")).Build(),
     69       context->GetDiagnostics()));
     70   ASSERT_TRUE(table.AddResource(
     71       test::ParseNameOrDie("android:string/one"),
     72       ConfigDescription::DefaultConfig(), "tablet",
     73       test::ValueBuilder<Id>().SetSource(Source("tablet.xml")).Build(),
     74       context->GetDiagnostics()));
     75 
     76   ProductFilter filter({});
     77   ASSERT_TRUE(filter.Consume(context.get(), &table));
     78 
     79   EXPECT_NE(nullptr, test::GetValueForConfigAndProduct<Id>(
     80                          &table, "android:string/one",
     81                          ConfigDescription::DefaultConfig(), ""));
     82   EXPECT_EQ(nullptr, test::GetValueForConfigAndProduct<Id>(
     83                          &table, "android:string/one",
     84                          ConfigDescription::DefaultConfig(), "tablet"));
     85 }
     86 
     87 TEST(ProductFilterTest, FailOnAmbiguousProduct) {
     88   std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
     89 
     90   ResourceTable table;
     91   ASSERT_TRUE(table.AddResource(
     92       test::ParseNameOrDie("android:string/one"),
     93       ConfigDescription::DefaultConfig(), "",
     94       test::ValueBuilder<Id>().SetSource(Source("default.xml")).Build(),
     95       context->GetDiagnostics()));
     96   ASSERT_TRUE(table.AddResource(
     97       test::ParseNameOrDie("android:string/one"),
     98       ConfigDescription::DefaultConfig(), "tablet",
     99       test::ValueBuilder<Id>().SetSource(Source("tablet.xml")).Build(),
    100       context->GetDiagnostics()));
    101   ASSERT_TRUE(table.AddResource(
    102       test::ParseNameOrDie("android:string/one"),
    103       ConfigDescription::DefaultConfig(), "no-sdcard",
    104       test::ValueBuilder<Id>().SetSource(Source("no-sdcard.xml")).Build(),
    105       context->GetDiagnostics()));
    106 
    107   ProductFilter filter({"tablet", "no-sdcard"});
    108   ASSERT_FALSE(filter.Consume(context.get(), &table));
    109 }
    110 
    111 TEST(ProductFilterTest, FailOnMultipleDefaults) {
    112   std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
    113 
    114   ResourceTable table;
    115   ASSERT_TRUE(table.AddResource(
    116       test::ParseNameOrDie("android:string/one"),
    117       ConfigDescription::DefaultConfig(), "",
    118       test::ValueBuilder<Id>().SetSource(Source(".xml")).Build(),
    119       context->GetDiagnostics()));
    120   ASSERT_TRUE(table.AddResource(
    121       test::ParseNameOrDie("android:string/one"),
    122       ConfigDescription::DefaultConfig(), "default",
    123       test::ValueBuilder<Id>().SetSource(Source("default.xml")).Build(),
    124       context->GetDiagnostics()));
    125 
    126   ProductFilter filter({});
    127   ASSERT_FALSE(filter.Consume(context.get(), &table));
    128 }
    129 
    130 }  // namespace aapt
    131