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 17 package com.android.server.backup.utils; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.Mockito.doThrow; 22 import static org.mockito.Mockito.verify; 23 24 import android.app.backup.IFullBackupRestoreObserver; 25 import android.os.RemoteException; 26 import android.platform.test.annotations.Presubmit; 27 import android.support.test.filters.SmallTest; 28 import android.support.test.runner.AndroidJUnit4; 29 30 import org.junit.Before; 31 import org.junit.Test; 32 import org.junit.runner.RunWith; 33 import org.mockito.Mock; 34 import org.mockito.MockitoAnnotations; 35 36 @SmallTest 37 @Presubmit 38 @RunWith(AndroidJUnit4.class) 39 public class FullBackupRestoreObserverUtilsTest { 40 private static final String PACKAGE_NAME = "some.package"; 41 @Mock 42 private IFullBackupRestoreObserver mFullBackupRestoreObserverMock; 43 44 @Before 45 public void setUp() throws Exception { 46 MockitoAnnotations.initMocks(this); 47 } 48 49 @Test 50 public void sendStartRestore_observerIsNull_returnsNull() throws Exception { 51 IFullBackupRestoreObserver result = FullBackupRestoreObserverUtils.sendStartRestore(null); 52 53 assertThat(result).isNull(); 54 } 55 56 @Test 57 public void sendStartRestore_callsObserver() throws Exception { 58 IFullBackupRestoreObserver result = FullBackupRestoreObserverUtils.sendStartRestore( 59 mFullBackupRestoreObserverMock); 60 61 assertThat(result).isEqualTo(mFullBackupRestoreObserverMock); 62 verify(mFullBackupRestoreObserverMock).onStartRestore(); 63 } 64 65 @Test 66 public void sendStartRestore_observerThrows_returnsNull() throws Exception { 67 doThrow(new RemoteException()).when(mFullBackupRestoreObserverMock).onStartRestore(); 68 69 IFullBackupRestoreObserver result = FullBackupRestoreObserverUtils.sendStartRestore( 70 mFullBackupRestoreObserverMock); 71 72 assertThat(result).isNull(); 73 verify(mFullBackupRestoreObserverMock).onStartRestore(); 74 } 75 76 @Test 77 public void sendOnRestorePackage_observerIsNull_returnsNull() throws Exception { 78 IFullBackupRestoreObserver result = FullBackupRestoreObserverUtils.sendOnRestorePackage( 79 null, PACKAGE_NAME); 80 81 assertThat(result).isNull(); 82 } 83 84 @Test 85 public void sendOnRestorePackage_callsObserver() throws Exception { 86 IFullBackupRestoreObserver result = FullBackupRestoreObserverUtils.sendOnRestorePackage( 87 mFullBackupRestoreObserverMock, PACKAGE_NAME); 88 89 assertThat(result).isEqualTo(mFullBackupRestoreObserverMock); 90 verify(mFullBackupRestoreObserverMock).onRestorePackage(PACKAGE_NAME); 91 } 92 93 @Test 94 public void sendOnRestorePackage_observerThrows_returnsNull() throws Exception { 95 doThrow(new RemoteException()).when(mFullBackupRestoreObserverMock).onRestorePackage( 96 PACKAGE_NAME); 97 98 IFullBackupRestoreObserver result = FullBackupRestoreObserverUtils.sendOnRestorePackage( 99 mFullBackupRestoreObserverMock, PACKAGE_NAME); 100 101 assertThat(result).isNull(); 102 verify(mFullBackupRestoreObserverMock).onRestorePackage(PACKAGE_NAME); 103 } 104 105 @Test 106 public void sendEndRestore_observerIsNull_returnsNull() throws Exception { 107 IFullBackupRestoreObserver result = FullBackupRestoreObserverUtils.sendEndRestore(null); 108 109 assertThat(result).isNull(); 110 } 111 112 @Test 113 public void sendEndRestore_callsObserver() throws Exception { 114 IFullBackupRestoreObserver result = FullBackupRestoreObserverUtils.sendEndRestore( 115 mFullBackupRestoreObserverMock); 116 117 assertThat(result).isEqualTo(mFullBackupRestoreObserverMock); 118 verify(mFullBackupRestoreObserverMock).onEndRestore(); 119 } 120 121 @Test 122 public void sendEndRestore_observerThrows_returnsNull() throws Exception { 123 doThrow(new RemoteException()).when(mFullBackupRestoreObserverMock).onEndRestore(); 124 125 IFullBackupRestoreObserver result = FullBackupRestoreObserverUtils.sendEndRestore( 126 mFullBackupRestoreObserverMock); 127 128 assertThat(result).isNull(); 129 verify(mFullBackupRestoreObserverMock).onEndRestore(); 130 } 131 }