1 // Copyright (C) 2017 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "condition/condition_util.h"
16 #include "frameworks/base/cmds/statsd/src/statsd_config.pb.h"
17 
18 #include <gtest/gtest.h>
19 
20 #include <stdio.h>
21 #include <vector>
22 
23 using namespace android::os::statsd;
24 using std::vector;
25 
26 #ifdef __ANDROID__
TEST(ConditionTrackerTest,TestUnknownCondition)27 TEST(ConditionTrackerTest, TestUnknownCondition) {
28     LogicalOperation operation = LogicalOperation::AND;
29 
30     vector<int> children;
31     children.push_back(0);
32     children.push_back(1);
33     children.push_back(2);
34 
35     vector<ConditionState> conditionResults;
36     conditionResults.push_back(ConditionState::kUnknown);
37     conditionResults.push_back(ConditionState::kFalse);
38     conditionResults.push_back(ConditionState::kTrue);
39 
40     EXPECT_EQ(evaluateCombinationCondition(children, operation, conditionResults),
41               ConditionState::kUnknown);
42 }
43 
TEST(ConditionTrackerTest,TestAndCondition)44 TEST(ConditionTrackerTest, TestAndCondition) {
45     // Set up the matcher
46     LogicalOperation operation = LogicalOperation::AND;
47 
48     vector<int> children;
49     children.push_back(0);
50     children.push_back(1);
51     children.push_back(2);
52 
53     vector<ConditionState> conditionResults;
54     conditionResults.push_back(ConditionState::kTrue);
55     conditionResults.push_back(ConditionState::kFalse);
56     conditionResults.push_back(ConditionState::kTrue);
57 
58     EXPECT_FALSE(evaluateCombinationCondition(children, operation, conditionResults));
59 
60     conditionResults.clear();
61     conditionResults.push_back(ConditionState::kTrue);
62     conditionResults.push_back(ConditionState::kTrue);
63     conditionResults.push_back(ConditionState::kTrue);
64 
65     EXPECT_TRUE(evaluateCombinationCondition(children, operation, conditionResults));
66 }
67 
TEST(ConditionTrackerTest,TestOrCondition)68 TEST(ConditionTrackerTest, TestOrCondition) {
69     // Set up the matcher
70     LogicalOperation operation = LogicalOperation::OR;
71 
72     vector<int> children;
73     children.push_back(0);
74     children.push_back(1);
75     children.push_back(2);
76 
77     vector<ConditionState> conditionResults;
78     conditionResults.push_back(ConditionState::kTrue);
79     conditionResults.push_back(ConditionState::kFalse);
80     conditionResults.push_back(ConditionState::kTrue);
81 
82     EXPECT_TRUE(evaluateCombinationCondition(children, operation, conditionResults));
83 
84     conditionResults.clear();
85     conditionResults.push_back(ConditionState::kFalse);
86     conditionResults.push_back(ConditionState::kFalse);
87     conditionResults.push_back(ConditionState::kFalse);
88 
89     EXPECT_FALSE(evaluateCombinationCondition(children, operation, conditionResults));
90 }
91 
TEST(ConditionTrackerTest,TestNotCondition)92 TEST(ConditionTrackerTest, TestNotCondition) {
93     // Set up the matcher
94     LogicalOperation operation = LogicalOperation::NOT;
95 
96     vector<int> children;
97     children.push_back(0);
98 
99     vector<ConditionState> conditionResults;
100     conditionResults.push_back(ConditionState::kTrue);
101 
102     EXPECT_FALSE(evaluateCombinationCondition(children, operation, conditionResults));
103 
104     conditionResults.clear();
105     conditionResults.push_back(ConditionState::kFalse);
106     EXPECT_TRUE(evaluateCombinationCondition(children, operation, conditionResults));
107 
108     children.clear();
109     conditionResults.clear();
110     EXPECT_EQ(evaluateCombinationCondition(children, operation, conditionResults),
111               ConditionState::kUnknown);
112 }
113 
TEST(ConditionTrackerTest,TestNandCondition)114 TEST(ConditionTrackerTest, TestNandCondition) {
115     // Set up the matcher
116     LogicalOperation operation = LogicalOperation::NAND;
117 
118     vector<int> children;
119     children.push_back(0);
120     children.push_back(1);
121 
122     vector<ConditionState> conditionResults;
123     conditionResults.push_back(ConditionState::kTrue);
124     conditionResults.push_back(ConditionState::kFalse);
125 
126     EXPECT_TRUE(evaluateCombinationCondition(children, operation, conditionResults));
127 
128     conditionResults.clear();
129     conditionResults.push_back(ConditionState::kFalse);
130     conditionResults.push_back(ConditionState::kFalse);
131     EXPECT_TRUE(evaluateCombinationCondition(children, operation, conditionResults));
132 
133     conditionResults.clear();
134     conditionResults.push_back(ConditionState::kTrue);
135     conditionResults.push_back(ConditionState::kTrue);
136     EXPECT_FALSE(evaluateCombinationCondition(children, operation, conditionResults));
137 }
138 
TEST(ConditionTrackerTest,TestNorCondition)139 TEST(ConditionTrackerTest, TestNorCondition) {
140     // Set up the matcher
141     LogicalOperation operation = LogicalOperation::NOR;
142 
143     vector<int> children;
144     children.push_back(0);
145     children.push_back(1);
146 
147     vector<ConditionState> conditionResults;
148     conditionResults.push_back(ConditionState::kTrue);
149     conditionResults.push_back(ConditionState::kFalse);
150 
151     EXPECT_FALSE(evaluateCombinationCondition(children, operation, conditionResults));
152 
153     conditionResults.clear();
154     conditionResults.push_back(ConditionState::kFalse);
155     conditionResults.push_back(ConditionState::kFalse);
156     EXPECT_TRUE(evaluateCombinationCondition(children, operation, conditionResults));
157 
158     conditionResults.clear();
159     conditionResults.push_back(ConditionState::kTrue);
160     conditionResults.push_back(ConditionState::kTrue);
161     EXPECT_FALSE(evaluateCombinationCondition(children, operation, conditionResults));
162 }
163 
164 #else
165 GTEST_LOG_(INFO) << "This test does nothing.\n";
166 #endif
167