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 "TestHelper.h"
23
24using android::String8;
25
26static ::testing::AssertionResult TestParse(const String8& input, ConfigDescription* config=NULL) {
27    if (AaptConfig::parse(String8(input), config)) {
28        return ::testing::AssertionSuccess() << input << " was successfully parsed";
29    }
30    return ::testing::AssertionFailure() << input << " could not be parsed";
31}
32
33static ::testing::AssertionResult TestParse(const char* input, ConfigDescription* config=NULL) {
34    return TestParse(String8(input), config);
35}
36
37TEST(AaptConfigTest, ParseFailWhenQualifiersAreOutOfOrder) {
38    EXPECT_FALSE(TestParse("en-sw600dp-ldrtl"));
39    EXPECT_FALSE(TestParse("land-en"));
40    EXPECT_FALSE(TestParse("hdpi-320dpi"));
41}
42
43TEST(AaptConfigTest, ParseFailWhenQualifiersAreNotMatched) {
44    EXPECT_FALSE(TestParse("en-sw600dp-ILLEGAL"));
45}
46
47TEST(AaptConfigTest, ParseFailWhenQualifiersHaveTrailingDash) {
48    EXPECT_FALSE(TestParse("en-sw600dp-land-"));
49}
50
51TEST(AaptConfigTest, ParseBasicQualifiers) {
52    ConfigDescription config;
53    EXPECT_TRUE(TestParse("", &config));
54    EXPECT_EQ(String8(""), config.toString());
55
56    EXPECT_TRUE(TestParse("fr-land", &config));
57    EXPECT_EQ(String8("fr-land"), config.toString());
58
59    EXPECT_TRUE(TestParse("mcc310-pl-sw720dp-normal-long-port-night-"
60                "xhdpi-keyssoft-qwerty-navexposed-nonav", &config));
61    EXPECT_EQ(String8("mcc310-pl-sw720dp-normal-long-port-night-"
62                "xhdpi-keyssoft-qwerty-navexposed-nonav-v13"), config.toString());
63}
64
65TEST(AaptConfigTest, ParseLocales) {
66    ConfigDescription config;
67    EXPECT_TRUE(TestParse("en-rUS", &config));
68    EXPECT_EQ(String8("en-US"), config.toString());
69}
70
71TEST(AaptConfigTest, ParseQualifierAddedInApi13) {
72    ConfigDescription config;
73    EXPECT_TRUE(TestParse("sw600dp", &config));
74    EXPECT_EQ(String8("sw600dp-v13"), config.toString());
75
76    EXPECT_TRUE(TestParse("sw600dp-v8", &config));
77    EXPECT_EQ(String8("sw600dp-v13"), config.toString());
78}
79