Bernardo Rufino | fa51853 | 2018-01-02 16:01:53 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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.testing; |
| 18 | |
Bernardo Rufino | 41349c0 | 2018-01-10 21:09:28 +0000 | [diff] [blame] | 19 | import static com.android.server.backup.testing.TestUtils.uncheck; |
| 20 | |
| 21 | import static org.junit.Assert.fail; |
Bernardo Rufino | fa51853 | 2018-01-02 16:01:53 +0000 | [diff] [blame] | 22 | import static org.mockito.ArgumentMatchers.any; |
| 23 | import static org.mockito.ArgumentMatchers.eq; |
| 24 | import static org.mockito.Mockito.mock; |
| 25 | import static org.mockito.Mockito.when; |
| 26 | |
Bernardo Rufino | 41349c0 | 2018-01-10 21:09:28 +0000 | [diff] [blame] | 27 | import static java.util.stream.Collectors.toList; |
| 28 | |
Bernardo Rufino | fa51853 | 2018-01-02 16:01:53 +0000 | [diff] [blame] | 29 | import android.annotation.Nullable; |
Bernardo Rufino | 516ac957 | 2018-01-04 14:16:32 +0000 | [diff] [blame] | 30 | import android.content.ComponentName; |
Bernardo Rufino | 41349c0 | 2018-01-10 21:09:28 +0000 | [diff] [blame] | 31 | import android.content.Intent; |
| 32 | import android.content.pm.ResolveInfo; |
| 33 | import android.content.pm.ServiceInfo; |
| 34 | import android.os.RemoteException; |
| 35 | import android.support.annotation.IntDef; |
Bernardo Rufino | fa51853 | 2018-01-02 16:01:53 +0000 | [diff] [blame] | 36 | |
| 37 | import com.android.internal.backup.IBackupTransport; |
| 38 | import com.android.server.backup.TransportManager; |
| 39 | import com.android.server.backup.transport.TransportClient; |
| 40 | import com.android.server.backup.transport.TransportNotAvailableException; |
Bernardo Rufino | 516ac957 | 2018-01-04 14:16:32 +0000 | [diff] [blame] | 41 | import com.android.server.backup.transport.TransportNotRegisteredException; |
Bernardo Rufino | fa51853 | 2018-01-02 16:01:53 +0000 | [diff] [blame] | 42 | |
Bernardo Rufino | 41349c0 | 2018-01-10 21:09:28 +0000 | [diff] [blame] | 43 | import org.robolectric.shadows.ShadowPackageManager; |
| 44 | |
Bernardo Rufino | fa51853 | 2018-01-02 16:01:53 +0000 | [diff] [blame] | 45 | import java.util.List; |
Bernardo Rufino | 41349c0 | 2018-01-10 21:09:28 +0000 | [diff] [blame] | 46 | import java.util.stream.Stream; |
Bernardo Rufino | fa51853 | 2018-01-02 16:01:53 +0000 | [diff] [blame] | 47 | |
| 48 | public class TransportTestUtils { |
Bernardo Rufino | 41349c0 | 2018-01-10 21:09:28 +0000 | [diff] [blame] | 49 | /** |
| 50 | * Differently from {@link #setUpTransports(TransportManager, TransportData...)}, which |
| 51 | * configures {@link TransportManager}, this is meant to mock the environment for a real |
| 52 | * TransportManager. |
| 53 | */ |
| 54 | public static void setUpTransportsForTransportManager( |
| 55 | ShadowPackageManager shadowPackageManager, TransportData... transports) |
| 56 | throws Exception { |
| 57 | for (TransportData transport : transports) { |
| 58 | ComponentName transportComponent = transport.getTransportComponent(); |
| 59 | String packageName = transportComponent.getPackageName(); |
| 60 | ResolveInfo resolveInfo = resolveInfo(transportComponent); |
| 61 | shadowPackageManager.addResolveInfoForIntent(transportIntent(), resolveInfo); |
| 62 | shadowPackageManager.addResolveInfoForIntent( |
| 63 | transportIntent().setPackage(packageName), resolveInfo); |
| 64 | } |
| 65 | } |
Bernardo Rufino | fa51853 | 2018-01-02 16:01:53 +0000 | [diff] [blame] | 66 | |
Bernardo Rufino | 41349c0 | 2018-01-10 21:09:28 +0000 | [diff] [blame] | 67 | private static Intent transportIntent() { |
| 68 | return new Intent(TransportManager.SERVICE_ACTION_TRANSPORT_HOST); |
| 69 | } |
Bernardo Rufino | fa51853 | 2018-01-02 16:01:53 +0000 | [diff] [blame] | 70 | |
Bernardo Rufino | 41349c0 | 2018-01-10 21:09:28 +0000 | [diff] [blame] | 71 | private static ResolveInfo resolveInfo(ComponentName transportComponent) { |
| 72 | ResolveInfo resolveInfo = new ResolveInfo(); |
| 73 | resolveInfo.serviceInfo = new ServiceInfo(); |
| 74 | resolveInfo.serviceInfo.packageName = transportComponent.getPackageName(); |
| 75 | resolveInfo.serviceInfo.name = transportComponent.getClassName(); |
| 76 | return resolveInfo; |
Bernardo Rufino | fa51853 | 2018-01-02 16:01:53 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Bernardo Rufino | 516ac957 | 2018-01-04 14:16:32 +0000 | [diff] [blame] | 79 | /** {@code transportName} has to be in the {@link ComponentName} format (with '/') */ |
Bernardo Rufino | 41349c0 | 2018-01-10 21:09:28 +0000 | [diff] [blame] | 80 | public static TransportMock setUpCurrentTransport( |
| 81 | TransportManager transportManager, TransportData transport) throws Exception { |
| 82 | TransportMock transportMock = setUpTransports(transportManager, transport).get(0); |
| 83 | if (transportMock.transportClient != null) { |
| 84 | when(transportManager.getCurrentTransportClient(any())) |
| 85 | .thenReturn(transportMock.transportClient); |
| 86 | } |
| 87 | return transportMock; |
Bernardo Rufino | fa51853 | 2018-01-02 16:01:53 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | /** @see #setUpTransport(TransportManager, TransportData) */ |
Bernardo Rufino | 41349c0 | 2018-01-10 21:09:28 +0000 | [diff] [blame] | 91 | public static List<TransportMock> setUpTransports( |
Bernardo Rufino | fa51853 | 2018-01-02 16:01:53 +0000 | [diff] [blame] | 92 | TransportManager transportManager, TransportData... transports) throws Exception { |
Bernardo Rufino | 41349c0 | 2018-01-10 21:09:28 +0000 | [diff] [blame] | 93 | return Stream.of(transports) |
| 94 | .map(transport -> uncheck(() -> setUpTransport(transportManager, transport))) |
| 95 | .collect(toList()); |
Bernardo Rufino | fa51853 | 2018-01-02 16:01:53 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Bernardo Rufino | 41349c0 | 2018-01-10 21:09:28 +0000 | [diff] [blame] | 98 | public static TransportMock setUpTransport( |
| 99 | TransportManager transportManager, TransportData transport) throws Exception { |
| 100 | int status = transport.transportStatus; |
Bernardo Rufino | fa51853 | 2018-01-02 16:01:53 +0000 | [diff] [blame] | 101 | String transportName = transport.transportName; |
Bernardo Rufino | 41349c0 | 2018-01-10 21:09:28 +0000 | [diff] [blame] | 102 | ComponentName transportComponent = transport.getTransportComponent(); |
| 103 | String transportDirName = transport.transportDirName; |
Bernardo Rufino | fa51853 | 2018-01-02 16:01:53 +0000 | [diff] [blame] | 104 | |
Bernardo Rufino | 41349c0 | 2018-01-10 21:09:28 +0000 | [diff] [blame] | 105 | TransportMock transportMock = mockTransport(transport); |
| 106 | if (status == TransportStatus.REGISTERED_AVAILABLE |
| 107 | || status == TransportStatus.REGISTERED_UNAVAILABLE) { |
Bernardo Rufino | 516ac957 | 2018-01-04 14:16:32 +0000 | [diff] [blame] | 108 | // Transport registered |
| 109 | when(transportManager.getTransportClient(eq(transportName), any())) |
Bernardo Rufino | 41349c0 | 2018-01-10 21:09:28 +0000 | [diff] [blame] | 110 | .thenReturn(transportMock.transportClient); |
Bernardo Rufino | 516ac957 | 2018-01-04 14:16:32 +0000 | [diff] [blame] | 111 | when(transportManager.getTransportClientOrThrow(eq(transportName), any())) |
Bernardo Rufino | 41349c0 | 2018-01-10 21:09:28 +0000 | [diff] [blame] | 112 | .thenReturn(transportMock.transportClient); |
Bernardo Rufino | 516ac957 | 2018-01-04 14:16:32 +0000 | [diff] [blame] | 113 | when(transportManager.getTransportName(transportComponent)).thenReturn(transportName); |
| 114 | when(transportManager.getTransportDirName(eq(transportName))) |
| 115 | .thenReturn(transportDirName); |
| 116 | when(transportManager.getTransportDirName(eq(transportComponent))) |
| 117 | .thenReturn(transportDirName); |
Bernardo Rufino | 41349c0 | 2018-01-10 21:09:28 +0000 | [diff] [blame] | 118 | // TODO: Mock rest of description methods |
Bernardo Rufino | 516ac957 | 2018-01-04 14:16:32 +0000 | [diff] [blame] | 119 | } else { |
| 120 | // Transport not registered |
| 121 | when(transportManager.getTransportClient(eq(transportName), any())).thenReturn(null); |
| 122 | when(transportManager.getTransportClientOrThrow(eq(transportName), any())) |
| 123 | .thenThrow(TransportNotRegisteredException.class); |
| 124 | when(transportManager.getTransportName(transportComponent)) |
| 125 | .thenThrow(TransportNotRegisteredException.class); |
| 126 | when(transportManager.getTransportDirName(eq(transportName))) |
| 127 | .thenThrow(TransportNotRegisteredException.class); |
| 128 | when(transportManager.getTransportDirName(eq(transportComponent))) |
| 129 | .thenThrow(TransportNotRegisteredException.class); |
Bernardo Rufino | fa51853 | 2018-01-02 16:01:53 +0000 | [diff] [blame] | 130 | } |
Bernardo Rufino | 41349c0 | 2018-01-10 21:09:28 +0000 | [diff] [blame] | 131 | return transportMock; |
Bernardo Rufino | fa51853 | 2018-01-02 16:01:53 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Bernardo Rufino | 41349c0 | 2018-01-10 21:09:28 +0000 | [diff] [blame] | 134 | public static TransportMock mockTransport(TransportData transport) throws Exception { |
| 135 | final TransportClient transportClientMock; |
| 136 | int status = transport.transportStatus; |
| 137 | ComponentName transportComponent = transport.getTransportComponent(); |
| 138 | if (status == TransportStatus.REGISTERED_AVAILABLE |
| 139 | || status == TransportStatus.REGISTERED_UNAVAILABLE) { |
| 140 | // Transport registered |
| 141 | transportClientMock = mock(TransportClient.class); |
| 142 | when(transportClientMock.getTransportComponent()).thenReturn(transportComponent); |
| 143 | if (status == TransportStatus.REGISTERED_AVAILABLE) { |
| 144 | // Transport registered and available |
| 145 | IBackupTransport transportMock = mockTransportBinder(transport); |
| 146 | when(transportClientMock.connectOrThrow(any())).thenReturn(transportMock); |
Robert Berry | 39194c0 | 2018-01-11 13:50:56 +0000 | [diff] [blame] | 147 | when(transportClientMock.connect(any())).thenReturn(transportMock); |
Bernardo Rufino | 516ac957 | 2018-01-04 14:16:32 +0000 | [diff] [blame] | 148 | |
Bernardo Rufino | 41349c0 | 2018-01-10 21:09:28 +0000 | [diff] [blame] | 149 | return new TransportMock(transportClientMock, transportMock); |
| 150 | } else { |
| 151 | // Transport registered but unavailable |
| 152 | when(transportClientMock.connectOrThrow(any())) |
| 153 | .thenThrow(TransportNotAvailableException.class); |
Robert Berry | 39194c0 | 2018-01-11 13:50:56 +0000 | [diff] [blame] | 154 | when(transportClientMock.connect(any())).thenReturn(null); |
Bernardo Rufino | fa51853 | 2018-01-02 16:01:53 +0000 | [diff] [blame] | 155 | |
Bernardo Rufino | 41349c0 | 2018-01-10 21:09:28 +0000 | [diff] [blame] | 156 | return new TransportMock(transportClientMock, null); |
| 157 | } |
| 158 | } else { |
| 159 | // Transport not registered |
| 160 | return new TransportMock(null, null); |
Bernardo Rufino | fa51853 | 2018-01-02 16:01:53 +0000 | [diff] [blame] | 161 | } |
Bernardo Rufino | 41349c0 | 2018-01-10 21:09:28 +0000 | [diff] [blame] | 162 | } |
Bernardo Rufino | fa51853 | 2018-01-02 16:01:53 +0000 | [diff] [blame] | 163 | |
Bernardo Rufino | 41349c0 | 2018-01-10 21:09:28 +0000 | [diff] [blame] | 164 | private static IBackupTransport mockTransportBinder(TransportData transport) throws Exception { |
| 165 | IBackupTransport transportBinder = mock(IBackupTransport.class); |
| 166 | try { |
| 167 | when(transportBinder.name()).thenReturn(transport.transportName); |
| 168 | when(transportBinder.transportDirName()).thenReturn(transport.transportDirName); |
| 169 | when(transportBinder.configurationIntent()).thenReturn(transport.configurationIntent); |
| 170 | when(transportBinder.currentDestinationString()) |
| 171 | .thenReturn(transport.currentDestinationString); |
| 172 | when(transportBinder.dataManagementIntent()).thenReturn(transport.dataManagementIntent); |
| 173 | when(transportBinder.dataManagementLabel()).thenReturn(transport.dataManagementLabel); |
| 174 | } catch (RemoteException e) { |
| 175 | fail("RemoteException?"); |
Bernardo Rufino | fa51853 | 2018-01-02 16:01:53 +0000 | [diff] [blame] | 176 | } |
Bernardo Rufino | 41349c0 | 2018-01-10 21:09:28 +0000 | [diff] [blame] | 177 | return transportBinder; |
| 178 | } |
| 179 | |
| 180 | public static class TransportMock { |
| 181 | @Nullable public final TransportClient transportClient; |
| 182 | @Nullable public final IBackupTransport transport; |
| 183 | |
| 184 | private TransportMock( |
| 185 | @Nullable TransportClient transportClient, @Nullable IBackupTransport transport) { |
| 186 | this.transportClient = transportClient; |
| 187 | this.transport = transport; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | @IntDef({ |
| 192 | TransportStatus.REGISTERED_AVAILABLE, |
| 193 | TransportStatus.REGISTERED_UNAVAILABLE, |
| 194 | TransportStatus.UNREGISTERED |
| 195 | }) |
| 196 | public @interface TransportStatus { |
| 197 | int REGISTERED_AVAILABLE = 0; |
| 198 | int REGISTERED_UNAVAILABLE = 1; |
| 199 | int UNREGISTERED = 2; |
Bernardo Rufino | fa51853 | 2018-01-02 16:01:53 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | private TransportTestUtils() {} |
| 203 | } |