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