Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 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 | |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 15 | #include "src/metrics/EventMetricProducer.h" |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 16 | #include "src/dimension.h" |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 17 | #include "metrics_test_helper.h" |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame^] | 18 | #include "tests/statsd_test_util.h" |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 19 | |
| 20 | #include <gmock/gmock.h> |
| 21 | #include <gtest/gtest.h> |
| 22 | #include <stdio.h> |
| 23 | #include <vector> |
| 24 | |
| 25 | using namespace testing; |
| 26 | using android::sp; |
| 27 | using std::set; |
| 28 | using std::unordered_map; |
| 29 | using std::vector; |
| 30 | |
| 31 | #ifdef __ANDROID__ |
| 32 | |
| 33 | namespace android { |
| 34 | namespace os { |
| 35 | namespace statsd { |
| 36 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame^] | 37 | const ConfigKey kConfigKey(0, 12345); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 38 | |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 39 | TEST(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-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame^] | 45 | metric.set_id(1); |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 46 | |
| 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 Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 52 | EventMetricProducer eventProducer(kConfigKey, metric, -1 /*-1 meaning no condition*/, wizard, |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 53 | bucketStartTimeNs); |
| 54 | |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 55 | eventProducer.onMatchedLogEvent(1 /*matcher index*/, event1); |
| 56 | eventProducer.onMatchedLogEvent(1 /*matcher index*/, event2); |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 57 | |
| 58 | // TODO: get the report and check the content after the ProtoOutputStream change is done. |
| 59 | // eventProducer.onDumpReport(); |
| 60 | } |
| 61 | |
| 62 | TEST(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-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame^] | 68 | metric.set_id(1); |
| 69 | metric.set_condition(StringToId("SCREEN_ON")); |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 70 | |
| 71 | LogEvent event1(1, bucketStartTimeNs + 1); |
| 72 | LogEvent event2(1, bucketStartTimeNs + 10); |
| 73 | |
| 74 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
| 75 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 76 | EventMetricProducer eventProducer(kConfigKey, metric, 1, wizard, bucketStartTimeNs); |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 77 | |
| 78 | eventProducer.onConditionChanged(true /*condition*/, bucketStartTimeNs); |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 79 | eventProducer.onMatchedLogEvent(1 /*matcher index*/, event1); |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 80 | |
| 81 | eventProducer.onConditionChanged(false /*condition*/, bucketStartTimeNs + 2); |
| 82 | |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 83 | eventProducer.onMatchedLogEvent(1 /*matcher index*/, event2); |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 84 | |
| 85 | // TODO: get the report and check the content after the ProtoOutputStream change is done. |
| 86 | // eventProducer.onDumpReport(); |
| 87 | } |
| 88 | |
| 89 | TEST(EventMetricProducerTest, TestEventsWithSlicedCondition) { |
| 90 | uint64_t bucketStartTimeNs = 10000000000; |
| 91 | uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL; |
| 92 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 93 | int tagId = 1; |
| 94 | int conditionTagId = 2; |
| 95 | |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 96 | EventMetric metric; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame^] | 97 | metric.set_id(1); |
| 98 | metric.set_condition(StringToId("APP_IN_BACKGROUND_PER_UID_AND_SCREEN_ON")); |
Stefan Lafon | a5b5191 | 2017-12-05 21:43:52 -0800 | [diff] [blame] | 99 | MetricConditionLink* link = metric.add_links(); |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame^] | 100 | link->set_condition(StringToId("APP_IN_BACKGROUND_PER_UID")); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 101 | *link->mutable_dimensions_in_what() = buildSimpleAtomFieldMatcher(tagId, 1); |
| 102 | *link->mutable_dimensions_in_condition() = buildSimpleAtomFieldMatcher(conditionTagId, 2); |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 103 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 104 | LogEvent event1(tagId, bucketStartTimeNs + 1); |
| 105 | EXPECT_TRUE(event1.write("111")); |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 106 | event1.init(); |
| 107 | ConditionKey key1; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame^] | 108 | key1[StringToId("APP_IN_BACKGROUND_PER_UID")] = {getMockedDimensionKey(conditionTagId, 2, "111")}; |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 109 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 110 | LogEvent event2(tagId, bucketStartTimeNs + 10); |
| 111 | EXPECT_TRUE(event2.write("222")); |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 112 | event2.init(); |
| 113 | ConditionKey key2; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame^] | 114 | key2[StringToId("APP_IN_BACKGROUND_PER_UID")] = {getMockedDimensionKey(conditionTagId, 2, "222")}; |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 115 | |
| 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 Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 121 | EventMetricProducer eventProducer(kConfigKey, metric, 1, wizard, bucketStartTimeNs); |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 122 | |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 123 | eventProducer.onMatchedLogEvent(1 /*matcher index*/, event1); |
| 124 | eventProducer.onMatchedLogEvent(1 /*matcher index*/, event2); |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 125 | |
| 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 |
| 134 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 135 | #endif |