@@ -27,6 +27,92 @@ namespace statsd {
2727
2828#ifdef __ANDROID__
2929
30+ /* *
31+ * Tests the initial condition and condition after the first log events for
32+ * count metrics with either a combination condition or simple condition.
33+ *
34+ * Metrics should be initialized with condition kUnknown (given that the
35+ * predicate is using the default InitialValue of UNKNOWN). The condition should
36+ * be updated to either kFalse or kTrue if a condition event is logged for all
37+ * children conditions.
38+ */
39+ TEST (CountMetricE2eTest, TestInitialConditionChanges) {
40+ // Initialize config.
41+ StatsdConfig config;
42+ config.add_allowed_log_source (" AID_ROOT" ); // LogEvent defaults to UID of root.
43+ config.add_default_pull_packages (" AID_ROOT" ); // Fake puller is registered with root.
44+
45+ auto syncStartMatcher = CreateSyncStartAtomMatcher ();
46+ *config.add_atom_matcher () = syncStartMatcher;
47+ *config.add_atom_matcher () = CreateScreenTurnedOnAtomMatcher ();
48+ *config.add_atom_matcher () = CreateScreenTurnedOffAtomMatcher ();
49+ *config.add_atom_matcher () = CreateBatteryStateNoneMatcher ();
50+ *config.add_atom_matcher () = CreateBatteryStateUsbMatcher ();
51+
52+ auto screenOnPredicate = CreateScreenIsOnPredicate ();
53+ *config.add_predicate () = screenOnPredicate;
54+
55+ auto deviceUnpluggedPredicate = CreateDeviceUnpluggedPredicate ();
56+ *config.add_predicate () = deviceUnpluggedPredicate;
57+
58+ auto screenOnOnBatteryPredicate = config.add_predicate ();
59+ screenOnOnBatteryPredicate->set_id (StringToId (" screenOnOnBatteryPredicate" ));
60+ screenOnOnBatteryPredicate->mutable_combination ()->set_operation (LogicalOperation::AND);
61+ addPredicateToPredicateCombination (screenOnPredicate, screenOnOnBatteryPredicate);
62+ addPredicateToPredicateCombination (deviceUnpluggedPredicate, screenOnOnBatteryPredicate);
63+
64+ // CountSyncStartWhileScreenOnOnBattery (CombinationCondition)
65+ CountMetric* countMetric1 = config.add_count_metric ();
66+ countMetric1->set_id (StringToId (" CountSyncStartWhileScreenOnOnBattery" ));
67+ countMetric1->set_what (syncStartMatcher.id ());
68+ countMetric1->set_condition (screenOnOnBatteryPredicate->id ());
69+ countMetric1->set_bucket (FIVE_MINUTES);
70+
71+ // CountSyncStartWhileOnBattery (SimpleCondition)
72+ CountMetric* countMetric2 = config.add_count_metric ();
73+ countMetric2->set_id (StringToId (" CountSyncStartWhileOnBatterySliceScreen" ));
74+ countMetric2->set_what (syncStartMatcher.id ());
75+ countMetric2->set_condition (deviceUnpluggedPredicate.id ());
76+ countMetric2->set_bucket (FIVE_MINUTES);
77+
78+ const uint64_t bucketStartTimeNs = 10000000000 ; // 0:10
79+ const uint64_t bucketSizeNs =
80+ TimeUnitToBucketSizeInMillis (config.count_metric (0 ).bucket ()) * 1000000LL ;
81+ int uid = 12345 ;
82+ int64_t cfgId = 98765 ;
83+ ConfigKey cfgKey (uid, cfgId);
84+ auto processor = CreateStatsLogProcessor (bucketStartTimeNs, bucketStartTimeNs, config, cfgKey);
85+
86+ EXPECT_EQ (processor->mMetricsManagers .size (), 1u );
87+ sp<MetricsManager> metricsManager = processor->mMetricsManagers .begin ()->second ;
88+ EXPECT_TRUE (metricsManager->isConfigValid ());
89+ EXPECT_EQ (2 , metricsManager->mAllMetricProducers .size ());
90+
91+ sp<MetricProducer> metricProducer1 = metricsManager->mAllMetricProducers [0 ];
92+ sp<MetricProducer> metricProducer2 = metricsManager->mAllMetricProducers [1 ];
93+
94+ EXPECT_EQ (ConditionState::kUnknown , metricProducer1->mCondition );
95+ EXPECT_EQ (ConditionState::kUnknown , metricProducer2->mCondition );
96+
97+ auto screenOnEvent =
98+ CreateScreenStateChangedEvent (bucketStartTimeNs + 30 , android::view::DISPLAY_STATE_ON);
99+ processor->OnLogEvent (screenOnEvent.get ());
100+ EXPECT_EQ (ConditionState::kUnknown , metricProducer1->mCondition );
101+ EXPECT_EQ (ConditionState::kUnknown , metricProducer2->mCondition );
102+
103+ auto pluggedUsbEvent = CreateBatteryStateChangedEvent (
104+ bucketStartTimeNs + 50 , BatteryPluggedStateEnum::BATTERY_PLUGGED_USB);
105+ processor->OnLogEvent (pluggedUsbEvent.get ());
106+ EXPECT_EQ (ConditionState::kFalse , metricProducer1->mCondition );
107+ EXPECT_EQ (ConditionState::kFalse , metricProducer2->mCondition );
108+
109+ auto pluggedNoneEvent = CreateBatteryStateChangedEvent (
110+ bucketStartTimeNs + 70 , BatteryPluggedStateEnum::BATTERY_PLUGGED_NONE);
111+ processor->OnLogEvent (pluggedNoneEvent.get ());
112+ EXPECT_EQ (ConditionState::kTrue , metricProducer1->mCondition );
113+ EXPECT_EQ (ConditionState::kTrue , metricProducer2->mCondition );
114+ }
115+
30116/* *
31117* Test a count metric that has one slice_by_state with no primary fields.
32118*
@@ -85,7 +171,7 @@ TEST(CountMetricE2eTest, TestSlicedState) {
85171 x x x x x x (syncStartEvents)
86172 | | (ScreenIsOnEvent)
87173 | | (ScreenIsOffEvent)
88- | (ScreenUnknownEvent )
174+ | (ScreenDozeEvent )
89175 */
90176 // Initialize log events - first bucket.
91177 std::vector<int > attributionUids1 = {123 };
@@ -243,9 +329,8 @@ TEST(CountMetricE2eTest, TestSlicedStateWithMap) {
243329 |-----------------------------|-----------------------------|--
244330 x x x x x x x x x (syncStartEvents)
245331 -----------------------------------------------------------SCREEN_OFF events
246- | (ScreenStateUnknownEvent = 0)
247332 | | (ScreenStateOffEvent = 1)
248- | (ScreenStateDozeEvent = 3)
333+ | | (ScreenStateDozeEvent = 3)
249334 | (ScreenStateDozeSuspendEvent =
250335 4)
251336 -----------------------------------------------------------SCREEN_ON events
@@ -262,7 +347,7 @@ TEST(CountMetricE2eTest, TestSlicedStateWithMap) {
262347 attributionTags1, " sync_name" )); // 0:30
263348 events.push_back (CreateScreenStateChangedEvent (
264349 bucketStartTimeNs + 30 * NS_PER_SEC,
265- android::view::DisplayStateEnum::DISPLAY_STATE_UNKNOWN )); // 0:40
350+ android::view::DisplayStateEnum::DISPLAY_STATE_DOZE )); // 0:40
266351 events.push_back (CreateSyncStartEvent (bucketStartTimeNs + 60 * NS_PER_SEC, attributionUids1,
267352 attributionTags1, " sync_name" )); // 1:10
268353 events.push_back (CreateScreenStateChangedEvent (
@@ -625,9 +710,8 @@ TEST(CountMetricE2eTest, TestMultipleSlicedStates) {
625710 |------------------------|------------------------|--
626711 1 1 1 1 1 2 1 1 2 (AppCrashEvents)
627712 ---------------------------------------------------SCREEN_OFF events
628- | (ScreenUnknownEvent = 0)
629713 | | (ScreenOffEvent = 1)
630- | (ScreenDozeEvent = 3)
714+ | | (ScreenDozeEvent = 3)
631715 ---------------------------------------------------SCREEN_ON events
632716 | | (ScreenOnEvent = 2)
633717 | (ScreenOnSuspendEvent = 6)
@@ -660,7 +744,7 @@ TEST(CountMetricE2eTest, TestMultipleSlicedStates) {
660744 CreateAppCrashOccurredEvent (bucketStartTimeNs + 20 * NS_PER_SEC, 1 /* uid*/ )); // 0:30
661745 events.push_back (CreateScreenStateChangedEvent (
662746 bucketStartTimeNs + 30 * NS_PER_SEC,
663- android::view::DisplayStateEnum::DISPLAY_STATE_UNKNOWN )); // 0:40
747+ android::view::DisplayStateEnum::DISPLAY_STATE_DOZE )); // 0:40
664748 events.push_back (
665749 CreateAppCrashOccurredEvent (bucketStartTimeNs + 60 * NS_PER_SEC, 1 /* uid*/ )); // 1:10
666750 events.push_back (CreateUidProcessStateChangedEvent (
0 commit comments