1 // Copyright (C) 2018 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 <gtest/gtest.h>
16 
17 #include "src/StatsLogProcessor.h"
18 #include "src/stats_log_util.h"
19 #include "tests/statsd_test_util.h"
20 
21 #include <vector>
22 
23 namespace android {
24 namespace os {
25 namespace statsd {
26 
27 #ifdef __ANDROID__
28 
29 namespace {
30 
CreateStatsdConfig()31 StatsdConfig CreateStatsdConfig() {
32     StatsdConfig config;
33     config.add_allowed_log_source("AID_ROOT"); // LogEvent defaults to UID of root.
34     auto saverModeMatcher = CreateBatterySaverModeStartAtomMatcher();
35     auto crashMatcher = CreateProcessCrashAtomMatcher();
36     auto screenOnMatcher = CreateScreenTurnedOnAtomMatcher();
37 
38     *config.add_atom_matcher() = saverModeMatcher;
39     *config.add_atom_matcher() = crashMatcher;
40     *config.add_atom_matcher() = screenOnMatcher;
41 
42     int64_t metricId = 123456;
43     auto countMetric = config.add_count_metric();
44     countMetric->set_id(metricId);
45     countMetric->set_what(crashMatcher.id());
46     countMetric->set_bucket(FIVE_MINUTES);
47     countMetric->mutable_dimensions_in_what()->set_field(
48         util::PROCESS_LIFE_CYCLE_STATE_CHANGED);
49     countMetric->mutable_dimensions_in_what()->add_child()->set_field(1);  // uid field
50 
51     auto metric_activation1 = config.add_metric_activation();
52     metric_activation1->set_metric_id(metricId);
53     auto event_activation1 = metric_activation1->add_event_activation();
54     event_activation1->set_atom_matcher_id(saverModeMatcher.id());
55     event_activation1->set_ttl_seconds(60 * 6);  // 6 minutes
56     auto event_activation2 = metric_activation1->add_event_activation();
57     event_activation2->set_atom_matcher_id(screenOnMatcher.id());
58     event_activation2->set_ttl_seconds(60 * 2);  // 2 minutes
59 
60     return config;
61 }
62 
CreateStatsdConfigWithOneDeactivation()63 StatsdConfig CreateStatsdConfigWithOneDeactivation() {
64     StatsdConfig config;
65     config.add_allowed_log_source("AID_ROOT"); // LogEvent defaults to UID of root.
66     auto saverModeMatcher = CreateBatterySaverModeStartAtomMatcher();
67     auto crashMatcher = CreateProcessCrashAtomMatcher();
68     auto screenOnMatcher = CreateScreenTurnedOnAtomMatcher();
69     auto brightnessChangedMatcher = CreateScreenBrightnessChangedAtomMatcher();
70 
71     *config.add_atom_matcher() = saverModeMatcher;
72     *config.add_atom_matcher() = crashMatcher;
73     *config.add_atom_matcher() = screenOnMatcher;
74     *config.add_atom_matcher() = brightnessChangedMatcher;
75 
76     int64_t metricId = 123456;
77     auto countMetric = config.add_count_metric();
78     countMetric->set_id(metricId);
79     countMetric->set_what(crashMatcher.id());
80     countMetric->set_bucket(FIVE_MINUTES);
81     countMetric->mutable_dimensions_in_what()->set_field(
82         util::PROCESS_LIFE_CYCLE_STATE_CHANGED);
83     countMetric->mutable_dimensions_in_what()->add_child()->set_field(1);  // uid field
84 
85     auto metric_activation1 = config.add_metric_activation();
86     metric_activation1->set_metric_id(metricId);
87     auto event_activation1 = metric_activation1->add_event_activation();
88     event_activation1->set_atom_matcher_id(saverModeMatcher.id());
89     event_activation1->set_ttl_seconds(60 * 6);  // 6 minutes
90     event_activation1->set_deactivation_atom_matcher_id(brightnessChangedMatcher.id());
91     auto event_activation2 = metric_activation1->add_event_activation();
92     event_activation2->set_atom_matcher_id(screenOnMatcher.id());
93     event_activation2->set_ttl_seconds(60 * 2);  // 2 minutes
94 
95     return config;
96 }
97 
CreateStatsdConfigWithTwoDeactivations()98 StatsdConfig CreateStatsdConfigWithTwoDeactivations() {
99     StatsdConfig config;
100     config.add_allowed_log_source("AID_ROOT"); // LogEvent defaults to UID of root.
101     auto saverModeMatcher = CreateBatterySaverModeStartAtomMatcher();
102     auto crashMatcher = CreateProcessCrashAtomMatcher();
103     auto screenOnMatcher = CreateScreenTurnedOnAtomMatcher();
104     auto brightnessChangedMatcher = CreateScreenBrightnessChangedAtomMatcher();
105     auto brightnessChangedMatcher2 = CreateScreenBrightnessChangedAtomMatcher();
106     brightnessChangedMatcher2.set_id(StringToId("ScreenBrightnessChanged2"));
107 
108     *config.add_atom_matcher() = saverModeMatcher;
109     *config.add_atom_matcher() = crashMatcher;
110     *config.add_atom_matcher() = screenOnMatcher;
111     *config.add_atom_matcher() = brightnessChangedMatcher;
112     *config.add_atom_matcher() = brightnessChangedMatcher2;
113 
114     int64_t metricId = 123456;
115     auto countMetric = config.add_count_metric();
116     countMetric->set_id(metricId);
117     countMetric->set_what(crashMatcher.id());
118     countMetric->set_bucket(FIVE_MINUTES);
119     countMetric->mutable_dimensions_in_what()->set_field(
120         util::PROCESS_LIFE_CYCLE_STATE_CHANGED);
121     countMetric->mutable_dimensions_in_what()->add_child()->set_field(1);  // uid field
122 
123     auto metric_activation1 = config.add_metric_activation();
124     metric_activation1->set_metric_id(metricId);
125     auto event_activation1 = metric_activation1->add_event_activation();
126     event_activation1->set_atom_matcher_id(saverModeMatcher.id());
127     event_activation1->set_ttl_seconds(60 * 6);  // 6 minutes
128     event_activation1->set_deactivation_atom_matcher_id(brightnessChangedMatcher.id());
129     auto event_activation2 = metric_activation1->add_event_activation();
130     event_activation2->set_atom_matcher_id(screenOnMatcher.id());
131     event_activation2->set_ttl_seconds(60 * 2);  // 2 minutes
132     event_activation2->set_deactivation_atom_matcher_id(brightnessChangedMatcher2.id());
133 
134     return config;
135 }
136 
CreateStatsdConfigWithSameDeactivations()137 StatsdConfig CreateStatsdConfigWithSameDeactivations() {
138     StatsdConfig config;
139     config.add_allowed_log_source("AID_ROOT"); // LogEvent defaults to UID of root.
140     auto saverModeMatcher = CreateBatterySaverModeStartAtomMatcher();
141     auto crashMatcher = CreateProcessCrashAtomMatcher();
142     auto screenOnMatcher = CreateScreenTurnedOnAtomMatcher();
143     auto brightnessChangedMatcher = CreateScreenBrightnessChangedAtomMatcher();
144 
145     *config.add_atom_matcher() = saverModeMatcher;
146     *config.add_atom_matcher() = crashMatcher;
147     *config.add_atom_matcher() = screenOnMatcher;
148     *config.add_atom_matcher() = brightnessChangedMatcher;
149 
150     int64_t metricId = 123456;
151     auto countMetric = config.add_count_metric();
152     countMetric->set_id(metricId);
153     countMetric->set_what(crashMatcher.id());
154     countMetric->set_bucket(FIVE_MINUTES);
155     countMetric->mutable_dimensions_in_what()->set_field(
156         util::PROCESS_LIFE_CYCLE_STATE_CHANGED);
157     countMetric->mutable_dimensions_in_what()->add_child()->set_field(1);  // uid field
158 
159     auto metric_activation1 = config.add_metric_activation();
160     metric_activation1->set_metric_id(metricId);
161     auto event_activation1 = metric_activation1->add_event_activation();
162     event_activation1->set_atom_matcher_id(saverModeMatcher.id());
163     event_activation1->set_ttl_seconds(60 * 6);  // 6 minutes
164     event_activation1->set_deactivation_atom_matcher_id(brightnessChangedMatcher.id());
165     auto event_activation2 = metric_activation1->add_event_activation();
166     event_activation2->set_atom_matcher_id(screenOnMatcher.id());
167     event_activation2->set_ttl_seconds(60 * 2);  // 2 minutes
168     event_activation2->set_deactivation_atom_matcher_id(brightnessChangedMatcher.id());
169 
170     return config;
171 }
172 
CreateStatsdConfigWithTwoMetricsTwoDeactivations()173 StatsdConfig CreateStatsdConfigWithTwoMetricsTwoDeactivations() {
174     StatsdConfig config;
175     config.add_allowed_log_source("AID_ROOT"); // LogEvent defaults to UID of root.
176     auto saverModeMatcher = CreateBatterySaverModeStartAtomMatcher();
177     auto crashMatcher = CreateProcessCrashAtomMatcher();
178     auto foregroundMatcher = CreateMoveToForegroundAtomMatcher();
179     auto screenOnMatcher = CreateScreenTurnedOnAtomMatcher();
180     auto brightnessChangedMatcher = CreateScreenBrightnessChangedAtomMatcher();
181     auto brightnessChangedMatcher2 = CreateScreenBrightnessChangedAtomMatcher();
182     brightnessChangedMatcher2.set_id(StringToId("ScreenBrightnessChanged2"));
183 
184     *config.add_atom_matcher() = saverModeMatcher;
185     *config.add_atom_matcher() = crashMatcher;
186     *config.add_atom_matcher() = screenOnMatcher;
187     *config.add_atom_matcher() = brightnessChangedMatcher;
188     *config.add_atom_matcher() = brightnessChangedMatcher2;
189     *config.add_atom_matcher() = foregroundMatcher;
190 
191     int64_t metricId = 123456;
192     auto countMetric = config.add_count_metric();
193     countMetric->set_id(metricId);
194     countMetric->set_what(crashMatcher.id());
195     countMetric->set_bucket(FIVE_MINUTES);
196     countMetric->mutable_dimensions_in_what()->set_field(
197         util::PROCESS_LIFE_CYCLE_STATE_CHANGED);
198     countMetric->mutable_dimensions_in_what()->add_child()->set_field(1);  // uid field
199 
200     int64_t metricId2 = 234567;
201     countMetric = config.add_count_metric();
202     countMetric->set_id(metricId2);
203     countMetric->set_what(foregroundMatcher.id());
204     countMetric->set_bucket(FIVE_MINUTES);
205     countMetric->mutable_dimensions_in_what()->set_field(
206         util::ACTIVITY_FOREGROUND_STATE_CHANGED);
207     countMetric->mutable_dimensions_in_what()->add_child()->set_field(1);  // uid field
208 
209     auto metric_activation1 = config.add_metric_activation();
210     metric_activation1->set_metric_id(metricId);
211     auto event_activation1 = metric_activation1->add_event_activation();
212     event_activation1->set_atom_matcher_id(saverModeMatcher.id());
213     event_activation1->set_ttl_seconds(60 * 6);  // 6 minutes
214     event_activation1->set_deactivation_atom_matcher_id(brightnessChangedMatcher.id());
215     auto event_activation2 = metric_activation1->add_event_activation();
216     event_activation2->set_atom_matcher_id(screenOnMatcher.id());
217     event_activation2->set_ttl_seconds(60 * 2);  // 2 minutes
218     event_activation2->set_deactivation_atom_matcher_id(brightnessChangedMatcher2.id());
219 
220     metric_activation1 = config.add_metric_activation();
221     metric_activation1->set_metric_id(metricId2);
222     event_activation1 = metric_activation1->add_event_activation();
223     event_activation1->set_atom_matcher_id(saverModeMatcher.id());
224     event_activation1->set_ttl_seconds(60 * 6);  // 6 minutes
225     event_activation1->set_deactivation_atom_matcher_id(brightnessChangedMatcher.id());
226     event_activation2 = metric_activation1->add_event_activation();
227     event_activation2->set_atom_matcher_id(screenOnMatcher.id());
228     event_activation2->set_ttl_seconds(60 * 2);  // 2 minutes
229     event_activation2->set_deactivation_atom_matcher_id(brightnessChangedMatcher2.id());
230 
231     return config;
232 }
233 
234 }  // namespace
235 
TEST(MetricActivationE2eTest,TestCountMetric)236 TEST(MetricActivationE2eTest, TestCountMetric) {
237     auto config = CreateStatsdConfig();
238 
239     int64_t bucketStartTimeNs = NS_PER_SEC * 10;  // 10 secs
240     int64_t bucketSizeNs =
241             TimeUnitToBucketSizeInMillis(config.count_metric(0).bucket()) * 1000LL * 1000LL;
242 
243     int uid = 12345;
244     int64_t cfgId = 98765;
245     ConfigKey cfgKey(uid, cfgId);
246 
247     sp<UidMap> m = new UidMap();
248     sp<StatsPullerManager> pullerManager = new StatsPullerManager();
249     sp<AlarmMonitor> anomalyAlarmMonitor;
250     sp<AlarmMonitor> subscriberAlarmMonitor;
251     vector<int64_t> activeConfigsBroadcast;
252 
253     long timeBase1 = 1;
254     int broadcastCount = 0;
255     StatsLogProcessor processor(
256             m, pullerManager, anomalyAlarmMonitor, subscriberAlarmMonitor, bucketStartTimeNs,
257             [](const ConfigKey& key) { return true; },
258             [&uid, &broadcastCount, &activeConfigsBroadcast](const int& broadcastUid,
259                                                              const vector<int64_t>& activeConfigs) {
260                 broadcastCount++;
261                 EXPECT_EQ(broadcastUid, uid);
262                 activeConfigsBroadcast.clear();
263                 activeConfigsBroadcast.insert(activeConfigsBroadcast.end(), activeConfigs.begin(),
264                                               activeConfigs.end());
265                 return true;
266             });
267 
268     processor.OnConfigUpdated(bucketStartTimeNs, cfgKey, config);
269 
270     ASSERT_EQ(processor.mMetricsManagers.size(), 1u);
271     sp<MetricsManager> metricsManager = processor.mMetricsManagers.begin()->second;
272     EXPECT_TRUE(metricsManager->isConfigValid());
273     ASSERT_EQ(metricsManager->mAllMetricProducers.size(), 1);
274     sp<MetricProducer> metricProducer = metricsManager->mAllMetricProducers[0];
275     auto& eventActivationMap = metricProducer->mEventActivationMap;
276 
277     EXPECT_FALSE(metricsManager->isActive());
278     EXPECT_FALSE(metricProducer->mIsActive);
279     // Two activations: one is triggered by battery saver mode (tracker index 0), the other is
280     // triggered by screen on event (tracker index 2).
281     ASSERT_EQ(eventActivationMap.size(), 2u);
282     EXPECT_TRUE(eventActivationMap.find(0) != eventActivationMap.end());
283     EXPECT_TRUE(eventActivationMap.find(2) != eventActivationMap.end());
284     EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kNotActive);
285     EXPECT_EQ(eventActivationMap[0]->start_ns, 0);
286     EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
287     EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kNotActive);
288     EXPECT_EQ(eventActivationMap[2]->start_ns, 0);
289     EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
290 
291     std::unique_ptr<LogEvent> event;
292 
293     event = CreateAppCrashEvent(bucketStartTimeNs + 5, 111);
294     processor.OnLogEvent(event.get(), bucketStartTimeNs + 5);
295     EXPECT_FALSE(metricsManager->isActive());
296     EXPECT_FALSE(metricProducer->mIsActive);
297     EXPECT_EQ(broadcastCount, 0);
298 
299     // Activated by battery save mode.
300     event = CreateBatterySaverOnEvent(bucketStartTimeNs + 10);
301     processor.OnLogEvent(event.get(), bucketStartTimeNs + 10);
302     EXPECT_TRUE(metricsManager->isActive());
303     EXPECT_TRUE(metricProducer->mIsActive);
304     EXPECT_EQ(broadcastCount, 1);
305     ASSERT_EQ(activeConfigsBroadcast.size(), 1);
306     EXPECT_EQ(activeConfigsBroadcast[0], cfgId);
307     EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kActive);
308     EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + 10);
309     EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
310     EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kNotActive);
311     EXPECT_EQ(eventActivationMap[2]->start_ns, 0);
312     EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
313 
314     // First processed event.
315     event = CreateAppCrashEvent(bucketStartTimeNs + 15, 222);
316     processor.OnLogEvent(event.get(), bucketStartTimeNs + 15);
317 
318     // Activated by screen on event.
319     event = CreateScreenStateChangedEvent(bucketStartTimeNs + 20, android::view::DISPLAY_STATE_ON);
320     processor.OnLogEvent(event.get(), bucketStartTimeNs + 20);
321     EXPECT_TRUE(metricsManager->isActive());
322     EXPECT_TRUE(metricProducer->mIsActive);
323     EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kActive);
324     EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + 10);
325     EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
326     EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kActive);
327     EXPECT_EQ(eventActivationMap[2]->start_ns, bucketStartTimeNs + 20);
328     EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
329 
330     // 2nd processed event.
331     // The activation by screen_on event expires, but the one by battery save mode is still active.
332     event = CreateAppCrashEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 2 + 25, 333);
333     processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 2 + 25);
334     EXPECT_TRUE(metricsManager->isActive());
335     EXPECT_TRUE(metricProducer->mIsActive);
336     EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kActive);
337     EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + 10);
338     EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
339     EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kNotActive);
340     EXPECT_EQ(eventActivationMap[2]->start_ns, bucketStartTimeNs + 20);
341     EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
342     // No new broadcast since the config should still be active.
343     EXPECT_EQ(broadcastCount, 1);
344 
345     // 3rd processed event.
346     event = CreateAppCrashEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 5 + 25, 444);
347     processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 5 + 25);
348 
349     // All activations expired.
350     event = CreateAppCrashEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 8, 555);
351     processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 8);
352     EXPECT_FALSE(metricsManager->isActive());
353     EXPECT_FALSE(metricProducer->mIsActive);
354     // New broadcast since the config is no longer active.
355     EXPECT_EQ(broadcastCount, 2);
356     ASSERT_EQ(activeConfigsBroadcast.size(), 0);
357     EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kNotActive);
358     EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + 10);
359     EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
360     EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kNotActive);
361     EXPECT_EQ(eventActivationMap[2]->start_ns, bucketStartTimeNs + 20);
362     EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
363 
364     // Re-activate metric via screen on.
365     event = CreateScreenStateChangedEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 10,
366                                           android::view::DISPLAY_STATE_ON);
367     processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 10);
368     EXPECT_TRUE(metricsManager->isActive());
369     EXPECT_TRUE(metricProducer->mIsActive);
370     EXPECT_EQ(broadcastCount, 3);
371     ASSERT_EQ(activeConfigsBroadcast.size(), 1);
372     EXPECT_EQ(activeConfigsBroadcast[0], cfgId);
373     EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kNotActive);
374     EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + 10);
375     EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
376     EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kActive);
377     EXPECT_EQ(eventActivationMap[2]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 10);
378     EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
379 
380     // 4th processed event.
381     event = CreateAppCrashEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 1, 666);
382     processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 1);
383 
384     ConfigMetricsReportList reports;
385     vector<uint8_t> buffer;
386     processor.onDumpReport(cfgKey, bucketStartTimeNs + NS_PER_SEC * 60 * 15 + 1, false, true,
387                            ADB_DUMP, FAST, &buffer);
388     EXPECT_TRUE(buffer.size() > 0);
389     EXPECT_TRUE(reports.ParseFromArray(&buffer[0], buffer.size()));
390     backfillDimensionPath(&reports);
391     backfillStartEndTimestamp(&reports);
392     ASSERT_EQ(1, reports.reports_size());
393     ASSERT_EQ(1, reports.reports(0).metrics_size());
394 
395     StatsLogReport::CountMetricDataWrapper countMetrics;
396     sortMetricDataByDimensionsValue(reports.reports(0).metrics(0).count_metrics(), &countMetrics);
397     ASSERT_EQ(4, countMetrics.data_size());
398 
399     auto data = countMetrics.data(0);
400     EXPECT_EQ(util::PROCESS_LIFE_CYCLE_STATE_CHANGED, data.dimensions_in_what().field());
401     ASSERT_EQ(1, data.dimensions_in_what().value_tuple().dimensions_value_size());
402     EXPECT_EQ(1 /* uid field */,
403               data.dimensions_in_what().value_tuple().dimensions_value(0).field());
404     EXPECT_EQ(222, data.dimensions_in_what().value_tuple().dimensions_value(0).value_int());
405     ASSERT_EQ(1, data.bucket_info_size());
406     EXPECT_EQ(1, data.bucket_info(0).count());
407     EXPECT_EQ(bucketStartTimeNs, data.bucket_info(0).start_bucket_elapsed_nanos());
408     EXPECT_EQ(bucketStartTimeNs + bucketSizeNs, data.bucket_info(0).end_bucket_elapsed_nanos());
409 
410     data = countMetrics.data(1);
411     EXPECT_EQ(util::PROCESS_LIFE_CYCLE_STATE_CHANGED, data.dimensions_in_what().field());
412     ASSERT_EQ(1, data.dimensions_in_what().value_tuple().dimensions_value_size());
413     EXPECT_EQ(1 /* uid field */,
414               data.dimensions_in_what().value_tuple().dimensions_value(0).field());
415     EXPECT_EQ(333, data.dimensions_in_what().value_tuple().dimensions_value(0).value_int());
416     ASSERT_EQ(1, data.bucket_info_size());
417     EXPECT_EQ(1, data.bucket_info(0).count());
418     EXPECT_EQ(bucketStartTimeNs, data.bucket_info(0).start_bucket_elapsed_nanos());
419     EXPECT_EQ(bucketStartTimeNs + bucketSizeNs, data.bucket_info(0).end_bucket_elapsed_nanos());
420 
421     data = countMetrics.data(2);
422     EXPECT_EQ(util::PROCESS_LIFE_CYCLE_STATE_CHANGED, data.dimensions_in_what().field());
423     ASSERT_EQ(1, data.dimensions_in_what().value_tuple().dimensions_value_size());
424     EXPECT_EQ(1 /* uid field */,
425               data.dimensions_in_what().value_tuple().dimensions_value(0).field());
426     EXPECT_EQ(444, data.dimensions_in_what().value_tuple().dimensions_value(0).value_int());
427     ASSERT_EQ(1, data.bucket_info_size());
428     EXPECT_EQ(1, data.bucket_info(0).count());
429     // Partial bucket as metric is deactivated.
430     EXPECT_EQ(bucketStartTimeNs + bucketSizeNs, data.bucket_info(0).start_bucket_elapsed_nanos());
431     EXPECT_EQ(bucketStartTimeNs + NS_PER_SEC * 60 * 8,
432               data.bucket_info(0).end_bucket_elapsed_nanos());
433 
434     data = countMetrics.data(3);
435     EXPECT_EQ(util::PROCESS_LIFE_CYCLE_STATE_CHANGED, data.dimensions_in_what().field());
436     ASSERT_EQ(1, data.dimensions_in_what().value_tuple().dimensions_value_size());
437     EXPECT_EQ(1 /* uid field */,
438               data.dimensions_in_what().value_tuple().dimensions_value(0).field());
439     EXPECT_EQ(666, data.dimensions_in_what().value_tuple().dimensions_value(0).value_int());
440     ASSERT_EQ(1, data.bucket_info_size());
441     EXPECT_EQ(1, data.bucket_info(0).count());
442     EXPECT_EQ(bucketStartTimeNs + 2 * bucketSizeNs,
443               data.bucket_info(0).start_bucket_elapsed_nanos());
444     EXPECT_EQ(bucketStartTimeNs + 3 * bucketSizeNs, data.bucket_info(0).end_bucket_elapsed_nanos());
445 }
446 
TEST(MetricActivationE2eTest,TestCountMetricWithOneDeactivation)447 TEST(MetricActivationE2eTest, TestCountMetricWithOneDeactivation) {
448     auto config = CreateStatsdConfigWithOneDeactivation();
449 
450     int64_t bucketStartTimeNs = NS_PER_SEC * 10;  // 10 secs
451     int64_t bucketSizeNs =
452             TimeUnitToBucketSizeInMillis(config.count_metric(0).bucket()) * 1000LL * 1000LL;
453 
454     int uid = 12345;
455     int64_t cfgId = 98765;
456     ConfigKey cfgKey(uid, cfgId);
457 
458     sp<UidMap> m = new UidMap();
459     sp<StatsPullerManager> pullerManager = new StatsPullerManager();
460     sp<AlarmMonitor> anomalyAlarmMonitor;
461     sp<AlarmMonitor> subscriberAlarmMonitor;
462     vector<int64_t> activeConfigsBroadcast;
463 
464     long timeBase1 = 1;
465     int broadcastCount = 0;
466     StatsLogProcessor processor(
467             m, pullerManager, anomalyAlarmMonitor, subscriberAlarmMonitor, bucketStartTimeNs,
468             [](const ConfigKey& key) { return true; },
469             [&uid, &broadcastCount, &activeConfigsBroadcast](const int& broadcastUid,
470                                                              const vector<int64_t>& activeConfigs) {
471                 broadcastCount++;
472                 EXPECT_EQ(broadcastUid, uid);
473                 activeConfigsBroadcast.clear();
474                 activeConfigsBroadcast.insert(activeConfigsBroadcast.end(), activeConfigs.begin(),
475                                               activeConfigs.end());
476                 return true;
477             });
478 
479     processor.OnConfigUpdated(bucketStartTimeNs, cfgKey, config);
480 
481     ASSERT_EQ(processor.mMetricsManagers.size(), 1u);
482     sp<MetricsManager> metricsManager = processor.mMetricsManagers.begin()->second;
483     EXPECT_TRUE(metricsManager->isConfigValid());
484     ASSERT_EQ(metricsManager->mAllMetricProducers.size(), 1);
485     sp<MetricProducer> metricProducer = metricsManager->mAllMetricProducers[0];
486     auto& eventActivationMap = metricProducer->mEventActivationMap;
487     auto& eventDeactivationMap = metricProducer->mEventDeactivationMap;
488 
489     EXPECT_FALSE(metricsManager->isActive());
490     EXPECT_FALSE(metricProducer->mIsActive);
491     // Two activations: one is triggered by battery saver mode (tracker index 0), the other is
492     // triggered by screen on event (tracker index 2).
493     ASSERT_EQ(eventActivationMap.size(), 2u);
494     EXPECT_TRUE(eventActivationMap.find(0) != eventActivationMap.end());
495     EXPECT_TRUE(eventActivationMap.find(2) != eventActivationMap.end());
496     EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kNotActive);
497     EXPECT_EQ(eventActivationMap[0]->start_ns, 0);
498     EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
499     EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kNotActive);
500     EXPECT_EQ(eventActivationMap[2]->start_ns, 0);
501     EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
502     ASSERT_EQ(eventDeactivationMap.size(), 1u);
503     EXPECT_TRUE(eventDeactivationMap.find(3) != eventDeactivationMap.end());
504     ASSERT_EQ(eventDeactivationMap[3].size(), 1u);
505     EXPECT_EQ(eventDeactivationMap[3][0], eventActivationMap[0]);
506 
507     std::unique_ptr<LogEvent> event;
508 
509     event = CreateAppCrashEvent(bucketStartTimeNs + 5, 111);
510     processor.OnLogEvent(event.get(), bucketStartTimeNs + 5);
511     EXPECT_FALSE(metricsManager->isActive());
512     EXPECT_FALSE(metricProducer->mIsActive);
513     EXPECT_EQ(broadcastCount, 0);
514 
515     // Activated by battery save mode.
516     event = CreateBatterySaverOnEvent(bucketStartTimeNs + 10);
517     processor.OnLogEvent(event.get(), bucketStartTimeNs + 10);
518     EXPECT_TRUE(metricsManager->isActive());
519     EXPECT_TRUE(metricProducer->mIsActive);
520     EXPECT_EQ(broadcastCount, 1);
521     ASSERT_EQ(activeConfigsBroadcast.size(), 1);
522     EXPECT_EQ(activeConfigsBroadcast[0], cfgId);
523     EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kActive);
524     EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + 10);
525     EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
526     EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kNotActive);
527     EXPECT_EQ(eventActivationMap[2]->start_ns, 0);
528     EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
529     EXPECT_EQ(eventDeactivationMap[3][0], eventActivationMap[0]);
530 
531     // First processed event.
532     event = CreateAppCrashEvent(bucketStartTimeNs + 15, 222);
533     processor.OnLogEvent(event.get(), bucketStartTimeNs + 15);
534 
535     // Activated by screen on event.
536     event = CreateScreenStateChangedEvent(bucketStartTimeNs + 20, android::view::DISPLAY_STATE_ON);
537     processor.OnLogEvent(event.get(), bucketStartTimeNs + 20);
538     EXPECT_TRUE(metricsManager->isActive());
539     EXPECT_TRUE(metricProducer->mIsActive);
540     EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kActive);
541     EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + 10);
542     EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
543     EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kActive);
544     EXPECT_EQ(eventActivationMap[2]->start_ns, bucketStartTimeNs + 20);
545     EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
546     EXPECT_EQ(eventDeactivationMap[3][0], eventActivationMap[0]);
547 
548     // 2nd processed event.
549     // The activation by screen_on event expires, but the one by battery save mode is still active.
550     event = CreateAppCrashEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 2 + 25, 333);
551     processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 2 + 25);
552     EXPECT_TRUE(metricsManager->isActive());
553     EXPECT_TRUE(metricProducer->mIsActive);
554     EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kActive);
555     EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + 10);
556     EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
557     EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kNotActive);
558     EXPECT_EQ(eventActivationMap[2]->start_ns, bucketStartTimeNs + 20);
559     EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
560     EXPECT_EQ(eventDeactivationMap[3][0], eventActivationMap[0]);
561     // No new broadcast since the config should still be active.
562     EXPECT_EQ(broadcastCount, 1);
563 
564     // 3rd processed event.
565     event = CreateAppCrashEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 5 + 25, 444);
566     processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 5 + 25);
567 
568     // All activations expired.
569     event = CreateAppCrashEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 8, 555);
570     processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 8);
571     EXPECT_FALSE(metricsManager->isActive());
572     EXPECT_FALSE(metricProducer->mIsActive);
573     // New broadcast since the config is no longer active.
574     EXPECT_EQ(broadcastCount, 2);
575     ASSERT_EQ(activeConfigsBroadcast.size(), 0);
576     EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kNotActive);
577     EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + 10);
578     EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
579     EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kNotActive);
580     EXPECT_EQ(eventActivationMap[2]->start_ns, bucketStartTimeNs + 20);
581     EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
582     EXPECT_EQ(eventDeactivationMap[3][0], eventActivationMap[0]);
583 
584     // Re-activate metric via screen on.
585     event = CreateScreenStateChangedEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 10,
586                                           android::view::DISPLAY_STATE_ON);
587     processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 10);
588     EXPECT_TRUE(metricsManager->isActive());
589     EXPECT_TRUE(metricProducer->mIsActive);
590     EXPECT_EQ(broadcastCount, 3);
591     ASSERT_EQ(activeConfigsBroadcast.size(), 1);
592     EXPECT_EQ(activeConfigsBroadcast[0], cfgId);
593     EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kNotActive);
594     EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + 10);
595     EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
596     EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kActive);
597     EXPECT_EQ(eventActivationMap[2]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 10);
598     EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
599     EXPECT_EQ(eventDeactivationMap[3][0], eventActivationMap[0]);
600 
601     // 4th processed event.
602     event = CreateAppCrashEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 1, 666);
603     processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 1);
604 
605     // Re-enable battery saver mode activation.
606     event = CreateBatterySaverOnEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 15);
607     processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 15);
608     EXPECT_TRUE(metricsManager->isActive());
609     EXPECT_TRUE(metricProducer->mIsActive);
610     EXPECT_EQ(broadcastCount, 3);
611     ASSERT_EQ(activeConfigsBroadcast.size(), 1);
612     EXPECT_EQ(activeConfigsBroadcast[0], cfgId);
613     EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kActive);
614     EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 15);
615     EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
616     EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kActive);
617     EXPECT_EQ(eventActivationMap[2]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 10);
618     EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
619     EXPECT_EQ(eventDeactivationMap[3][0], eventActivationMap[0]);
620 
621     // 5th processed event.
622     event = CreateAppCrashEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 40, 777);
623     processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 40);
624 
625     // Cancel battery saver mode activation.
626     event = CreateScreenBrightnessChangedEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 60, 64);
627     processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 60);
628     EXPECT_TRUE(metricsManager->isActive());
629     EXPECT_TRUE(metricProducer->mIsActive);
630     EXPECT_EQ(broadcastCount, 3);
631     ASSERT_EQ(activeConfigsBroadcast.size(), 1);
632     EXPECT_EQ(activeConfigsBroadcast[0], cfgId);
633     EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kNotActive);
634     EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 15);
635     EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
636     EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kActive);
637     EXPECT_EQ(eventActivationMap[2]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 10);
638     EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
639     EXPECT_EQ(eventDeactivationMap[3][0], eventActivationMap[0]);
640 
641     // Screen-on activation expired.
642     event = CreateAppCrashEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 13, 888);
643     processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 13);
644     EXPECT_FALSE(metricsManager->isActive());
645     EXPECT_FALSE(metricProducer->mIsActive);
646     // New broadcast since the config is no longer active.
647     EXPECT_EQ(broadcastCount, 4);
648     ASSERT_EQ(activeConfigsBroadcast.size(), 0);
649     EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kNotActive);
650     EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 15);
651     EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
652     EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kNotActive);
653     EXPECT_EQ(eventActivationMap[2]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 10);
654     EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
655     EXPECT_EQ(eventDeactivationMap[3][0], eventActivationMap[0]);
656 
657     event = CreateAppCrashEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 14 + 1, 999);
658     processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 14 + 1);
659 
660     // Re-enable battery saver mode activation.
661     event = CreateBatterySaverOnEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 15 + 15);
662     processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 15 + 15);
663     EXPECT_TRUE(metricsManager->isActive());
664     EXPECT_TRUE(metricProducer->mIsActive);
665     EXPECT_EQ(broadcastCount, 5);
666     ASSERT_EQ(activeConfigsBroadcast.size(), 1);
667     EXPECT_EQ(activeConfigsBroadcast[0], cfgId);
668     EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kActive);
669     EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 15 + 15);
670     EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
671     EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kNotActive);
672     EXPECT_EQ(eventActivationMap[2]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 10);
673     EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
674     EXPECT_EQ(eventDeactivationMap[3][0], eventActivationMap[0]);
675 
676     // Cancel battery saver mode activation.
677     event = CreateScreenBrightnessChangedEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 16, 140);
678     processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 16);
679     EXPECT_FALSE(metricsManager->isActive());
680     EXPECT_FALSE(metricProducer->mIsActive);
681     EXPECT_EQ(broadcastCount, 6);
682     ASSERT_EQ(activeConfigsBroadcast.size(), 0);
683     EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kNotActive);
684     EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 15 + 15);
685     EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
686     EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kNotActive);
687     EXPECT_EQ(eventActivationMap[2]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 10);
688     EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
689     EXPECT_EQ(eventDeactivationMap[3][0], eventActivationMap[0]);
690 
691     ConfigMetricsReportList reports;
692     vector<uint8_t> buffer;
693     processor.onDumpReport(cfgKey, bucketStartTimeNs + NS_PER_SEC * 60 * 15 + 1, false, true,
694                            ADB_DUMP, FAST, &buffer);
695     EXPECT_TRUE(buffer.size() > 0);
696     EXPECT_TRUE(reports.ParseFromArray(&buffer[0], buffer.size()));
697     backfillDimensionPath(&reports);
698     backfillStartEndTimestamp(&reports);
699     ASSERT_EQ(1, reports.reports_size());
700     ASSERT_EQ(1, reports.reports(0).metrics_size());
701 
702     StatsLogReport::CountMetricDataWrapper countMetrics;
703     sortMetricDataByDimensionsValue(reports.reports(0).metrics(0).count_metrics(), &countMetrics);
704     ASSERT_EQ(5, countMetrics.data_size());
705 
706     auto data = countMetrics.data(0);
707     EXPECT_EQ(util::PROCESS_LIFE_CYCLE_STATE_CHANGED, data.dimensions_in_what().field());
708     ASSERT_EQ(1, data.dimensions_in_what().value_tuple().dimensions_value_size());
709     EXPECT_EQ(1 /* uid field */,
710               data.dimensions_in_what().value_tuple().dimensions_value(0).field());
711     EXPECT_EQ(222, data.dimensions_in_what().value_tuple().dimensions_value(0).value_int());
712     ASSERT_EQ(1, data.bucket_info_size());
713     EXPECT_EQ(1, data.bucket_info(0).count());
714     EXPECT_EQ(bucketStartTimeNs, data.bucket_info(0).start_bucket_elapsed_nanos());
715     EXPECT_EQ(bucketStartTimeNs + bucketSizeNs, data.bucket_info(0).end_bucket_elapsed_nanos());
716 
717     data = countMetrics.data(1);
718     EXPECT_EQ(util::PROCESS_LIFE_CYCLE_STATE_CHANGED, data.dimensions_in_what().field());
719     ASSERT_EQ(1, data.dimensions_in_what().value_tuple().dimensions_value_size());
720     EXPECT_EQ(1 /* uid field */,
721               data.dimensions_in_what().value_tuple().dimensions_value(0).field());
722     EXPECT_EQ(333, data.dimensions_in_what().value_tuple().dimensions_value(0).value_int());
723     ASSERT_EQ(1, data.bucket_info_size());
724     EXPECT_EQ(1, data.bucket_info(0).count());
725     EXPECT_EQ(bucketStartTimeNs, data.bucket_info(0).start_bucket_elapsed_nanos());
726     EXPECT_EQ(bucketStartTimeNs + bucketSizeNs, data.bucket_info(0).end_bucket_elapsed_nanos());
727 
728     data = countMetrics.data(2);
729     EXPECT_EQ(util::PROCESS_LIFE_CYCLE_STATE_CHANGED, data.dimensions_in_what().field());
730     ASSERT_EQ(1, data.dimensions_in_what().value_tuple().dimensions_value_size());
731     EXPECT_EQ(1 /* uid field */,
732               data.dimensions_in_what().value_tuple().dimensions_value(0).field());
733     EXPECT_EQ(444, data.dimensions_in_what().value_tuple().dimensions_value(0).value_int());
734     ASSERT_EQ(1, data.bucket_info_size());
735     EXPECT_EQ(1, data.bucket_info(0).count());
736     // Partial bucket as metric is deactivated.
737     EXPECT_EQ(bucketStartTimeNs + bucketSizeNs, data.bucket_info(0).start_bucket_elapsed_nanos());
738     EXPECT_EQ(bucketStartTimeNs + NS_PER_SEC * 60 * 8,
739               data.bucket_info(0).end_bucket_elapsed_nanos());
740 
741     data = countMetrics.data(3);
742     EXPECT_EQ(util::PROCESS_LIFE_CYCLE_STATE_CHANGED, data.dimensions_in_what().field());
743     ASSERT_EQ(1, data.dimensions_in_what().value_tuple().dimensions_value_size());
744     EXPECT_EQ(1 /* uid field */,
745               data.dimensions_in_what().value_tuple().dimensions_value(0).field());
746     EXPECT_EQ(666, data.dimensions_in_what().value_tuple().dimensions_value(0).value_int());
747     ASSERT_EQ(1, data.bucket_info_size());
748     EXPECT_EQ(1, data.bucket_info(0).count());
749     EXPECT_EQ(bucketStartTimeNs + 2 * bucketSizeNs,
750               data.bucket_info(0).start_bucket_elapsed_nanos());
751     EXPECT_EQ(bucketStartTimeNs + NS_PER_SEC * 60 * 13,
752               data.bucket_info(0).end_bucket_elapsed_nanos());
753 
754     data = countMetrics.data(4);
755     EXPECT_EQ(util::PROCESS_LIFE_CYCLE_STATE_CHANGED, data.dimensions_in_what().field());
756     ASSERT_EQ(1, data.dimensions_in_what().value_tuple().dimensions_value_size());
757     EXPECT_EQ(1 /* uid field */,
758               data.dimensions_in_what().value_tuple().dimensions_value(0).field());
759     EXPECT_EQ(777, data.dimensions_in_what().value_tuple().dimensions_value(0).value_int());
760     ASSERT_EQ(1, data.bucket_info_size());
761     EXPECT_EQ(1, data.bucket_info(0).count());
762     EXPECT_EQ(bucketStartTimeNs + 2 * bucketSizeNs,
763               data.bucket_info(0).start_bucket_elapsed_nanos());
764     EXPECT_EQ(bucketStartTimeNs + NS_PER_SEC * 60 * 13,
765               data.bucket_info(0).end_bucket_elapsed_nanos());
766 }
767 
TEST(MetricActivationE2eTest,TestCountMetricWithTwoDeactivations)768 TEST(MetricActivationE2eTest, TestCountMetricWithTwoDeactivations) {
769     auto config = CreateStatsdConfigWithTwoDeactivations();
770 
771     int64_t bucketStartTimeNs = NS_PER_SEC * 10;  // 10 secs
772     int64_t bucketSizeNs =
773             TimeUnitToBucketSizeInMillis(config.count_metric(0).bucket()) * 1000LL * 1000LL;
774 
775     int uid = 12345;
776     int64_t cfgId = 98765;
777     ConfigKey cfgKey(uid, cfgId);
778 
779     sp<UidMap> m = new UidMap();
780     sp<StatsPullerManager> pullerManager = new StatsPullerManager();
781     sp<AlarmMonitor> anomalyAlarmMonitor;
782     sp<AlarmMonitor> subscriberAlarmMonitor;
783     vector<int64_t> activeConfigsBroadcast;
784 
785     long timeBase1 = 1;
786     int broadcastCount = 0;
787     StatsLogProcessor processor(
788             m, pullerManager, anomalyAlarmMonitor, subscriberAlarmMonitor, bucketStartTimeNs,
789             [](const ConfigKey& key) { return true; },
790             [&uid, &broadcastCount, &activeConfigsBroadcast](const int& broadcastUid,
791                                                              const vector<int64_t>& activeConfigs) {
792                 broadcastCount++;
793                 EXPECT_EQ(broadcastUid, uid);
794                 activeConfigsBroadcast.clear();
795                 activeConfigsBroadcast.insert(activeConfigsBroadcast.end(), activeConfigs.begin(),
796                                               activeConfigs.end());
797                 return true;
798             });
799 
800     processor.OnConfigUpdated(bucketStartTimeNs, cfgKey, config);
801 
802     ASSERT_EQ(processor.mMetricsManagers.size(), 1u);
803     sp<MetricsManager> metricsManager = processor.mMetricsManagers.begin()->second;
804     EXPECT_TRUE(metricsManager->isConfigValid());
805     ASSERT_EQ(metricsManager->mAllMetricProducers.size(), 1);
806     sp<MetricProducer> metricProducer = metricsManager->mAllMetricProducers[0];
807     auto& eventActivationMap = metricProducer->mEventActivationMap;
808     auto& eventDeactivationMap = metricProducer->mEventDeactivationMap;
809 
810     EXPECT_FALSE(metricsManager->isActive());
811     EXPECT_FALSE(metricProducer->mIsActive);
812     // Two activations: one is triggered by battery saver mode (tracker index 0), the other is
813     // triggered by screen on event (tracker index 2).
814     ASSERT_EQ(eventActivationMap.size(), 2u);
815     EXPECT_TRUE(eventActivationMap.find(0) != eventActivationMap.end());
816     EXPECT_TRUE(eventActivationMap.find(2) != eventActivationMap.end());
817     EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kNotActive);
818     EXPECT_EQ(eventActivationMap[0]->start_ns, 0);
819     EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
820     EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kNotActive);
821     EXPECT_EQ(eventActivationMap[2]->start_ns, 0);
822     EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
823     ASSERT_EQ(eventDeactivationMap.size(), 2u);
824     EXPECT_TRUE(eventDeactivationMap.find(3) != eventDeactivationMap.end());
825     EXPECT_TRUE(eventDeactivationMap.find(4) != eventDeactivationMap.end());
826     ASSERT_EQ(eventDeactivationMap[3].size(), 1u);
827     ASSERT_EQ(eventDeactivationMap[4].size(), 1u);
828     EXPECT_EQ(eventDeactivationMap[3][0], eventActivationMap[0]);
829     EXPECT_EQ(eventDeactivationMap[4][0], eventActivationMap[2]);
830 
831     std::unique_ptr<LogEvent> event;
832 
833     event = CreateAppCrashEvent(bucketStartTimeNs + 5, 111);
834     processor.OnLogEvent(event.get(), bucketStartTimeNs + 5);
835     EXPECT_FALSE(metricsManager->isActive());
836     EXPECT_FALSE(metricProducer->mIsActive);
837     EXPECT_EQ(broadcastCount, 0);
838 
839     // Activated by battery save mode.
840     event = CreateBatterySaverOnEvent(bucketStartTimeNs + 10);
841     processor.OnLogEvent(event.get(), bucketStartTimeNs + 10);
842     EXPECT_TRUE(metricsManager->isActive());
843     EXPECT_TRUE(metricProducer->mIsActive);
844     EXPECT_EQ(broadcastCount, 1);
845     ASSERT_EQ(activeConfigsBroadcast.size(), 1);
846     EXPECT_EQ(activeConfigsBroadcast[0], cfgId);
847     EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kActive);
848     EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + 10);
849     EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
850     EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kNotActive);
851     EXPECT_EQ(eventActivationMap[2]->start_ns, 0);
852     EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
853     EXPECT_EQ(eventDeactivationMap[3][0], eventActivationMap[0]);
854     EXPECT_EQ(eventDeactivationMap[4][0], eventActivationMap[2]);
855 
856     // First processed event.
857     event = CreateAppCrashEvent(bucketStartTimeNs + 15, 222);
858     processor.OnLogEvent(event.get(), bucketStartTimeNs + 15);
859 
860     // Activated by screen on event.
861     event = CreateScreenStateChangedEvent(bucketStartTimeNs + 20, android::view::DISPLAY_STATE_ON);
862     processor.OnLogEvent(event.get(), bucketStartTimeNs + 20);
863     EXPECT_TRUE(metricsManager->isActive());
864     EXPECT_TRUE(metricProducer->mIsActive);
865     EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kActive);
866     EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + 10);
867     EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
868     EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kActive);
869     EXPECT_EQ(eventActivationMap[2]->start_ns, bucketStartTimeNs + 20);
870     EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
871     EXPECT_EQ(eventDeactivationMap[3][0], eventActivationMap[0]);
872     EXPECT_EQ(eventDeactivationMap[4][0], eventActivationMap[2]);
873 
874     // 2nd processed event.
875     // The activation by screen_on event expires, but the one by battery save mode is still active.
876     event = CreateAppCrashEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 2 + 25, 333);
877     processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 2 + 25);
878     EXPECT_TRUE(metricsManager->isActive());
879     EXPECT_TRUE(metricProducer->mIsActive);
880     EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kActive);
881     EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + 10);
882     EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
883     EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kNotActive);
884     EXPECT_EQ(eventActivationMap[2]->start_ns, bucketStartTimeNs + 20);
885     EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
886     EXPECT_EQ(eventDeactivationMap[3][0], eventActivationMap[0]);
887     EXPECT_EQ(eventDeactivationMap[4][0], eventActivationMap[2]);
888     // No new broadcast since the config should still be active.
889     EXPECT_EQ(broadcastCount, 1);
890 
891     // 3rd processed event.
892     event = CreateAppCrashEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 5 + 25, 444);
893     processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 5 + 25);
894 
895     // All activations expired.
896     event = CreateAppCrashEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 8, 555);
897     processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 8);
898     EXPECT_FALSE(metricsManager->isActive());
899     EXPECT_FALSE(metricProducer->mIsActive);
900     // New broadcast since the config is no longer active.
901     EXPECT_EQ(broadcastCount, 2);
902     ASSERT_EQ(activeConfigsBroadcast.size(), 0);
903     EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kNotActive);
904     EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + 10);
905     EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
906     EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kNotActive);
907     EXPECT_EQ(eventActivationMap[2]->start_ns, bucketStartTimeNs + 20);
908     EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
909     EXPECT_EQ(eventDeactivationMap[3][0], eventActivationMap[0]);
910     EXPECT_EQ(eventDeactivationMap[4][0], eventActivationMap[2]);
911 
912     // Re-activate metric via screen on.
913     event = CreateScreenStateChangedEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 10,
914                                           android::view::DISPLAY_STATE_ON);
915     processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 10);
916     EXPECT_TRUE(metricsManager->isActive());
917     EXPECT_TRUE(metricProducer->mIsActive);
918     EXPECT_EQ(broadcastCount, 3);
919     ASSERT_EQ(activeConfigsBroadcast.size(), 1);
920     EXPECT_EQ(activeConfigsBroadcast[0], cfgId);
921     EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kNotActive);
922     EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + 10);
923     EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
924     EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kActive);
925     EXPECT_EQ(eventActivationMap[2]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 10);
926     EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
927     EXPECT_EQ(eventDeactivationMap[3][0], eventActivationMap[0]);
928     EXPECT_EQ(eventDeactivationMap[4][0], eventActivationMap[2]);
929 
930     // 4th processed event.
931     event = CreateAppCrashEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 1, 666);
932     processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 1);
933 
934     // Re-enable battery saver mode activation.
935     event = CreateBatterySaverOnEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 15);
936     processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 15);
937     EXPECT_TRUE(metricsManager->isActive());
938     EXPECT_TRUE(metricProducer->mIsActive);
939     EXPECT_EQ(broadcastCount, 3);
940     ASSERT_EQ(activeConfigsBroadcast.size(), 1);
941     EXPECT_EQ(activeConfigsBroadcast[0], cfgId);
942     EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kActive);
943     EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 15);
944     EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
945     EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kActive);
946     EXPECT_EQ(eventActivationMap[2]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 10);
947     EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
948     EXPECT_EQ(eventDeactivationMap[3][0], eventActivationMap[0]);
949     EXPECT_EQ(eventDeactivationMap[4][0], eventActivationMap[2]);
950 
951     // 5th processed event.
952     event = CreateAppCrashEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 40, 777);
953     processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 40);
954 
955     // Cancel battery saver mode and screen on activation.
956     event = CreateScreenBrightnessChangedEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 60, 64);
957     processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 60);
958     EXPECT_FALSE(metricsManager->isActive());
959     EXPECT_FALSE(metricProducer->mIsActive);
960     // New broadcast since the config is no longer active.
961     EXPECT_EQ(broadcastCount, 4);
962     ASSERT_EQ(activeConfigsBroadcast.size(), 0);
963     EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kNotActive);
964     EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 15);
965     EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
966     EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kNotActive);
967     EXPECT_EQ(eventActivationMap[2]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 10);
968     EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
969     EXPECT_EQ(eventDeactivationMap[3][0], eventActivationMap[0]);
970     EXPECT_EQ(eventDeactivationMap[4][0], eventActivationMap[2]);
971 
972     // Screen-on activation expired.
973     event = CreateAppCrashEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 13, 888);
974     processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 13);
975     EXPECT_FALSE(metricsManager->isActive());
976     EXPECT_FALSE(metricProducer->mIsActive);
977     EXPECT_EQ(broadcastCount, 4);
978     ASSERT_EQ(activeConfigsBroadcast.size(), 0);
979     EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kNotActive);
980     EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 15);
981     EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
982     EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kNotActive);
983     EXPECT_EQ(eventActivationMap[2]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 10);
984     EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
985     EXPECT_EQ(eventDeactivationMap[3][0], eventActivationMap[0]);
986     EXPECT_EQ(eventDeactivationMap[4][0], eventActivationMap[2]);
987 
988     event = CreateAppCrashEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 14 + 1, 999);
989     processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 14 + 1);
990 
991     // Re-enable battery saver mode activation.
992     event = CreateBatterySaverOnEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 15 + 15);
993     processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 15 + 15);
994     EXPECT_TRUE(metricsManager->isActive());
995     EXPECT_TRUE(metricProducer->mIsActive);
996     EXPECT_EQ(broadcastCount, 5);
997     ASSERT_EQ(activeConfigsBroadcast.size(), 1);
998     EXPECT_EQ(activeConfigsBroadcast[0], cfgId);
999     EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kActive);
1000     EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 15 + 15);
1001     EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
1002     EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kNotActive);
1003     EXPECT_EQ(eventActivationMap[2]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 10);
1004     EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
1005     EXPECT_EQ(eventDeactivationMap[3][0], eventActivationMap[0]);
1006     EXPECT_EQ(eventDeactivationMap[4][0], eventActivationMap[2]);
1007 
1008     // Cancel battery saver mode and screen on activation.
1009     event = CreateScreenBrightnessChangedEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 16, 140);
1010     processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 16);
1011     EXPECT_FALSE(metricsManager->isActive());
1012     EXPECT_FALSE(metricProducer->mIsActive);
1013     EXPECT_EQ(broadcastCount, 6);
1014     ASSERT_EQ(activeConfigsBroadcast.size(), 0);
1015     EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kNotActive);
1016     EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 15 + 15);
1017     EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
1018     EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kNotActive);
1019     EXPECT_EQ(eventActivationMap[2]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 10);
1020     EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
1021     EXPECT_EQ(eventDeactivationMap[3][0], eventActivationMap[0]);
1022     EXPECT_EQ(eventDeactivationMap[4][0], eventActivationMap[2]);
1023 
1024     ConfigMetricsReportList reports;
1025     vector<uint8_t> buffer;
1026     processor.onDumpReport(cfgKey, bucketStartTimeNs + NS_PER_SEC * 60 * 15 + 1, false, true,
1027                            ADB_DUMP, FAST, &buffer);
1028     EXPECT_TRUE(buffer.size() > 0);
1029     EXPECT_TRUE(reports.ParseFromArray(&buffer[0], buffer.size()));
1030     backfillDimensionPath(&reports);
1031     backfillStartEndTimestamp(&reports);
1032     ASSERT_EQ(1, reports.reports_size());
1033     ASSERT_EQ(1, reports.reports(0).metrics_size());
1034 
1035     StatsLogReport::CountMetricDataWrapper countMetrics;
1036     sortMetricDataByDimensionsValue(reports.reports(0).metrics(0).count_metrics(), &countMetrics);
1037     ASSERT_EQ(5, countMetrics.data_size());
1038 
1039     auto data = countMetrics.data(0);
1040     EXPECT_EQ(util::PROCESS_LIFE_CYCLE_STATE_CHANGED, data.dimensions_in_what().field());
1041     ASSERT_EQ(1, data.dimensions_in_what().value_tuple().dimensions_value_size());
1042     EXPECT_EQ(1 /* uid field */,
1043               data.dimensions_in_what().value_tuple().dimensions_value(0).field());
1044     EXPECT_EQ(222, data.dimensions_in_what().value_tuple().dimensions_value(0).value_int());
1045     ASSERT_EQ(1, data.bucket_info_size());
1046     EXPECT_EQ(1, data.bucket_info(0).count());
1047     EXPECT_EQ(bucketStartTimeNs, data.bucket_info(0).start_bucket_elapsed_nanos());
1048     EXPECT_EQ(bucketStartTimeNs + bucketSizeNs, data.bucket_info(0).end_bucket_elapsed_nanos());
1049 
1050     data = countMetrics.data(1);
1051     EXPECT_EQ(util::PROCESS_LIFE_CYCLE_STATE_CHANGED, data.dimensions_in_what().field());
1052     ASSERT_EQ(1, data.dimensions_in_what().value_tuple().dimensions_value_size());
1053     EXPECT_EQ(1 /* uid field */,
1054               data.dimensions_in_what().value_tuple().dimensions_value(0).field());
1055     EXPECT_EQ(333, data.dimensions_in_what().value_tuple().dimensions_value(0).value_int());
1056     ASSERT_EQ(1, data.bucket_info_size());
1057     EXPECT_EQ(1, data.bucket_info(0).count());
1058     EXPECT_EQ(bucketStartTimeNs, data.bucket_info(0).start_bucket_elapsed_nanos());
1059     EXPECT_EQ(bucketStartTimeNs + bucketSizeNs, data.bucket_info(0).end_bucket_elapsed_nanos());
1060 
1061     data = countMetrics.data(2);
1062     EXPECT_EQ(util::PROCESS_LIFE_CYCLE_STATE_CHANGED, data.dimensions_in_what().field());
1063     ASSERT_EQ(1, data.dimensions_in_what().value_tuple().dimensions_value_size());
1064     EXPECT_EQ(1 /* uid field */,
1065               data.dimensions_in_what().value_tuple().dimensions_value(0).field());
1066     EXPECT_EQ(444, data.dimensions_in_what().value_tuple().dimensions_value(0).value_int());
1067     ASSERT_EQ(1, data.bucket_info_size());
1068     EXPECT_EQ(1, data.bucket_info(0).count());
1069     // Partial bucket as metric is deactivated.
1070     EXPECT_EQ(bucketStartTimeNs + bucketSizeNs, data.bucket_info(0).start_bucket_elapsed_nanos());
1071     EXPECT_EQ(bucketStartTimeNs + NS_PER_SEC * 60 * 8,
1072               data.bucket_info(0).end_bucket_elapsed_nanos());
1073 
1074     data = countMetrics.data(3);
1075     EXPECT_EQ(util::PROCESS_LIFE_CYCLE_STATE_CHANGED, data.dimensions_in_what().field());
1076     ASSERT_EQ(1, data.dimensions_in_what().value_tuple().dimensions_value_size());
1077     EXPECT_EQ(1 /* uid field */,
1078               data.dimensions_in_what().value_tuple().dimensions_value(0).field());
1079     EXPECT_EQ(666, data.dimensions_in_what().value_tuple().dimensions_value(0).value_int());
1080     ASSERT_EQ(1, data.bucket_info_size());
1081     EXPECT_EQ(1, data.bucket_info(0).count());
1082     EXPECT_EQ(bucketStartTimeNs + 2 * bucketSizeNs,
1083               data.bucket_info(0).start_bucket_elapsed_nanos());
1084     EXPECT_EQ(bucketStartTimeNs + NS_PER_SEC * 60 * 11,
1085               data.bucket_info(0).end_bucket_elapsed_nanos());
1086 
1087     data = countMetrics.data(4);
1088     EXPECT_EQ(util::PROCESS_LIFE_CYCLE_STATE_CHANGED, data.dimensions_in_what().field());
1089     ASSERT_EQ(1, data.dimensions_in_what().value_tuple().dimensions_value_size());
1090     EXPECT_EQ(1 /* uid field */,
1091               data.dimensions_in_what().value_tuple().dimensions_value(0).field());
1092     EXPECT_EQ(777, data.dimensions_in_what().value_tuple().dimensions_value(0).value_int());
1093     ASSERT_EQ(1, data.bucket_info_size());
1094     EXPECT_EQ(1, data.bucket_info(0).count());
1095     EXPECT_EQ(bucketStartTimeNs + 2 * bucketSizeNs,
1096               data.bucket_info(0).start_bucket_elapsed_nanos());
1097     EXPECT_EQ(bucketStartTimeNs + NS_PER_SEC * 60 * 11,
1098               data.bucket_info(0).end_bucket_elapsed_nanos());
1099 }
1100 
TEST(MetricActivationE2eTest,TestCountMetricWithSameDeactivation)1101 TEST(MetricActivationE2eTest, TestCountMetricWithSameDeactivation) {
1102     auto config = CreateStatsdConfigWithSameDeactivations();
1103 
1104     int64_t bucketStartTimeNs = NS_PER_SEC * 10;  // 10 secs
1105     int64_t bucketSizeNs =
1106             TimeUnitToBucketSizeInMillis(config.count_metric(0).bucket()) * 1000LL * 1000LL;
1107 
1108     int uid = 12345;
1109     int64_t cfgId = 98765;
1110     ConfigKey cfgKey(uid, cfgId);
1111 
1112     sp<UidMap> m = new UidMap();
1113     sp<StatsPullerManager> pullerManager = new StatsPullerManager();
1114     sp<AlarmMonitor> anomalyAlarmMonitor;
1115     sp<AlarmMonitor> subscriberAlarmMonitor;
1116     vector<int64_t> activeConfigsBroadcast;
1117 
1118     long timeBase1 = 1;
1119     int broadcastCount = 0;
1120     StatsLogProcessor processor(
1121             m, pullerManager, anomalyAlarmMonitor, subscriberAlarmMonitor, bucketStartTimeNs,
1122             [](const ConfigKey& key) { return true; },
1123             [&uid, &broadcastCount, &activeConfigsBroadcast](const int& broadcastUid,
1124                                                              const vector<int64_t>& activeConfigs) {
1125                 broadcastCount++;
1126                 EXPECT_EQ(broadcastUid, uid);
1127                 activeConfigsBroadcast.clear();
1128                 activeConfigsBroadcast.insert(activeConfigsBroadcast.end(), activeConfigs.begin(),
1129                                               activeConfigs.end());
1130                 return true;
1131             });
1132 
1133     processor.OnConfigUpdated(bucketStartTimeNs, cfgKey, config);
1134 
1135     ASSERT_EQ(processor.mMetricsManagers.size(), 1u);
1136     sp<MetricsManager> metricsManager = processor.mMetricsManagers.begin()->second;
1137     EXPECT_TRUE(metricsManager->isConfigValid());
1138     ASSERT_EQ(metricsManager->mAllMetricProducers.size(), 1);
1139     sp<MetricProducer> metricProducer = metricsManager->mAllMetricProducers[0];
1140     auto& eventActivationMap = metricProducer->mEventActivationMap;
1141     auto& eventDeactivationMap = metricProducer->mEventDeactivationMap;
1142 
1143     EXPECT_FALSE(metricsManager->isActive());
1144     EXPECT_FALSE(metricProducer->mIsActive);
1145     // Two activations: one is triggered by battery saver mode (tracker index 0), the other is
1146     // triggered by screen on event (tracker index 2).
1147     ASSERT_EQ(eventActivationMap.size(), 2u);
1148     EXPECT_TRUE(eventActivationMap.find(0) != eventActivationMap.end());
1149     EXPECT_TRUE(eventActivationMap.find(2) != eventActivationMap.end());
1150     EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kNotActive);
1151     EXPECT_EQ(eventActivationMap[0]->start_ns, 0);
1152     EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
1153     EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kNotActive);
1154     EXPECT_EQ(eventActivationMap[2]->start_ns, 0);
1155     EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
1156     ASSERT_EQ(eventDeactivationMap.size(), 1u);
1157     EXPECT_TRUE(eventDeactivationMap.find(3) != eventDeactivationMap.end());
1158     ASSERT_EQ(eventDeactivationMap[3].size(), 2u);
1159     EXPECT_EQ(eventDeactivationMap[3][0], eventActivationMap[0]);
1160     EXPECT_EQ(eventDeactivationMap[3][1], eventActivationMap[2]);
1161     EXPECT_EQ(broadcastCount, 0);
1162 
1163     std::unique_ptr<LogEvent> event;
1164 
1165     // Event that should be ignored.
1166     event = CreateAppCrashEvent(bucketStartTimeNs + 1, 111);
1167     processor.OnLogEvent(event.get(), bucketStartTimeNs + 1);
1168 
1169     // Activate metric via screen on for 2 minutes.
1170     event = CreateScreenStateChangedEvent(bucketStartTimeNs + 10, android::view::DISPLAY_STATE_ON);
1171     processor.OnLogEvent(event.get(), bucketStartTimeNs + 10);
1172     EXPECT_TRUE(metricsManager->isActive());
1173     EXPECT_TRUE(metricProducer->mIsActive);
1174     EXPECT_EQ(broadcastCount, 1);
1175     ASSERT_EQ(activeConfigsBroadcast.size(), 1);
1176     EXPECT_EQ(activeConfigsBroadcast[0], cfgId);
1177     EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kNotActive);
1178     EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kActive);
1179     EXPECT_EQ(eventActivationMap[2]->start_ns, bucketStartTimeNs + 10);
1180 
1181     // 1st processed event.
1182     event = CreateAppCrashEvent(bucketStartTimeNs + 15, 222);
1183     processor.OnLogEvent(event.get(), bucketStartTimeNs + 15);
1184 
1185     // Enable battery saver mode activation for 5 minutes.
1186     event = CreateBatterySaverOnEvent(bucketStartTimeNs + NS_PER_SEC * 60 + 10);
1187     processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 + 10);
1188     EXPECT_TRUE(metricsManager->isActive());
1189     EXPECT_TRUE(metricProducer->mIsActive);
1190     EXPECT_EQ(broadcastCount, 1);
1191     EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kActive);
1192     EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 + 10);
1193     EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kActive);
1194     EXPECT_EQ(eventActivationMap[2]->start_ns, bucketStartTimeNs + 10);
1195 
1196     // 2nd processed event.
1197     event = CreateAppCrashEvent(bucketStartTimeNs + NS_PER_SEC * 60 + 40, 333);
1198     processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 + 40);
1199 
1200     // Cancel battery saver mode and screen on activation.
1201     int64_t firstDeactivation = bucketStartTimeNs + NS_PER_SEC * 61;
1202     event = CreateScreenBrightnessChangedEvent(firstDeactivation, 64);
1203     processor.OnLogEvent(event.get(), firstDeactivation);
1204     EXPECT_FALSE(metricsManager->isActive());
1205     EXPECT_FALSE(metricProducer->mIsActive);
1206     // New broadcast since the config is no longer active.
1207     EXPECT_EQ(broadcastCount, 2);
1208     ASSERT_EQ(activeConfigsBroadcast.size(), 0);
1209     EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kNotActive);
1210     EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kNotActive);
1211 
1212     // Should be ignored
1213     event = CreateAppCrashEvent(bucketStartTimeNs + NS_PER_SEC * 61 + 80, 444);
1214     processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 61 + 80);
1215 
1216     // Re-enable battery saver mode activation.
1217     event = CreateBatterySaverOnEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 15);
1218     processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 15);
1219     EXPECT_TRUE(metricsManager->isActive());
1220     EXPECT_TRUE(metricProducer->mIsActive);
1221     EXPECT_EQ(broadcastCount, 3);
1222     ASSERT_EQ(activeConfigsBroadcast.size(), 1);
1223     EXPECT_EQ(activeConfigsBroadcast[0], cfgId);
1224     EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kActive);
1225     EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 15);
1226     EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kNotActive);
1227 
1228     // 3rd processed event.
1229     event = CreateAppCrashEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 80, 555);
1230     processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 80);
1231 
1232     // Cancel battery saver mode activation.
1233     int64_t secondDeactivation = bucketStartTimeNs + NS_PER_SEC * 60 * 13;
1234     event = CreateScreenBrightnessChangedEvent(secondDeactivation, 140);
1235     processor.OnLogEvent(event.get(), secondDeactivation);
1236     EXPECT_FALSE(metricsManager->isActive());
1237     EXPECT_FALSE(metricProducer->mIsActive);
1238     EXPECT_EQ(broadcastCount, 4);
1239     ASSERT_EQ(activeConfigsBroadcast.size(), 0);
1240     EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kNotActive);
1241     EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kNotActive);
1242 
1243     // Should be ignored.
1244     event = CreateAppCrashEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 13 + 80, 666);
1245     processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 13 + 80);
1246 
1247     ConfigMetricsReportList reports;
1248     vector<uint8_t> buffer;
1249     processor.onDumpReport(cfgKey, bucketStartTimeNs + NS_PER_SEC * 60 * 15 + 1, false, true,
1250                            ADB_DUMP, FAST, &buffer);
1251     EXPECT_TRUE(buffer.size() > 0);
1252     EXPECT_TRUE(reports.ParseFromArray(&buffer[0], buffer.size()));
1253     backfillDimensionPath(&reports);
1254     backfillStartEndTimestamp(&reports);
1255     ASSERT_EQ(1, reports.reports_size());
1256     ASSERT_EQ(1, reports.reports(0).metrics_size());
1257 
1258     StatsLogReport::CountMetricDataWrapper countMetrics;
1259     sortMetricDataByDimensionsValue(reports.reports(0).metrics(0).count_metrics(), &countMetrics);
1260     ASSERT_EQ(3, countMetrics.data_size());
1261 
1262     auto data = countMetrics.data(0);
1263     EXPECT_EQ(util::PROCESS_LIFE_CYCLE_STATE_CHANGED, data.dimensions_in_what().field());
1264     ASSERT_EQ(1, data.dimensions_in_what().value_tuple().dimensions_value_size());
1265     EXPECT_EQ(1 /* uid field */,
1266               data.dimensions_in_what().value_tuple().dimensions_value(0).field());
1267     EXPECT_EQ(222, data.dimensions_in_what().value_tuple().dimensions_value(0).value_int());
1268     ASSERT_EQ(1, data.bucket_info_size());
1269     EXPECT_EQ(1, data.bucket_info(0).count());
1270     EXPECT_EQ(bucketStartTimeNs, data.bucket_info(0).start_bucket_elapsed_nanos());
1271     EXPECT_EQ(firstDeactivation, data.bucket_info(0).end_bucket_elapsed_nanos());
1272 
1273     data = countMetrics.data(1);
1274     EXPECT_EQ(util::PROCESS_LIFE_CYCLE_STATE_CHANGED, data.dimensions_in_what().field());
1275     ASSERT_EQ(1, data.dimensions_in_what().value_tuple().dimensions_value_size());
1276     EXPECT_EQ(1 /* uid field */,
1277               data.dimensions_in_what().value_tuple().dimensions_value(0).field());
1278     EXPECT_EQ(333, data.dimensions_in_what().value_tuple().dimensions_value(0).value_int());
1279     ASSERT_EQ(1, data.bucket_info_size());
1280     EXPECT_EQ(1, data.bucket_info(0).count());
1281     EXPECT_EQ(bucketStartTimeNs, data.bucket_info(0).start_bucket_elapsed_nanos());
1282     EXPECT_EQ(firstDeactivation, data.bucket_info(0).end_bucket_elapsed_nanos());
1283 
1284     data = countMetrics.data(2);
1285     EXPECT_EQ(util::PROCESS_LIFE_CYCLE_STATE_CHANGED, data.dimensions_in_what().field());
1286     ASSERT_EQ(1, data.dimensions_in_what().value_tuple().dimensions_value_size());
1287     EXPECT_EQ(1 /* uid field */,
1288               data.dimensions_in_what().value_tuple().dimensions_value(0).field());
1289     EXPECT_EQ(555, data.dimensions_in_what().value_tuple().dimensions_value(0).value_int());
1290     ASSERT_EQ(1, data.bucket_info_size());
1291     EXPECT_EQ(1, data.bucket_info(0).count());
1292     // Partial bucket as metric is deactivated.
1293     EXPECT_EQ(bucketStartTimeNs + 2 * bucketSizeNs,
1294               data.bucket_info(0).start_bucket_elapsed_nanos());
1295     EXPECT_EQ(secondDeactivation, data.bucket_info(0).end_bucket_elapsed_nanos());
1296 }
1297 
TEST(MetricActivationE2eTest,TestCountMetricWithTwoMetricsTwoDeactivations)1298 TEST(MetricActivationE2eTest, TestCountMetricWithTwoMetricsTwoDeactivations) {
1299     auto config = CreateStatsdConfigWithTwoMetricsTwoDeactivations();
1300 
1301     int64_t bucketStartTimeNs = NS_PER_SEC * 10;  // 10 secs
1302     int64_t bucketSizeNs =
1303             TimeUnitToBucketSizeInMillis(config.count_metric(0).bucket()) * 1000LL * 1000LL;
1304 
1305     int uid = 12345;
1306     int64_t cfgId = 98765;
1307     ConfigKey cfgKey(uid, cfgId);
1308 
1309     sp<UidMap> m = new UidMap();
1310     sp<StatsPullerManager> pullerManager = new StatsPullerManager();
1311     sp<AlarmMonitor> anomalyAlarmMonitor;
1312     sp<AlarmMonitor> subscriberAlarmMonitor;
1313     vector<int64_t> activeConfigsBroadcast;
1314 
1315     long timeBase1 = 1;
1316     int broadcastCount = 0;
1317     StatsLogProcessor processor(
1318             m, pullerManager, anomalyAlarmMonitor, subscriberAlarmMonitor, bucketStartTimeNs,
1319             [](const ConfigKey& key) { return true; },
1320             [&uid, &broadcastCount, &activeConfigsBroadcast](const int& broadcastUid,
1321                                                              const vector<int64_t>& activeConfigs) {
1322                 broadcastCount++;
1323                 EXPECT_EQ(broadcastUid, uid);
1324                 activeConfigsBroadcast.clear();
1325                 activeConfigsBroadcast.insert(activeConfigsBroadcast.end(), activeConfigs.begin(),
1326                                               activeConfigs.end());
1327                 return true;
1328             });
1329 
1330     processor.OnConfigUpdated(bucketStartTimeNs, cfgKey, config);
1331 
1332     ASSERT_EQ(processor.mMetricsManagers.size(), 1u);
1333     sp<MetricsManager> metricsManager = processor.mMetricsManagers.begin()->second;
1334     EXPECT_TRUE(metricsManager->isConfigValid());
1335     ASSERT_EQ(metricsManager->mAllMetricProducers.size(), 2);
1336     sp<MetricProducer> metricProducer = metricsManager->mAllMetricProducers[0];
1337     auto& eventActivationMap = metricProducer->mEventActivationMap;
1338     auto& eventDeactivationMap = metricProducer->mEventDeactivationMap;
1339     sp<MetricProducer> metricProducer2 = metricsManager->mAllMetricProducers[1];
1340     auto& eventActivationMap2 = metricProducer2->mEventActivationMap;
1341     auto& eventDeactivationMap2 = metricProducer2->mEventDeactivationMap;
1342 
1343     EXPECT_FALSE(metricsManager->isActive());
1344     EXPECT_FALSE(metricProducer->mIsActive);
1345     EXPECT_FALSE(metricProducer2->mIsActive);
1346     // Two activations: one is triggered by battery saver mode (tracker index 0), the other is
1347     // triggered by screen on event (tracker index 2).
1348     ASSERT_EQ(eventActivationMap.size(), 2u);
1349     EXPECT_TRUE(eventActivationMap.find(0) != eventActivationMap.end());
1350     EXPECT_TRUE(eventActivationMap.find(2) != eventActivationMap.end());
1351     EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kNotActive);
1352     EXPECT_EQ(eventActivationMap[0]->start_ns, 0);
1353     EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
1354     EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kNotActive);
1355     EXPECT_EQ(eventActivationMap[2]->start_ns, 0);
1356     EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
1357     ASSERT_EQ(eventDeactivationMap.size(), 2u);
1358     EXPECT_TRUE(eventDeactivationMap.find(3) != eventDeactivationMap.end());
1359     EXPECT_TRUE(eventDeactivationMap.find(4) != eventDeactivationMap.end());
1360     ASSERT_EQ(eventDeactivationMap[3].size(), 1u);
1361     ASSERT_EQ(eventDeactivationMap[4].size(), 1u);
1362     EXPECT_EQ(eventDeactivationMap[3][0], eventActivationMap[0]);
1363     EXPECT_EQ(eventDeactivationMap[4][0], eventActivationMap[2]);
1364 
1365     ASSERT_EQ(eventActivationMap2.size(), 2u);
1366     EXPECT_TRUE(eventActivationMap2.find(0) != eventActivationMap2.end());
1367     EXPECT_TRUE(eventActivationMap2.find(2) != eventActivationMap2.end());
1368     EXPECT_EQ(eventActivationMap2[0]->state, ActivationState::kNotActive);
1369     EXPECT_EQ(eventActivationMap2[0]->start_ns, 0);
1370     EXPECT_EQ(eventActivationMap2[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
1371     EXPECT_EQ(eventActivationMap2[2]->state, ActivationState::kNotActive);
1372     EXPECT_EQ(eventActivationMap2[2]->start_ns, 0);
1373     EXPECT_EQ(eventActivationMap2[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
1374     ASSERT_EQ(eventDeactivationMap2.size(), 2u);
1375     EXPECT_TRUE(eventDeactivationMap2.find(3) != eventDeactivationMap2.end());
1376     EXPECT_TRUE(eventDeactivationMap2.find(4) != eventDeactivationMap2.end());
1377     ASSERT_EQ(eventDeactivationMap[3].size(), 1u);
1378     ASSERT_EQ(eventDeactivationMap[4].size(), 1u);
1379     EXPECT_EQ(eventDeactivationMap2[3][0], eventActivationMap2[0]);
1380     EXPECT_EQ(eventDeactivationMap2[4][0], eventActivationMap2[2]);
1381 
1382     std::unique_ptr<LogEvent> event;
1383 
1384     event = CreateAppCrashEvent(bucketStartTimeNs + 5, 111);
1385     processor.OnLogEvent(event.get(), bucketStartTimeNs + 5);
1386     event = CreateMoveToForegroundEvent(bucketStartTimeNs + 5, 1111);
1387     processor.OnLogEvent(event.get(), bucketStartTimeNs + 5);
1388     EXPECT_FALSE(metricsManager->isActive());
1389     EXPECT_FALSE(metricProducer->mIsActive);
1390     EXPECT_FALSE(metricProducer2->mIsActive);
1391     EXPECT_EQ(broadcastCount, 0);
1392 
1393     // Activated by battery save mode.
1394     event = CreateBatterySaverOnEvent(bucketStartTimeNs + 10);
1395     processor.OnLogEvent(event.get(), bucketStartTimeNs + 10);
1396     EXPECT_TRUE(metricsManager->isActive());
1397     EXPECT_EQ(broadcastCount, 1);
1398     ASSERT_EQ(activeConfigsBroadcast.size(), 1);
1399     EXPECT_EQ(activeConfigsBroadcast[0], cfgId);
1400     EXPECT_TRUE(metricProducer->mIsActive);
1401     EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kActive);
1402     EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + 10);
1403     EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
1404     EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kNotActive);
1405     EXPECT_EQ(eventActivationMap[2]->start_ns, 0);
1406     EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
1407     EXPECT_EQ(eventDeactivationMap[3][0], eventActivationMap[0]);
1408     EXPECT_EQ(eventDeactivationMap[4][0], eventActivationMap[2]);
1409     EXPECT_TRUE(metricProducer2->mIsActive);
1410     EXPECT_EQ(eventActivationMap2[0]->state, ActivationState::kActive);
1411     EXPECT_EQ(eventActivationMap2[0]->start_ns, bucketStartTimeNs + 10);
1412     EXPECT_EQ(eventActivationMap2[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
1413     EXPECT_EQ(eventActivationMap2[2]->state, ActivationState::kNotActive);
1414     EXPECT_EQ(eventActivationMap2[2]->start_ns, 0);
1415     EXPECT_EQ(eventActivationMap2[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
1416     EXPECT_EQ(eventDeactivationMap2[3][0], eventActivationMap2[0]);
1417     EXPECT_EQ(eventDeactivationMap2[4][0], eventActivationMap2[2]);
1418 
1419     // First processed event.
1420     event = CreateAppCrashEvent(bucketStartTimeNs + 15, 222);
1421     processor.OnLogEvent(event.get(), bucketStartTimeNs + 15);
1422     event = CreateMoveToForegroundEvent(bucketStartTimeNs + 15, 2222);
1423     processor.OnLogEvent(event.get(), bucketStartTimeNs + 15);
1424 
1425     // Activated by screen on event.
1426     event = CreateScreenStateChangedEvent(bucketStartTimeNs + 20, android::view::DISPLAY_STATE_ON);
1427     processor.OnLogEvent(event.get(), bucketStartTimeNs + 20);
1428     EXPECT_TRUE(metricsManager->isActive());
1429     EXPECT_TRUE(metricProducer->mIsActive);
1430     EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kActive);
1431     EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + 10);
1432     EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
1433     EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kActive);
1434     EXPECT_EQ(eventActivationMap[2]->start_ns, bucketStartTimeNs + 20);
1435     EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
1436     EXPECT_EQ(eventDeactivationMap[3][0], eventActivationMap[0]);
1437     EXPECT_EQ(eventDeactivationMap[4][0], eventActivationMap[2]);
1438     EXPECT_TRUE(metricProducer2->mIsActive);
1439     EXPECT_EQ(eventActivationMap2[0]->state, ActivationState::kActive);
1440     EXPECT_EQ(eventActivationMap2[0]->start_ns, bucketStartTimeNs + 10);
1441     EXPECT_EQ(eventActivationMap2[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
1442     EXPECT_EQ(eventActivationMap2[2]->state, ActivationState::kActive);
1443     EXPECT_EQ(eventActivationMap2[2]->start_ns, bucketStartTimeNs + 20);
1444     EXPECT_EQ(eventActivationMap2[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
1445     EXPECT_EQ(eventDeactivationMap2[3][0], eventActivationMap2[0]);
1446     EXPECT_EQ(eventDeactivationMap2[4][0], eventActivationMap2[2]);
1447 
1448     // 2nd processed event.
1449     // The activation by screen_on event expires, but the one by battery save mode is still active.
1450     event = CreateAppCrashEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 2 + 25, 333);
1451     processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 2 + 25);
1452     event = CreateMoveToForegroundEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 2 + 25, 3333);
1453     processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 2 + 25);
1454     EXPECT_TRUE(metricsManager->isActive());
1455     EXPECT_TRUE(metricProducer->mIsActive);
1456     EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kActive);
1457     EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + 10);
1458     EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
1459     EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kNotActive);
1460     EXPECT_EQ(eventActivationMap[2]->start_ns, bucketStartTimeNs + 20);
1461     EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
1462     EXPECT_EQ(eventDeactivationMap[3][0], eventActivationMap[0]);
1463     EXPECT_EQ(eventDeactivationMap[4][0], eventActivationMap[2]);
1464     EXPECT_TRUE(metricProducer2->mIsActive);
1465     EXPECT_EQ(eventActivationMap2[0]->state, ActivationState::kActive);
1466     EXPECT_EQ(eventActivationMap2[0]->start_ns, bucketStartTimeNs + 10);
1467     EXPECT_EQ(eventActivationMap2[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
1468     EXPECT_EQ(eventActivationMap2[2]->state, ActivationState::kNotActive);
1469     EXPECT_EQ(eventActivationMap2[2]->start_ns, bucketStartTimeNs + 20);
1470     EXPECT_EQ(eventActivationMap2[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
1471     EXPECT_EQ(eventDeactivationMap2[3][0], eventActivationMap2[0]);
1472     EXPECT_EQ(eventDeactivationMap2[4][0], eventActivationMap2[2]);
1473     // No new broadcast since the config should still be active.
1474     EXPECT_EQ(broadcastCount, 1);
1475 
1476     // 3rd processed event.
1477     event = CreateAppCrashEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 5 + 25, 444);
1478     processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 5 + 25);
1479     event = CreateMoveToForegroundEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 5 + 25, 4444);
1480     processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 5 + 25);
1481 
1482     // All activations expired.
1483     event = CreateAppCrashEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 8, 555);
1484     processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 8);
1485     event = CreateMoveToForegroundEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 8, 5555);
1486     processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 8);
1487     EXPECT_FALSE(metricsManager->isActive());
1488     // New broadcast since the config is no longer active.
1489     EXPECT_EQ(broadcastCount, 2);
1490     ASSERT_EQ(activeConfigsBroadcast.size(), 0);
1491     EXPECT_FALSE(metricProducer->mIsActive);
1492     EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kNotActive);
1493     EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + 10);
1494     EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
1495     EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kNotActive);
1496     EXPECT_EQ(eventActivationMap[2]->start_ns, bucketStartTimeNs + 20);
1497     EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
1498     EXPECT_EQ(eventDeactivationMap[3][0], eventActivationMap[0]);
1499     EXPECT_EQ(eventDeactivationMap[4][0], eventActivationMap[2]);
1500     EXPECT_FALSE(metricProducer2->mIsActive);
1501     EXPECT_EQ(eventActivationMap2[0]->state, ActivationState::kNotActive);
1502     EXPECT_EQ(eventActivationMap2[0]->start_ns, bucketStartTimeNs + 10);
1503     EXPECT_EQ(eventActivationMap2[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
1504     EXPECT_EQ(eventActivationMap2[2]->state, ActivationState::kNotActive);
1505     EXPECT_EQ(eventActivationMap2[2]->start_ns, bucketStartTimeNs + 20);
1506     EXPECT_EQ(eventActivationMap2[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
1507     EXPECT_EQ(eventDeactivationMap2[3][0], eventActivationMap2[0]);
1508     EXPECT_EQ(eventDeactivationMap2[4][0], eventActivationMap2[2]);
1509 
1510     // Re-activate metric via screen on.
1511     event = CreateScreenStateChangedEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 10,
1512                                           android::view::DISPLAY_STATE_ON);
1513     processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 10);
1514     EXPECT_TRUE(metricsManager->isActive());
1515     EXPECT_EQ(broadcastCount, 3);
1516     ASSERT_EQ(activeConfigsBroadcast.size(), 1);
1517     EXPECT_EQ(activeConfigsBroadcast[0], cfgId);
1518     EXPECT_TRUE(metricProducer->mIsActive);
1519     EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kNotActive);
1520     EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + 10);
1521     EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
1522     EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kActive);
1523     EXPECT_EQ(eventActivationMap[2]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 10);
1524     EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
1525     EXPECT_EQ(eventDeactivationMap[3][0], eventActivationMap[0]);
1526     EXPECT_EQ(eventDeactivationMap[4][0], eventActivationMap[2]);
1527     EXPECT_TRUE(metricProducer2->mIsActive);
1528     EXPECT_EQ(eventActivationMap2[0]->state, ActivationState::kNotActive);
1529     EXPECT_EQ(eventActivationMap2[0]->start_ns, bucketStartTimeNs + 10);
1530     EXPECT_EQ(eventActivationMap2[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
1531     EXPECT_EQ(eventActivationMap2[2]->state, ActivationState::kActive);
1532     EXPECT_EQ(eventActivationMap2[2]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 10);
1533     EXPECT_EQ(eventActivationMap2[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
1534     EXPECT_EQ(eventDeactivationMap2[3][0], eventActivationMap2[0]);
1535     EXPECT_EQ(eventDeactivationMap2[4][0], eventActivationMap2[2]);
1536 
1537     // 4th processed event.
1538     event = CreateAppCrashEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 1, 666);
1539     processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 1);
1540     event = CreateMoveToForegroundEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 1, 6666);
1541     processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 1);
1542 
1543     // Re-enable battery saver mode activation.
1544     event = CreateBatterySaverOnEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 15);
1545     processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 15);
1546     EXPECT_TRUE(metricsManager->isActive());
1547     EXPECT_EQ(broadcastCount, 3);
1548     ASSERT_EQ(activeConfigsBroadcast.size(), 1);
1549     EXPECT_EQ(activeConfigsBroadcast[0], cfgId);
1550     EXPECT_TRUE(metricProducer->mIsActive);
1551     EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kActive);
1552     EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 15);
1553     EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
1554     EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kActive);
1555     EXPECT_EQ(eventActivationMap[2]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 10);
1556     EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
1557     EXPECT_EQ(eventDeactivationMap[3][0], eventActivationMap[0]);
1558     EXPECT_EQ(eventDeactivationMap[4][0], eventActivationMap[2]);
1559     EXPECT_TRUE(metricProducer2->mIsActive);
1560     EXPECT_EQ(eventActivationMap2[0]->state, ActivationState::kActive);
1561     EXPECT_EQ(eventActivationMap2[0]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 15);
1562     EXPECT_EQ(eventActivationMap2[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
1563     EXPECT_EQ(eventActivationMap2[2]->state, ActivationState::kActive);
1564     EXPECT_EQ(eventActivationMap2[2]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 10);
1565     EXPECT_EQ(eventActivationMap2[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
1566     EXPECT_EQ(eventDeactivationMap2[3][0], eventActivationMap2[0]);
1567     EXPECT_EQ(eventDeactivationMap2[4][0], eventActivationMap2[2]);
1568 
1569     // 5th processed event.
1570     event = CreateAppCrashEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 40, 777);
1571     processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 40);
1572     event = CreateMoveToForegroundEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 40, 7777);
1573     processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 40);
1574 
1575     // Cancel battery saver mode and screen on activation.
1576     event = CreateScreenBrightnessChangedEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 60, 64);
1577     processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 60);
1578     EXPECT_FALSE(metricsManager->isActive());
1579     // New broadcast since the config is no longer active.
1580     EXPECT_EQ(broadcastCount, 4);
1581     ASSERT_EQ(activeConfigsBroadcast.size(), 0);
1582     EXPECT_FALSE(metricProducer->mIsActive);
1583     EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kNotActive);
1584     EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 15);
1585     EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
1586     EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kNotActive);
1587     EXPECT_EQ(eventActivationMap[2]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 10);
1588     EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
1589     EXPECT_EQ(eventDeactivationMap[3][0], eventActivationMap[0]);
1590     EXPECT_EQ(eventDeactivationMap[4][0], eventActivationMap[2]);
1591     EXPECT_FALSE(metricProducer2->mIsActive);
1592     EXPECT_EQ(eventActivationMap2[0]->state, ActivationState::kNotActive);
1593     EXPECT_EQ(eventActivationMap2[0]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 15);
1594     EXPECT_EQ(eventActivationMap2[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
1595     EXPECT_EQ(eventActivationMap2[2]->state, ActivationState::kNotActive);
1596     EXPECT_EQ(eventActivationMap2[2]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 10);
1597     EXPECT_EQ(eventActivationMap2[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
1598     EXPECT_EQ(eventDeactivationMap2[3][0], eventActivationMap2[0]);
1599     EXPECT_EQ(eventDeactivationMap2[4][0], eventActivationMap2[2]);
1600 
1601     // Screen-on activation expired.
1602     event = CreateAppCrashEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 13, 888);
1603     processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 13);
1604     event = CreateMoveToForegroundEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 13, 8888);
1605     processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 13);
1606     EXPECT_FALSE(metricsManager->isActive());
1607     EXPECT_EQ(broadcastCount, 4);
1608     ASSERT_EQ(activeConfigsBroadcast.size(), 0);
1609     EXPECT_FALSE(metricProducer->mIsActive);
1610     EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kNotActive);
1611     EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 15);
1612     EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
1613     EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kNotActive);
1614     EXPECT_EQ(eventActivationMap[2]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 10);
1615     EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
1616     EXPECT_EQ(eventDeactivationMap[3][0], eventActivationMap[0]);
1617     EXPECT_EQ(eventDeactivationMap[4][0], eventActivationMap[2]);
1618     EXPECT_FALSE(metricProducer2->mIsActive);
1619     EXPECT_EQ(eventActivationMap2[0]->state, ActivationState::kNotActive);
1620     EXPECT_EQ(eventActivationMap2[0]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 11 + 15);
1621     EXPECT_EQ(eventActivationMap2[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
1622     EXPECT_EQ(eventActivationMap2[2]->state, ActivationState::kNotActive);
1623     EXPECT_EQ(eventActivationMap2[2]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 10);
1624     EXPECT_EQ(eventActivationMap2[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
1625     EXPECT_EQ(eventDeactivationMap2[3][0], eventActivationMap2[0]);
1626     EXPECT_EQ(eventDeactivationMap2[4][0], eventActivationMap2[2]);
1627 
1628     event = CreateAppCrashEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 14 + 1, 999);
1629     processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 14 + 1);
1630     event = CreateMoveToForegroundEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 14 + 1, 9999);
1631     processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 14 + 1);
1632 
1633     // Re-enable battery saver mode activation.
1634     event = CreateBatterySaverOnEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 15 + 15);
1635     processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 15 + 15);
1636     EXPECT_TRUE(metricsManager->isActive());
1637     EXPECT_EQ(broadcastCount, 5);
1638     ASSERT_EQ(activeConfigsBroadcast.size(), 1);
1639     EXPECT_EQ(activeConfigsBroadcast[0], cfgId);
1640     EXPECT_TRUE(metricProducer->mIsActive);
1641     EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kActive);
1642     EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 15 + 15);
1643     EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
1644     EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kNotActive);
1645     EXPECT_EQ(eventActivationMap[2]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 10);
1646     EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
1647     EXPECT_EQ(eventDeactivationMap[3][0], eventActivationMap[0]);
1648     EXPECT_EQ(eventDeactivationMap[4][0], eventActivationMap[2]);
1649     EXPECT_TRUE(metricProducer2->mIsActive);
1650     EXPECT_EQ(eventActivationMap2[0]->state, ActivationState::kActive);
1651     EXPECT_EQ(eventActivationMap2[0]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 15 + 15);
1652     EXPECT_EQ(eventActivationMap2[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
1653     EXPECT_EQ(eventActivationMap2[2]->state, ActivationState::kNotActive);
1654     EXPECT_EQ(eventActivationMap2[2]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 10);
1655     EXPECT_EQ(eventActivationMap2[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
1656     EXPECT_EQ(eventDeactivationMap2[3][0], eventActivationMap2[0]);
1657     EXPECT_EQ(eventDeactivationMap2[4][0], eventActivationMap2[2]);
1658 
1659     // Cancel battery saver mode and screen on activation.
1660     event = CreateScreenBrightnessChangedEvent(bucketStartTimeNs + NS_PER_SEC * 60 * 16, 140);
1661     processor.OnLogEvent(event.get(), bucketStartTimeNs + NS_PER_SEC * 60 * 16);
1662     EXPECT_FALSE(metricsManager->isActive());
1663     EXPECT_EQ(broadcastCount, 6);
1664     ASSERT_EQ(activeConfigsBroadcast.size(), 0);
1665     EXPECT_FALSE(metricProducer->mIsActive);
1666     EXPECT_EQ(eventActivationMap[0]->state, ActivationState::kNotActive);
1667     EXPECT_EQ(eventActivationMap[0]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 15 + 15);
1668     EXPECT_EQ(eventActivationMap[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
1669     EXPECT_EQ(eventActivationMap[2]->state, ActivationState::kNotActive);
1670     EXPECT_EQ(eventActivationMap[2]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 10);
1671     EXPECT_EQ(eventActivationMap[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
1672     EXPECT_EQ(eventDeactivationMap[3][0], eventActivationMap[0]);
1673     EXPECT_EQ(eventDeactivationMap[4][0], eventActivationMap[2]);
1674     EXPECT_FALSE(metricProducer2->mIsActive);
1675     EXPECT_EQ(eventActivationMap2[0]->state, ActivationState::kNotActive);
1676     EXPECT_EQ(eventActivationMap2[0]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 15 + 15);
1677     EXPECT_EQ(eventActivationMap2[0]->ttl_ns, 60 * 6 * NS_PER_SEC);
1678     EXPECT_EQ(eventActivationMap2[2]->state, ActivationState::kNotActive);
1679     EXPECT_EQ(eventActivationMap2[2]->start_ns, bucketStartTimeNs + NS_PER_SEC * 60 * 10 + 10);
1680     EXPECT_EQ(eventActivationMap2[2]->ttl_ns, 60 * 2 * NS_PER_SEC);
1681     EXPECT_EQ(eventDeactivationMap2[3][0], eventActivationMap2[0]);
1682     EXPECT_EQ(eventDeactivationMap2[4][0], eventActivationMap2[2]);
1683 
1684     ConfigMetricsReportList reports;
1685     vector<uint8_t> buffer;
1686     processor.onDumpReport(cfgKey, bucketStartTimeNs + NS_PER_SEC * 60 * 15 + 1, false, true,
1687                            ADB_DUMP, FAST, &buffer);
1688     EXPECT_TRUE(buffer.size() > 0);
1689     EXPECT_TRUE(reports.ParseFromArray(&buffer[0], buffer.size()));
1690     backfillDimensionPath(&reports);
1691     backfillStartEndTimestamp(&reports);
1692     ASSERT_EQ(1, reports.reports_size());
1693     ASSERT_EQ(2, reports.reports(0).metrics_size());
1694 
1695     StatsLogReport::CountMetricDataWrapper countMetrics;
1696 
1697     sortMetricDataByDimensionsValue(reports.reports(0).metrics(0).count_metrics(), &countMetrics);
1698     ASSERT_EQ(5, countMetrics.data_size());
1699 
1700     auto data = countMetrics.data(0);
1701     EXPECT_EQ(util::PROCESS_LIFE_CYCLE_STATE_CHANGED, data.dimensions_in_what().field());
1702     ASSERT_EQ(1, data.dimensions_in_what().value_tuple().dimensions_value_size());
1703     EXPECT_EQ(1 /* uid field */,
1704               data.dimensions_in_what().value_tuple().dimensions_value(0).field());
1705     EXPECT_EQ(222, data.dimensions_in_what().value_tuple().dimensions_value(0).value_int());
1706     ASSERT_EQ(1, data.bucket_info_size());
1707     EXPECT_EQ(1, data.bucket_info(0).count());
1708     EXPECT_EQ(bucketStartTimeNs, data.bucket_info(0).start_bucket_elapsed_nanos());
1709     EXPECT_EQ(bucketStartTimeNs + bucketSizeNs, data.bucket_info(0).end_bucket_elapsed_nanos());
1710 
1711     data = countMetrics.data(1);
1712     EXPECT_EQ(util::PROCESS_LIFE_CYCLE_STATE_CHANGED, data.dimensions_in_what().field());
1713     ASSERT_EQ(1, data.dimensions_in_what().value_tuple().dimensions_value_size());
1714     EXPECT_EQ(1 /* uid field */,
1715               data.dimensions_in_what().value_tuple().dimensions_value(0).field());
1716     EXPECT_EQ(333, data.dimensions_in_what().value_tuple().dimensions_value(0).value_int());
1717     ASSERT_EQ(1, data.bucket_info_size());
1718     EXPECT_EQ(1, data.bucket_info(0).count());
1719     EXPECT_EQ(bucketStartTimeNs, data.bucket_info(0).start_bucket_elapsed_nanos());
1720     EXPECT_EQ(bucketStartTimeNs + bucketSizeNs, data.bucket_info(0).end_bucket_elapsed_nanos());
1721 
1722     data = countMetrics.data(2);
1723     EXPECT_EQ(util::PROCESS_LIFE_CYCLE_STATE_CHANGED, data.dimensions_in_what().field());
1724     ASSERT_EQ(1, data.dimensions_in_what().value_tuple().dimensions_value_size());
1725     EXPECT_EQ(1 /* uid field */,
1726               data.dimensions_in_what().value_tuple().dimensions_value(0).field());
1727     EXPECT_EQ(444, data.dimensions_in_what().value_tuple().dimensions_value(0).value_int());
1728     ASSERT_EQ(1, data.bucket_info_size());
1729     EXPECT_EQ(1, data.bucket_info(0).count());
1730     // Partial bucket as metric is deactivated.
1731     EXPECT_EQ(bucketStartTimeNs + bucketSizeNs, data.bucket_info(0).start_bucket_elapsed_nanos());
1732     EXPECT_EQ(bucketStartTimeNs + NS_PER_SEC * 60 * 8,
1733               data.bucket_info(0).end_bucket_elapsed_nanos());
1734 
1735     data = countMetrics.data(3);
1736     EXPECT_EQ(util::PROCESS_LIFE_CYCLE_STATE_CHANGED, data.dimensions_in_what().field());
1737     ASSERT_EQ(1, data.dimensions_in_what().value_tuple().dimensions_value_size());
1738     EXPECT_EQ(1 /* uid field */,
1739               data.dimensions_in_what().value_tuple().dimensions_value(0).field());
1740     EXPECT_EQ(666, data.dimensions_in_what().value_tuple().dimensions_value(0).value_int());
1741     ASSERT_EQ(1, data.bucket_info_size());
1742     EXPECT_EQ(1, data.bucket_info(0).count());
1743     EXPECT_EQ(bucketStartTimeNs + 2 * bucketSizeNs,
1744               data.bucket_info(0).start_bucket_elapsed_nanos());
1745     EXPECT_EQ(bucketStartTimeNs + NS_PER_SEC * 60 * 11,
1746               data.bucket_info(0).end_bucket_elapsed_nanos());
1747 
1748     data = countMetrics.data(4);
1749     EXPECT_EQ(util::PROCESS_LIFE_CYCLE_STATE_CHANGED, data.dimensions_in_what().field());
1750     ASSERT_EQ(1, data.dimensions_in_what().value_tuple().dimensions_value_size());
1751     EXPECT_EQ(1 /* uid field */,
1752               data.dimensions_in_what().value_tuple().dimensions_value(0).field());
1753     EXPECT_EQ(777, data.dimensions_in_what().value_tuple().dimensions_value(0).value_int());
1754     ASSERT_EQ(1, data.bucket_info_size());
1755     EXPECT_EQ(1, data.bucket_info(0).count());
1756     EXPECT_EQ(bucketStartTimeNs + 2 * bucketSizeNs,
1757               data.bucket_info(0).start_bucket_elapsed_nanos());
1758     EXPECT_EQ(bucketStartTimeNs + NS_PER_SEC * 60 * 11,
1759               data.bucket_info(0).end_bucket_elapsed_nanos());
1760 
1761     countMetrics.clear_data();
1762     sortMetricDataByDimensionsValue(reports.reports(0).metrics(1).count_metrics(), &countMetrics);
1763     ASSERT_EQ(5, countMetrics.data_size());
1764 
1765     data = countMetrics.data(0);
1766     EXPECT_EQ(util::ACTIVITY_FOREGROUND_STATE_CHANGED, data.dimensions_in_what().field());
1767     ASSERT_EQ(1, data.dimensions_in_what().value_tuple().dimensions_value_size());
1768     EXPECT_EQ(1 /* uid field */,
1769               data.dimensions_in_what().value_tuple().dimensions_value(0).field());
1770     EXPECT_EQ(2222, data.dimensions_in_what().value_tuple().dimensions_value(0).value_int());
1771     ASSERT_EQ(1, data.bucket_info_size());
1772     EXPECT_EQ(1, data.bucket_info(0).count());
1773     EXPECT_EQ(bucketStartTimeNs, data.bucket_info(0).start_bucket_elapsed_nanos());
1774     EXPECT_EQ(bucketStartTimeNs + bucketSizeNs, data.bucket_info(0).end_bucket_elapsed_nanos());
1775 
1776     data = countMetrics.data(1);
1777     EXPECT_EQ(util::ACTIVITY_FOREGROUND_STATE_CHANGED, data.dimensions_in_what().field());
1778     ASSERT_EQ(1, data.dimensions_in_what().value_tuple().dimensions_value_size());
1779     EXPECT_EQ(1 /* uid field */,
1780               data.dimensions_in_what().value_tuple().dimensions_value(0).field());
1781     EXPECT_EQ(3333, data.dimensions_in_what().value_tuple().dimensions_value(0).value_int());
1782     ASSERT_EQ(1, data.bucket_info_size());
1783     EXPECT_EQ(1, data.bucket_info(0).count());
1784     EXPECT_EQ(bucketStartTimeNs, data.bucket_info(0).start_bucket_elapsed_nanos());
1785     EXPECT_EQ(bucketStartTimeNs + bucketSizeNs, data.bucket_info(0).end_bucket_elapsed_nanos());
1786 
1787     data = countMetrics.data(2);
1788     EXPECT_EQ(util::ACTIVITY_FOREGROUND_STATE_CHANGED, data.dimensions_in_what().field());
1789     ASSERT_EQ(1, data.dimensions_in_what().value_tuple().dimensions_value_size());
1790     EXPECT_EQ(1 /* uid field */,
1791               data.dimensions_in_what().value_tuple().dimensions_value(0).field());
1792     EXPECT_EQ(4444, data.dimensions_in_what().value_tuple().dimensions_value(0).value_int());
1793     ASSERT_EQ(1, data.bucket_info_size());
1794     EXPECT_EQ(1, data.bucket_info(0).count());
1795     // Partial bucket as metric is deactivated.
1796     EXPECT_EQ(bucketStartTimeNs + bucketSizeNs, data.bucket_info(0).start_bucket_elapsed_nanos());
1797     EXPECT_EQ(bucketStartTimeNs + NS_PER_SEC * 60 * 8,
1798               data.bucket_info(0).end_bucket_elapsed_nanos());
1799 
1800     data = countMetrics.data(3);
1801     EXPECT_EQ(util::ACTIVITY_FOREGROUND_STATE_CHANGED, data.dimensions_in_what().field());
1802     ASSERT_EQ(1, data.dimensions_in_what().value_tuple().dimensions_value_size());
1803     EXPECT_EQ(1 /* uid field */,
1804               data.dimensions_in_what().value_tuple().dimensions_value(0).field());
1805     EXPECT_EQ(6666, data.dimensions_in_what().value_tuple().dimensions_value(0).value_int());
1806     ASSERT_EQ(1, data.bucket_info_size());
1807     EXPECT_EQ(1, data.bucket_info(0).count());
1808     EXPECT_EQ(bucketStartTimeNs + 2 * bucketSizeNs,
1809               data.bucket_info(0).start_bucket_elapsed_nanos());
1810     EXPECT_EQ(bucketStartTimeNs + NS_PER_SEC * 60 * 11,
1811               data.bucket_info(0).end_bucket_elapsed_nanos());
1812 
1813     data = countMetrics.data(4);
1814     EXPECT_EQ(util::ACTIVITY_FOREGROUND_STATE_CHANGED, data.dimensions_in_what().field());
1815     ASSERT_EQ(1, data.dimensions_in_what().value_tuple().dimensions_value_size());
1816     EXPECT_EQ(1 /* uid field */,
1817               data.dimensions_in_what().value_tuple().dimensions_value(0).field());
1818     EXPECT_EQ(7777, data.dimensions_in_what().value_tuple().dimensions_value(0).value_int());
1819     ASSERT_EQ(1, data.bucket_info_size());
1820     EXPECT_EQ(1, data.bucket_info(0).count());
1821     EXPECT_EQ(bucketStartTimeNs + 2 * bucketSizeNs,
1822               data.bucket_info(0).start_bucket_elapsed_nanos());
1823     EXPECT_EQ(bucketStartTimeNs + NS_PER_SEC * 60 * 11,
1824               data.bucket_info(0).end_bucket_elapsed_nanos());
1825 }
1826 
1827 #else
1828 GTEST_LOG_(INFO) << "This test does nothing.\n";
1829 #endif
1830 
1831 }  // namespace statsd
1832 }  // namespace os
1833 }  // namespace android
1834