blob: c6cff0d0220e46be78a418193fd7e4b2119fb70b [file] [log] [blame]
Adam Lesinski40e8eef2014-09-16 14:43:29 -07001/*
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 Lesinskidcdfe9f2014-11-06 12:54:36 -080020#include "TestRules.h"
Adam Lesinski40e8eef2014-09-16 14:43:29 -070021
22#include <algorithm>
Adam Lesinski40e8eef2014-09-16 14:43:29 -070023#include <gtest/gtest.h>
Adam Lesinskidcdfe9f2014-11-06 12:54:36 -080024#include <string>
Adam Lesinski40e8eef2014-09-16 14:43:29 -070025#include <utils/String8.h>
26
27using namespace android;
Adam Lesinskidcdfe9f2014-11-06 12:54:36 -080028using namespace split::test;
Adam Lesinski40e8eef2014-09-16 14:43:29 -070029
30namespace split {
31
32TEST(RuleTest, generatesValidJson) {
Adam Lesinskidcdfe9f2014-11-06 12:54:36 -080033 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 Lesinski40e8eef2014-09-16 14:43:29 -070040
Adam Lesinskidcdfe9f2014-11-06 12:54:36 -080041 // Expected
Adam Lesinski40e8eef2014-09-16 14:43:29 -070042 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 Lesinski40e8eef2014-09-16 14:43:29 -070068 expected.erase(std::remove_if(expected.begin(), expected.end(), ::isspace), expected.end());
69
Adam Lesinskidcdfe9f2014-11-06 12:54:36 -080070 // Result
71 std::string result(rule.toJson().string());
Adam Lesinski40e8eef2014-09-16 14:43:29 -070072 result.erase(std::remove_if(result.begin(), result.end(), ::isspace), result.end());
73
74 ASSERT_EQ(expected, result);
75}
76
77TEST(RuleTest, simplifiesSingleSubruleRules) {
Adam Lesinskidcdfe9f2014-11-06 12:54:36 -080078 sp<Rule> rule = new Rule(AndRule()
79 .add(EqRule(Rule::SDK_VERSION, 7))
80 );
Adam Lesinski40e8eef2014-09-16 14:43:29 -070081
Adam Lesinskidcdfe9f2014-11-06 12:54:36 -080082 EXPECT_RULES_EQ(Rule::simplify(rule), EqRule(Rule::SDK_VERSION, 7));
Adam Lesinski40e8eef2014-09-16 14:43:29 -070083}
84
85TEST(RuleTest, simplifiesNestedSameOpSubrules) {
Adam Lesinskidcdfe9f2014-11-06 12:54:36 -080086 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 Lesinski40e8eef2014-09-16 14:43:29 -070092
Adam Lesinskidcdfe9f2014-11-06 12:54:36 -080093 EXPECT_RULES_EQ(Rule::simplify(rule),
94 AndRule()
95 .add(EqRule(Rule::SDK_VERSION, 7))
96 .add(EqRule(Rule::SDK_VERSION, 8))
97 );
Adam Lesinski40e8eef2014-09-16 14:43:29 -070098}
99
100} // namespace split