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