blob: 01ba82d31549c7263310d1b5bf66396c3f8da813 [file] [log] [blame]
Yao Chen967b2052017-11-07 16:36:43 -08001// 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#include "src/condition/SimpleConditionTracker.h"
15
16#include <gmock/gmock.h>
17#include <gtest/gtest.h>
18#include <stdio.h>
19#include <vector>
20
21using std::map;
22using std::unordered_map;
23using std::vector;
24
25#ifdef __ANDROID__
26
27namespace android {
28namespace os {
29namespace statsd {
30
Yao Chenb3561512017-11-21 18:07:17 -080031const ConfigKey kConfigKey(0, "test");
32
Stefan Lafon12d01fa2017-12-04 20:56:09 -080033SimplePredicate getWakeLockHeldCondition(bool countNesting, bool defaultFalse,
Yao Chen967b2052017-11-07 16:36:43 -080034 bool outputSlicedUid) {
Stefan Lafon12d01fa2017-12-04 20:56:09 -080035 SimplePredicate simplePredicate;
36 simplePredicate.set_start("WAKE_LOCK_ACQUIRE");
37 simplePredicate.set_stop("WAKE_LOCK_RELEASE");
38 simplePredicate.set_stop_all("RELEASE_ALL");
Yao Chen967b2052017-11-07 16:36:43 -080039 if (outputSlicedUid) {
Stefan Lafon12d01fa2017-12-04 20:56:09 -080040 KeyMatcher* keyMatcher = simplePredicate.add_dimension();
Yao Chen967b2052017-11-07 16:36:43 -080041 keyMatcher->set_key(1);
42 }
43
Stefan Lafon12d01fa2017-12-04 20:56:09 -080044 simplePredicate.set_count_nesting(countNesting);
45 simplePredicate.set_initial_value(defaultFalse ? SimplePredicate_InitialValue_FALSE
46 : SimplePredicate_InitialValue_UNKNOWN);
47 return simplePredicate;
Yao Chen967b2052017-11-07 16:36:43 -080048}
49
50void makeWakeLockEvent(LogEvent* event, int uid, const string& wl, int acquire) {
Yao Chen80235402017-11-13 20:42:25 -080051 event->write(uid); // uid
52 event->write(wl);
53 event->write(acquire);
Yao Chen967b2052017-11-07 16:36:43 -080054 event->init();
55}
56
57map<string, HashableDimensionKey> getWakeLockQueryKey(int key, int uid,
58 const string& conditionName) {
59 // test query
60 KeyValuePair kv1;
61 kv1.set_key(key);
62 kv1.set_value_int(uid);
63 vector<KeyValuePair> kv_list;
64 kv_list.push_back(kv1);
65 map<string, HashableDimensionKey> queryKey;
66 queryKey[conditionName] = getHashableKey(kv_list);
67 return queryKey;
68}
69
70TEST(SimpleConditionTrackerTest, TestNonSlicedCondition) {
Stefan Lafon12d01fa2017-12-04 20:56:09 -080071 SimplePredicate simplePredicate;
72 simplePredicate.set_start("SCREEN_TURNED_ON");
73 simplePredicate.set_stop("SCREEN_TURNED_OFF");
74 simplePredicate.set_count_nesting(false);
75 simplePredicate.set_initial_value(SimplePredicate_InitialValue_UNKNOWN);
Yao Chen967b2052017-11-07 16:36:43 -080076
77 unordered_map<string, int> trackerNameIndexMap;
78 trackerNameIndexMap["SCREEN_TURNED_ON"] = 0;
79 trackerNameIndexMap["SCREEN_TURNED_OFF"] = 1;
80
Yao Chenb3561512017-11-21 18:07:17 -080081 SimpleConditionTracker conditionTracker(kConfigKey, "SCREEN_IS_ON", 0 /*tracker index*/,
Stefan Lafon12d01fa2017-12-04 20:56:09 -080082 simplePredicate, trackerNameIndexMap);
Yao Chen967b2052017-11-07 16:36:43 -080083
84 LogEvent event(1 /*tagId*/, 0 /*timestamp*/);
85
86 vector<MatchingState> matcherState;
87 matcherState.push_back(MatchingState::kNotMatched);
88 matcherState.push_back(MatchingState::kNotMatched);
89
Stefan Lafon12d01fa2017-12-04 20:56:09 -080090 vector<sp<ConditionTracker>> allPredicates;
Yao Chen967b2052017-11-07 16:36:43 -080091 vector<ConditionState> conditionCache(1, ConditionState::kNotEvaluated);
92 vector<bool> changedCache(1, false);
93
Stefan Lafon12d01fa2017-12-04 20:56:09 -080094 conditionTracker.evaluateCondition(event, matcherState, allPredicates, conditionCache,
Yao Chen967b2052017-11-07 16:36:43 -080095 changedCache);
96 // not matched start or stop. condition doesn't change
97 EXPECT_EQ(ConditionState::kUnknown, conditionCache[0]);
98 EXPECT_FALSE(changedCache[0]);
99
100 // prepare a case for match start.
101 matcherState.clear();
102 matcherState.push_back(MatchingState::kMatched);
103 matcherState.push_back(MatchingState::kNotMatched);
104 conditionCache[0] = ConditionState::kNotEvaluated;
105 changedCache[0] = false;
106
Stefan Lafon12d01fa2017-12-04 20:56:09 -0800107 conditionTracker.evaluateCondition(event, matcherState, allPredicates, conditionCache,
Yao Chen967b2052017-11-07 16:36:43 -0800108 changedCache);
109 // now condition should change to true.
110 EXPECT_EQ(ConditionState::kTrue, conditionCache[0]);
111 EXPECT_TRUE(changedCache[0]);
112
Yao Chend41c4222017-11-15 19:26:14 -0800113 // match nothing.
114 matcherState.clear();
115 matcherState.push_back(MatchingState::kNotMatched);
116 matcherState.push_back(MatchingState::kNotMatched);
117 conditionCache[0] = ConditionState::kNotEvaluated;
118 changedCache[0] = false;
119
Stefan Lafon12d01fa2017-12-04 20:56:09 -0800120 conditionTracker.evaluateCondition(event, matcherState, allPredicates, conditionCache,
Yao Chend41c4222017-11-15 19:26:14 -0800121 changedCache);
122 EXPECT_EQ(ConditionState::kTrue, conditionCache[0]);
123 EXPECT_FALSE(changedCache[0]);
124
Yao Chen967b2052017-11-07 16:36:43 -0800125 // the case for match stop.
126 matcherState.clear();
127 matcherState.push_back(MatchingState::kNotMatched);
128 matcherState.push_back(MatchingState::kMatched);
129 conditionCache[0] = ConditionState::kNotEvaluated;
130 changedCache[0] = false;
131
Stefan Lafon12d01fa2017-12-04 20:56:09 -0800132 conditionTracker.evaluateCondition(event, matcherState, allPredicates, conditionCache,
Yao Chen967b2052017-11-07 16:36:43 -0800133 changedCache);
134
135 // condition changes to false.
136 EXPECT_EQ(ConditionState::kFalse, conditionCache[0]);
137 EXPECT_TRUE(changedCache[0]);
138
139 // match stop again.
140 matcherState.clear();
141 matcherState.push_back(MatchingState::kNotMatched);
142 matcherState.push_back(MatchingState::kMatched);
143 conditionCache[0] = ConditionState::kNotEvaluated;
144 changedCache[0] = false;
145
Stefan Lafon12d01fa2017-12-04 20:56:09 -0800146 conditionTracker.evaluateCondition(event, matcherState, allPredicates, conditionCache,
Yao Chen967b2052017-11-07 16:36:43 -0800147 changedCache);
148 // condition should still be false. not changed.
149 EXPECT_EQ(ConditionState::kFalse, conditionCache[0]);
150 EXPECT_FALSE(changedCache[0]);
151}
152
153TEST(SimpleConditionTrackerTest, TestNonSlicedConditionNestCounting) {
Stefan Lafon12d01fa2017-12-04 20:56:09 -0800154 SimplePredicate simplePredicate;
155 simplePredicate.set_start("SCREEN_TURNED_ON");
156 simplePredicate.set_stop("SCREEN_TURNED_OFF");
157 simplePredicate.set_count_nesting(true);
Yao Chen967b2052017-11-07 16:36:43 -0800158
159 unordered_map<string, int> trackerNameIndexMap;
160 trackerNameIndexMap["SCREEN_TURNED_ON"] = 0;
161 trackerNameIndexMap["SCREEN_TURNED_OFF"] = 1;
162
Yao Chenb3561512017-11-21 18:07:17 -0800163 SimpleConditionTracker conditionTracker(kConfigKey, "SCREEN_IS_ON",
Stefan Lafon12d01fa2017-12-04 20:56:09 -0800164 0 /*condition tracker index*/, simplePredicate,
Yao Chenb3561512017-11-21 18:07:17 -0800165 trackerNameIndexMap);
Yao Chen967b2052017-11-07 16:36:43 -0800166
167 LogEvent event(1 /*tagId*/, 0 /*timestamp*/);
168
169 // one matched start
170 vector<MatchingState> matcherState;
171 matcherState.push_back(MatchingState::kMatched);
172 matcherState.push_back(MatchingState::kNotMatched);
Stefan Lafon12d01fa2017-12-04 20:56:09 -0800173 vector<sp<ConditionTracker>> allPredicates;
Yao Chen967b2052017-11-07 16:36:43 -0800174 vector<ConditionState> conditionCache(1, ConditionState::kNotEvaluated);
175 vector<bool> changedCache(1, false);
176
Stefan Lafon12d01fa2017-12-04 20:56:09 -0800177 conditionTracker.evaluateCondition(event, matcherState, allPredicates, conditionCache,
Yao Chen967b2052017-11-07 16:36:43 -0800178 changedCache);
179
180 EXPECT_EQ(ConditionState::kTrue, conditionCache[0]);
181 EXPECT_TRUE(changedCache[0]);
182
183 // prepare for another matched start.
184 matcherState.clear();
185 matcherState.push_back(MatchingState::kMatched);
186 matcherState.push_back(MatchingState::kNotMatched);
187 conditionCache[0] = ConditionState::kNotEvaluated;
188 changedCache[0] = false;
189
Stefan Lafon12d01fa2017-12-04 20:56:09 -0800190 conditionTracker.evaluateCondition(event, matcherState, allPredicates, conditionCache,
Yao Chen967b2052017-11-07 16:36:43 -0800191 changedCache);
192
193 EXPECT_EQ(ConditionState::kTrue, conditionCache[0]);
194 EXPECT_FALSE(changedCache[0]);
195
196 // ONE MATCHED STOP
197 matcherState.clear();
198 matcherState.push_back(MatchingState::kNotMatched);
199 matcherState.push_back(MatchingState::kMatched);
200 conditionCache[0] = ConditionState::kNotEvaluated;
201 changedCache[0] = false;
202
Stefan Lafon12d01fa2017-12-04 20:56:09 -0800203 conditionTracker.evaluateCondition(event, matcherState, allPredicates, conditionCache,
Yao Chen967b2052017-11-07 16:36:43 -0800204 changedCache);
205 // result should still be true
206 EXPECT_EQ(ConditionState::kTrue, conditionCache[0]);
207 EXPECT_FALSE(changedCache[0]);
208
209 // ANOTHER MATCHED STOP
210 matcherState.clear();
211 matcherState.push_back(MatchingState::kNotMatched);
212 matcherState.push_back(MatchingState::kMatched);
213 conditionCache[0] = ConditionState::kNotEvaluated;
214 changedCache[0] = false;
215
Stefan Lafon12d01fa2017-12-04 20:56:09 -0800216 conditionTracker.evaluateCondition(event, matcherState, allPredicates, conditionCache,
Yao Chen967b2052017-11-07 16:36:43 -0800217 changedCache);
218 // result should still be true
219 EXPECT_EQ(ConditionState::kFalse, conditionCache[0]);
220 EXPECT_TRUE(changedCache[0]);
221}
222
223TEST(SimpleConditionTrackerTest, TestSlicedCondition) {
Stefan Lafon12d01fa2017-12-04 20:56:09 -0800224 SimplePredicate simplePredicate = getWakeLockHeldCondition(
Yao Chen967b2052017-11-07 16:36:43 -0800225 true /*nesting*/, true /*default to false*/, true /*output slice by uid*/);
226 string conditionName = "WL_HELD_BY_UID2";
227
228 unordered_map<string, int> trackerNameIndexMap;
229 trackerNameIndexMap["WAKE_LOCK_ACQUIRE"] = 0;
230 trackerNameIndexMap["WAKE_LOCK_RELEASE"] = 1;
231 trackerNameIndexMap["RELEASE_ALL"] = 2;
232
Yao Chenb3561512017-11-21 18:07:17 -0800233 SimpleConditionTracker conditionTracker(kConfigKey, conditionName,
Stefan Lafon12d01fa2017-12-04 20:56:09 -0800234 0 /*condition tracker index*/, simplePredicate,
Yao Chenb3561512017-11-21 18:07:17 -0800235 trackerNameIndexMap);
Yao Chen967b2052017-11-07 16:36:43 -0800236 int uid = 111;
237
238 LogEvent event(1 /*tagId*/, 0 /*timestamp*/);
239 makeWakeLockEvent(&event, uid, "wl1", 1);
240
241 // one matched start
242 vector<MatchingState> matcherState;
243 matcherState.push_back(MatchingState::kMatched);
244 matcherState.push_back(MatchingState::kNotMatched);
Yao Chen99752052017-11-27 11:40:45 -0800245 matcherState.push_back(MatchingState::kNotMatched);
Stefan Lafon12d01fa2017-12-04 20:56:09 -0800246 vector<sp<ConditionTracker>> allPredicates;
Yao Chen967b2052017-11-07 16:36:43 -0800247 vector<ConditionState> conditionCache(1, ConditionState::kNotEvaluated);
248 vector<bool> changedCache(1, false);
249
Stefan Lafon12d01fa2017-12-04 20:56:09 -0800250 conditionTracker.evaluateCondition(event, matcherState, allPredicates, conditionCache,
Yao Chen967b2052017-11-07 16:36:43 -0800251 changedCache);
252
253 EXPECT_EQ(1UL, conditionTracker.mSlicedConditionState.size());
254 EXPECT_TRUE(changedCache[0]);
255
256 // Now test query
257 const auto queryKey = getWakeLockQueryKey(1, uid, conditionName);
258 conditionCache[0] = ConditionState::kNotEvaluated;
259
Stefan Lafon12d01fa2017-12-04 20:56:09 -0800260 conditionTracker.isConditionMet(queryKey, allPredicates, conditionCache);
Yao Chen967b2052017-11-07 16:36:43 -0800261 EXPECT_EQ(ConditionState::kTrue, conditionCache[0]);
262
263 // another wake lock acquired by this uid
264 LogEvent event2(1 /*tagId*/, 0 /*timestamp*/);
265 makeWakeLockEvent(&event2, uid, "wl2", 1);
266 matcherState.clear();
267 matcherState.push_back(MatchingState::kMatched);
268 matcherState.push_back(MatchingState::kNotMatched);
269 conditionCache[0] = ConditionState::kNotEvaluated;
270 changedCache[0] = false;
Stefan Lafon12d01fa2017-12-04 20:56:09 -0800271 conditionTracker.evaluateCondition(event2, matcherState, allPredicates, conditionCache,
Yao Chen967b2052017-11-07 16:36:43 -0800272 changedCache);
273 EXPECT_FALSE(changedCache[0]);
274
275 // wake lock 1 release
276 LogEvent event3(1 /*tagId*/, 0 /*timestamp*/);
277 makeWakeLockEvent(&event3, uid, "wl1", 0); // now release it.
278 matcherState.clear();
279 matcherState.push_back(MatchingState::kNotMatched);
280 matcherState.push_back(MatchingState::kMatched);
281 conditionCache[0] = ConditionState::kNotEvaluated;
282 changedCache[0] = false;
Stefan Lafon12d01fa2017-12-04 20:56:09 -0800283 conditionTracker.evaluateCondition(event3, matcherState, allPredicates, conditionCache,
Yao Chen967b2052017-11-07 16:36:43 -0800284 changedCache);
285 // nothing changes, because wake lock 2 is still held for this uid
286 EXPECT_FALSE(changedCache[0]);
287
288 LogEvent event4(1 /*tagId*/, 0 /*timestamp*/);
289 makeWakeLockEvent(&event4, uid, "wl2", 0); // now release it.
290 matcherState.clear();
291 matcherState.push_back(MatchingState::kNotMatched);
292 matcherState.push_back(MatchingState::kMatched);
293 conditionCache[0] = ConditionState::kNotEvaluated;
294 changedCache[0] = false;
Stefan Lafon12d01fa2017-12-04 20:56:09 -0800295 conditionTracker.evaluateCondition(event4, matcherState, allPredicates, conditionCache,
Yao Chen967b2052017-11-07 16:36:43 -0800296 changedCache);
297 EXPECT_EQ(0UL, conditionTracker.mSlicedConditionState.size());
298 EXPECT_TRUE(changedCache[0]);
299
300 // query again
301 conditionCache[0] = ConditionState::kNotEvaluated;
Stefan Lafon12d01fa2017-12-04 20:56:09 -0800302 conditionTracker.isConditionMet(queryKey, allPredicates, conditionCache);
Yao Chen967b2052017-11-07 16:36:43 -0800303 EXPECT_EQ(ConditionState::kFalse, conditionCache[0]);
304}
305
306TEST(SimpleConditionTrackerTest, TestSlicedWithNoOutputDim) {
Stefan Lafon12d01fa2017-12-04 20:56:09 -0800307 SimplePredicate simplePredicate = getWakeLockHeldCondition(
Yao Chen967b2052017-11-07 16:36:43 -0800308 true /*nesting*/, true /*default to false*/, false /*slice output by uid*/);
309 string conditionName = "WL_HELD";
310
311 unordered_map<string, int> trackerNameIndexMap;
312 trackerNameIndexMap["WAKE_LOCK_ACQUIRE"] = 0;
313 trackerNameIndexMap["WAKE_LOCK_RELEASE"] = 1;
314 trackerNameIndexMap["RELEASE_ALL"] = 2;
315
Yao Chenb3561512017-11-21 18:07:17 -0800316 SimpleConditionTracker conditionTracker(kConfigKey, conditionName,
Stefan Lafon12d01fa2017-12-04 20:56:09 -0800317 0 /*condition tracker index*/, simplePredicate,
Yao Chenb3561512017-11-21 18:07:17 -0800318 trackerNameIndexMap);
Yao Chen967b2052017-11-07 16:36:43 -0800319 int uid1 = 111;
320 string uid1_wl1 = "wl1_1";
321 int uid2 = 222;
322 string uid2_wl1 = "wl2_1";
323
324 LogEvent event(1 /*tagId*/, 0 /*timestamp*/);
325 makeWakeLockEvent(&event, uid1, uid1_wl1, 1);
326
327 // one matched start for uid1
328 vector<MatchingState> matcherState;
329 matcherState.push_back(MatchingState::kMatched);
330 matcherState.push_back(MatchingState::kNotMatched);
Yao Chen99752052017-11-27 11:40:45 -0800331 matcherState.push_back(MatchingState::kNotMatched);
Stefan Lafon12d01fa2017-12-04 20:56:09 -0800332 vector<sp<ConditionTracker>> allPredicates;
Yao Chen967b2052017-11-07 16:36:43 -0800333 vector<ConditionState> conditionCache(1, ConditionState::kNotEvaluated);
334 vector<bool> changedCache(1, false);
335
Stefan Lafon12d01fa2017-12-04 20:56:09 -0800336 conditionTracker.evaluateCondition(event, matcherState, allPredicates, conditionCache,
Yao Chen967b2052017-11-07 16:36:43 -0800337 changedCache);
338
339 EXPECT_EQ(1UL, conditionTracker.mSlicedConditionState.size());
340 EXPECT_TRUE(changedCache[0]);
341
342 // Now test query
343 map<string, HashableDimensionKey> queryKey;
344 conditionCache[0] = ConditionState::kNotEvaluated;
345
Stefan Lafon12d01fa2017-12-04 20:56:09 -0800346 conditionTracker.isConditionMet(queryKey, allPredicates, conditionCache);
Yao Chen967b2052017-11-07 16:36:43 -0800347 EXPECT_EQ(ConditionState::kTrue, conditionCache[0]);
348
349 // another wake lock acquired by this uid
350 LogEvent event2(1 /*tagId*/, 0 /*timestamp*/);
351 makeWakeLockEvent(&event2, uid2, uid2_wl1, 1);
352 matcherState.clear();
353 matcherState.push_back(MatchingState::kMatched);
354 matcherState.push_back(MatchingState::kNotMatched);
355 conditionCache[0] = ConditionState::kNotEvaluated;
356 changedCache[0] = false;
Stefan Lafon12d01fa2017-12-04 20:56:09 -0800357 conditionTracker.evaluateCondition(event2, matcherState, allPredicates, conditionCache,
Yao Chen967b2052017-11-07 16:36:43 -0800358 changedCache);
359 EXPECT_FALSE(changedCache[0]);
360
361 // uid1 wake lock 1 release
362 LogEvent event3(1 /*tagId*/, 0 /*timestamp*/);
363 makeWakeLockEvent(&event3, uid1, uid1_wl1, 0); // now release it.
364 matcherState.clear();
365 matcherState.push_back(MatchingState::kNotMatched);
366 matcherState.push_back(MatchingState::kMatched);
367 conditionCache[0] = ConditionState::kNotEvaluated;
368 changedCache[0] = false;
Stefan Lafon12d01fa2017-12-04 20:56:09 -0800369 conditionTracker.evaluateCondition(event3, matcherState, allPredicates, conditionCache,
Yao Chen967b2052017-11-07 16:36:43 -0800370 changedCache);
371 // nothing changes, because uid2 is still holding wl.
372 EXPECT_FALSE(changedCache[0]);
373
374 LogEvent event4(1 /*tagId*/, 0 /*timestamp*/);
375 makeWakeLockEvent(&event4, uid2, uid2_wl1, 0); // now release it.
376 matcherState.clear();
377 matcherState.push_back(MatchingState::kNotMatched);
378 matcherState.push_back(MatchingState::kMatched);
379 conditionCache[0] = ConditionState::kNotEvaluated;
380 changedCache[0] = false;
Stefan Lafon12d01fa2017-12-04 20:56:09 -0800381 conditionTracker.evaluateCondition(event4, matcherState, allPredicates, conditionCache,
Yao Chen967b2052017-11-07 16:36:43 -0800382 changedCache);
383 EXPECT_EQ(0UL, conditionTracker.mSlicedConditionState.size());
384 EXPECT_TRUE(changedCache[0]);
385
386 // query again
387 conditionCache[0] = ConditionState::kNotEvaluated;
Stefan Lafon12d01fa2017-12-04 20:56:09 -0800388 conditionTracker.isConditionMet(queryKey, allPredicates, conditionCache);
Yao Chen967b2052017-11-07 16:36:43 -0800389 EXPECT_EQ(ConditionState::kFalse, conditionCache[0]);
390}
391
392TEST(SimpleConditionTrackerTest, TestStopAll) {
Stefan Lafon12d01fa2017-12-04 20:56:09 -0800393 SimplePredicate simplePredicate = getWakeLockHeldCondition(
Yao Chen967b2052017-11-07 16:36:43 -0800394 true /*nesting*/, true /*default to false*/, true /*output slice by uid*/);
395 string conditionName = "WL_HELD_BY_UID3";
396
397 unordered_map<string, int> trackerNameIndexMap;
398 trackerNameIndexMap["WAKE_LOCK_ACQUIRE"] = 0;
399 trackerNameIndexMap["WAKE_LOCK_RELEASE"] = 1;
400 trackerNameIndexMap["RELEASE_ALL"] = 2;
401
Yao Chenb3561512017-11-21 18:07:17 -0800402 SimpleConditionTracker conditionTracker(kConfigKey, conditionName,
Stefan Lafon12d01fa2017-12-04 20:56:09 -0800403 0 /*condition tracker index*/, simplePredicate,
Yao Chenb3561512017-11-21 18:07:17 -0800404 trackerNameIndexMap);
Yao Chen967b2052017-11-07 16:36:43 -0800405 int uid1 = 111;
406 int uid2 = 222;
407
408 LogEvent event(1 /*tagId*/, 0 /*timestamp*/);
409 makeWakeLockEvent(&event, uid1, "wl1", 1);
410
411 // one matched start
412 vector<MatchingState> matcherState;
413 matcherState.push_back(MatchingState::kMatched);
414 matcherState.push_back(MatchingState::kNotMatched);
415 matcherState.push_back(MatchingState::kNotMatched);
Stefan Lafon12d01fa2017-12-04 20:56:09 -0800416 vector<sp<ConditionTracker>> allPredicates;
Yao Chen967b2052017-11-07 16:36:43 -0800417 vector<ConditionState> conditionCache(1, ConditionState::kNotEvaluated);
418 vector<bool> changedCache(1, false);
419
Stefan Lafon12d01fa2017-12-04 20:56:09 -0800420 conditionTracker.evaluateCondition(event, matcherState, allPredicates, conditionCache,
Yao Chen967b2052017-11-07 16:36:43 -0800421 changedCache);
422
423 EXPECT_EQ(1UL, conditionTracker.mSlicedConditionState.size());
424 EXPECT_TRUE(changedCache[0]);
425
426 // Now test query
427 const auto queryKey = getWakeLockQueryKey(1, uid1, conditionName);
428 conditionCache[0] = ConditionState::kNotEvaluated;
429
Stefan Lafon12d01fa2017-12-04 20:56:09 -0800430 conditionTracker.isConditionMet(queryKey, allPredicates, conditionCache);
Yao Chen967b2052017-11-07 16:36:43 -0800431 EXPECT_EQ(ConditionState::kTrue, conditionCache[0]);
432
433 // another wake lock acquired by uid2
434 LogEvent event2(1 /*tagId*/, 0 /*timestamp*/);
435 makeWakeLockEvent(&event2, uid2, "wl2", 1);
436 matcherState.clear();
437 matcherState.push_back(MatchingState::kMatched);
438 matcherState.push_back(MatchingState::kNotMatched);
439 matcherState.push_back(MatchingState::kNotMatched);
440 conditionCache[0] = ConditionState::kNotEvaluated;
441 changedCache[0] = false;
Stefan Lafon12d01fa2017-12-04 20:56:09 -0800442 conditionTracker.evaluateCondition(event2, matcherState, allPredicates, conditionCache,
Yao Chen967b2052017-11-07 16:36:43 -0800443 changedCache);
444 EXPECT_EQ(2UL, conditionTracker.mSlicedConditionState.size());
445 EXPECT_TRUE(changedCache[0]);
446
447 // TEST QUERY
448 const auto queryKey2 = getWakeLockQueryKey(1, uid2, conditionName);
449 conditionCache[0] = ConditionState::kNotEvaluated;
450
Stefan Lafon12d01fa2017-12-04 20:56:09 -0800451 conditionTracker.isConditionMet(queryKey, allPredicates, conditionCache);
Yao Chen967b2052017-11-07 16:36:43 -0800452 EXPECT_EQ(ConditionState::kTrue, conditionCache[0]);
453
454
455 // stop all event
456 LogEvent event3(2 /*tagId*/, 0 /*timestamp*/);
457 matcherState.clear();
458 matcherState.push_back(MatchingState::kNotMatched);
459 matcherState.push_back(MatchingState::kNotMatched);
460 matcherState.push_back(MatchingState::kMatched);
461
462 conditionCache[0] = ConditionState::kNotEvaluated;
463 changedCache[0] = false;
Stefan Lafon12d01fa2017-12-04 20:56:09 -0800464 conditionTracker.evaluateCondition(event3, matcherState, allPredicates, conditionCache,
Yao Chen967b2052017-11-07 16:36:43 -0800465 changedCache);
466 EXPECT_TRUE(changedCache[0]);
467 EXPECT_EQ(0UL, conditionTracker.mSlicedConditionState.size());
468
469 // TEST QUERY
470 const auto queryKey3 = getWakeLockQueryKey(1, uid1, conditionName);
471 conditionCache[0] = ConditionState::kNotEvaluated;
472
Stefan Lafon12d01fa2017-12-04 20:56:09 -0800473 conditionTracker.isConditionMet(queryKey, allPredicates, conditionCache);
Yao Chen967b2052017-11-07 16:36:43 -0800474 EXPECT_EQ(ConditionState::kFalse, conditionCache[0]);
475
476 // TEST QUERY
477 const auto queryKey4 = getWakeLockQueryKey(1, uid2, conditionName);
478 conditionCache[0] = ConditionState::kNotEvaluated;
479
Stefan Lafon12d01fa2017-12-04 20:56:09 -0800480 conditionTracker.isConditionMet(queryKey, allPredicates, conditionCache);
Yao Chen967b2052017-11-07 16:36:43 -0800481 EXPECT_EQ(ConditionState::kFalse, conditionCache[0]);
482}
483
484} // namespace statsd
485} // namespace os
486} // namespace android
487#else
488GTEST_LOG_(INFO) << "This test does nothing.\n";
489#endif