• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 CalendarConfidenceCheckerTest {
35     private class CalendarConfidenceCheckerTestable extends CalendarConfidenceChecker {
CalendarConfidenceCheckerTestable(Context context)36         protected CalendarConfidenceCheckerTestable(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 CalendarConfidenceCheckerTestable mConfidenceChecker;
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         mConfidenceChecker = new CalendarConfidenceCheckerTestable(mContext);
67         mConfidenceChecker.mPrefs.edit().clear().commit();
68     }
69 
70     @Test
testWithoutLastCheckTime()71     public void testWithoutLastCheckTime() {
72         assertTrue(mConfidenceChecker.checkLastCheckTime());
73 
74         // Unlock.
75         mInjectedUnlockTime = mInjectedRealtimeMillis;
76 
77         mInjectedRealtimeMillis += 15 * 60 * 1000;
78         assertTrue(mConfidenceChecker.checkLastCheckTime());
79 
80         mInjectedRealtimeMillis += 1;
81         assertFalse(mConfidenceChecker.checkLastCheckTime());
82     }
83 
84     @Test
testWithLastCheckTime()85     public void testWithLastCheckTime() {
86         assertTrue(mConfidenceChecker.checkLastCheckTime());
87 
88         mInjectedUnlockTime = mInjectedRealtimeMillis;
89 
90         // Update the last check time.
91         mInjectedRealtimeMillis += 1 * 60 * 1000;
92         mConfidenceChecker.updateLastCheckTime();
93 
94         // Right after, okay.
95         assertTrue(mConfidenceChecker.checkLastCheckTime());
96 
97         // Still okay.
98         mInjectedRealtimeMillis += DateUtils.DAY_IN_MILLIS - (15 * DateUtils.MINUTE_IN_MILLIS);
99         assertTrue(mConfidenceChecker.checkLastCheckTime());
100 
101         mInjectedRealtimeMillis += 1;
102         assertFalse(mConfidenceChecker.checkLastCheckTime());
103 
104         // Repeat the same thing.
105         mInjectedRealtimeMillis += 1 * 60 * 1000;
106         mConfidenceChecker.updateLastCheckTime();
107 
108         // Right after, okay.
109         assertTrue(mConfidenceChecker.checkLastCheckTime());
110 
111         // Still okay.
112         mInjectedRealtimeMillis += DateUtils.DAY_IN_MILLIS - (15 * DateUtils.MINUTE_IN_MILLIS);
113         assertTrue(mConfidenceChecker.checkLastCheckTime());
114 
115         mInjectedRealtimeMillis += 1;
116         assertFalse(mConfidenceChecker.checkLastCheckTime());
117 
118         // Check again right after. This should pass because of WTF_INTERVAL_MS.
119         assertTrue(mConfidenceChecker.checkLastCheckTime());
120 
121         mInjectedRealtimeMillis += 60 * 60 * 1000;
122 
123         // Still okay.
124         assertTrue(mConfidenceChecker.checkLastCheckTime());
125 
126         // Now WTF again.
127         mInjectedRealtimeMillis += 1;
128         assertFalse(mConfidenceChecker.checkLastCheckTime());
129 
130         // Reboot.
131         mInjectedRealtimeMillis = 1000000L;
132         mInjectedBootCount++;
133 
134         // Unlock.
135         mInjectedUnlockTime = mInjectedRealtimeMillis;
136 
137         mInjectedRealtimeMillis += 15 * 60 * 1000;
138         assertTrue(mConfidenceChecker.checkLastCheckTime());
139 
140         mInjectedRealtimeMillis += 1;
141         assertFalse(mConfidenceChecker.checkLastCheckTime());
142 
143         // Check again right after. This should pass because of WTF_INTERVAL_MS.
144         assertTrue(mConfidenceChecker.checkLastCheckTime());
145     }
146 }
147