blob: 32661dcc47525a7363586b4da710a77e09a7cb6a [file] [log] [blame]
Yao Chencaf339d2017-10-06 16:01:10 -07001// 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
Yao Chencaf339d2017-10-06 16:01:10 -070015#include <gtest/gtest.h>
Joe Onorato9fc9edf2017-10-15 20:08:52 -070016
17#include "src/condition/ConditionTracker.h"
18#include "src/matchers/LogMatchingTracker.h"
19#include "src/metrics/CountMetricProducer.h"
20#include "src/metrics/MetricProducer.h"
21#include "src/metrics/metrics_manager_util.h"
Yao Chencaf339d2017-10-06 16:01:10 -070022
23#include "frameworks/base/cmds/statsd/src/statsd_config.pb.h"
24
25#include <stdio.h>
26#include <set>
27#include <unordered_map>
28#include <vector>
29
30using namespace android::os::statsd;
31using android::sp;
32using std::set;
33using std::unordered_map;
34using std::vector;
35
36#ifdef __ANDROID__
37
38// TODO: ADD MORE TEST CASES.
39
40StatsdConfig buildGoodConfig() {
41 StatsdConfig config;
42 config.set_config_id(12345L);
43
44 LogEntryMatcher* eventMatcher = config.add_log_entry_matcher();
45 eventMatcher->set_name("SCREEN_IS_ON");
46
47 SimpleLogEntryMatcher* simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
48 simpleLogEntryMatcher->add_tag(2 /*SCREEN_STATE_CHANGE*/);
49 simpleLogEntryMatcher->add_key_value_matcher()->mutable_key_matcher()->set_key(
50 1 /*SCREEN_STATE_CHANGE__DISPLAY_STATE*/);
51 simpleLogEntryMatcher->mutable_key_value_matcher(0)->set_eq_int(
52 2 /*SCREEN_STATE_CHANGE__DISPLAY_STATE__STATE_ON*/);
53
54 eventMatcher = config.add_log_entry_matcher();
55 eventMatcher->set_name("SCREEN_IS_OFF");
56
57 simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
58 simpleLogEntryMatcher->add_tag(2 /*SCREEN_STATE_CHANGE*/);
59 simpleLogEntryMatcher->add_key_value_matcher()->mutable_key_matcher()->set_key(
60 1 /*SCREEN_STATE_CHANGE__DISPLAY_STATE*/);
61 simpleLogEntryMatcher->mutable_key_value_matcher(0)->set_eq_int(
62 1 /*SCREEN_STATE_CHANGE__DISPLAY_STATE__STATE_OFF*/);
63
64 eventMatcher = config.add_log_entry_matcher();
65 eventMatcher->set_name("SCREEN_ON_OR_OFF");
66
67 LogEntryMatcher_Combination* combination = eventMatcher->mutable_combination();
68 combination->set_operation(LogicalOperation::OR);
69 combination->add_matcher("SCREEN_IS_ON");
70 combination->add_matcher("SCREEN_IS_OFF");
71
72 return config;
73}
74
75StatsdConfig buildCircleMatchers() {
76 StatsdConfig config;
77 config.set_config_id(12345L);
78
79 LogEntryMatcher* eventMatcher = config.add_log_entry_matcher();
80 eventMatcher->set_name("SCREEN_IS_ON");
81
82 SimpleLogEntryMatcher* simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
83 simpleLogEntryMatcher->add_tag(2 /*SCREEN_STATE_CHANGE*/);
84 simpleLogEntryMatcher->add_key_value_matcher()->mutable_key_matcher()->set_key(
85 1 /*SCREEN_STATE_CHANGE__DISPLAY_STATE*/);
86 simpleLogEntryMatcher->mutable_key_value_matcher(0)->set_eq_int(
87 2 /*SCREEN_STATE_CHANGE__DISPLAY_STATE__STATE_ON*/);
88
89 eventMatcher = config.add_log_entry_matcher();
90 eventMatcher->set_name("SCREEN_ON_OR_OFF");
91
92 LogEntryMatcher_Combination* combination = eventMatcher->mutable_combination();
93 combination->set_operation(LogicalOperation::OR);
94 combination->add_matcher("SCREEN_IS_ON");
95 // Circle dependency
96 combination->add_matcher("SCREEN_ON_OR_OFF");
97
98 return config;
99}
100
101StatsdConfig buildMissingMatchers() {
102 StatsdConfig config;
103 config.set_config_id(12345L);
104
105 LogEntryMatcher* eventMatcher = config.add_log_entry_matcher();
106 eventMatcher->set_name("SCREEN_IS_ON");
107
108 SimpleLogEntryMatcher* simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
109 simpleLogEntryMatcher->add_tag(2 /*SCREEN_STATE_CHANGE*/);
110 simpleLogEntryMatcher->add_key_value_matcher()->mutable_key_matcher()->set_key(
111 1 /*SCREEN_STATE_CHANGE__DISPLAY_STATE*/);
112 simpleLogEntryMatcher->mutable_key_value_matcher(0)->set_eq_int(
113 2 /*SCREEN_STATE_CHANGE__DISPLAY_STATE__STATE_ON*/);
114
115 eventMatcher = config.add_log_entry_matcher();
116 eventMatcher->set_name("SCREEN_ON_OR_OFF");
117
118 LogEntryMatcher_Combination* combination = eventMatcher->mutable_combination();
119 combination->set_operation(LogicalOperation::OR);
120 combination->add_matcher("SCREEN_IS_ON");
121 // undefined matcher
122 combination->add_matcher("ABC");
123
124 return config;
125}
126
127StatsdConfig buildCircleConditions() {
128 StatsdConfig config;
129 config.set_config_id(12345L);
130
131 LogEntryMatcher* eventMatcher = config.add_log_entry_matcher();
132 eventMatcher->set_name("SCREEN_IS_ON");
133
134 SimpleLogEntryMatcher* simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
135 simpleLogEntryMatcher->add_tag(2 /*SCREEN_STATE_CHANGE*/);
136 simpleLogEntryMatcher->add_key_value_matcher()->mutable_key_matcher()->set_key(
137 1 /*SCREEN_STATE_CHANGE__DISPLAY_STATE*/);
138 simpleLogEntryMatcher->mutable_key_value_matcher(0)->set_eq_int(
139 2 /*SCREEN_STATE_CHANGE__DISPLAY_STATE__STATE_ON*/);
140
141 eventMatcher = config.add_log_entry_matcher();
142 eventMatcher->set_name("SCREEN_IS_OFF");
143
144 simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
145 simpleLogEntryMatcher->add_tag(2 /*SCREEN_STATE_CHANGE*/);
146 simpleLogEntryMatcher->add_key_value_matcher()->mutable_key_matcher()->set_key(
147 1 /*SCREEN_STATE_CHANGE__DISPLAY_STATE*/);
148 simpleLogEntryMatcher->mutable_key_value_matcher(0)->set_eq_int(
149 1 /*SCREEN_STATE_CHANGE__DISPLAY_STATE__STATE_OFF*/);
150
151 Condition* condition = config.add_condition();
152 condition->set_name("SCREEN_IS_ON");
153 SimpleCondition* simpleCondition = condition->mutable_simple_condition();
154 simpleCondition->set_start("SCREEN_IS_ON");
155 simpleCondition->set_stop("SCREEN_IS_OFF");
156
157 condition = config.add_condition();
158 condition->set_name("SCREEN_IS_EITHER_ON_OFF");
159
160 Condition_Combination* combination = condition->mutable_combination();
161 combination->set_operation(LogicalOperation::OR);
162 combination->add_condition("SCREEN_IS_ON");
163 combination->add_condition("SCREEN_IS_EITHER_ON_OFF");
164
165 return config;
166}
167
168TEST(MetricsManagerTest, TestGoodConfig) {
169 StatsdConfig config = buildGoodConfig();
170 set<int> allTagIds;
171 vector<sp<LogMatchingTracker>> allLogEntryMatchers;
172 vector<sp<ConditionTracker>> allConditionTrackers;
173 vector<sp<MetricProducer>> allMetricProducers;
174 unordered_map<int, std::vector<int>> conditionToMetricMap;
175 unordered_map<int, std::vector<int>> trackerToMetricMap;
176 unordered_map<int, std::vector<int>> trackerToConditionMap;
177
178 EXPECT_TRUE(initStatsdConfig(config, allTagIds, allLogEntryMatchers, allConditionTrackers,
179 allMetricProducers, conditionToMetricMap, trackerToMetricMap,
180 trackerToConditionMap));
181}
182
183TEST(MetricsManagerTest, TestCircleLogMatcherDependency) {
184 StatsdConfig config = buildCircleMatchers();
185 set<int> allTagIds;
186 vector<sp<LogMatchingTracker>> allLogEntryMatchers;
187 vector<sp<ConditionTracker>> allConditionTrackers;
188 vector<sp<MetricProducer>> allMetricProducers;
189 unordered_map<int, std::vector<int>> conditionToMetricMap;
190 unordered_map<int, std::vector<int>> trackerToMetricMap;
191 unordered_map<int, std::vector<int>> trackerToConditionMap;
192
193 EXPECT_FALSE(initStatsdConfig(config, allTagIds, allLogEntryMatchers, allConditionTrackers,
194 allMetricProducers, conditionToMetricMap, trackerToMetricMap,
195 trackerToConditionMap));
196}
197
198TEST(MetricsManagerTest, TestMissingMatchers) {
199 StatsdConfig config = buildMissingMatchers();
200 set<int> allTagIds;
201 vector<sp<LogMatchingTracker>> allLogEntryMatchers;
202 vector<sp<ConditionTracker>> allConditionTrackers;
203 vector<sp<MetricProducer>> allMetricProducers;
204 unordered_map<int, std::vector<int>> conditionToMetricMap;
205 unordered_map<int, std::vector<int>> trackerToMetricMap;
206 unordered_map<int, std::vector<int>> trackerToConditionMap;
207
208 EXPECT_FALSE(initStatsdConfig(config, allTagIds, allLogEntryMatchers, allConditionTrackers,
209 allMetricProducers, conditionToMetricMap, trackerToMetricMap,
210 trackerToConditionMap));
211}
212
213TEST(MetricsManagerTest, TestCircleConditionDependency) {
214 StatsdConfig config = buildCircleConditions();
215 set<int> allTagIds;
216 vector<sp<LogMatchingTracker>> allLogEntryMatchers;
217 vector<sp<ConditionTracker>> allConditionTrackers;
218 vector<sp<MetricProducer>> allMetricProducers;
219 unordered_map<int, std::vector<int>> conditionToMetricMap;
220 unordered_map<int, std::vector<int>> trackerToMetricMap;
221 unordered_map<int, std::vector<int>> trackerToConditionMap;
222
223 EXPECT_FALSE(initStatsdConfig(config, allTagIds, allLogEntryMatchers, allConditionTrackers,
224 allMetricProducers, conditionToMetricMap, trackerToMetricMap,
225 trackerToConditionMap));
226}
227
228#else
229GTEST_LOG_(INFO) << "This test does nothing.\n";
230#endif