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