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.test.suitebuilder.annotation.SmallTest;
24 
25 import androidx.test.runner.AndroidJUnit4;
26 
27 import com.android.server.UiServiceTestCase;
28 
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 
33 @SmallTest
34 @RunWith(AndroidJUnit4.class)
35 public class AlertRateLimiterTest extends UiServiceTestCase {
36 
37     private long mTestStartTime;
38     private
39     AlertRateLimiter mLimiter;
40 
41     @Before
setUp()42     public void setUp() {
43         mTestStartTime = 1225731600000L;
44         mLimiter = new AlertRateLimiter();
45     }
46 
47     @Test
testFirstAlertAllowed()48     public void testFirstAlertAllowed() throws Exception {
49         assertFalse(mLimiter.shouldRateLimitAlert(mTestStartTime));
50     }
51 
52     @Test
testAllowedAfterSecond()53     public void testAllowedAfterSecond() throws Exception {
54         assertFalse(mLimiter.shouldRateLimitAlert(mTestStartTime));
55         assertFalse(mLimiter.shouldRateLimitAlert(mTestStartTime + ALLOWED_ALERT_INTERVAL));
56     }
57 
58     @Test
testAllowedAfterSecondEvenWithBlockedEntries()59     public void testAllowedAfterSecondEvenWithBlockedEntries() throws Exception {
60         assertFalse(mLimiter.shouldRateLimitAlert(mTestStartTime));
61         assertTrue(mLimiter.shouldRateLimitAlert(mTestStartTime + ALLOWED_ALERT_INTERVAL - 1));
62         assertFalse(mLimiter.shouldRateLimitAlert(mTestStartTime + ALLOWED_ALERT_INTERVAL));
63     }
64 
65     @Test
testAllowedDisallowedBeforeSecond()66     public void testAllowedDisallowedBeforeSecond() throws Exception {
67         assertFalse(mLimiter.shouldRateLimitAlert(mTestStartTime));
68         assertTrue(mLimiter.shouldRateLimitAlert(mTestStartTime + ALLOWED_ALERT_INTERVAL - 1));
69     }
70 
71     @Test
testDisallowedTimePast()72     public void testDisallowedTimePast() throws Exception {
73         assertFalse(mLimiter.shouldRateLimitAlert(mTestStartTime));
74         assertTrue(mLimiter.shouldRateLimitAlert(mTestStartTime - ALLOWED_ALERT_INTERVAL));
75     }
76 }
77