1// Copyright (C) 2017 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#include "src/metrics/EventMetricProducer.h"
16#include "metrics_test_helper.h"
17#include "tests/statsd_test_util.h"
18
19#include <gmock/gmock.h>
20#include <gtest/gtest.h>
21#include <stdio.h>
22#include <vector>
23
24using namespace testing;
25using android::sp;
26using std::set;
27using std::unordered_map;
28using std::vector;
29
30#ifdef __ANDROID__
31
32namespace android {
33namespace os {
34namespace statsd {
35
36const ConfigKey kConfigKey(0, 12345);
37
38TEST(EventMetricProducerTest, TestNoCondition) {
39    int64_t bucketStartTimeNs = 10000000000;
40    int64_t eventStartTimeNs = bucketStartTimeNs + 1;
41    int64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
42
43    EventMetric metric;
44    metric.set_id(1);
45
46    LogEvent event1(1 /*tag id*/, bucketStartTimeNs + 1);
47    LogEvent event2(1 /*tag id*/, bucketStartTimeNs + 2);
48
49    sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
50
51    EventMetricProducer eventProducer(kConfigKey, metric, -1 /*-1 meaning no condition*/, wizard,
52                                      bucketStartTimeNs);
53
54    eventProducer.onMatchedLogEvent(1 /*matcher index*/, event1);
55    eventProducer.onMatchedLogEvent(1 /*matcher index*/, event2);
56
57    // TODO: get the report and check the content after the ProtoOutputStream change is done.
58    // eventProducer.onDumpReport();
59}
60
61TEST(EventMetricProducerTest, TestEventsWithNonSlicedCondition) {
62    int64_t bucketStartTimeNs = 10000000000;
63    int64_t eventStartTimeNs = bucketStartTimeNs + 1;
64    int64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
65
66    EventMetric metric;
67    metric.set_id(1);
68    metric.set_condition(StringToId("SCREEN_ON"));
69
70    LogEvent event1(1, bucketStartTimeNs + 1);
71    LogEvent event2(1, bucketStartTimeNs + 10);
72
73    sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
74
75    EventMetricProducer eventProducer(kConfigKey, metric, 1, wizard, bucketStartTimeNs);
76
77    eventProducer.onConditionChanged(true /*condition*/, bucketStartTimeNs);
78    eventProducer.onMatchedLogEvent(1 /*matcher index*/, event1);
79
80    eventProducer.onConditionChanged(false /*condition*/, bucketStartTimeNs + 2);
81
82    eventProducer.onMatchedLogEvent(1 /*matcher index*/, event2);
83
84    // TODO: get the report and check the content after the ProtoOutputStream change is done.
85    // eventProducer.onDumpReport();
86}
87
88TEST(EventMetricProducerTest, TestEventsWithSlicedCondition) {
89    int64_t bucketStartTimeNs = 10000000000;
90    int64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
91
92    int tagId = 1;
93    int conditionTagId = 2;
94
95    EventMetric metric;
96    metric.set_id(1);
97    metric.set_condition(StringToId("APP_IN_BACKGROUND_PER_UID_AND_SCREEN_ON"));
98    MetricConditionLink* link = metric.add_links();
99    link->set_condition(StringToId("APP_IN_BACKGROUND_PER_UID"));
100    buildSimpleAtomFieldMatcher(tagId, 1, link->mutable_fields_in_what());
101    buildSimpleAtomFieldMatcher(conditionTagId, 2, link->mutable_fields_in_condition());
102
103    LogEvent event1(tagId, bucketStartTimeNs + 1);
104    EXPECT_TRUE(event1.write("111"));
105    event1.init();
106    ConditionKey key1;
107    key1[StringToId("APP_IN_BACKGROUND_PER_UID")] = {getMockedDimensionKey(conditionTagId, 2, "111")};
108
109    LogEvent event2(tagId, bucketStartTimeNs + 10);
110    EXPECT_TRUE(event2.write("222"));
111    event2.init();
112    ConditionKey key2;
113    key2[StringToId("APP_IN_BACKGROUND_PER_UID")] = {getMockedDimensionKey(conditionTagId, 2, "222")};
114
115    sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
116    EXPECT_CALL(*wizard, query(_, key1, _, _, _, _)).WillOnce(Return(ConditionState::kFalse));
117
118    EXPECT_CALL(*wizard, query(_, key2, _, _, _, _)).WillOnce(Return(ConditionState::kTrue));
119
120    EventMetricProducer eventProducer(kConfigKey, metric, 1, wizard, bucketStartTimeNs);
121
122    eventProducer.onMatchedLogEvent(1 /*matcher index*/, event1);
123    eventProducer.onMatchedLogEvent(1 /*matcher index*/, event2);
124
125    // TODO: get the report and check the content after the ProtoOutputStream change is done.
126    // eventProducer.onDumpReport();
127}
128
129}  // namespace statsd
130}  // namespace os
131}  // namespace android
132#else
133GTEST_LOG_(INFO) << "This test does nothing.\n";
134#endif
135