Loading services/backup/java/com/android/server/backup/TransportManager.java +14 −4 Original line number Diff line number Diff line Loading @@ -416,9 +416,13 @@ public class TransportManager { * {@link TransportClient#connectAsync(TransportConnectionListener, String)} for more * details. * @return A {@link TransportClient} or null if not registered. * @throws IllegalStateException if no transport is selected. */ @Nullable public TransportClient getCurrentTransportClient(String caller) { if (mCurrentTransportName == null) { throw new IllegalStateException("No transport selected"); } synchronized (mTransportLock) { return getTransportClient(mCurrentTransportName, caller); } Loading @@ -432,9 +436,13 @@ public class TransportManager { * details. * @return A {@link TransportClient}. * @throws TransportNotRegisteredException if the transport is not registered. * @throws IllegalStateException if no transport is selected. */ public TransportClient getCurrentTransportClientOrThrow(String caller) throws TransportNotRegisteredException { if (mCurrentTransportName == null) { throw new IllegalStateException("No transport selected"); } synchronized (mTransportLock) { return getTransportClientOrThrow(mCurrentTransportName, caller); } Loading Loading @@ -547,8 +555,9 @@ public class TransportManager { /** Transport has to be whitelisted and privileged. */ private boolean isTransportTrusted(ComponentName transport) { if (!mTransportWhitelist.contains(transport)) { Slog.w(TAG, "BackupTransport " + transport.flattenToShortString() + " not whitelisted."); Slog.w( TAG, "BackupTransport " + transport.flattenToShortString() + " not whitelisted."); return false; } try { Loading Loading @@ -588,7 +597,8 @@ public class TransportManager { Bundle extras = new Bundle(); extras.putBoolean(BackupTransport.EXTRA_TRANSPORT_REGISTRATION, true); TransportClient transportClient = mTransportClientManager.getTransportClient( TransportClient transportClient = mTransportClientManager.getTransportClient( transportComponent, extras, callerLogString); final IBackupTransport transport; try { Loading services/robotests/src/com/android/server/backup/TransportManagerTest.java +138 −85 Original line number Diff line number Diff line Loading @@ -204,8 +204,10 @@ public class TransportManagerTest { verify(mTransportClientManager) .getTransportClient( eq(mTransportA1.getTransportComponent()), argThat(bundle -> bundle.getBoolean(BackupTransport.EXTRA_TRANSPORT_REGISTRATION)), argThat( bundle -> bundle.getBoolean( BackupTransport.EXTRA_TRANSPORT_REGISTRATION)), anyString()); } Loading @@ -224,11 +226,8 @@ public class TransportManagerTest { @Test public void testOnPackageRemoved_unregisterTransports() throws Exception { setUpPackage(PACKAGE_A, ApplicationInfo.PRIVATE_FLAG_PRIVILEGED); setUpPackage(PACKAGE_B, ApplicationInfo.PRIVATE_FLAG_PRIVILEGED); setUpTransports(mTransportA1, mTransportB1); TransportManager transportManager = createTransportManager(mTransportA1, mTransportB1); transportManager.registerTransports(); TransportManager transportManager = createTransportManagerWithRegisteredTransports(mTransportA1, mTransportB1); transportManager.onPackageRemoved(PACKAGE_A); Loading @@ -237,10 +236,8 @@ public class TransportManagerTest { @Test public void testOnPackageRemoved_whenUnknownPackage_nothingHappens() throws Exception { setUpPackage(PACKAGE_A, ApplicationInfo.PRIVATE_FLAG_PRIVILEGED); setUpTransports(mTransportA1); TransportManager transportManager = createTransportManager(mTransportA1); transportManager.registerTransports(); TransportManager transportManager = createTransportManagerWithRegisteredTransports(mTransportA1); transportManager.onPackageRemoved(PACKAGE_A + "unknown"); Loading @@ -250,10 +247,8 @@ public class TransportManagerTest { @Test public void testOnPackageChanged_whenOneComponentChanged_onlyOneTransportReRegistered() throws Exception { setUpPackage(PACKAGE_A, ApplicationInfo.PRIVATE_FLAG_PRIVILEGED); setUpTransports(mTransportA1, mTransportA2); TransportManager transportManager = createTransportManager(mTransportA1, mTransportA2); transportManager.registerTransports(); TransportManager transportManager = createTransportManagerWithRegisteredTransports(mTransportA1, mTransportA2); // Reset listener to verify calls after registerTransports() above reset(mListener); Loading @@ -270,10 +265,8 @@ public class TransportManagerTest { @Test public void testOnPackageChanged_whenNoComponentsChanged_doesNotRegisterTransports() throws Exception { setUpPackage(PACKAGE_A, ApplicationInfo.PRIVATE_FLAG_PRIVILEGED); setUpTransports(mTransportA1); TransportManager transportManager = createTransportManager(mTransportA1); transportManager.registerTransports(); TransportManager transportManager = createTransportManagerWithRegisteredTransports(mTransportA1); reset(mListener); transportManager.onPackageChanged(PACKAGE_A); Loading @@ -285,10 +278,8 @@ public class TransportManagerTest { @Test public void testOnPackageChanged_whenUnknownComponentChanged_noTransportsRegistered() throws Exception { setUpPackage(PACKAGE_A, ApplicationInfo.PRIVATE_FLAG_PRIVILEGED); setUpTransports(mTransportA1); TransportManager transportManager = createTransportManager(mTransportA1); transportManager.registerTransports(); TransportManager transportManager = createTransportManagerWithRegisteredTransports(mTransportA1); reset(mListener); transportManager.onPackageChanged(PACKAGE_A, PACKAGE_A + ".UnknownComponent"); Loading @@ -299,10 +290,8 @@ public class TransportManagerTest { @Test public void testOnPackageChanged_reRegisterTransports() throws Exception { setUpPackage(PACKAGE_A, ApplicationInfo.PRIVATE_FLAG_PRIVILEGED); setUpTransports(mTransportA1, mTransportA2); TransportManager transportManager = createTransportManager(mTransportA1, mTransportA2); transportManager.registerTransports(); TransportManager transportManager = createTransportManagerWithRegisteredTransports(mTransportA1, mTransportA2); reset(mListener); transportManager.onPackageChanged( Loading @@ -319,10 +308,8 @@ public class TransportManagerTest { @Test public void testRegisterAndSelectTransport_whenTransportRegistered() throws Exception { setUpPackage(PACKAGE_A, ApplicationInfo.PRIVATE_FLAG_PRIVILEGED); setUpTransports(mTransportA1); TransportManager transportManager = createTransportManager(null, mTransportA1); transportManager.registerTransports(); TransportManager transportManager = createTransportManagerWithRegisteredTransports(null, mTransportA1); ComponentName transportComponent = mTransportA1.getTransportComponent(); int result = transportManager.registerAndSelectTransport(transportComponent); Loading Loading @@ -357,10 +344,8 @@ public class TransportManagerTest { @Test public void testGetCurrentTransportName_whenSelectTransportNotCalled_returnsDefaultTransport() throws Exception { setUpPackage(PACKAGE_A, ApplicationInfo.PRIVATE_FLAG_PRIVILEGED); setUpTransports(mTransportA1, mTransportA2); TransportManager transportManager = createTransportManager(mTransportA1, mTransportA2); transportManager.registerTransports(); TransportManager transportManager = createTransportManagerWithRegisteredTransports(mTransportA1, mTransportA2); String currentTransportName = transportManager.getCurrentTransportName(); Loading @@ -370,10 +355,8 @@ public class TransportManagerTest { @Test public void testGetCurrentTransport_whenSelectTransportCalled_returnsSelectedTransport() throws Exception { setUpPackage(PACKAGE_A, ApplicationInfo.PRIVATE_FLAG_PRIVILEGED); setUpTransports(mTransportA1, mTransportA2); TransportManager transportManager = createTransportManager(mTransportA1, mTransportA2); transportManager.registerTransports(); TransportManager transportManager = createTransportManagerWithRegisteredTransports(mTransportA1, mTransportA2); transportManager.selectTransport(mTransportA2.transportName); String currentTransportName = transportManager.getCurrentTransportName(); Loading Loading @@ -412,10 +395,8 @@ public class TransportManagerTest { @Test public void testGetTransportClient_forRegisteredTransport() throws Exception { setUpPackage(PACKAGE_A, ApplicationInfo.PRIVATE_FLAG_PRIVILEGED); setUpTransports(mTransportA1, mTransportA2); TransportManager transportManager = createTransportManager(mTransportA1, mTransportA2); transportManager.registerTransports(); TransportManager transportManager = createTransportManagerWithRegisteredTransports(mTransportA1, mTransportA2); TransportClient transportClient = transportManager.getTransportClient(mTransportA1.transportName, "caller"); Loading @@ -427,10 +408,8 @@ public class TransportManagerTest { @Test public void testGetTransportClient_forOldNameOfTransportThatChangedName_returnsNull() throws Exception { setUpPackage(PACKAGE_A, ApplicationInfo.PRIVATE_FLAG_PRIVILEGED); setUpTransports(mTransportA1, mTransportA2); TransportManager transportManager = createTransportManager(mTransportA1, mTransportA2); transportManager.registerTransports(); TransportManager transportManager = createTransportManagerWithRegisteredTransports(mTransportA1, mTransportA2); transportManager.updateTransportAttributes( mTransportA1.getTransportComponent(), "newName", Loading @@ -446,12 +425,9 @@ public class TransportManagerTest { } @Test public void testGetTransportClient_forNewNameOfTransportThatChangedName_returnsCorrectly() throws Exception { setUpPackage(PACKAGE_A, ApplicationInfo.PRIVATE_FLAG_PRIVILEGED); setUpTransports(mTransportA1, mTransportA2); TransportManager transportManager = createTransportManager(mTransportA1, mTransportA2); transportManager.registerTransports(); public void testGetTransportClient_forNewNameOfTransportThatChangedName() throws Exception { TransportManager transportManager = createTransportManagerWithRegisteredTransports(mTransportA1, mTransportA2); transportManager.updateTransportAttributes( mTransportA1.getTransportComponent(), "newName", Loading @@ -466,12 +442,41 @@ public class TransportManagerTest { .isEqualTo(mTransportA1.getTransportComponent()); } @Test public void testGetCurrentTransportClient() throws Exception { TransportManager transportManager = createTransportManagerWithRegisteredTransports(mTransportA1, mTransportA2); TransportClient transportClient = transportManager.getCurrentTransportClient("caller"); assertThat(transportClient.getTransportComponent()) .isEqualTo(mTransportA1.getTransportComponent()); } @Test public void testGetCurrentTransportClient_whenNoTransportSelected() throws Exception { TransportManager transportManager = createTransportManagerWithRegisteredTransports(null, mTransportA1); expectThrows( IllegalStateException.class, () -> transportManager.getCurrentTransportClient("caller")); } @Test public void testGetCurrentTransportClientOrThrow_whenNoTransportSelected() throws Exception { TransportManager transportManager = createTransportManagerWithRegisteredTransports(null, mTransportA1); expectThrows( IllegalStateException.class, () -> transportManager.getCurrentTransportClientOrThrow("caller")); } @Test public void testGetTransportName_forTransportThatChangedName_returnsNewName() throws Exception { setUpPackage(PACKAGE_A, ApplicationInfo.PRIVATE_FLAG_PRIVILEGED); setUpTransports(mTransportA1, mTransportA2); TransportManager transportManager = createTransportManager(mTransportA1, mTransportA2); transportManager.registerTransports(); TransportManager transportManager = createTransportManagerWithRegisteredTransports(mTransportA1, mTransportA2); transportManager.updateTransportAttributes( mTransportA1.getTransportComponent(), "newName", Loading Loading @@ -505,35 +510,42 @@ public class TransportManagerTest { @Test public void testGetTransportAttributes_forRegisteredTransport_returnsCorrectValues() throws Exception { setUpPackage(PACKAGE_A, ApplicationInfo.PRIVATE_FLAG_PRIVILEGED); setUpTransports(mTransportA1); TransportManager transportManager = createTransportManager(mTransportA1); transportManager.registerTransports(); TransportManager transportManager = createTransportManagerWithRegisteredTransports(mTransportA1); Intent configurationIntent = transportManager.getTransportConfigurationIntent(mTransportA1.transportName); String destinationString = transportManager.getTransportCurrentDestinationString(mTransportA1.transportName); Intent dataManagementIntent = transportManager.getTransportDataManagementIntent(mTransportA1.transportName); String dataManagementLabel = transportManager.getTransportDataManagementLabel(mTransportA1.transportName); String transportDirName = transportManager.getTransportDirName(mTransportA1.transportName); String transportDirNameByComponent = transportManager.getTransportDirName(mTransportA1.getTransportComponent()); assertThat(configurationIntent).isEqualTo(mTransportA1.configurationIntent); assertThat(destinationString).isEqualTo(mTransportA1.currentDestinationString); assertThat(dataManagementIntent).isEqualTo(mTransportA1.dataManagementIntent); assertThat(dataManagementLabel).isEqualTo(mTransportA1.dataManagementLabel); assertThat(transportDirName).isEqualTo(mTransportA1.transportDirName); assertThat(transportDirNameByComponent).isEqualTo(mTransportA1.transportDirName); } @Test public void testGetTransportAttributes_forUnregisteredTransport_throws() throws Exception { setUpPackage(PACKAGE_A, ApplicationInfo.PRIVATE_FLAG_PRIVILEGED); setUpTransports(mTransportA1); TransportManager transportManager = createTransportManager(mTransportA1); transportManager.registerTransports(); TransportManager transportManager = createTransportManagerWithRegisteredTransports(mTransportA1); expectThrows( TransportNotRegisteredException.class, () -> transportManager.getTransportConfigurationIntent(mTransportA2.transportName)); expectThrows( TransportNotRegisteredException.class, () -> transportManager.getTransportCurrentDestinationString( mTransportA2.transportName)); expectThrows( TransportNotRegisteredException.class, () -> Loading @@ -545,48 +557,69 @@ public class TransportManagerTest { expectThrows( TransportNotRegisteredException.class, () -> transportManager.getTransportDirName(mTransportA2.transportName)); expectThrows( TransportNotRegisteredException.class, () -> transportManager.getTransportDirName(mTransportA2.getTransportComponent())); } @Test public void testGetRegisteredTransportNames() throws Exception { setUpPackage(PACKAGE_A, ApplicationInfo.PRIVATE_FLAG_PRIVILEGED); setUpPackage(PACKAGE_B, ApplicationInfo.PRIVATE_FLAG_PRIVILEGED); TransportData[] transportsData = {mTransportA1, mTransportA2, mTransportB1}; setUpTransports(transportsData); TransportManager transportManager = createTransportManager(mTransportA1, mTransportA2, mTransportB1); transportManager.registerTransports(); createTransportManagerWithRegisteredTransports( mTransportA1, mTransportA2, mTransportB1); String[] transportNames = transportManager.getRegisteredTransportNames(); assertThat(transportNames) .asList() .containsExactlyElementsIn( Stream.of(transportsData) Stream.of(mTransportA1, mTransportA2, mTransportB1) .map(transportData -> transportData.transportName) .collect(toList())); } @Test public void testGetRegisteredTransportComponents() throws Exception { setUpPackage(PACKAGE_A, ApplicationInfo.PRIVATE_FLAG_PRIVILEGED); setUpPackage(PACKAGE_B, ApplicationInfo.PRIVATE_FLAG_PRIVILEGED); TransportData[] transportsData = {mTransportA1, mTransportA2, mTransportB1}; setUpTransports(transportsData); TransportManager transportManager = createTransportManager(mTransportA1, mTransportA2, mTransportB1); transportManager.registerTransports(); createTransportManagerWithRegisteredTransports( mTransportA1, mTransportA2, mTransportB1); ComponentName[] transportNames = transportManager.getRegisteredTransportComponents(); assertThat(transportNames) .asList() .containsExactlyElementsIn( Stream.of(transportsData) Stream.of(mTransportA1, mTransportA2, mTransportB1) .map(TransportData::getTransportComponent) .collect(toList())); } @Test public void testForEachRegisteredTransport() throws Exception { TransportManager transportManager = createTransportManagerWithRegisteredTransports( mTransportA1, mTransportA2, mTransportB1); List<String> calls = new ArrayList<>(); transportManager.forEachRegisteredTransport(calls::add); assertThat(calls) .containsExactly( mTransportA1.transportName, mTransportA2.transportName, mTransportB1.transportName); } @Test public void testForEachRegisteredTransport_whenNoTransportsRegistered() throws Exception { TransportManager transportManager = createTransportManagerWithRegisteredTransports(null); List<String> calls = new ArrayList<>(); transportManager.forEachRegisteredTransport(calls::add); assertThat(calls).isEmpty(); } private List<TransportMock> setUpTransports(TransportData... transports) throws Exception { setUpTransportsForTransportManager(mShadowPackageManager, transports); List<TransportMock> transportMocks = new ArrayList<>(transports.length); Loading @@ -611,23 +644,43 @@ public class TransportManagerTest { mShadowPackageManager.addPackage(packageInfo); } private TransportManager createTransportManagerWithRegisteredTransports( @Nullable TransportData selectedTransport, TransportData... transports) throws Exception { Set<TransportData> transportSet = merge(selectedTransport, transports); for (TransportData transport : transportSet) { setUpPackage( transport.getTransportComponent().getPackageName(), ApplicationInfo.PRIVATE_FLAG_PRIVILEGED); } setUpTransports(transportSet.toArray(new TransportData[transportSet.size()])); TransportManager transportManager = createTransportManager(selectedTransport, transports); transportManager.registerTransports(); return transportManager; } private TransportManager createTransportManager( @Nullable TransportData selectedTransport, TransportData... transports) { Set<ComponentName> whitelist = concat(Stream.of(selectedTransport), Stream.of(transports)) .filter(Objects::nonNull) .map(TransportData::getTransportComponent) .collect(toSet()); TransportManager transportManager = new TransportManager( mContext, whitelist, merge(selectedTransport, transports) .stream() .map(TransportData::getTransportComponent) .collect(toSet()), selectedTransport != null ? selectedTransport.transportName : null, mTransportClientManager); transportManager.setOnTransportRegisteredListener(mListener); return transportManager; } private Set<TransportData> merge( @Nullable TransportData selectedTransport, TransportData... transports) { return concat(Stream.of(selectedTransport), Stream.of(transports)) .filter(Objects::nonNull) .collect(toSet()); } private void assertRegisteredTransports( TransportManager transportManager, List<TransportData> transports) { assertThat(transportManager.getRegisteredTransportComponents()) Loading Loading
services/backup/java/com/android/server/backup/TransportManager.java +14 −4 Original line number Diff line number Diff line Loading @@ -416,9 +416,13 @@ public class TransportManager { * {@link TransportClient#connectAsync(TransportConnectionListener, String)} for more * details. * @return A {@link TransportClient} or null if not registered. * @throws IllegalStateException if no transport is selected. */ @Nullable public TransportClient getCurrentTransportClient(String caller) { if (mCurrentTransportName == null) { throw new IllegalStateException("No transport selected"); } synchronized (mTransportLock) { return getTransportClient(mCurrentTransportName, caller); } Loading @@ -432,9 +436,13 @@ public class TransportManager { * details. * @return A {@link TransportClient}. * @throws TransportNotRegisteredException if the transport is not registered. * @throws IllegalStateException if no transport is selected. */ public TransportClient getCurrentTransportClientOrThrow(String caller) throws TransportNotRegisteredException { if (mCurrentTransportName == null) { throw new IllegalStateException("No transport selected"); } synchronized (mTransportLock) { return getTransportClientOrThrow(mCurrentTransportName, caller); } Loading Loading @@ -547,8 +555,9 @@ public class TransportManager { /** Transport has to be whitelisted and privileged. */ private boolean isTransportTrusted(ComponentName transport) { if (!mTransportWhitelist.contains(transport)) { Slog.w(TAG, "BackupTransport " + transport.flattenToShortString() + " not whitelisted."); Slog.w( TAG, "BackupTransport " + transport.flattenToShortString() + " not whitelisted."); return false; } try { Loading Loading @@ -588,7 +597,8 @@ public class TransportManager { Bundle extras = new Bundle(); extras.putBoolean(BackupTransport.EXTRA_TRANSPORT_REGISTRATION, true); TransportClient transportClient = mTransportClientManager.getTransportClient( TransportClient transportClient = mTransportClientManager.getTransportClient( transportComponent, extras, callerLogString); final IBackupTransport transport; try { Loading
services/robotests/src/com/android/server/backup/TransportManagerTest.java +138 −85 Original line number Diff line number Diff line Loading @@ -204,8 +204,10 @@ public class TransportManagerTest { verify(mTransportClientManager) .getTransportClient( eq(mTransportA1.getTransportComponent()), argThat(bundle -> bundle.getBoolean(BackupTransport.EXTRA_TRANSPORT_REGISTRATION)), argThat( bundle -> bundle.getBoolean( BackupTransport.EXTRA_TRANSPORT_REGISTRATION)), anyString()); } Loading @@ -224,11 +226,8 @@ public class TransportManagerTest { @Test public void testOnPackageRemoved_unregisterTransports() throws Exception { setUpPackage(PACKAGE_A, ApplicationInfo.PRIVATE_FLAG_PRIVILEGED); setUpPackage(PACKAGE_B, ApplicationInfo.PRIVATE_FLAG_PRIVILEGED); setUpTransports(mTransportA1, mTransportB1); TransportManager transportManager = createTransportManager(mTransportA1, mTransportB1); transportManager.registerTransports(); TransportManager transportManager = createTransportManagerWithRegisteredTransports(mTransportA1, mTransportB1); transportManager.onPackageRemoved(PACKAGE_A); Loading @@ -237,10 +236,8 @@ public class TransportManagerTest { @Test public void testOnPackageRemoved_whenUnknownPackage_nothingHappens() throws Exception { setUpPackage(PACKAGE_A, ApplicationInfo.PRIVATE_FLAG_PRIVILEGED); setUpTransports(mTransportA1); TransportManager transportManager = createTransportManager(mTransportA1); transportManager.registerTransports(); TransportManager transportManager = createTransportManagerWithRegisteredTransports(mTransportA1); transportManager.onPackageRemoved(PACKAGE_A + "unknown"); Loading @@ -250,10 +247,8 @@ public class TransportManagerTest { @Test public void testOnPackageChanged_whenOneComponentChanged_onlyOneTransportReRegistered() throws Exception { setUpPackage(PACKAGE_A, ApplicationInfo.PRIVATE_FLAG_PRIVILEGED); setUpTransports(mTransportA1, mTransportA2); TransportManager transportManager = createTransportManager(mTransportA1, mTransportA2); transportManager.registerTransports(); TransportManager transportManager = createTransportManagerWithRegisteredTransports(mTransportA1, mTransportA2); // Reset listener to verify calls after registerTransports() above reset(mListener); Loading @@ -270,10 +265,8 @@ public class TransportManagerTest { @Test public void testOnPackageChanged_whenNoComponentsChanged_doesNotRegisterTransports() throws Exception { setUpPackage(PACKAGE_A, ApplicationInfo.PRIVATE_FLAG_PRIVILEGED); setUpTransports(mTransportA1); TransportManager transportManager = createTransportManager(mTransportA1); transportManager.registerTransports(); TransportManager transportManager = createTransportManagerWithRegisteredTransports(mTransportA1); reset(mListener); transportManager.onPackageChanged(PACKAGE_A); Loading @@ -285,10 +278,8 @@ public class TransportManagerTest { @Test public void testOnPackageChanged_whenUnknownComponentChanged_noTransportsRegistered() throws Exception { setUpPackage(PACKAGE_A, ApplicationInfo.PRIVATE_FLAG_PRIVILEGED); setUpTransports(mTransportA1); TransportManager transportManager = createTransportManager(mTransportA1); transportManager.registerTransports(); TransportManager transportManager = createTransportManagerWithRegisteredTransports(mTransportA1); reset(mListener); transportManager.onPackageChanged(PACKAGE_A, PACKAGE_A + ".UnknownComponent"); Loading @@ -299,10 +290,8 @@ public class TransportManagerTest { @Test public void testOnPackageChanged_reRegisterTransports() throws Exception { setUpPackage(PACKAGE_A, ApplicationInfo.PRIVATE_FLAG_PRIVILEGED); setUpTransports(mTransportA1, mTransportA2); TransportManager transportManager = createTransportManager(mTransportA1, mTransportA2); transportManager.registerTransports(); TransportManager transportManager = createTransportManagerWithRegisteredTransports(mTransportA1, mTransportA2); reset(mListener); transportManager.onPackageChanged( Loading @@ -319,10 +308,8 @@ public class TransportManagerTest { @Test public void testRegisterAndSelectTransport_whenTransportRegistered() throws Exception { setUpPackage(PACKAGE_A, ApplicationInfo.PRIVATE_FLAG_PRIVILEGED); setUpTransports(mTransportA1); TransportManager transportManager = createTransportManager(null, mTransportA1); transportManager.registerTransports(); TransportManager transportManager = createTransportManagerWithRegisteredTransports(null, mTransportA1); ComponentName transportComponent = mTransportA1.getTransportComponent(); int result = transportManager.registerAndSelectTransport(transportComponent); Loading Loading @@ -357,10 +344,8 @@ public class TransportManagerTest { @Test public void testGetCurrentTransportName_whenSelectTransportNotCalled_returnsDefaultTransport() throws Exception { setUpPackage(PACKAGE_A, ApplicationInfo.PRIVATE_FLAG_PRIVILEGED); setUpTransports(mTransportA1, mTransportA2); TransportManager transportManager = createTransportManager(mTransportA1, mTransportA2); transportManager.registerTransports(); TransportManager transportManager = createTransportManagerWithRegisteredTransports(mTransportA1, mTransportA2); String currentTransportName = transportManager.getCurrentTransportName(); Loading @@ -370,10 +355,8 @@ public class TransportManagerTest { @Test public void testGetCurrentTransport_whenSelectTransportCalled_returnsSelectedTransport() throws Exception { setUpPackage(PACKAGE_A, ApplicationInfo.PRIVATE_FLAG_PRIVILEGED); setUpTransports(mTransportA1, mTransportA2); TransportManager transportManager = createTransportManager(mTransportA1, mTransportA2); transportManager.registerTransports(); TransportManager transportManager = createTransportManagerWithRegisteredTransports(mTransportA1, mTransportA2); transportManager.selectTransport(mTransportA2.transportName); String currentTransportName = transportManager.getCurrentTransportName(); Loading Loading @@ -412,10 +395,8 @@ public class TransportManagerTest { @Test public void testGetTransportClient_forRegisteredTransport() throws Exception { setUpPackage(PACKAGE_A, ApplicationInfo.PRIVATE_FLAG_PRIVILEGED); setUpTransports(mTransportA1, mTransportA2); TransportManager transportManager = createTransportManager(mTransportA1, mTransportA2); transportManager.registerTransports(); TransportManager transportManager = createTransportManagerWithRegisteredTransports(mTransportA1, mTransportA2); TransportClient transportClient = transportManager.getTransportClient(mTransportA1.transportName, "caller"); Loading @@ -427,10 +408,8 @@ public class TransportManagerTest { @Test public void testGetTransportClient_forOldNameOfTransportThatChangedName_returnsNull() throws Exception { setUpPackage(PACKAGE_A, ApplicationInfo.PRIVATE_FLAG_PRIVILEGED); setUpTransports(mTransportA1, mTransportA2); TransportManager transportManager = createTransportManager(mTransportA1, mTransportA2); transportManager.registerTransports(); TransportManager transportManager = createTransportManagerWithRegisteredTransports(mTransportA1, mTransportA2); transportManager.updateTransportAttributes( mTransportA1.getTransportComponent(), "newName", Loading @@ -446,12 +425,9 @@ public class TransportManagerTest { } @Test public void testGetTransportClient_forNewNameOfTransportThatChangedName_returnsCorrectly() throws Exception { setUpPackage(PACKAGE_A, ApplicationInfo.PRIVATE_FLAG_PRIVILEGED); setUpTransports(mTransportA1, mTransportA2); TransportManager transportManager = createTransportManager(mTransportA1, mTransportA2); transportManager.registerTransports(); public void testGetTransportClient_forNewNameOfTransportThatChangedName() throws Exception { TransportManager transportManager = createTransportManagerWithRegisteredTransports(mTransportA1, mTransportA2); transportManager.updateTransportAttributes( mTransportA1.getTransportComponent(), "newName", Loading @@ -466,12 +442,41 @@ public class TransportManagerTest { .isEqualTo(mTransportA1.getTransportComponent()); } @Test public void testGetCurrentTransportClient() throws Exception { TransportManager transportManager = createTransportManagerWithRegisteredTransports(mTransportA1, mTransportA2); TransportClient transportClient = transportManager.getCurrentTransportClient("caller"); assertThat(transportClient.getTransportComponent()) .isEqualTo(mTransportA1.getTransportComponent()); } @Test public void testGetCurrentTransportClient_whenNoTransportSelected() throws Exception { TransportManager transportManager = createTransportManagerWithRegisteredTransports(null, mTransportA1); expectThrows( IllegalStateException.class, () -> transportManager.getCurrentTransportClient("caller")); } @Test public void testGetCurrentTransportClientOrThrow_whenNoTransportSelected() throws Exception { TransportManager transportManager = createTransportManagerWithRegisteredTransports(null, mTransportA1); expectThrows( IllegalStateException.class, () -> transportManager.getCurrentTransportClientOrThrow("caller")); } @Test public void testGetTransportName_forTransportThatChangedName_returnsNewName() throws Exception { setUpPackage(PACKAGE_A, ApplicationInfo.PRIVATE_FLAG_PRIVILEGED); setUpTransports(mTransportA1, mTransportA2); TransportManager transportManager = createTransportManager(mTransportA1, mTransportA2); transportManager.registerTransports(); TransportManager transportManager = createTransportManagerWithRegisteredTransports(mTransportA1, mTransportA2); transportManager.updateTransportAttributes( mTransportA1.getTransportComponent(), "newName", Loading Loading @@ -505,35 +510,42 @@ public class TransportManagerTest { @Test public void testGetTransportAttributes_forRegisteredTransport_returnsCorrectValues() throws Exception { setUpPackage(PACKAGE_A, ApplicationInfo.PRIVATE_FLAG_PRIVILEGED); setUpTransports(mTransportA1); TransportManager transportManager = createTransportManager(mTransportA1); transportManager.registerTransports(); TransportManager transportManager = createTransportManagerWithRegisteredTransports(mTransportA1); Intent configurationIntent = transportManager.getTransportConfigurationIntent(mTransportA1.transportName); String destinationString = transportManager.getTransportCurrentDestinationString(mTransportA1.transportName); Intent dataManagementIntent = transportManager.getTransportDataManagementIntent(mTransportA1.transportName); String dataManagementLabel = transportManager.getTransportDataManagementLabel(mTransportA1.transportName); String transportDirName = transportManager.getTransportDirName(mTransportA1.transportName); String transportDirNameByComponent = transportManager.getTransportDirName(mTransportA1.getTransportComponent()); assertThat(configurationIntent).isEqualTo(mTransportA1.configurationIntent); assertThat(destinationString).isEqualTo(mTransportA1.currentDestinationString); assertThat(dataManagementIntent).isEqualTo(mTransportA1.dataManagementIntent); assertThat(dataManagementLabel).isEqualTo(mTransportA1.dataManagementLabel); assertThat(transportDirName).isEqualTo(mTransportA1.transportDirName); assertThat(transportDirNameByComponent).isEqualTo(mTransportA1.transportDirName); } @Test public void testGetTransportAttributes_forUnregisteredTransport_throws() throws Exception { setUpPackage(PACKAGE_A, ApplicationInfo.PRIVATE_FLAG_PRIVILEGED); setUpTransports(mTransportA1); TransportManager transportManager = createTransportManager(mTransportA1); transportManager.registerTransports(); TransportManager transportManager = createTransportManagerWithRegisteredTransports(mTransportA1); expectThrows( TransportNotRegisteredException.class, () -> transportManager.getTransportConfigurationIntent(mTransportA2.transportName)); expectThrows( TransportNotRegisteredException.class, () -> transportManager.getTransportCurrentDestinationString( mTransportA2.transportName)); expectThrows( TransportNotRegisteredException.class, () -> Loading @@ -545,48 +557,69 @@ public class TransportManagerTest { expectThrows( TransportNotRegisteredException.class, () -> transportManager.getTransportDirName(mTransportA2.transportName)); expectThrows( TransportNotRegisteredException.class, () -> transportManager.getTransportDirName(mTransportA2.getTransportComponent())); } @Test public void testGetRegisteredTransportNames() throws Exception { setUpPackage(PACKAGE_A, ApplicationInfo.PRIVATE_FLAG_PRIVILEGED); setUpPackage(PACKAGE_B, ApplicationInfo.PRIVATE_FLAG_PRIVILEGED); TransportData[] transportsData = {mTransportA1, mTransportA2, mTransportB1}; setUpTransports(transportsData); TransportManager transportManager = createTransportManager(mTransportA1, mTransportA2, mTransportB1); transportManager.registerTransports(); createTransportManagerWithRegisteredTransports( mTransportA1, mTransportA2, mTransportB1); String[] transportNames = transportManager.getRegisteredTransportNames(); assertThat(transportNames) .asList() .containsExactlyElementsIn( Stream.of(transportsData) Stream.of(mTransportA1, mTransportA2, mTransportB1) .map(transportData -> transportData.transportName) .collect(toList())); } @Test public void testGetRegisteredTransportComponents() throws Exception { setUpPackage(PACKAGE_A, ApplicationInfo.PRIVATE_FLAG_PRIVILEGED); setUpPackage(PACKAGE_B, ApplicationInfo.PRIVATE_FLAG_PRIVILEGED); TransportData[] transportsData = {mTransportA1, mTransportA2, mTransportB1}; setUpTransports(transportsData); TransportManager transportManager = createTransportManager(mTransportA1, mTransportA2, mTransportB1); transportManager.registerTransports(); createTransportManagerWithRegisteredTransports( mTransportA1, mTransportA2, mTransportB1); ComponentName[] transportNames = transportManager.getRegisteredTransportComponents(); assertThat(transportNames) .asList() .containsExactlyElementsIn( Stream.of(transportsData) Stream.of(mTransportA1, mTransportA2, mTransportB1) .map(TransportData::getTransportComponent) .collect(toList())); } @Test public void testForEachRegisteredTransport() throws Exception { TransportManager transportManager = createTransportManagerWithRegisteredTransports( mTransportA1, mTransportA2, mTransportB1); List<String> calls = new ArrayList<>(); transportManager.forEachRegisteredTransport(calls::add); assertThat(calls) .containsExactly( mTransportA1.transportName, mTransportA2.transportName, mTransportB1.transportName); } @Test public void testForEachRegisteredTransport_whenNoTransportsRegistered() throws Exception { TransportManager transportManager = createTransportManagerWithRegisteredTransports(null); List<String> calls = new ArrayList<>(); transportManager.forEachRegisteredTransport(calls::add); assertThat(calls).isEmpty(); } private List<TransportMock> setUpTransports(TransportData... transports) throws Exception { setUpTransportsForTransportManager(mShadowPackageManager, transports); List<TransportMock> transportMocks = new ArrayList<>(transports.length); Loading @@ -611,23 +644,43 @@ public class TransportManagerTest { mShadowPackageManager.addPackage(packageInfo); } private TransportManager createTransportManagerWithRegisteredTransports( @Nullable TransportData selectedTransport, TransportData... transports) throws Exception { Set<TransportData> transportSet = merge(selectedTransport, transports); for (TransportData transport : transportSet) { setUpPackage( transport.getTransportComponent().getPackageName(), ApplicationInfo.PRIVATE_FLAG_PRIVILEGED); } setUpTransports(transportSet.toArray(new TransportData[transportSet.size()])); TransportManager transportManager = createTransportManager(selectedTransport, transports); transportManager.registerTransports(); return transportManager; } private TransportManager createTransportManager( @Nullable TransportData selectedTransport, TransportData... transports) { Set<ComponentName> whitelist = concat(Stream.of(selectedTransport), Stream.of(transports)) .filter(Objects::nonNull) .map(TransportData::getTransportComponent) .collect(toSet()); TransportManager transportManager = new TransportManager( mContext, whitelist, merge(selectedTransport, transports) .stream() .map(TransportData::getTransportComponent) .collect(toSet()), selectedTransport != null ? selectedTransport.transportName : null, mTransportClientManager); transportManager.setOnTransportRegisteredListener(mListener); return transportManager; } private Set<TransportData> merge( @Nullable TransportData selectedTransport, TransportData... transports) { return concat(Stream.of(selectedTransport), Stream.of(transports)) .filter(Objects::nonNull) .collect(toSet()); } private void assertRegisteredTransports( TransportManager transportManager, List<TransportData> transports) { assertThat(transportManager.getRegisteredTransportComponents()) Loading