diff options
| author | 2017-06-01 13:06:29 +0100 | |
|---|---|---|
| committer | 2017-09-01 15:28:31 +0100 | |
| commit | 026e93323eea6197b8cf86a13b635504359737eb (patch) | |
| tree | 141fa0562b216b572d30b0eb9687ed5c1b203595 /services/robotests/src | |
| parent | d27f7ffc2dcf3f40eb444786f23e2c51ec70549b (diff) | |
Add tests for TransportManager#ensureTransportReady().
This involves some refactoring, so that TransportManager doesn't use API
not available in pre-O.
Bug: 37616038
Test: make RunFrameworksServicesRoboTests
Change-Id: I6a79bdbbee2a37a9fecff7865bdec37be2c7e59a
Diffstat (limited to 'services/robotests/src')
| -rw-r--r-- | services/robotests/src/com/android/server/backup/TransportManagerTest.java | 58 | ||||
| -rw-r--r-- | services/robotests/src/com/android/server/backup/testing/TransportReadyCallbackStub.java | 55 |
2 files changed, 113 insertions, 0 deletions
diff --git a/services/robotests/src/com/android/server/backup/TransportManagerTest.java b/services/robotests/src/com/android/server/backup/TransportManagerTest.java index f345da2b548c..2824b35636b7 100644 --- a/services/robotests/src/com/android/server/backup/TransportManagerTest.java +++ b/services/robotests/src/com/android/server/backup/TransportManagerTest.java @@ -20,6 +20,7 @@ import static com.google.common.truth.Truth.assertThat; import static org.mockito.Mockito.mock; +import android.app.backup.BackupManager; import android.content.ComponentName; import android.content.Intent; import android.content.pm.ApplicationInfo; @@ -34,6 +35,7 @@ import com.android.server.backup.testing.DefaultPackageManagerWithQueryIntentSer import com.android.server.backup.testing.ShadowBackupTransportStub; import com.android.server.backup.testing.ShadowContextImplForBackup; import com.android.server.backup.testing.TransportBoundListenerStub; +import com.android.server.backup.testing.TransportReadyCallbackStub; import org.junit.After; import org.junit.Before; @@ -75,6 +77,9 @@ public class TransportManagerTest { private final TransportBoundListenerStub mTransportBoundListenerStub = new TransportBoundListenerStub(true); + private final TransportReadyCallbackStub mTransportReadyCallbackStub = + new TransportReadyCallbackStub(); + @Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); @@ -417,6 +422,59 @@ public class TransportManagerTest { assertThat(transportManager.selectTransport(mTransport1.name)).isEqualTo(mTransport2.name); } + @Test + public void ensureTransportReady_transportNotYetBound_callsListenerOnFailure() + throws Exception { + setUpPackageWithTransports(PACKAGE_NAME, Arrays.asList(mTransport1, mTransport2), + ApplicationInfo.PRIVATE_FLAG_PRIVILEGED); + + TransportManager transportManager = new TransportManager( + RuntimeEnvironment.application.getApplicationContext(), + new HashSet<>(Arrays.asList(mTransport1.componentName, mTransport2.componentName)), + mTransport1.name, + mTransportBoundListenerStub, + ShadowLooper.getMainLooper()); + + transportManager.ensureTransportReady(mTransport1.componentName, + mTransportReadyCallbackStub); + + assertThat(mTransportReadyCallbackStub.getSuccessCalls()).isEmpty(); + assertThat(mTransportReadyCallbackStub.getFailureCalls()).containsExactlyElementsIn( + Collections.singleton( + BackupManager.ERROR_TRANSPORT_UNAVAILABLE)); + } + + @Test + public void ensureTransportReady_transportCannotBeBound_callsListenerOnFailure() + throws Exception { + TransportManager transportManager = + createTransportManagerAndSetUpTransports(Collections.singletonList(mTransport2), + Collections.singletonList(mTransport1), mTransport1.name); + + transportManager.ensureTransportReady(mTransport1.componentName, + mTransportReadyCallbackStub); + + assertThat(mTransportReadyCallbackStub.getSuccessCalls()).isEmpty(); + assertThat(mTransportReadyCallbackStub.getFailureCalls()).containsExactlyElementsIn( + Collections.singleton( + BackupManager.ERROR_TRANSPORT_UNAVAILABLE)); + } + + @Test + public void ensureTransportReady_transportsAlreadyBound_callsListenerOnSuccess() + throws Exception { + TransportManager transportManager = + createTransportManagerAndSetUpTransports(Collections.singletonList(mTransport2), + Collections.singletonList(mTransport1), mTransport1.name); + + transportManager.ensureTransportReady(mTransport2.componentName, + mTransportReadyCallbackStub); + + assertThat(mTransportReadyCallbackStub.getSuccessCalls()).containsExactlyElementsIn( + Collections.singleton(mTransport2.name)); + assertThat(mTransportReadyCallbackStub.getFailureCalls()).isEmpty(); + } + private void setUpPackageWithTransports(String packageName, List<TransportInfo> transports, int flags) throws Exception { PackageInfo packageInfo = new PackageInfo(); diff --git a/services/robotests/src/com/android/server/backup/testing/TransportReadyCallbackStub.java b/services/robotests/src/com/android/server/backup/testing/TransportReadyCallbackStub.java new file mode 100644 index 000000000000..bbe7eba149c6 --- /dev/null +++ b/services/robotests/src/com/android/server/backup/testing/TransportReadyCallbackStub.java @@ -0,0 +1,55 @@ +/* + * Copyright (C) 2017 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 com.android.server.backup.testing; + +import com.android.server.backup.TransportManager; + +import java.util.HashSet; +import java.util.Set; + +/** + * Stub implementation of TransportReadyCallback, which can tell which calls were made. + */ +public class TransportReadyCallbackStub implements + TransportManager.TransportReadyCallback { + private final Set<String> mSuccessCalls = new HashSet<>(); + private final Set<Integer> mFailureCalls = new HashSet<>(); + + @Override + public void onSuccess(String transportName) { + mSuccessCalls.add(transportName); + } + + @Override + public void onFailure(int reason) { + mFailureCalls.add(reason); + } + + /** + * Returns set of transport names for which {@link #onSuccess(String)} was called. + */ + public Set<String> getSuccessCalls() { + return mSuccessCalls; + } + + /** + * Returns set of reasons for which {@link #onFailure(int)} } was called. + */ + public Set<Integer> getFailureCalls() { + return mFailureCalls; + } +} |