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 "src/condition/StateTracker.h"
16 #include "tests/statsd_test_util.h"
17
18 #include <gmock/gmock.h>
19 #include <gtest/gtest.h>
20 #include <stdio.h>
21 #include <numeric>
22 #include <vector>
23
24 using std::map;
25 using std::unordered_map;
26 using std::vector;
27
28 #ifdef __ANDROID__
29 namespace android {
30 namespace os {
31 namespace statsd {
32
33 const int kUidProcTag = 27;
34
getUidProcStatePredicate()35 SimplePredicate getUidProcStatePredicate() {
36 SimplePredicate simplePredicate;
37 simplePredicate.set_start(StringToId("UidProcState"));
38
39 simplePredicate.mutable_dimensions()->set_field(kUidProcTag);
40 simplePredicate.mutable_dimensions()->add_child()->set_field(1);
41 simplePredicate.mutable_dimensions()->add_child()->set_field(2);
42
43 simplePredicate.set_count_nesting(false);
44 return simplePredicate;
45 }
46
makeUidProcStateEvent(int32_t uid,int32_t state,LogEvent * event)47 void makeUidProcStateEvent(int32_t uid, int32_t state, LogEvent* event) {
48 event->write(uid);
49 event->write(state);
50 event->init();
51 }
52
TEST(StateTrackerTest,TestStateChange)53 TEST(StateTrackerTest, TestStateChange) {
54 int uid1 = 111;
55 int uid2 = 222;
56
57 int state1 = 1001;
58 int state2 = 1002;
59 unordered_map<int64_t, int> trackerNameIndexMap;
60 trackerNameIndexMap[StringToId("UidProcState")] = 0;
61 vector<Matcher> primaryFields;
62 primaryFields.push_back(getSimpleMatcher(kUidProcTag, 1));
63 StateTracker tracker(ConfigKey(12, 123), 123, 0, getUidProcStatePredicate(),
64 trackerNameIndexMap, primaryFields);
65
66 LogEvent event(kUidProcTag, 0 /*timestamp*/);
67 makeUidProcStateEvent(uid1, state1, &event);
68
69 vector<MatchingState> matcherState;
70 matcherState.push_back(MatchingState::kMatched);
71 vector<sp<ConditionTracker>> allPredicates;
72 vector<ConditionState> conditionCache(1, ConditionState::kNotEvaluated);
73 vector<bool> changedCache(1, false);
74
75 tracker.evaluateCondition(event, matcherState, allPredicates, conditionCache, changedCache);
76 EXPECT_EQ(1ULL, tracker.mLastChangedToTrueDimensions.size());
77 EXPECT_EQ(0ULL, tracker.mLastChangedToFalseDimensions.size());
78 EXPECT_TRUE(changedCache[0]);
79
80 changedCache[0] = false;
81 conditionCache[0] = ConditionState::kNotEvaluated;
82 tracker.evaluateCondition(event, matcherState, allPredicates, conditionCache, changedCache);
83 EXPECT_EQ(0ULL, tracker.mLastChangedToTrueDimensions.size());
84 EXPECT_EQ(0ULL, tracker.mLastChangedToFalseDimensions.size());
85 EXPECT_FALSE(changedCache[0]);
86
87 LogEvent event2(kUidProcTag, 0 /*timestamp*/);
88 makeUidProcStateEvent(uid1, state2, &event2);
89
90 changedCache[0] = false;
91 conditionCache[0] = ConditionState::kNotEvaluated;
92 tracker.evaluateCondition(event2, matcherState, allPredicates, conditionCache, changedCache);
93 EXPECT_EQ(1ULL, tracker.mLastChangedToTrueDimensions.size());
94 EXPECT_EQ(1ULL, tracker.mLastChangedToFalseDimensions.size());
95 EXPECT_TRUE(changedCache[0]);
96
97 LogEvent event3(kUidProcTag, 0 /*timestamp*/);
98 makeUidProcStateEvent(uid2, state1, &event3);
99 changedCache[0] = false;
100 conditionCache[0] = ConditionState::kNotEvaluated;
101 tracker.evaluateCondition(event3, matcherState, allPredicates, conditionCache, changedCache);
102 EXPECT_EQ(1ULL, tracker.mLastChangedToTrueDimensions.size());
103 EXPECT_EQ(0ULL, tracker.mLastChangedToFalseDimensions.size());
104 EXPECT_TRUE(changedCache[0]);
105 }
106
107 } // namespace statsd
108 } // namespace os
109 } // namespace android
110 #else
111 GTEST_LOG_(INFO) << "This test does nothing.\n";
112 #endif
113