Home | History | Annotate | Download | only in tests
      1 /*
      2  * Copyright (C) 2015 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 "androidfw/ConfigDescription.h"
     18 #include "androidfw/StringPiece.h"
     19 
     20 #include "android-base/logging.h"
     21 
     22 #include "gtest/gtest.h"
     23 
     24 #include <string>
     25 
     26 namespace android {
     27 
     28 static ::testing::AssertionResult TestParse(
     29     const StringPiece& input, ConfigDescription* config = nullptr) {
     30   if (ConfigDescription::Parse(input, config)) {
     31     return ::testing::AssertionSuccess() << input << " was successfully parsed";
     32   }
     33   return ::testing::AssertionFailure() << input << " could not be parsed";
     34 }
     35 
     36 TEST(ConfigDescriptionTest, ParseFailWhenQualifiersAreOutOfOrder) {
     37   EXPECT_FALSE(TestParse("en-sw600dp-ldrtl"));
     38   EXPECT_FALSE(TestParse("land-en"));
     39   EXPECT_FALSE(TestParse("hdpi-320dpi"));
     40 }
     41 
     42 TEST(ConfigDescriptionTest, ParseFailWhenQualifiersAreNotMatched) {
     43   EXPECT_FALSE(TestParse("en-sw600dp-ILLEGAL"));
     44 }
     45 
     46 TEST(ConfigDescriptionTest, ParseFailWhenQualifiersHaveTrailingDash) {
     47   EXPECT_FALSE(TestParse("en-sw600dp-land-"));
     48 }
     49 
     50 TEST(ConfigDescriptionTest, ParseBasicQualifiers) {
     51   ConfigDescription config;
     52   EXPECT_TRUE(TestParse("", &config));
     53   EXPECT_EQ(std::string(""), config.toString().string());
     54 
     55   EXPECT_TRUE(TestParse("fr-land", &config));
     56   EXPECT_EQ(std::string("fr-land"), config.toString().string());
     57 
     58   EXPECT_TRUE(
     59       TestParse("mcc310-pl-sw720dp-normal-long-port-night-"
     60                 "xhdpi-keyssoft-qwerty-navexposed-nonav",
     61                 &config));
     62   EXPECT_EQ(std::string("mcc310-pl-sw720dp-normal-long-port-night-"
     63                         "xhdpi-keyssoft-qwerty-navexposed-nonav-v13"),
     64             config.toString().string());
     65 }
     66 
     67 TEST(ConfigDescriptionTest, ParseLocales) {
     68   ConfigDescription config;
     69   EXPECT_TRUE(TestParse("en-rUS", &config));
     70   EXPECT_EQ(std::string("en-rUS"), config.toString().string());
     71 }
     72 
     73 TEST(ConfigDescriptionTest, ParseQualifierAddedInApi13) {
     74   ConfigDescription config;
     75   EXPECT_TRUE(TestParse("sw600dp", &config));
     76   EXPECT_EQ(std::string("sw600dp-v13"), config.toString().string());
     77 
     78   EXPECT_TRUE(TestParse("sw600dp-v8", &config));
     79   EXPECT_EQ(std::string("sw600dp-v13"), config.toString().string());
     80 }
     81 
     82 TEST(ConfigDescriptionTest, ParseCarAttribute) {
     83   ConfigDescription config;
     84   EXPECT_TRUE(TestParse("car", &config));
     85   EXPECT_EQ(android::ResTable_config::UI_MODE_TYPE_CAR, config.uiMode);
     86 }
     87 
     88 TEST(ConfigDescriptionTest, TestParsingRoundQualifier) {
     89   ConfigDescription config;
     90   EXPECT_TRUE(TestParse("round", &config));
     91   EXPECT_EQ(android::ResTable_config::SCREENROUND_YES,
     92             config.screenLayout2 & android::ResTable_config::MASK_SCREENROUND);
     93   EXPECT_EQ(SDK_MARSHMALLOW, config.sdkVersion);
     94   EXPECT_EQ(std::string("round-v23"), config.toString().string());
     95 
     96   EXPECT_TRUE(TestParse("notround", &config));
     97   EXPECT_EQ(android::ResTable_config::SCREENROUND_NO,
     98             config.screenLayout2 & android::ResTable_config::MASK_SCREENROUND);
     99   EXPECT_EQ(SDK_MARSHMALLOW, config.sdkVersion);
    100   EXPECT_EQ(std::string("notround-v23"), config.toString().string());
    101 }
    102 
    103 TEST(ConfigDescriptionTest, TestWideColorGamutQualifier) {
    104   ConfigDescription config;
    105   EXPECT_TRUE(TestParse("widecg", &config));
    106   EXPECT_EQ(android::ResTable_config::WIDE_COLOR_GAMUT_YES,
    107             config.colorMode & android::ResTable_config::MASK_WIDE_COLOR_GAMUT);
    108   EXPECT_EQ(SDK_O, config.sdkVersion);
    109   EXPECT_EQ(std::string("widecg-v26"), config.toString().string());
    110 
    111   EXPECT_TRUE(TestParse("nowidecg", &config));
    112   EXPECT_EQ(android::ResTable_config::WIDE_COLOR_GAMUT_NO,
    113             config.colorMode & android::ResTable_config::MASK_WIDE_COLOR_GAMUT);
    114   EXPECT_EQ(SDK_O, config.sdkVersion);
    115   EXPECT_EQ(std::string("nowidecg-v26"), config.toString().string());
    116 }
    117 
    118 TEST(ConfigDescriptionTest, TestHdrQualifier) {
    119   ConfigDescription config;
    120   EXPECT_TRUE(TestParse("highdr", &config));
    121   EXPECT_EQ(android::ResTable_config::HDR_YES,
    122             config.colorMode & android::ResTable_config::MASK_HDR);
    123   EXPECT_EQ(SDK_O, config.sdkVersion);
    124   EXPECT_EQ(std::string("highdr-v26"), config.toString().string());
    125 
    126   EXPECT_TRUE(TestParse("lowdr", &config));
    127   EXPECT_EQ(android::ResTable_config::HDR_NO,
    128             config.colorMode & android::ResTable_config::MASK_HDR);
    129   EXPECT_EQ(SDK_O, config.sdkVersion);
    130   EXPECT_EQ(std::string("lowdr-v26"), config.toString().string());
    131 }
    132 
    133 TEST(ConfigDescriptionTest, ParseVrAttribute) {
    134   ConfigDescription config;
    135   EXPECT_TRUE(TestParse("vrheadset", &config));
    136   EXPECT_EQ(android::ResTable_config::UI_MODE_TYPE_VR_HEADSET, config.uiMode);
    137   EXPECT_EQ(SDK_O, config.sdkVersion);
    138   EXPECT_EQ(std::string("vrheadset-v26"), config.toString().string());
    139 }
    140 
    141 static inline ConfigDescription ParseConfigOrDie(const android::StringPiece& str) {
    142   ConfigDescription config;
    143   CHECK(ConfigDescription::Parse(str, &config)) << "invalid configuration: " << str;
    144   return config;
    145 }
    146 
    147 TEST(ConfigDescriptionTest, RangeQualifiersDoNotConflict) {
    148   EXPECT_FALSE(ParseConfigOrDie("large").ConflictsWith(ParseConfigOrDie("normal-land")));
    149   EXPECT_FALSE(ParseConfigOrDie("long-hdpi").ConflictsWith(ParseConfigOrDie("xhdpi")));
    150   EXPECT_FALSE(ParseConfigOrDie("sw600dp").ConflictsWith(ParseConfigOrDie("sw700dp")));
    151   EXPECT_FALSE(ParseConfigOrDie("v11").ConflictsWith(ParseConfigOrDie("v21")));
    152   EXPECT_FALSE(ParseConfigOrDie("h600dp").ConflictsWith(ParseConfigOrDie("h300dp")));
    153   EXPECT_FALSE(ParseConfigOrDie("w400dp").ConflictsWith(ParseConfigOrDie("w300dp")));
    154   EXPECT_FALSE(ParseConfigOrDie("600x400").ConflictsWith(ParseConfigOrDie("300x200")));
    155 }
    156 
    157 }  // namespace android
    158