Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package com.android.server.notification; |
| 17 | |
| 18 | import static com.android.server.notification.AlertRateLimiter.ALLOWED_ALERT_INTERVAL; |
| 19 | |
| 20 | import static junit.framework.Assert.assertFalse; |
| 21 | import static junit.framework.Assert.assertTrue; |
| 22 | |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 23 | import android.test.suitebuilder.annotation.SmallTest; |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 24 | |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 25 | import androidx.test.runner.AndroidJUnit4; |
| 26 | |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 27 | import com.android.server.UiServiceTestCase; |
| 28 | |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 29 | import org.junit.Before; |
| 30 | import org.junit.Test; |
| 31 | import org.junit.runner.RunWith; |
| 32 | |
| 33 | @SmallTest |
| 34 | @RunWith(AndroidJUnit4.class) |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 35 | public class AlertRateLimiterTest extends UiServiceTestCase { |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 36 | |
| 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 Reynolds | 3ff26d2 | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 49 | assertFalse(mLimiter.shouldRateLimitAlert(mTestStartTime)); |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | @Test |
| 53 | public void testAllowedAfterSecond() throws Exception { |
Julia Reynolds | 3ff26d2 | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 54 | assertFalse(mLimiter.shouldRateLimitAlert(mTestStartTime)); |
| 55 | assertFalse(mLimiter.shouldRateLimitAlert(mTestStartTime + ALLOWED_ALERT_INTERVAL)); |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | @Test |
| 59 | public void testAllowedAfterSecondEvenWithBlockedEntries() throws Exception { |
Julia Reynolds | 3ff26d2 | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 60 | assertFalse(mLimiter.shouldRateLimitAlert(mTestStartTime)); |
| 61 | assertTrue(mLimiter.shouldRateLimitAlert(mTestStartTime + ALLOWED_ALERT_INTERVAL - 1)); |
| 62 | assertFalse(mLimiter.shouldRateLimitAlert(mTestStartTime + ALLOWED_ALERT_INTERVAL)); |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | @Test |
| 66 | public void testAllowedDisallowedBeforeSecond() throws Exception { |
Julia Reynolds | 3ff26d2 | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 67 | assertFalse(mLimiter.shouldRateLimitAlert(mTestStartTime)); |
| 68 | assertTrue(mLimiter.shouldRateLimitAlert(mTestStartTime + ALLOWED_ALERT_INTERVAL - 1)); |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | @Test |
| 72 | public void testDisallowedTimePast() throws Exception { |
Julia Reynolds | 3ff26d2 | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 73 | assertFalse(mLimiter.shouldRateLimitAlert(mTestStartTime)); |
| 74 | assertTrue(mLimiter.shouldRateLimitAlert(mTestStartTime - ALLOWED_ALERT_INTERVAL)); |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 75 | } |
| 76 | } |