blob: 74a6f11754f3ea3f224871a1a42a7d13397ee0f5 [file] [log] [blame]
Yao Chen5154a372017-10-30 22:57:06 -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 "metrics_test_helper.h"
Yao Chen5154a372017-10-30 22:57:06 -070016#include "src/condition/ConditionWizard.h"
17#include "src/metrics/duration_helper/OringDurationTracker.h"
18
Yao Chen93fe3a32017-11-02 13:52:59 -070019#include <gmock/gmock.h>
20#include <gtest/gtest.h>
Yao Chen5154a372017-10-30 22:57:06 -070021#include <stdio.h>
22#include <set>
23#include <unordered_map>
24#include <vector>
25
Yao Chen5154a372017-10-30 22:57:06 -070026using namespace testing;
27using android::sp;
28using std::set;
29using std::unordered_map;
30using std::vector;
31
32#ifdef __ANDROID__
Yao Chen93fe3a32017-11-02 13:52:59 -070033namespace android {
34namespace os {
35namespace statsd {
Yao Chen5154a372017-10-30 22:57:06 -070036
37TEST(OringDurationTrackerTest, TestDurationOverlap) {
38 sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
39
40 ConditionKey key1;
41 key1["APP_BACKGROUND"] = "1:maps|";
42
yro2b0f8862017-11-06 14:27:31 -080043 vector<DurationBucket> buckets;
Yao Chen5154a372017-10-30 22:57:06 -070044
45 uint64_t bucketStartTimeNs = 10000000000;
46 uint64_t eventStartTimeNs = bucketStartTimeNs + 1;
47 uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
48 int64_t durationTimeNs = 2 * 1000;
49
50 OringDurationTracker tracker(wizard, 1, bucketStartTimeNs, bucketSizeNs, buckets);
51
52 tracker.noteStart("2:maps", true, eventStartTimeNs, key1);
53 tracker.noteStart("2:maps", true, eventStartTimeNs + 10, key1); // overlapping wl
54
55 tracker.noteStop("2:maps", eventStartTimeNs + durationTimeNs);
56
57 tracker.flushIfNeeded(bucketStartTimeNs + bucketSizeNs + 1);
58 EXPECT_EQ(1u, buckets.size());
yro2b0f8862017-11-06 14:27:31 -080059 EXPECT_EQ(durationTimeNs, buckets[0].mDuration);
Yao Chen5154a372017-10-30 22:57:06 -070060}
61
62TEST(OringDurationTrackerTest, TestDurationConditionChange) {
63 sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
64
65 ConditionKey key1;
66 key1["APP_BACKGROUND"] = "1:maps|";
67
68 EXPECT_CALL(*wizard, query(_, key1)) // #4
69 .WillOnce(Return(ConditionState::kFalse));
70
yro2b0f8862017-11-06 14:27:31 -080071 vector<DurationBucket> buckets;
Yao Chen5154a372017-10-30 22:57:06 -070072
73 uint64_t bucketStartTimeNs = 10000000000;
74 uint64_t eventStartTimeNs = bucketStartTimeNs + 1;
75 uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
76 int64_t durationTimeNs = 2 * 1000;
77
78 OringDurationTracker tracker(wizard, 1, bucketStartTimeNs, bucketSizeNs, buckets);
79
80 tracker.noteStart("2:maps", true, eventStartTimeNs, key1);
81
82 tracker.onSlicedConditionMayChange(eventStartTimeNs + 5);
83
84 tracker.noteStop("2:maps", eventStartTimeNs + durationTimeNs);
85
86 tracker.flushIfNeeded(bucketStartTimeNs + bucketSizeNs + 1);
87 EXPECT_EQ(1u, buckets.size());
yro2b0f8862017-11-06 14:27:31 -080088 EXPECT_EQ(5, buckets[0].mDuration);
Yao Chen5154a372017-10-30 22:57:06 -070089}
Yao Chen93fe3a32017-11-02 13:52:59 -070090} // namespace statsd
91} // namespace os
92} // namespace android
Yao Chen5154a372017-10-30 22:57:06 -070093#else
94GTEST_LOG_(INFO) << "This test does nothing.\n";
95#endif