blob: 23d31717d99dcaafe669b526be34f18fa41734c4 [file] [log] [blame]
Yao Chen93fe3a32017-11-02 13:52:59 -07001// Copyright (C) 2017 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Yao Chen93fe3a32017-11-02 13:52:59 -070015#include "src/metrics/EventMetricProducer.h"
Yao Chend5aa01b32017-12-19 16:46:36 -080016#include "metrics_test_helper.h"
Yangster-mac94e197c2018-01-02 16:03:03 -080017#include "tests/statsd_test_util.h"
Yao Chen93fe3a32017-11-02 13:52:59 -070018
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
Yangster-mac94e197c2018-01-02 16:03:03 -080036const ConfigKey kConfigKey(0, 12345);
Yao Chenb3561512017-11-21 18:07:17 -080037
Yao Chen93fe3a32017-11-02 13:52:59 -070038TEST(EventMetricProducerTest, TestNoCondition) {
39 uint64_t bucketStartTimeNs = 10000000000;
40 uint64_t eventStartTimeNs = bucketStartTimeNs + 1;
41 uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
42
43 EventMetric metric;
Yangster-mac94e197c2018-01-02 16:03:03 -080044 metric.set_id(1);
Yao Chen93fe3a32017-11-02 13:52:59 -070045
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
Yao Chenb3561512017-11-21 18:07:17 -080051 EventMetricProducer eventProducer(kConfigKey, metric, -1 /*-1 meaning no condition*/, wizard,
Yao Chen93fe3a32017-11-02 13:52:59 -070052 bucketStartTimeNs);
53
Chenjie Yua7259ab2017-12-10 08:31:05 -080054 eventProducer.onMatchedLogEvent(1 /*matcher index*/, event1);
55 eventProducer.onMatchedLogEvent(1 /*matcher index*/, event2);
Yao Chen93fe3a32017-11-02 13:52:59 -070056
57 // TODO: get the report and check the content after the ProtoOutputStream change is done.
58 // eventProducer.onDumpReport();
59}
60
61TEST(EventMetricProducerTest, TestEventsWithNonSlicedCondition) {
62 uint64_t bucketStartTimeNs = 10000000000;
63 uint64_t eventStartTimeNs = bucketStartTimeNs + 1;
64 uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
65
66 EventMetric metric;
Yangster-mac94e197c2018-01-02 16:03:03 -080067 metric.set_id(1);
68 metric.set_condition(StringToId("SCREEN_ON"));
Yao Chen93fe3a32017-11-02 13:52:59 -070069
70 LogEvent event1(1, bucketStartTimeNs + 1);
71 LogEvent event2(1, bucketStartTimeNs + 10);
72
73 sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
74
Yao Chenb3561512017-11-21 18:07:17 -080075 EventMetricProducer eventProducer(kConfigKey, metric, 1, wizard, bucketStartTimeNs);
Yao Chen93fe3a32017-11-02 13:52:59 -070076
77 eventProducer.onConditionChanged(true /*condition*/, bucketStartTimeNs);
Chenjie Yua7259ab2017-12-10 08:31:05 -080078 eventProducer.onMatchedLogEvent(1 /*matcher index*/, event1);
Yao Chen93fe3a32017-11-02 13:52:59 -070079
80 eventProducer.onConditionChanged(false /*condition*/, bucketStartTimeNs + 2);
81
Chenjie Yua7259ab2017-12-10 08:31:05 -080082 eventProducer.onMatchedLogEvent(1 /*matcher index*/, event2);
Yao Chen93fe3a32017-11-02 13:52:59 -070083
84 // TODO: get the report and check the content after the ProtoOutputStream change is done.
85 // eventProducer.onDumpReport();
86}
87
88TEST(EventMetricProducerTest, TestEventsWithSlicedCondition) {
89 uint64_t bucketStartTimeNs = 10000000000;
90 uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
91
Yangster-mac20877162017-12-22 17:19:39 -080092 int tagId = 1;
93 int conditionTagId = 2;
94
Yao Chen93fe3a32017-11-02 13:52:59 -070095 EventMetric metric;
Yangster-mac94e197c2018-01-02 16:03:03 -080096 metric.set_id(1);
97 metric.set_condition(StringToId("APP_IN_BACKGROUND_PER_UID_AND_SCREEN_ON"));
Stefan Lafona5b51912017-12-05 21:43:52 -080098 MetricConditionLink* link = metric.add_links();
Yangster-mac94e197c2018-01-02 16:03:03 -080099 link->set_condition(StringToId("APP_IN_BACKGROUND_PER_UID"));
Yangster-mac7ba8fc32018-01-24 16:16:46 -0800100 buildSimpleAtomFieldMatcher(tagId, 1, link->mutable_fields_in_what());
101 buildSimpleAtomFieldMatcher(conditionTagId, 2, link->mutable_fields_in_condition());
Yao Chen93fe3a32017-11-02 13:52:59 -0700102
Yangster-mac20877162017-12-22 17:19:39 -0800103 LogEvent event1(tagId, bucketStartTimeNs + 1);
104 EXPECT_TRUE(event1.write("111"));
Yao Chen93fe3a32017-11-02 13:52:59 -0700105 event1.init();
106 ConditionKey key1;
Yangster-mac94e197c2018-01-02 16:03:03 -0800107 key1[StringToId("APP_IN_BACKGROUND_PER_UID")] = {getMockedDimensionKey(conditionTagId, 2, "111")};
Yao Chen93fe3a32017-11-02 13:52:59 -0700108
Yangster-mac20877162017-12-22 17:19:39 -0800109 LogEvent event2(tagId, bucketStartTimeNs + 10);
110 EXPECT_TRUE(event2.write("222"));
Yao Chen93fe3a32017-11-02 13:52:59 -0700111 event2.init();
112 ConditionKey key2;
Yangster-mac94e197c2018-01-02 16:03:03 -0800113 key2[StringToId("APP_IN_BACKGROUND_PER_UID")] = {getMockedDimensionKey(conditionTagId, 2, "222")};
Yao Chen93fe3a32017-11-02 13:52:59 -0700114
115 sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
Yangster13fb7e42018-03-07 17:30:49 -0800116 EXPECT_CALL(*wizard, query(_, key1, _, _, _, _)).WillOnce(Return(ConditionState::kFalse));
Yao Chen93fe3a32017-11-02 13:52:59 -0700117
Yangster13fb7e42018-03-07 17:30:49 -0800118 EXPECT_CALL(*wizard, query(_, key2, _, _, _, _)).WillOnce(Return(ConditionState::kTrue));
Yao Chen93fe3a32017-11-02 13:52:59 -0700119
Yao Chenb3561512017-11-21 18:07:17 -0800120 EventMetricProducer eventProducer(kConfigKey, metric, 1, wizard, bucketStartTimeNs);
Yao Chen93fe3a32017-11-02 13:52:59 -0700121
Chenjie Yua7259ab2017-12-10 08:31:05 -0800122 eventProducer.onMatchedLogEvent(1 /*matcher index*/, event1);
123 eventProducer.onMatchedLogEvent(1 /*matcher index*/, event2);
Yao Chen93fe3a32017-11-02 13:52:59 -0700124
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