blob: 7171de939c62a24662dd47aa9244120422e7e314 [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"
Yangster-mac20877162017-12-22 17:19:39 -080016#include "src/dimension.h"
Yao Chend5aa01b32017-12-19 16:46:36 -080017#include "metrics_test_helper.h"
Yangster-mac94e197c2018-01-02 16:03:03 -080018#include "tests/statsd_test_util.h"
Yao Chen93fe3a32017-11-02 13:52:59 -070019
20#include <gmock/gmock.h>
21#include <gtest/gtest.h>
22#include <stdio.h>
23#include <vector>
24
25using namespace testing;
26using android::sp;
27using std::set;
28using std::unordered_map;
29using std::vector;
30
31#ifdef __ANDROID__
32
33namespace android {
34namespace os {
35namespace statsd {
36
Yangster-mac94e197c2018-01-02 16:03:03 -080037const ConfigKey kConfigKey(0, 12345);
Yao Chenb3561512017-11-21 18:07:17 -080038
Yao Chen93fe3a32017-11-02 13:52:59 -070039TEST(EventMetricProducerTest, TestNoCondition) {
40 uint64_t bucketStartTimeNs = 10000000000;
41 uint64_t eventStartTimeNs = bucketStartTimeNs + 1;
42 uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
43
44 EventMetric metric;
Yangster-mac94e197c2018-01-02 16:03:03 -080045 metric.set_id(1);
Yao Chen93fe3a32017-11-02 13:52:59 -070046
47 LogEvent event1(1 /*tag id*/, bucketStartTimeNs + 1);
48 LogEvent event2(1 /*tag id*/, bucketStartTimeNs + 2);
49
50 sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
51
Yao Chenb3561512017-11-21 18:07:17 -080052 EventMetricProducer eventProducer(kConfigKey, metric, -1 /*-1 meaning no condition*/, wizard,
Yao Chen93fe3a32017-11-02 13:52:59 -070053 bucketStartTimeNs);
54
Chenjie Yua7259ab2017-12-10 08:31:05 -080055 eventProducer.onMatchedLogEvent(1 /*matcher index*/, event1);
56 eventProducer.onMatchedLogEvent(1 /*matcher index*/, event2);
Yao Chen93fe3a32017-11-02 13:52:59 -070057
58 // TODO: get the report and check the content after the ProtoOutputStream change is done.
59 // eventProducer.onDumpReport();
60}
61
62TEST(EventMetricProducerTest, TestEventsWithNonSlicedCondition) {
63 uint64_t bucketStartTimeNs = 10000000000;
64 uint64_t eventStartTimeNs = bucketStartTimeNs + 1;
65 uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
66
67 EventMetric metric;
Yangster-mac94e197c2018-01-02 16:03:03 -080068 metric.set_id(1);
69 metric.set_condition(StringToId("SCREEN_ON"));
Yao Chen93fe3a32017-11-02 13:52:59 -070070
71 LogEvent event1(1, bucketStartTimeNs + 1);
72 LogEvent event2(1, bucketStartTimeNs + 10);
73
74 sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
75
Yao Chenb3561512017-11-21 18:07:17 -080076 EventMetricProducer eventProducer(kConfigKey, metric, 1, wizard, bucketStartTimeNs);
Yao Chen93fe3a32017-11-02 13:52:59 -070077
78 eventProducer.onConditionChanged(true /*condition*/, bucketStartTimeNs);
Chenjie Yua7259ab2017-12-10 08:31:05 -080079 eventProducer.onMatchedLogEvent(1 /*matcher index*/, event1);
Yao Chen93fe3a32017-11-02 13:52:59 -070080
81 eventProducer.onConditionChanged(false /*condition*/, bucketStartTimeNs + 2);
82
Chenjie Yua7259ab2017-12-10 08:31:05 -080083 eventProducer.onMatchedLogEvent(1 /*matcher index*/, event2);
Yao Chen93fe3a32017-11-02 13:52:59 -070084
85 // TODO: get the report and check the content after the ProtoOutputStream change is done.
86 // eventProducer.onDumpReport();
87}
88
89TEST(EventMetricProducerTest, TestEventsWithSlicedCondition) {
90 uint64_t bucketStartTimeNs = 10000000000;
91 uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
92
Yangster-mac20877162017-12-22 17:19:39 -080093 int tagId = 1;
94 int conditionTagId = 2;
95
Yao Chen93fe3a32017-11-02 13:52:59 -070096 EventMetric metric;
Yangster-mac94e197c2018-01-02 16:03:03 -080097 metric.set_id(1);
98 metric.set_condition(StringToId("APP_IN_BACKGROUND_PER_UID_AND_SCREEN_ON"));
Stefan Lafona5b51912017-12-05 21:43:52 -080099 MetricConditionLink* link = metric.add_links();
Yangster-mac94e197c2018-01-02 16:03:03 -0800100 link->set_condition(StringToId("APP_IN_BACKGROUND_PER_UID"));
Yangster-mac20877162017-12-22 17:19:39 -0800101 *link->mutable_dimensions_in_what() = buildSimpleAtomFieldMatcher(tagId, 1);
102 *link->mutable_dimensions_in_condition() = buildSimpleAtomFieldMatcher(conditionTagId, 2);
Yao Chen93fe3a32017-11-02 13:52:59 -0700103
Yangster-mac20877162017-12-22 17:19:39 -0800104 LogEvent event1(tagId, bucketStartTimeNs + 1);
105 EXPECT_TRUE(event1.write("111"));
Yao Chen93fe3a32017-11-02 13:52:59 -0700106 event1.init();
107 ConditionKey key1;
Yangster-mac94e197c2018-01-02 16:03:03 -0800108 key1[StringToId("APP_IN_BACKGROUND_PER_UID")] = {getMockedDimensionKey(conditionTagId, 2, "111")};
Yao Chen93fe3a32017-11-02 13:52:59 -0700109
Yangster-mac20877162017-12-22 17:19:39 -0800110 LogEvent event2(tagId, bucketStartTimeNs + 10);
111 EXPECT_TRUE(event2.write("222"));
Yao Chen93fe3a32017-11-02 13:52:59 -0700112 event2.init();
113 ConditionKey key2;
Yangster-mac94e197c2018-01-02 16:03:03 -0800114 key2[StringToId("APP_IN_BACKGROUND_PER_UID")] = {getMockedDimensionKey(conditionTagId, 2, "222")};
Yao Chen93fe3a32017-11-02 13:52:59 -0700115
116 sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
117 EXPECT_CALL(*wizard, query(_, key1)).WillOnce(Return(ConditionState::kFalse));
118
119 EXPECT_CALL(*wizard, query(_, key2)).WillOnce(Return(ConditionState::kTrue));
120
Yao Chenb3561512017-11-21 18:07:17 -0800121 EventMetricProducer eventProducer(kConfigKey, metric, 1, wizard, bucketStartTimeNs);
Yao Chen93fe3a32017-11-02 13:52:59 -0700122
Chenjie Yua7259ab2017-12-10 08:31:05 -0800123 eventProducer.onMatchedLogEvent(1 /*matcher index*/, event1);
124 eventProducer.onMatchedLogEvent(1 /*matcher index*/, event2);
Yao Chen93fe3a32017-11-02 13:52:59 -0700125
126 // TODO: get the report and check the content after the ProtoOutputStream change is done.
127 // eventProducer.onDumpReport();
128}
129
130} // namespace statsd
131} // namespace os
132} // namespace android
133#else
134GTEST_LOG_(INFO) << "This test does nothing.\n";
135#endif