Rule_test.cpp revision 40e8eefbedcafc51948945647d746daaee092f16
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 21#include <algorithm> 22#include <string> 23#include <gtest/gtest.h> 24#include <utils/String8.h> 25 26using namespace android; 27 28namespace split { 29 30TEST(RuleTest, generatesValidJson) { 31 sp<Rule> rule = new Rule(); 32 rule->op = Rule::AND_SUBRULES; 33 34 sp<Rule> subrule = new Rule(); 35 subrule->op = Rule::EQUALS; 36 subrule->key = Rule::SDK_VERSION; 37 subrule->longArgs.add(7); 38 rule->subrules.add(subrule); 39 40 subrule = new Rule(); 41 subrule->op = Rule::OR_SUBRULES; 42 rule->subrules.add(subrule); 43 44 sp<Rule> subsubrule = new Rule(); 45 subsubrule->op = Rule::GREATER_THAN; 46 subsubrule->key = Rule::SCREEN_DENSITY; 47 subsubrule->longArgs.add(10); 48 subrule->subrules.add(subsubrule); 49 50 subsubrule = new Rule(); 51 subsubrule->op = Rule::LESS_THAN; 52 subsubrule->key = Rule::SCREEN_DENSITY; 53 subsubrule->longArgs.add(5); 54 subrule->subrules.add(subsubrule); 55 56 std::string expected( 57 "{" 58 " \"op\": \"AND_SUBRULES\"," 59 " \"subrules\": [" 60 " {" 61 " \"op\": \"EQUALS\"," 62 " \"property\": \"SDK_VERSION\"," 63 " \"args\": [7]" 64 " }," 65 " {" 66 " \"op\": \"OR_SUBRULES\"," 67 " \"subrules\": [" 68 " {" 69 " \"op\": \"GREATER_THAN\"," 70 " \"property\": \"SCREEN_DENSITY\"," 71 " \"args\": [10]" 72 " }," 73 " {" 74 " \"op\": \"LESS_THAN\"," 75 " \"property\": \"SCREEN_DENSITY\"," 76 " \"args\": [5]" 77 " }" 78 " ]" 79 " }" 80 " ]" 81 "}"); 82 // Trim 83 expected.erase(std::remove_if(expected.begin(), expected.end(), ::isspace), expected.end()); 84 85 std::string result(rule->toJson().string()); 86 87 // Trim 88 result.erase(std::remove_if(result.begin(), result.end(), ::isspace), result.end()); 89 90 ASSERT_EQ(expected, result); 91} 92 93TEST(RuleTest, simplifiesSingleSubruleRules) { 94 sp<Rule> rule = new Rule(); 95 rule->op = Rule::AND_SUBRULES; 96 97 sp<Rule> subrule = new Rule(); 98 subrule->op = Rule::EQUALS; 99 subrule->key = Rule::SDK_VERSION; 100 subrule->longArgs.add(7); 101 rule->subrules.add(subrule); 102 103 sp<Rule> simplified = Rule::simplify(rule); 104 EXPECT_EQ(Rule::EQUALS, simplified->op); 105 EXPECT_EQ(Rule::SDK_VERSION, simplified->key); 106 ASSERT_EQ(1u, simplified->longArgs.size()); 107 EXPECT_EQ(7, simplified->longArgs[0]); 108} 109 110TEST(RuleTest, simplifiesNestedSameOpSubrules) { 111 sp<Rule> rule = new Rule(); 112 rule->op = Rule::AND_SUBRULES; 113 114 sp<Rule> subrule = new Rule(); 115 subrule->op = Rule::AND_SUBRULES; 116 rule->subrules.add(subrule); 117 118 sp<Rule> subsubrule = new Rule(); 119 subsubrule->op = Rule::EQUALS; 120 subsubrule->key = Rule::SDK_VERSION; 121 subsubrule->longArgs.add(7); 122 subrule->subrules.add(subsubrule); 123 124 subrule = new Rule(); 125 subrule->op = Rule::EQUALS; 126 subrule->key = Rule::SDK_VERSION; 127 subrule->longArgs.add(8); 128 rule->subrules.add(subrule); 129 130 sp<Rule> simplified = Rule::simplify(rule); 131 EXPECT_EQ(Rule::AND_SUBRULES, simplified->op); 132 ASSERT_EQ(2u, simplified->subrules.size()); 133 134 sp<Rule> simplifiedSubrule = simplified->subrules[0]; 135 EXPECT_EQ(Rule::EQUALS, simplifiedSubrule->op); 136 EXPECT_EQ(Rule::SDK_VERSION, simplifiedSubrule->key); 137 ASSERT_EQ(1u, simplifiedSubrule->longArgs.size()); 138 EXPECT_EQ(7, simplifiedSubrule->longArgs[0]); 139 140 simplifiedSubrule = simplified->subrules[1]; 141 EXPECT_EQ(Rule::EQUALS, simplifiedSubrule->op); 142 EXPECT_EQ(Rule::SDK_VERSION, simplifiedSubrule->key); 143 ASSERT_EQ(1u, simplifiedSubrule->longArgs.size()); 144 EXPECT_EQ(8, simplifiedSubrule->longArgs[0]); 145} 146 147} // namespace split 148