Home | History | Annotate | Download | only in split-select
      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 "Rule.h"
     18 
     19 #include "SplitDescription.h"
     20 #include "TestRules.h"
     21 
     22 #include <algorithm>
     23 #include <gtest/gtest.h>
     24 #include <string>
     25 #include <utils/String8.h>
     26 
     27 using namespace android;
     28 using namespace split::test;
     29 
     30 namespace split {
     31 
     32 TEST(RuleTest, generatesValidJson) {
     33     Rule rule(AndRule()
     34         .add(EqRule(Rule::SDK_VERSION, 7))
     35         .add(OrRule()
     36                 .add(GtRule(Rule::SCREEN_DENSITY, 10))
     37                 .add(LtRule(Rule::SCREEN_DENSITY, 5))
     38         )
     39     );
     40 
     41     // Expected
     42     std::string expected(
     43             "{"
     44             "  \"op\": \"AND_SUBRULES\","
     45             "  \"subrules\": ["
     46             "    {"
     47             "      \"op\": \"EQUALS\","
     48             "      \"property\": \"SDK_VERSION\","
     49             "      \"args\": [7]"
     50             "    },"
     51             "    {"
     52             "      \"op\": \"OR_SUBRULES\","
     53             "      \"subrules\": ["
     54             "        {"
     55             "          \"op\": \"GREATER_THAN\","
     56             "          \"property\": \"SCREEN_DENSITY\","
     57             "          \"args\": [10]"
     58             "        },"
     59             "        {"
     60             "          \"op\": \"LESS_THAN\","
     61             "          \"property\": \"SCREEN_DENSITY\","
     62             "          \"args\": [5]"
     63             "        }"
     64             "      ]"
     65             "     }"
     66             "  ]"
     67             "}");
     68     expected.erase(std::remove_if(expected.begin(), expected.end(), ::isspace), expected.end());
     69 
     70     // Result
     71     std::string result(rule.toJson().string());
     72     result.erase(std::remove_if(result.begin(), result.end(), ::isspace), result.end());
     73 
     74     ASSERT_EQ(expected, result);
     75 }
     76 
     77 TEST(RuleTest, simplifiesSingleSubruleRules) {
     78     sp<Rule> rule = new Rule(AndRule()
     79         .add(EqRule(Rule::SDK_VERSION, 7))
     80     );
     81 
     82     EXPECT_RULES_EQ(Rule::simplify(rule), EqRule(Rule::SDK_VERSION, 7));
     83 }
     84 
     85 TEST(RuleTest, simplifiesNestedSameOpSubrules) {
     86     sp<Rule> rule = new Rule(AndRule()
     87         .add(AndRule()
     88             .add(EqRule(Rule::SDK_VERSION, 7))
     89         )
     90         .add(EqRule(Rule::SDK_VERSION, 8))
     91     );
     92 
     93     EXPECT_RULES_EQ(Rule::simplify(rule),
     94             AndRule()
     95                 .add(EqRule(Rule::SDK_VERSION, 7))
     96                 .add(EqRule(Rule::SDK_VERSION, 8))
     97     );
     98 }
     99 
    100 } // namespace split
    101