OSDN Git Service

Rate limit notification sounds/vibrations
[android-x86/frameworks-base.git] / services / tests / notification / src / com / android / server / notification / AlertRateLimiterTest.java
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;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28
29 @SmallTest
30 @RunWith(AndroidJUnit4.class)
31 public class AlertRateLimiterTest extends NotificationTestCase {
32
33     private long mTestStartTime;
34     private
35     AlertRateLimiter mLimiter;
36
37     @Before
38     public void setUp() {
39         mTestStartTime = 1225731600000L;
40         mLimiter = new AlertRateLimiter();
41     }
42
43     @Test
44     public void testFirstAlertAllowed() throws Exception {
45         assertFalse(mLimiter.shouldRateLimitAlert(mTestStartTime));
46     }
47
48     @Test
49     public void testAllowedAfterSecond() throws Exception {
50         assertFalse(mLimiter.shouldRateLimitAlert(mTestStartTime));
51         assertFalse(mLimiter.shouldRateLimitAlert(mTestStartTime + ALLOWED_ALERT_INTERVAL));
52     }
53
54     @Test
55     public void testAllowedAfterSecondEvenWithBlockedEntries() throws Exception {
56         assertFalse(mLimiter.shouldRateLimitAlert(mTestStartTime));
57         assertTrue(mLimiter.shouldRateLimitAlert(mTestStartTime + ALLOWED_ALERT_INTERVAL - 1));
58         assertFalse(mLimiter.shouldRateLimitAlert(mTestStartTime + ALLOWED_ALERT_INTERVAL));
59     }
60
61     @Test
62     public void testAllowedDisallowedBeforeSecond() throws Exception {
63         assertFalse(mLimiter.shouldRateLimitAlert(mTestStartTime));
64         assertTrue(mLimiter.shouldRateLimitAlert(mTestStartTime + ALLOWED_ALERT_INTERVAL - 1));
65     }
66
67     @Test
68     public void testDisallowedTimePast() throws Exception {
69         assertFalse(mLimiter.shouldRateLimitAlert(mTestStartTime));
70         assertTrue(mLimiter.shouldRateLimitAlert(mTestStartTime - ALLOWED_ALERT_INTERVAL));
71     }
72 }