blob: bad02a5cb332acfae35d1d2a37270a0a49d04415 [file] [log] [blame]
Adam Lesinski355f2852016-02-13 20:26:45 -08001/*
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"
Adam Lesinskifb6312f2016-06-28 14:40:32 -070018#include "test/Test.h"
Adam Lesinski355f2852016-02-13 20:26:45 -080019
20namespace aapt {
21
22TEST(TableSplitterTest, NoSplitPreferredDensity) {
23 std::unique_ptr<ResourceTable> table = test::ResourceTableBuilder()
Adam Lesinski58a20a62016-07-25 17:56:58 -070024 .addFileReference("android:drawable/icon", "res/drawable-mdpi/icon.png",
Adam Lesinski355f2852016-02-13 20:26:45 -080025 test::parseConfigOrDie("mdpi"))
Adam Lesinski58a20a62016-07-25 17:56:58 -070026 .addFileReference("android:drawable/icon", "res/drawable-hdpi/icon.png",
Adam Lesinski355f2852016-02-13 20:26:45 -080027 test::parseConfigOrDie("hdpi"))
Adam Lesinski58a20a62016-07-25 17:56:58 -070028 .addFileReference("android:drawable/icon", "res/drawable-xhdpi/icon.png",
Adam Lesinski355f2852016-02-13 20:26:45 -080029 test::parseConfigOrDie("xhdpi"))
Adam Lesinski58a20a62016-07-25 17:56:58 -070030 .addFileReference("android:drawable/icon", "res/drawable-xxhdpi/icon.png",
Adam Lesinski355f2852016-02-13 20:26:45 -080031 test::parseConfigOrDie("xxhdpi"))
Adam Lesinski58a20a62016-07-25 17:56:58 -070032 .addSimple("android:string/one")
Adam Lesinski355f2852016-02-13 20:26:45 -080033 .build();
34
35 TableSplitterOptions options;
36 options.preferredDensity = ConfigDescription::DENSITY_XHIGH;
37 TableSplitter splitter({}, options);
38 splitter.splitTable(table.get());
39
40 EXPECT_EQ(nullptr, test::getValueForConfig<FileReference>(table.get(),
Adam Lesinski58a20a62016-07-25 17:56:58 -070041 "android:drawable/icon",
Adam Lesinski355f2852016-02-13 20:26:45 -080042 test::parseConfigOrDie("mdpi")));
43 EXPECT_EQ(nullptr, test::getValueForConfig<FileReference>(table.get(),
Adam Lesinski58a20a62016-07-25 17:56:58 -070044 "android:drawable/icon",
Adam Lesinski355f2852016-02-13 20:26:45 -080045 test::parseConfigOrDie("hdpi")));
46 EXPECT_NE(nullptr, test::getValueForConfig<FileReference>(table.get(),
Adam Lesinski58a20a62016-07-25 17:56:58 -070047 "android:drawable/icon",
Adam Lesinski355f2852016-02-13 20:26:45 -080048 test::parseConfigOrDie("xhdpi")));
49 EXPECT_EQ(nullptr, test::getValueForConfig<FileReference>(table.get(),
Adam Lesinski58a20a62016-07-25 17:56:58 -070050 "android:drawable/icon",
Adam Lesinski355f2852016-02-13 20:26:45 -080051 test::parseConfigOrDie("xxhdpi")));
Adam Lesinski58a20a62016-07-25 17:56:58 -070052 EXPECT_NE(nullptr, test::getValue<Id>(table.get(), "android:string/one"));
Adam Lesinski355f2852016-02-13 20:26:45 -080053}
54
55TEST(TableSplitterTest, SplitTableByConfigAndDensity) {
56 ResourceTable table;
57
Adam Lesinski58a20a62016-07-25 17:56:58 -070058 const ResourceName foo = test::parseNameOrDie("android:string/foo");
Adam Lesinski355f2852016-02-13 20:26:45 -080059 ASSERT_TRUE(table.addResource(foo, test::parseConfigOrDie("land-hdpi"), {},
60 util::make_unique<Id>(),
61 test::getDiagnostics()));
62 ASSERT_TRUE(table.addResource(foo, test::parseConfigOrDie("land-xhdpi"), {},
63 util::make_unique<Id>(),
64 test::getDiagnostics()));
65 ASSERT_TRUE(table.addResource(foo, test::parseConfigOrDie("land-xxhdpi"), {},
66 util::make_unique<Id>(),
67 test::getDiagnostics()));
68
69 std::vector<SplitConstraints> constraints;
70 constraints.push_back(SplitConstraints{ { test::parseConfigOrDie("land-mdpi") } });
71 constraints.push_back(SplitConstraints{ { test::parseConfigOrDie("land-xhdpi") } });
72
73 TableSplitter splitter(constraints, TableSplitterOptions{});
74 splitter.splitTable(&table);
75
76 ASSERT_EQ(2u, splitter.getSplits().size());
77
78 ResourceTable* splitOne = splitter.getSplits()[0].get();
79 ResourceTable* splitTwo = splitter.getSplits()[1].get();
80
81 // Since a split was defined, all densities should be gone from base.
Adam Lesinski58a20a62016-07-25 17:56:58 -070082 EXPECT_EQ(nullptr, test::getValueForConfig<Id>(&table, "android:string/foo",
Adam Lesinski355f2852016-02-13 20:26:45 -080083 test::parseConfigOrDie("land-hdpi")));
Adam Lesinski58a20a62016-07-25 17:56:58 -070084 EXPECT_EQ(nullptr, test::getValueForConfig<Id>(&table, "android:string/foo",
Adam Lesinski355f2852016-02-13 20:26:45 -080085 test::parseConfigOrDie("land-xhdpi")));
Adam Lesinski58a20a62016-07-25 17:56:58 -070086 EXPECT_EQ(nullptr, test::getValueForConfig<Id>(&table, "android:string/foo",
Adam Lesinski355f2852016-02-13 20:26:45 -080087 test::parseConfigOrDie("land-xxhdpi")));
88
Adam Lesinski58a20a62016-07-25 17:56:58 -070089 EXPECT_NE(nullptr, test::getValueForConfig<Id>(splitOne, "android:string/foo",
Adam Lesinski355f2852016-02-13 20:26:45 -080090 test::parseConfigOrDie("land-hdpi")));
Adam Lesinski58a20a62016-07-25 17:56:58 -070091 EXPECT_EQ(nullptr, test::getValueForConfig<Id>(splitOne, "android:string/foo",
Adam Lesinski355f2852016-02-13 20:26:45 -080092 test::parseConfigOrDie("land-xhdpi")));
Adam Lesinski58a20a62016-07-25 17:56:58 -070093 EXPECT_EQ(nullptr, test::getValueForConfig<Id>(splitOne, "android:string/foo",
Adam Lesinski355f2852016-02-13 20:26:45 -080094 test::parseConfigOrDie("land-xxhdpi")));
95
Adam Lesinski58a20a62016-07-25 17:56:58 -070096 EXPECT_EQ(nullptr, test::getValueForConfig<Id>(splitTwo, "android:string/foo",
Adam Lesinski355f2852016-02-13 20:26:45 -080097 test::parseConfigOrDie("land-hdpi")));
Adam Lesinski58a20a62016-07-25 17:56:58 -070098 EXPECT_NE(nullptr, test::getValueForConfig<Id>(splitTwo, "android:string/foo",
Adam Lesinski355f2852016-02-13 20:26:45 -080099 test::parseConfigOrDie("land-xhdpi")));
Adam Lesinski58a20a62016-07-25 17:56:58 -0700100 EXPECT_EQ(nullptr, test::getValueForConfig<Id>(splitTwo, "android:string/foo",
Adam Lesinski355f2852016-02-13 20:26:45 -0800101 test::parseConfigOrDie("land-xxhdpi")));
102}
103
104} // namespace aapt