blob: d4c41e0285bdcb42a4295b284f3af9de1eb38c82 [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
23import android.support.test.runner.AndroidJUnit4;
24import android.test.suitebuilder.annotation.SmallTest;
Jason Monk74f5e362017-12-06 08:56:33 -050025
26import com.android.server.UiServiceTestCase;
27
Julia Reynolds76c096d2017-06-19 08:16:04 -040028import org.junit.Before;
29import org.junit.Test;
30import org.junit.runner.RunWith;
31
32@SmallTest
33@RunWith(AndroidJUnit4.class)
Jason Monk74f5e362017-12-06 08:56:33 -050034public class AlertRateLimiterTest extends UiServiceTestCase {
Julia Reynolds76c096d2017-06-19 08:16:04 -040035
36 private long mTestStartTime;
37 private
38 AlertRateLimiter mLimiter;
39
40 @Before
41 public void setUp() {
42 mTestStartTime = 1225731600000L;
43 mLimiter = new AlertRateLimiter();
44 }
45
46 @Test
47 public void testFirstAlertAllowed() throws Exception {
Julia Reynolds3ff26d22017-06-19 08:16:04 -040048 assertFalse(mLimiter.shouldRateLimitAlert(mTestStartTime));
Julia Reynolds76c096d2017-06-19 08:16:04 -040049 }
50
51 @Test
52 public void testAllowedAfterSecond() throws Exception {
Julia Reynolds3ff26d22017-06-19 08:16:04 -040053 assertFalse(mLimiter.shouldRateLimitAlert(mTestStartTime));
54 assertFalse(mLimiter.shouldRateLimitAlert(mTestStartTime + ALLOWED_ALERT_INTERVAL));
Julia Reynolds76c096d2017-06-19 08:16:04 -040055 }
56
57 @Test
58 public void testAllowedAfterSecondEvenWithBlockedEntries() throws Exception {
Julia Reynolds3ff26d22017-06-19 08:16:04 -040059 assertFalse(mLimiter.shouldRateLimitAlert(mTestStartTime));
60 assertTrue(mLimiter.shouldRateLimitAlert(mTestStartTime + ALLOWED_ALERT_INTERVAL - 1));
61 assertFalse(mLimiter.shouldRateLimitAlert(mTestStartTime + ALLOWED_ALERT_INTERVAL));
Julia Reynolds76c096d2017-06-19 08:16:04 -040062 }
63
64 @Test
65 public void testAllowedDisallowedBeforeSecond() throws Exception {
Julia Reynolds3ff26d22017-06-19 08:16:04 -040066 assertFalse(mLimiter.shouldRateLimitAlert(mTestStartTime));
67 assertTrue(mLimiter.shouldRateLimitAlert(mTestStartTime + ALLOWED_ALERT_INTERVAL - 1));
Julia Reynolds76c096d2017-06-19 08:16:04 -040068 }
69
70 @Test
71 public void testDisallowedTimePast() throws Exception {
Julia Reynolds3ff26d22017-06-19 08:16:04 -040072 assertFalse(mLimiter.shouldRateLimitAlert(mTestStartTime));
73 assertTrue(mLimiter.shouldRateLimitAlert(mTestStartTime - ALLOWED_ALERT_INTERVAL));
Julia Reynolds76c096d2017-06-19 08:16:04 -040074 }
75}