1 /* 2 * Copyright (C) 2015 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 17 package com.android.calllogbackup; 18 19 import static org.mockito.Mockito.never; 20 import static org.mockito.Mockito.verify; 21 import static org.mockito.Mockito.when; 22 import static org.mockito.Mockito.eq; 23 24 import android.app.backup.BackupDataOutput; 25 import android.test.AndroidTestCase; 26 import android.test.suitebuilder.annotation.SmallTest; 27 28 import com.android.calllogbackup.CallLogBackupAgent.Call; 29 import com.android.calllogbackup.CallLogBackupAgent.CallLogBackupState; 30 31 import org.mockito.InOrder; 32 import org.mockito.Matchers; 33 import org.mockito.Mock; 34 import org.mockito.Mockito; 35 import org.mockito.MockitoAnnotations; 36 37 import java.io.DataInput; 38 import java.io.DataOutput; 39 import java.io.EOFException; 40 import java.util.LinkedList; 41 import java.util.List; 42 import java.util.TreeSet; 43 44 /** 45 * Test cases for {@link com.android.providers.contacts.CallLogBackupAgent} 46 */ 47 @SmallTest 48 public class CallLogBackupAgentTest extends AndroidTestCase { 49 50 @Mock DataInput mDataInput; 51 @Mock DataOutput mDataOutput; 52 @Mock BackupDataOutput mBackupDataOutput; 53 54 CallLogBackupAgent mCallLogBackupAgent; 55 56 MockitoHelper mMockitoHelper = new MockitoHelper(); 57 58 @Override setUp()59 public void setUp() throws Exception { 60 super.setUp(); 61 62 mMockitoHelper.setUp(getClass()); 63 // Since we're testing a system app, AppDataDirGuesser doesn't find our 64 // cache dir, so set it explicitly. 65 System.setProperty("dexmaker.dexcache", getContext().getCacheDir().toString()); 66 67 MockitoAnnotations.initMocks(this); 68 69 mCallLogBackupAgent = new CallLogBackupAgent(); 70 } 71 72 @Override tearDown()73 public void tearDown() throws Exception { 74 mMockitoHelper.tearDown(); 75 } 76 testReadState_NoCall()77 public void testReadState_NoCall() throws Exception { 78 when(mDataInput.readInt()).thenThrow(new EOFException()); 79 80 CallLogBackupState state = mCallLogBackupAgent.readState(mDataInput); 81 82 assertEquals(state.version, CallLogBackupAgent.VERSION_NO_PREVIOUS_STATE); 83 assertEquals(state.callIds.size(), 0); 84 } 85 testReadState_OneCall()86 public void testReadState_OneCall() throws Exception { 87 when(mDataInput.readInt()).thenReturn( 88 1 /* version */, 89 1 /* size */, 90 101 /* call-ID */ ); 91 92 CallLogBackupState state = mCallLogBackupAgent.readState(mDataInput); 93 94 assertEquals(1, state.version); 95 assertEquals(1, state.callIds.size()); 96 assertTrue(state.callIds.contains(101)); 97 } 98 testReadState_MultipleCalls()99 public void testReadState_MultipleCalls() throws Exception { 100 when(mDataInput.readInt()).thenReturn( 101 1 /* version */, 102 2 /* size */, 103 101 /* call-ID */, 104 102 /* call-ID */); 105 106 CallLogBackupState state = mCallLogBackupAgent.readState(mDataInput); 107 108 assertEquals(1, state.version); 109 assertEquals(2, state.callIds.size()); 110 assertTrue(state.callIds.contains(101)); 111 assertTrue(state.callIds.contains(102)); 112 } 113 testWriteState_NoCalls()114 public void testWriteState_NoCalls() throws Exception { 115 CallLogBackupState state = new CallLogBackupState(); 116 state.version = CallLogBackupAgent.VERSION; 117 state.callIds = new TreeSet<>(); 118 119 mCallLogBackupAgent.writeState(mDataOutput, state); 120 121 InOrder inOrder = Mockito.inOrder(mDataOutput); 122 inOrder.verify(mDataOutput).writeInt(CallLogBackupAgent.VERSION); 123 inOrder.verify(mDataOutput).writeInt(0 /* size */); 124 } 125 testWriteState_OneCall()126 public void testWriteState_OneCall() throws Exception { 127 CallLogBackupState state = new CallLogBackupState(); 128 state.version = CallLogBackupAgent.VERSION; 129 state.callIds = new TreeSet<>(); 130 state.callIds.add(101); 131 132 mCallLogBackupAgent.writeState(mDataOutput, state); 133 134 InOrder inOrder = Mockito.inOrder(mDataOutput); 135 inOrder.verify(mDataOutput).writeInt(CallLogBackupAgent.VERSION); 136 inOrder.verify(mDataOutput).writeInt(1); 137 inOrder.verify(mDataOutput).writeInt(101 /* call-ID */); 138 } 139 testWriteState_MultipleCalls()140 public void testWriteState_MultipleCalls() throws Exception { 141 CallLogBackupState state = new CallLogBackupState(); 142 state.version = CallLogBackupAgent.VERSION; 143 state.callIds = new TreeSet<>(); 144 state.callIds.add(101); 145 state.callIds.add(102); 146 state.callIds.add(103); 147 148 mCallLogBackupAgent.writeState(mDataOutput, state); 149 150 InOrder inOrder = Mockito.inOrder(mDataOutput); 151 inOrder.verify(mDataOutput).writeInt(CallLogBackupAgent.VERSION); 152 inOrder.verify(mDataOutput).writeInt(3 /* size */); 153 inOrder.verify(mDataOutput).writeInt(101 /* call-ID */); 154 inOrder.verify(mDataOutput).writeInt(102 /* call-ID */); 155 inOrder.verify(mDataOutput).writeInt(103 /* call-ID */); 156 } 157 testRunBackup_NoCalls()158 public void testRunBackup_NoCalls() throws Exception { 159 CallLogBackupState state = new CallLogBackupState(); 160 state.version = CallLogBackupAgent.VERSION; 161 state.callIds = new TreeSet<>(); 162 List<Call> calls = new LinkedList<>(); 163 164 mCallLogBackupAgent.runBackup(state, mBackupDataOutput, calls); 165 166 Mockito.verifyNoMoreInteractions(mBackupDataOutput); 167 } 168 testRunBackup_OneNewCall()169 public void testRunBackup_OneNewCall() throws Exception { 170 CallLogBackupState state = new CallLogBackupState(); 171 state.version = CallLogBackupAgent.VERSION; 172 state.callIds = new TreeSet<>(); 173 List<Call> calls = new LinkedList<>(); 174 calls.add(makeCall(101, 0L, 0L, "555-5555")); 175 mCallLogBackupAgent.runBackup(state, mBackupDataOutput, calls); 176 177 verify(mBackupDataOutput).writeEntityHeader(eq("101"), Matchers.anyInt()); 178 verify(mBackupDataOutput).writeEntityData((byte[]) Matchers.any(), Matchers.anyInt()); 179 } 180 testRunBackup_MultipleCall()181 public void testRunBackup_MultipleCall() throws Exception { 182 CallLogBackupState state = new CallLogBackupState(); 183 state.version = CallLogBackupAgent.VERSION; 184 state.callIds = new TreeSet<>(); 185 List<Call> calls = new LinkedList<>(); 186 calls.add(makeCall(101, 0L, 0L, "555-1234")); 187 calls.add(makeCall(102, 0L, 0L, "555-5555")); 188 189 mCallLogBackupAgent.runBackup(state, mBackupDataOutput, calls); 190 191 InOrder inOrder = Mockito.inOrder(mBackupDataOutput); 192 inOrder.verify(mBackupDataOutput).writeEntityHeader(eq("101"), Matchers.anyInt()); 193 inOrder.verify(mBackupDataOutput). 194 writeEntityData((byte[]) Matchers.any(), Matchers.anyInt()); 195 inOrder.verify(mBackupDataOutput).writeEntityHeader(eq("102"), Matchers.anyInt()); 196 inOrder.verify(mBackupDataOutput). 197 writeEntityData((byte[]) Matchers.any(), Matchers.anyInt()); 198 } 199 testRunBackup_PartialMultipleCall()200 public void testRunBackup_PartialMultipleCall() throws Exception { 201 CallLogBackupState state = new CallLogBackupState(); 202 203 state.version = CallLogBackupAgent.VERSION; 204 state.callIds = new TreeSet<>(); 205 state.callIds.add(101); 206 207 List<Call> calls = new LinkedList<>(); 208 calls.add(makeCall(101, 0L, 0L, "555-1234")); 209 calls.add(makeCall(102, 0L, 0L, "555-5555")); 210 211 mCallLogBackupAgent.runBackup(state, mBackupDataOutput, calls); 212 213 InOrder inOrder = Mockito.inOrder(mBackupDataOutput); 214 inOrder.verify(mBackupDataOutput).writeEntityHeader(eq("102"), Matchers.anyInt()); 215 inOrder.verify(mBackupDataOutput). 216 writeEntityData((byte[]) Matchers.any(), Matchers.anyInt()); 217 } 218 makeCall(int id, long date, long duration, String number)219 private static Call makeCall(int id, long date, long duration, String number) { 220 Call c = new Call(); 221 c.id = id; 222 c.date = date; 223 c.duration = duration; 224 c.number = number; 225 c.accountComponentName = "account-component"; 226 c.accountId = "account-id"; 227 return c; 228 } 229 230 } 231