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 "split/TableSplitter.h"
18#include "test/Builders.h"
19#include "test/Common.h"
20
21#include <gtest/gtest.h>
22
23namespace aapt {
24
25TEST(TableSplitterTest, NoSplitPreferredDensity) {
26    std::unique_ptr<ResourceTable> table = test::ResourceTableBuilder()
27            .addFileReference(u"@android:drawable/icon", u"res/drawable-mdpi/icon.png",
28                              test::parseConfigOrDie("mdpi"))
29            .addFileReference(u"@android:drawable/icon", u"res/drawable-hdpi/icon.png",
30                              test::parseConfigOrDie("hdpi"))
31            .addFileReference(u"@android:drawable/icon", u"res/drawable-xhdpi/icon.png",
32                              test::parseConfigOrDie("xhdpi"))
33            .addFileReference(u"@android:drawable/icon", u"res/drawable-xxhdpi/icon.png",
34                              test::parseConfigOrDie("xxhdpi"))
35            .addSimple(u"@android:string/one", {})
36            .build();
37
38    TableSplitterOptions options;
39    options.preferredDensity = ConfigDescription::DENSITY_XHIGH;
40    TableSplitter splitter({}, options);
41    splitter.splitTable(table.get());
42
43    EXPECT_EQ(nullptr, test::getValueForConfig<FileReference>(table.get(),
44                                                              u"@android:drawable/icon",
45                                                              test::parseConfigOrDie("mdpi")));
46    EXPECT_EQ(nullptr, test::getValueForConfig<FileReference>(table.get(),
47                                                              u"@android:drawable/icon",
48                                                              test::parseConfigOrDie("hdpi")));
49    EXPECT_NE(nullptr, test::getValueForConfig<FileReference>(table.get(),
50                                                              u"@android:drawable/icon",
51                                                              test::parseConfigOrDie("xhdpi")));
52    EXPECT_EQ(nullptr, test::getValueForConfig<FileReference>(table.get(),
53                                                              u"@android:drawable/icon",
54                                                              test::parseConfigOrDie("xxhdpi")));
55    EXPECT_NE(nullptr, test::getValue<Id>(table.get(), u"@android:string/one"));
56}
57
58TEST(TableSplitterTest, SplitTableByConfigAndDensity) {
59    ResourceTable table;
60
61    const ResourceName foo = test::parseNameOrDie(u"@android:string/foo");
62    ASSERT_TRUE(table.addResource(foo, test::parseConfigOrDie("land-hdpi"), {},
63                                  util::make_unique<Id>(),
64                                  test::getDiagnostics()));
65    ASSERT_TRUE(table.addResource(foo, test::parseConfigOrDie("land-xhdpi"), {},
66                                  util::make_unique<Id>(),
67                                  test::getDiagnostics()));
68    ASSERT_TRUE(table.addResource(foo, test::parseConfigOrDie("land-xxhdpi"), {},
69                                  util::make_unique<Id>(),
70                                  test::getDiagnostics()));
71
72    std::vector<SplitConstraints> constraints;
73    constraints.push_back(SplitConstraints{ { test::parseConfigOrDie("land-mdpi") } });
74    constraints.push_back(SplitConstraints{ { test::parseConfigOrDie("land-xhdpi") } });
75
76    TableSplitter splitter(constraints, TableSplitterOptions{});
77    splitter.splitTable(&table);
78
79    ASSERT_EQ(2u, splitter.getSplits().size());
80
81    ResourceTable* splitOne = splitter.getSplits()[0].get();
82    ResourceTable* splitTwo = splitter.getSplits()[1].get();
83
84    // Since a split was defined, all densities should be gone from base.
85    EXPECT_EQ(nullptr, test::getValueForConfig<Id>(&table, u"@android:string/foo",
86                                                   test::parseConfigOrDie("land-hdpi")));
87    EXPECT_EQ(nullptr, test::getValueForConfig<Id>(&table, u"@android:string/foo",
88                                                   test::parseConfigOrDie("land-xhdpi")));
89    EXPECT_EQ(nullptr, test::getValueForConfig<Id>(&table, u"@android:string/foo",
90                                                   test::parseConfigOrDie("land-xxhdpi")));
91
92    EXPECT_NE(nullptr, test::getValueForConfig<Id>(splitOne, u"@android:string/foo",
93                                                   test::parseConfigOrDie("land-hdpi")));
94    EXPECT_EQ(nullptr, test::getValueForConfig<Id>(splitOne, u"@android:string/foo",
95                                                   test::parseConfigOrDie("land-xhdpi")));
96    EXPECT_EQ(nullptr, test::getValueForConfig<Id>(splitOne, u"@android:string/foo",
97                                                   test::parseConfigOrDie("land-xxhdpi")));
98
99    EXPECT_EQ(nullptr, test::getValueForConfig<Id>(splitTwo, u"@android:string/foo",
100                                                   test::parseConfigOrDie("land-hdpi")));
101    EXPECT_NE(nullptr, test::getValueForConfig<Id>(splitTwo, u"@android:string/foo",
102                                                   test::parseConfigOrDie("land-xhdpi")));
103    EXPECT_EQ(nullptr, test::getValueForConfig<Id>(splitTwo, u"@android:string/foo",
104                                                   test::parseConfigOrDie("land-xxhdpi")));
105}
106
107} // namespace aapt
108