Adam Lesinski | 40e8eef | 2014-09-16 14:43:29 -0700 | [diff] [blame] | 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" |
Adam Lesinski | dcdfe9f | 2014-11-06 12:54:36 -0800 | [diff] [blame] | 20 | #include "TestRules.h" |
Adam Lesinski | 40e8eef | 2014-09-16 14:43:29 -0700 | [diff] [blame] | 21 | |
| 22 | #include <algorithm> |
Adam Lesinski | 40e8eef | 2014-09-16 14:43:29 -0700 | [diff] [blame] | 23 | #include <gtest/gtest.h> |
Adam Lesinski | dcdfe9f | 2014-11-06 12:54:36 -0800 | [diff] [blame] | 24 | #include <string> |
Adam Lesinski | 40e8eef | 2014-09-16 14:43:29 -0700 | [diff] [blame] | 25 | #include <utils/String8.h> |
| 26 | |
| 27 | using namespace android; |
Adam Lesinski | dcdfe9f | 2014-11-06 12:54:36 -0800 | [diff] [blame] | 28 | using namespace split::test; |
Adam Lesinski | 40e8eef | 2014-09-16 14:43:29 -0700 | [diff] [blame] | 29 | |
| 30 | namespace split { |
| 31 | |
| 32 | TEST(RuleTest, generatesValidJson) { |
Adam Lesinski | dcdfe9f | 2014-11-06 12:54:36 -0800 | [diff] [blame] | 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 | ); |
Adam Lesinski | 40e8eef | 2014-09-16 14:43:29 -0700 | [diff] [blame] | 40 | |
Adam Lesinski | dcdfe9f | 2014-11-06 12:54:36 -0800 | [diff] [blame] | 41 | // Expected |
Adam Lesinski | 40e8eef | 2014-09-16 14:43:29 -0700 | [diff] [blame] | 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 | "}"); |
Adam Lesinski | 40e8eef | 2014-09-16 14:43:29 -0700 | [diff] [blame] | 68 | expected.erase(std::remove_if(expected.begin(), expected.end(), ::isspace), expected.end()); |
| 69 | |
Adam Lesinski | dcdfe9f | 2014-11-06 12:54:36 -0800 | [diff] [blame] | 70 | // Result |
| 71 | std::string result(rule.toJson().string()); |
Adam Lesinski | 40e8eef | 2014-09-16 14:43:29 -0700 | [diff] [blame] | 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) { |
Adam Lesinski | dcdfe9f | 2014-11-06 12:54:36 -0800 | [diff] [blame] | 78 | sp<Rule> rule = new Rule(AndRule() |
| 79 | .add(EqRule(Rule::SDK_VERSION, 7)) |
| 80 | ); |
Adam Lesinski | 40e8eef | 2014-09-16 14:43:29 -0700 | [diff] [blame] | 81 | |
Adam Lesinski | dcdfe9f | 2014-11-06 12:54:36 -0800 | [diff] [blame] | 82 | EXPECT_RULES_EQ(Rule::simplify(rule), EqRule(Rule::SDK_VERSION, 7)); |
Adam Lesinski | 40e8eef | 2014-09-16 14:43:29 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | TEST(RuleTest, simplifiesNestedSameOpSubrules) { |
Adam Lesinski | dcdfe9f | 2014-11-06 12:54:36 -0800 | [diff] [blame] | 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 | ); |
Adam Lesinski | 40e8eef | 2014-09-16 14:43:29 -0700 | [diff] [blame] | 92 | |
Adam Lesinski | dcdfe9f | 2014-11-06 12:54:36 -0800 | [diff] [blame] | 93 | EXPECT_RULES_EQ(Rule::simplify(rule), |
| 94 | AndRule() |
| 95 | .add(EqRule(Rule::SDK_VERSION, 7)) |
| 96 | .add(EqRule(Rule::SDK_VERSION, 8)) |
| 97 | ); |
Adam Lesinski | 40e8eef | 2014-09-16 14:43:29 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | } // namespace split |