blob: dc7f118628b76c739dbd61c7520dc00e234ecdd7 [file] [log] [blame]
Julia Reynolds76c096d2017-06-19 08:16:04 -04001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.android.server.notification;
17
18import static com.android.server.notification.AlertRateLimiter.ALLOWED_ALERT_INTERVAL;
19
20import static junit.framework.Assert.assertFalse;
21import static junit.framework.Assert.assertTrue;
22
Julia Reynolds76c096d2017-06-19 08:16:04 -040023import android.test.suitebuilder.annotation.SmallTest;
Jason Monk74f5e362017-12-06 08:56:33 -050024
Brett Chabot84151d92019-02-27 15:37:59 -080025import androidx.test.runner.AndroidJUnit4;
26
Jason Monk74f5e362017-12-06 08:56:33 -050027import com.android.server.UiServiceTestCase;
28
Julia Reynolds76c096d2017-06-19 08:16:04 -040029import org.junit.Before;
30import org.junit.Test;
31import org.junit.runner.RunWith;
32
33@SmallTest
34@RunWith(AndroidJUnit4.class)
Jason Monk74f5e362017-12-06 08:56:33 -050035public class AlertRateLimiterTest extends UiServiceTestCase {
Julia Reynolds76c096d2017-06-19 08:16:04 -040036
37 private long mTestStartTime;
38 private
39 AlertRateLimiter mLimiter;
40
41 @Before
42 public void setUp() {
43 mTestStartTime = 1225731600000L;
44 mLimiter = new AlertRateLimiter();
45 }
46
47 @Test
48 public void testFirstAlertAllowed() throws Exception {
Julia Reynolds3ff26d22017-06-19 08:16:04 -040049 assertFalse(mLimiter.shouldRateLimitAlert(mTestStartTime));
Julia Reynolds76c096d2017-06-19 08:16:04 -040050 }
51
52 @Test
53 public void testAllowedAfterSecond() throws Exception {
Julia Reynolds3ff26d22017-06-19 08:16:04 -040054 assertFalse(mLimiter.shouldRateLimitAlert(mTestStartTime));
55 assertFalse(mLimiter.shouldRateLimitAlert(mTestStartTime + ALLOWED_ALERT_INTERVAL));
Julia Reynolds76c096d2017-06-19 08:16:04 -040056 }
57
58 @Test
59 public void testAllowedAfterSecondEvenWithBlockedEntries() throws Exception {
Julia Reynolds3ff26d22017-06-19 08:16:04 -040060 assertFalse(mLimiter.shouldRateLimitAlert(mTestStartTime));
61 assertTrue(mLimiter.shouldRateLimitAlert(mTestStartTime + ALLOWED_ALERT_INTERVAL - 1));
62 assertFalse(mLimiter.shouldRateLimitAlert(mTestStartTime + ALLOWED_ALERT_INTERVAL));
Julia Reynolds76c096d2017-06-19 08:16:04 -040063 }
64
65 @Test
66 public void testAllowedDisallowedBeforeSecond() throws Exception {
Julia Reynolds3ff26d22017-06-19 08:16:04 -040067 assertFalse(mLimiter.shouldRateLimitAlert(mTestStartTime));
68 assertTrue(mLimiter.shouldRateLimitAlert(mTestStartTime + ALLOWED_ALERT_INTERVAL - 1));
Julia Reynolds76c096d2017-06-19 08:16:04 -040069 }
70
71 @Test
72 public void testDisallowedTimePast() throws Exception {
Julia Reynolds3ff26d22017-06-19 08:16:04 -040073 assertFalse(mLimiter.shouldRateLimitAlert(mTestStartTime));
74 assertTrue(mLimiter.shouldRateLimitAlert(mTestStartTime - ALLOWED_ALERT_INTERVAL));
Julia Reynolds76c096d2017-06-19 08:16:04 -040075 }
76}