summaryrefslogtreecommitdiff
path: root/cmds/statsd/tests/condition/StateTracker_test.cpp
blob: 9a66254afce0cc9107421cfa02da31ef884b54eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright (C) 2017 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "src/condition/StateTracker.h"
#include "tests/statsd_test_util.h"

#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <stdio.h>
#include <numeric>
#include <vector>

using std::map;
using std::unordered_map;
using std::vector;

#ifdef __ANDROID__
namespace android {
namespace os {
namespace statsd {

const int kUidProcTag = 27;

SimplePredicate getUidProcStatePredicate() {
    SimplePredicate simplePredicate;
    simplePredicate.set_start(StringToId("UidProcState"));

    simplePredicate.mutable_dimensions()->set_field(kUidProcTag);
    simplePredicate.mutable_dimensions()->add_child()->set_field(1);
    simplePredicate.mutable_dimensions()->add_child()->set_field(2);

    simplePredicate.set_count_nesting(false);
    return simplePredicate;
}

void makeUidProcStateEvent(int32_t uid, int32_t state, LogEvent* event) {
    event->write(uid);
    event->write(state);
    event->init();
}

TEST(StateTrackerTest, TestStateChange) {
    int uid1 = 111;
    int uid2 = 222;

    int state1 = 1001;
    int state2 = 1002;
    unordered_map<int64_t, int> trackerNameIndexMap;
    trackerNameIndexMap[StringToId("UidProcState")] = 0;
    vector<Matcher> primaryFields;
    primaryFields.push_back(getSimpleMatcher(kUidProcTag, 1));
    StateTracker tracker(ConfigKey(12, 123), 123, 0, getUidProcStatePredicate(),
                         trackerNameIndexMap, primaryFields);

    LogEvent event(kUidProcTag, 0 /*timestamp*/);
    makeUidProcStateEvent(uid1, state1, &event);

    vector<MatchingState> matcherState;
    matcherState.push_back(MatchingState::kMatched);
    vector<sp<ConditionTracker>> allPredicates;
    vector<ConditionState> conditionCache(1, ConditionState::kNotEvaluated);
    vector<bool> changedCache(1, false);

    tracker.evaluateCondition(event, matcherState, allPredicates, conditionCache, changedCache);
    EXPECT_EQ(1ULL, tracker.mLastChangedToTrueDimensions.size());
    EXPECT_EQ(0ULL, tracker.mLastChangedToFalseDimensions.size());
    EXPECT_TRUE(changedCache[0]);

    changedCache[0] = false;
    conditionCache[0] = ConditionState::kNotEvaluated;
    tracker.evaluateCondition(event, matcherState, allPredicates, conditionCache, changedCache);
    EXPECT_EQ(0ULL, tracker.mLastChangedToTrueDimensions.size());
    EXPECT_EQ(0ULL, tracker.mLastChangedToFalseDimensions.size());
    EXPECT_FALSE(changedCache[0]);

    LogEvent event2(kUidProcTag, 0 /*timestamp*/);
    makeUidProcStateEvent(uid1, state2, &event2);

    changedCache[0] = false;
    conditionCache[0] = ConditionState::kNotEvaluated;
    tracker.evaluateCondition(event2, matcherState, allPredicates, conditionCache, changedCache);
    EXPECT_EQ(1ULL, tracker.mLastChangedToTrueDimensions.size());
    EXPECT_EQ(1ULL, tracker.mLastChangedToFalseDimensions.size());
    EXPECT_TRUE(changedCache[0]);

    LogEvent event3(kUidProcTag, 0 /*timestamp*/);
    makeUidProcStateEvent(uid2, state1, &event3);
    changedCache[0] = false;
    conditionCache[0] = ConditionState::kNotEvaluated;
    tracker.evaluateCondition(event3, matcherState, allPredicates, conditionCache, changedCache);
    EXPECT_EQ(1ULL, tracker.mLastChangedToTrueDimensions.size());
    EXPECT_EQ(0ULL, tracker.mLastChangedToFalseDimensions.size());
    EXPECT_TRUE(changedCache[0]);
}

}  // namespace statsd
}  // namespace os
}  // namespace android
#else
GTEST_LOG_(INFO) << "This test does nothing.\n";
#endif