Merge "Allow BackupManagerMonitorWrapper to wrap null values" into udc-dev

This commit is contained in:
Ruslan Tkhakokhov
2023-05-12 20:00:11 +00:00
committed by Android (Google) Code Review
2 changed files with 79 additions and 2 deletions

View File

@@ -16,9 +16,12 @@
package android.app.backup;
import android.annotation.Nullable;
import android.os.Bundle;
import android.os.RemoteException;
import com.android.internal.annotations.VisibleForTesting;
/**
* Wrapper around {@link BackupManagerMonitor} that helps with IPC between the caller of backup
* APIs and the backup service.
@@ -26,16 +29,24 @@ import android.os.RemoteException;
* The caller implements {@link BackupManagerMonitor} and passes it into framework APIs that run on
* the caller's process. Those framework APIs will then wrap it around this class when doing the
* actual IPC.
*
* @hide
*/
class BackupManagerMonitorWrapper extends IBackupManagerMonitor.Stub {
@VisibleForTesting
public class BackupManagerMonitorWrapper extends IBackupManagerMonitor.Stub {
@Nullable
private final BackupManagerMonitor mMonitor;
BackupManagerMonitorWrapper(BackupManagerMonitor monitor) {
public BackupManagerMonitorWrapper(@Nullable BackupManagerMonitor monitor) {
mMonitor = monitor;
}
@Override
public void onEvent(final Bundle event) throws RemoteException {
if (mMonitor == null) {
// It's valid for the underlying monitor to be null, so just return.
return;
}
mMonitor.onEvent(event);
}
}

View File

@@ -0,0 +1,66 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.app.backup;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import android.os.Bundle;
import android.platform.test.annotations.Presubmit;
import androidx.test.runner.AndroidJUnit4;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
@Presubmit
@RunWith(AndroidJUnit4.class)
public class BackupManagerMonitorWrapperTest {
@Mock
private BackupManagerMonitor mMonitor;
private BackupManagerMonitorWrapper mMonitorWrapper;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}
@Test
public void testOnEvent_propagatesToMonitor() throws Exception {
mMonitorWrapper = new BackupManagerMonitorWrapper(mMonitor);
Bundle eventBundle = new Bundle();
mMonitorWrapper.onEvent(eventBundle);
verify(mMonitor, times(/* wantedNumberOfInvocations */ 1)).onEvent(eq(eventBundle));
}
@Test
public void testOnEvent_nullMonitor_eventIsIgnored() throws Exception {
mMonitorWrapper = new BackupManagerMonitorWrapper(/* monitor */ null);
mMonitorWrapper.onEvent(new Bundle());
verify(mMonitor, never()).onEvent(any());
}
}