1 /*
2 * Copyright (C) 2014 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 <utils/String8.h>
18 #include <gtest/gtest.h>
19
20 #include "AaptConfig.h"
21 #include "ConfigDescription.h"
22 #include "SdkConstants.h"
23 #include "TestHelper.h"
24
25 using android::String8;
26
TestParse(const String8 & input,ConfigDescription * config=NULL)27 static ::testing::AssertionResult TestParse(const String8& input, ConfigDescription* config=NULL) {
28 if (AaptConfig::parse(String8(input), config)) {
29 return ::testing::AssertionSuccess() << input << " was successfully parsed";
30 }
31 return ::testing::AssertionFailure() << input << " could not be parsed";
32 }
33
TestParse(const char * input,ConfigDescription * config=NULL)34 static ::testing::AssertionResult TestParse(const char* input, ConfigDescription* config=NULL) {
35 return TestParse(String8(input), config);
36 }
37
TEST(AaptConfigTest,ParseFailWhenQualifiersAreOutOfOrder)38 TEST(AaptConfigTest, ParseFailWhenQualifiersAreOutOfOrder) {
39 EXPECT_FALSE(TestParse("en-sw600dp-ldrtl"));
40 EXPECT_FALSE(TestParse("land-en"));
41 EXPECT_FALSE(TestParse("hdpi-320dpi"));
42 }
43
TEST(AaptConfigTest,ParseFailWhenQualifiersAreNotMatched)44 TEST(AaptConfigTest, ParseFailWhenQualifiersAreNotMatched) {
45 EXPECT_FALSE(TestParse("en-sw600dp-ILLEGAL"));
46 }
47
TEST(AaptConfigTest,ParseFailWhenQualifiersHaveTrailingDash)48 TEST(AaptConfigTest, ParseFailWhenQualifiersHaveTrailingDash) {
49 EXPECT_FALSE(TestParse("en-sw600dp-land-"));
50 }
51
TEST(AaptConfigTest,ParseBasicQualifiers)52 TEST(AaptConfigTest, ParseBasicQualifiers) {
53 ConfigDescription config;
54 EXPECT_TRUE(TestParse("", &config));
55 EXPECT_EQ(String8(""), config.toString());
56
57 EXPECT_TRUE(TestParse("fr-land", &config));
58 EXPECT_EQ(String8("fr-land"), config.toString());
59
60 EXPECT_TRUE(TestParse("mcc310-pl-sw720dp-normal-long-port-night-"
61 "xhdpi-keyssoft-qwerty-navexposed-nonav", &config));
62 EXPECT_EQ(String8("mcc310-pl-sw720dp-normal-long-port-night-"
63 "xhdpi-keyssoft-qwerty-navexposed-nonav-v13"), config.toString());
64 }
65
TEST(AaptConfigTest,ParseLocales)66 TEST(AaptConfigTest, ParseLocales) {
67 ConfigDescription config;
68 EXPECT_TRUE(TestParse("en-rUS", &config));
69 EXPECT_EQ(String8("en-rUS"), config.toString());
70 }
71
TEST(AaptConfigTest,ParseQualifierAddedInApi13)72 TEST(AaptConfigTest, ParseQualifierAddedInApi13) {
73 ConfigDescription config;
74 EXPECT_TRUE(TestParse("sw600dp", &config));
75 EXPECT_EQ(String8("sw600dp-v13"), config.toString());
76
77 EXPECT_TRUE(TestParse("sw600dp-v8", &config));
78 EXPECT_EQ(String8("sw600dp-v13"), config.toString());
79 }
80
TEST(AaptConfigTest,TestParsingOfCarAttribute)81 TEST(AaptConfigTest, TestParsingOfCarAttribute) {
82 ConfigDescription config;
83 EXPECT_TRUE(TestParse("car", &config));
84 EXPECT_EQ(android::ResTable_config::UI_MODE_TYPE_CAR, config.uiMode);
85 }
86
TEST(AaptConfigTest,TestParsingRoundQualifier)87 TEST(AaptConfigTest, TestParsingRoundQualifier) {
88 ConfigDescription config;
89 EXPECT_TRUE(TestParse("round", &config));
90 EXPECT_EQ(android::ResTable_config::SCREENROUND_YES,
91 config.screenLayout2 & android::ResTable_config::MASK_SCREENROUND);
92 EXPECT_EQ(SDK_MNC, config.sdkVersion);
93 EXPECT_EQ(String8("round-v23"), config.toString());
94
95 EXPECT_TRUE(TestParse("notround", &config));
96 EXPECT_EQ(android::ResTable_config::SCREENROUND_NO,
97 config.screenLayout2 & android::ResTable_config::MASK_SCREENROUND);
98 EXPECT_EQ(SDK_MNC, config.sdkVersion);
99 EXPECT_EQ(String8("notround-v23"), config.toString());
100 }
101