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