1 /*
2  * Copyright (C) 2017 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.providers.calendar;
17 
18 import static org.junit.Assert.assertFalse;
19 import static org.junit.Assert.assertTrue;
20 
21 import android.content.Context;
22 import android.test.suitebuilder.annotation.SmallTest;
23 import android.text.format.DateUtils;
24 
25 import androidx.test.InstrumentationRegistry;
26 import androidx.test.runner.AndroidJUnit4;
27 
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 
32 @RunWith(AndroidJUnit4.class)
33 @SmallTest
34 public class CalendarSanityCheckerTest {
35     private class CalendarSanityCheckerTestable extends CalendarSanityChecker {
CalendarSanityCheckerTestable(Context context)36         protected CalendarSanityCheckerTestable(Context context) {
37             super(context);
38         }
39 
40         @Override
getRealtimeMillis()41         protected long getRealtimeMillis() {
42             return mInjectedRealtimeMillis;
43         }
44 
45         @Override
getBootCount()46         protected long getBootCount() {
47             return mInjectedBootCount;
48         }
49 
50         @Override
getUserUnlockTime()51         protected long getUserUnlockTime() {
52             return mInjectedUnlockTime;
53         }
54     }
55 
56     private Context mContext;
57     private CalendarSanityCheckerTestable mSanityChecker;
58 
59     private long mInjectedRealtimeMillis = 1000000L;
60     private long mInjectedBootCount = 1000;
61     private long mInjectedUnlockTime = 0;
62 
63     @Before
setUp()64     public void setUp() {
65         mContext = InstrumentationRegistry.getContext();
66         mSanityChecker = new CalendarSanityCheckerTestable(mContext);
67         mSanityChecker.mPrefs.edit().clear().commit();
68     }
69 
70     @Test
testWithoutLastCheckTime()71     public void testWithoutLastCheckTime() {
72         assertTrue(mSanityChecker.checkLastCheckTime());
73 
74         // Unlock.
75         mInjectedUnlockTime = mInjectedRealtimeMillis;
76 
77         mInjectedRealtimeMillis += 15 * 60 * 1000;
78         assertTrue(mSanityChecker.checkLastCheckTime());
79 
80         mInjectedRealtimeMillis += 1;
81         assertFalse(mSanityChecker.checkLastCheckTime());
82     }
83 
84     @Test
testWithLastCheckTime()85     public void testWithLastCheckTime() {
86         assertTrue(mSanityChecker.checkLastCheckTime());
87 
88         mInjectedUnlockTime = mInjectedRealtimeMillis;
89 
90         // Update the last check time.
91         mInjectedRealtimeMillis += 1 * 60 * 1000;
92         mSanityChecker.updateLastCheckTime();
93 
94         // Right after, okay.
95         assertTrue(mSanityChecker.checkLastCheckTime());
96 
97         // Still okay.
98         mInjectedRealtimeMillis += DateUtils.DAY_IN_MILLIS - (15 * DateUtils.MINUTE_IN_MILLIS);
99         assertTrue(mSanityChecker.checkLastCheckTime());
100 
101         mInjectedRealtimeMillis += 1;
102         assertFalse(mSanityChecker.checkLastCheckTime());
103 
104         // Repeat the same thing.
105         mInjectedRealtimeMillis += 1 * 60 * 1000;
106         mSanityChecker.updateLastCheckTime();
107 
108         // Right after, okay.
109         assertTrue(mSanityChecker.checkLastCheckTime());
110 
111         // Still okay.
112         mInjectedRealtimeMillis += DateUtils.DAY_IN_MILLIS - (15 * DateUtils.MINUTE_IN_MILLIS);
113         assertTrue(mSanityChecker.checkLastCheckTime());
114 
115         mInjectedRealtimeMillis += 1;
116         assertFalse(mSanityChecker.checkLastCheckTime());
117 
118         // Check again right after. This should pass because of WTF_INTERVAL_MS.
119         assertTrue(mSanityChecker.checkLastCheckTime());
120 
121         mInjectedRealtimeMillis += 60 * 60 * 1000;
122 
123         // Still okay.
124         assertTrue(mSanityChecker.checkLastCheckTime());
125 
126         // Now WTF again.
127         mInjectedRealtimeMillis += 1;
128         assertFalse(mSanityChecker.checkLastCheckTime());
129 
130         // Reboot.
131         mInjectedRealtimeMillis = 1000000L;
132         mInjectedBootCount++;
133 
134         // Unlock.
135         mInjectedUnlockTime = mInjectedRealtimeMillis;
136 
137         mInjectedRealtimeMillis += 15 * 60 * 1000;
138         assertTrue(mSanityChecker.checkLastCheckTime());
139 
140         mInjectedRealtimeMillis += 1;
141         assertFalse(mSanityChecker.checkLastCheckTime());
142 
143         // Check again right after. This should pass because of WTF_INTERVAL_MS.
144         assertTrue(mSanityChecker.checkLastCheckTime());
145     }
146 }
147