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 android.app.backup.BackupManagerMonitor.EXTRA_LOG_EVENT_CATEGORY; 20 import static android.app.backup.BackupManagerMonitor.EXTRA_LOG_EVENT_ID; 21 import static android.app.backup.BackupManagerMonitor.EXTRA_LOG_EVENT_PACKAGE_NAME; 22 import static android.app.backup.BackupManagerMonitor.EXTRA_LOG_EVENT_PACKAGE_VERSION; 23 import static android.app.backup.BackupManagerMonitor.EXTRA_LOG_EVENT_PACKAGE_LONG_VERSION; 24 25 import static com.google.common.truth.Truth.assertThat; 26 27 import static org.mockito.ArgumentMatchers.any; 28 import static org.mockito.Mockito.doThrow; 29 import static org.mockito.Mockito.verify; 30 31 import android.app.backup.IBackupManagerMonitor; 32 import android.content.pm.PackageInfo; 33 import android.os.Bundle; 34 import android.os.RemoteException; 35 import android.platform.test.annotations.Presubmit; 36 import android.support.test.filters.SmallTest; 37 import android.support.test.runner.AndroidJUnit4; 38 39 import org.junit.Before; 40 import org.junit.Test; 41 import org.junit.runner.RunWith; 42 import org.mockito.ArgumentCaptor; 43 import org.mockito.Mock; 44 import org.mockito.MockitoAnnotations; 45 46 @SmallTest 47 @Presubmit 48 @RunWith(AndroidJUnit4.class) 49 public class BackupManagerMonitorUtilsTest { 50 @Mock private IBackupManagerMonitor mMonitorMock; 51 52 @Before 53 public void setUp() { 54 MockitoAnnotations.initMocks(this); 55 } 56 57 @Test 58 public void monitorEvent_monitorIsNull_returnsNull() throws Exception { 59 IBackupManagerMonitor result = BackupManagerMonitorUtils.monitorEvent(null, 0, null, 0, 60 null); 61 62 assertThat(result).isNull(); 63 } 64 65 @Test 66 public void monitorEvent_monitorOnEventThrows_returnsNull() throws Exception { 67 doThrow(new RemoteException()).when(mMonitorMock).onEvent(any(Bundle.class)); 68 69 IBackupManagerMonitor result = BackupManagerMonitorUtils.monitorEvent(mMonitorMock, 0, null, 70 0, null); 71 72 verify(mMonitorMock).onEvent(any(Bundle.class)); 73 assertThat(result).isNull(); 74 } 75 76 @Test 77 public void monitorEvent_packageAndExtrasAreNull_fillsBundleCorrectly() throws Exception { 78 IBackupManagerMonitor result = BackupManagerMonitorUtils.monitorEvent(mMonitorMock, 1, null, 79 2, null); 80 81 assertThat(result).isEqualTo(mMonitorMock); 82 ArgumentCaptor<Bundle> bundleCaptor = ArgumentCaptor.forClass(Bundle.class); 83 verify(mMonitorMock).onEvent(bundleCaptor.capture()); 84 Bundle eventBundle = bundleCaptor.getValue(); 85 assertThat(eventBundle.size()).isEqualTo(2); 86 assertThat(eventBundle.getInt(EXTRA_LOG_EVENT_ID)).isEqualTo(1); 87 assertThat(eventBundle.getInt(EXTRA_LOG_EVENT_CATEGORY)).isEqualTo(2); 88 } 89 90 @Test 91 public void monitorEvent_packageAndExtrasAreNotNull_fillsBundleCorrectly() throws Exception { 92 PackageInfo packageInfo = new PackageInfo(); 93 packageInfo.packageName = "test.package"; 94 packageInfo.versionCode = 3; 95 Bundle extras = new Bundle(); 96 extras.putInt("key1", 4); 97 extras.putString("key2", "value2"); 98 99 IBackupManagerMonitor result = BackupManagerMonitorUtils.monitorEvent(mMonitorMock, 1, 100 packageInfo, 2, extras); 101 102 assertThat(result).isEqualTo(mMonitorMock); 103 ArgumentCaptor<Bundle> bundleCaptor = ArgumentCaptor.forClass(Bundle.class); 104 verify(mMonitorMock).onEvent(bundleCaptor.capture()); 105 Bundle eventBundle = bundleCaptor.getValue(); 106 assertThat(eventBundle.size()).isEqualTo(7); 107 assertThat(eventBundle.getInt(EXTRA_LOG_EVENT_ID)).isEqualTo(1); 108 assertThat(eventBundle.getInt(EXTRA_LOG_EVENT_CATEGORY)).isEqualTo(2); 109 assertThat(eventBundle.getString(EXTRA_LOG_EVENT_PACKAGE_NAME)).isEqualTo("test.package"); 110 assertThat(eventBundle.getInt(EXTRA_LOG_EVENT_PACKAGE_VERSION)).isEqualTo(3); 111 assertThat(eventBundle.getLong(EXTRA_LOG_EVENT_PACKAGE_LONG_VERSION)).isEqualTo(3); 112 assertThat(eventBundle.getInt("key1")).isEqualTo(4); 113 assertThat(eventBundle.getString("key2")).isEqualTo("value2"); 114 } 115 116 @Test 117 public void monitorEvent_packageAndExtrasAreNotNull_fillsBundleCorrectlyLong() throws Exception { 118 PackageInfo packageInfo = new PackageInfo(); 119 packageInfo.packageName = "test.package"; 120 packageInfo.versionCode = 3; 121 packageInfo.versionCodeMajor = 10; 122 Bundle extras = new Bundle(); 123 extras.putInt("key1", 4); 124 extras.putString("key2", "value2"); 125 126 IBackupManagerMonitor result = BackupManagerMonitorUtils.monitorEvent(mMonitorMock, 1, 127 packageInfo, 2, extras); 128 129 assertThat(result).isEqualTo(mMonitorMock); 130 ArgumentCaptor<Bundle> bundleCaptor = ArgumentCaptor.forClass(Bundle.class); 131 verify(mMonitorMock).onEvent(bundleCaptor.capture()); 132 Bundle eventBundle = bundleCaptor.getValue(); 133 assertThat(eventBundle.size()).isEqualTo(7); 134 assertThat(eventBundle.getInt(EXTRA_LOG_EVENT_ID)).isEqualTo(1); 135 assertThat(eventBundle.getInt(EXTRA_LOG_EVENT_CATEGORY)).isEqualTo(2); 136 assertThat(eventBundle.getString(EXTRA_LOG_EVENT_PACKAGE_NAME)).isEqualTo("test.package"); 137 assertThat(eventBundle.getInt(EXTRA_LOG_EVENT_PACKAGE_VERSION)).isEqualTo(3); 138 assertThat(eventBundle.getLong(EXTRA_LOG_EVENT_PACKAGE_LONG_VERSION)).isEqualTo( 139 (10L << 32) | 3); 140 assertThat(eventBundle.getInt("key1")).isEqualTo(4); 141 assertThat(eventBundle.getString("key2")).isEqualTo("value2"); 142 } 143 144 @Test 145 public void putMonitoringExtraString_bundleExists_fillsBundleCorrectly() throws Exception { 146 Bundle bundle = new Bundle(); 147 148 Bundle result = BackupManagerMonitorUtils.putMonitoringExtra(bundle, "key", "value"); 149 150 assertThat(result).isEqualTo(bundle); 151 assertThat(result.size()).isEqualTo(1); 152 assertThat(result.getString("key")).isEqualTo("value"); 153 } 154 155 @Test 156 public void putMonitoringExtraString_bundleDoesNotExist_fillsBundleCorrectly() 157 throws Exception { 158 Bundle result = BackupManagerMonitorUtils.putMonitoringExtra(null, "key", "value"); 159 160 assertThat(result).isNotNull(); 161 assertThat(result.size()).isEqualTo(1); 162 assertThat(result.getString("key")).isEqualTo("value"); 163 } 164 165 166 @Test 167 public void putMonitoringExtraLong_bundleExists_fillsBundleCorrectly() throws Exception { 168 Bundle bundle = new Bundle(); 169 170 Bundle result = BackupManagerMonitorUtils.putMonitoringExtra(bundle, "key", 123); 171 172 assertThat(result).isEqualTo(bundle); 173 assertThat(result.size()).isEqualTo(1); 174 assertThat(result.getLong("key")).isEqualTo(123); 175 } 176 177 @Test 178 public void putMonitoringExtraLong_bundleDoesNotExist_fillsBundleCorrectly() throws Exception { 179 Bundle result = BackupManagerMonitorUtils.putMonitoringExtra(null, "key", 123); 180 181 assertThat(result).isNotNull(); 182 assertThat(result.size()).isEqualTo(1); 183 assertThat(result.getLong("key")).isEqualTo(123); 184 } 185 186 @Test 187 public void putMonitoringExtraBoolean_bundleExists_fillsBundleCorrectly() throws Exception { 188 Bundle bundle = new Bundle(); 189 190 Bundle result = BackupManagerMonitorUtils.putMonitoringExtra(bundle, "key", true); 191 192 assertThat(result).isEqualTo(bundle); 193 assertThat(result.size()).isEqualTo(1); 194 assertThat(result.getBoolean("key")).isTrue(); 195 } 196 197 @Test 198 public void putMonitoringExtraBoolean_bundleDoesNotExist_fillsBundleCorrectly() 199 throws Exception { 200 Bundle result = BackupManagerMonitorUtils.putMonitoringExtra(null, "key", true); 201 202 assertThat(result).isNotNull(); 203 assertThat(result.size()).isEqualTo(1); 204 assertThat(result.getBoolean("key")).isTrue(); 205 } 206 207 }