diff --git a/src/java/com/google/devtools/mobileharness/api/model/error/AndroidErrorId.java b/src/java/com/google/devtools/mobileharness/api/model/error/AndroidErrorId.java index 2a0bd9a78a..1fe80faf29 100644 --- a/src/java/com/google/devtools/mobileharness/api/model/error/AndroidErrorId.java +++ b/src/java/com/google/devtools/mobileharness/api/model/error/AndroidErrorId.java @@ -711,6 +711,9 @@ public enum AndroidErrorId implements ErrorId { // AndroidSetWifiDecorator: 129_201 - 129_250 ANDROID_SET_WIFI_DECORATOR_GET_DEFAULT_SSID_ERROR(129_201, ErrorType.CUSTOMER_ISSUE), ANDROID_SET_WIFI_DECORATOR_SSID_NOT_PRESENT_ERROR(129_202, ErrorType.CUSTOMER_ISSUE), + ANDROID_SET_WIFI_DECORATOR_WIFI_CONNECT_ERROR(129_203, ErrorType.INFRA_ISSUE), + + // [AndroidSmsDecorator Error: 129301 ~ 129400] // AndroidDeviceFeaturesCheckDecorator: 129_351 ~ 129_400 ANDROID_DEVICE_FEATURES_CHECK_DECORATOR_CEHCK_FAILURE(129_351, ErrorType.DEPENDENCY_ISSUE), diff --git a/src/java/com/google/devtools/mobileharness/api/model/error/InfraErrorId.java b/src/java/com/google/devtools/mobileharness/api/model/error/InfraErrorId.java index 2cdef158d2..8ea9908590 100644 --- a/src/java/com/google/devtools/mobileharness/api/model/error/InfraErrorId.java +++ b/src/java/com/google/devtools/mobileharness/api/model/error/InfraErrorId.java @@ -189,6 +189,8 @@ public enum InfraErrorId implements ErrorId { LAB_RPC_DEVICE_OPS_RUN_TROUBLESHOOT_SCRIPT_GRPC_ERROR(40_488, ErrorType.UNDETERMINED), LAB_RPC_DEVICE_OPS_RUN_TROUBLESHOOT_SCRIPT_STUBBY_ERROR(40_489, ErrorType.UNDETERMINED), LAB_RPC_DEVICE_OPS_TROUBLESHOOT_SCRIPT_PRECONDITION_FAILED(40_490, ErrorType.CUSTOMER_ISSUE), + LAB_RPC_DEVICE_OPS_CONNECT_TO_DEFAULT_WIFI_GRPC_ERROR(40_495, ErrorType.UNDETERMINED), + LAB_RPC_DEVICE_OPS_CONNECT_TO_DEFAULT_WIFI_STUBBY_ERROR(40_496, ErrorType.UNDETERMINED), // Test engine/container: 40_701 ~ 40_800 TE_CREATE_DEVICE_HELPER_CONTAINER_DOES_NOT_HAVE(40_701, ErrorType.INFRA_ISSUE), diff --git a/src/java/com/google/devtools/mobileharness/infra/client/api/util/stub/BUILD b/src/java/com/google/devtools/mobileharness/infra/client/api/util/stub/BUILD index 982b81a378..71910b73da 100644 --- a/src/java/com/google/devtools/mobileharness/infra/client/api/util/stub/BUILD +++ b/src/java/com/google/devtools/mobileharness/infra/client/api/util/stub/BUILD @@ -29,10 +29,13 @@ java_library( srcs = ["GrpcStubManager.java"], visibility = [ "//src/java/com/google/wireless/qa/mobileharness/client/api/util/stub:__pkg__", + "//src/java/com/google/wireless/qa/mobileharness/shared/api/decorator:__pkg__", ], deps = [ + "//src/java/com/google/devtools/mobileharness/infra/lab/rpc/stub:device_ops", "//src/java/com/google/devtools/mobileharness/infra/lab/rpc/stub:exec_test", "//src/java/com/google/devtools/mobileharness/infra/lab/rpc/stub:prepare_test", + "//src/java/com/google/devtools/mobileharness/infra/lab/rpc/stub/grpc:device_ops_grpc_stub", "//src/java/com/google/devtools/mobileharness/infra/lab/rpc/stub/grpc:exec_test_grpc_stub", "//src/java/com/google/devtools/mobileharness/infra/lab/rpc/stub/grpc:prepare_test_grpc_stub", "//src/java/com/google/devtools/mobileharness/shared/util/comm/stub:channel_factory", diff --git a/src/java/com/google/devtools/mobileharness/infra/client/api/util/stub/GrpcStubManager.java b/src/java/com/google/devtools/mobileharness/infra/client/api/util/stub/GrpcStubManager.java index 62b9928ea7..7d3077712b 100644 --- a/src/java/com/google/devtools/mobileharness/infra/client/api/util/stub/GrpcStubManager.java +++ b/src/java/com/google/devtools/mobileharness/infra/client/api/util/stub/GrpcStubManager.java @@ -16,8 +16,10 @@ package com.google.devtools.mobileharness.infra.client.api.util.stub; +import com.google.devtools.mobileharness.infra.lab.rpc.stub.DeviceOpsStub; import com.google.devtools.mobileharness.infra.lab.rpc.stub.ExecTestStub; import com.google.devtools.mobileharness.infra.lab.rpc.stub.PrepareTestStub; +import com.google.devtools.mobileharness.infra.lab.rpc.stub.grpc.DeviceOpsGrpcStub; import com.google.devtools.mobileharness.infra.lab.rpc.stub.grpc.ExecTestGrpcStub; import com.google.devtools.mobileharness.infra.lab.rpc.stub.grpc.PrepareTestGrpcStub; import com.google.devtools.mobileharness.shared.util.comm.stub.ChannelManager; @@ -61,4 +63,9 @@ public ExecTestStub getExecTestGrpcStub(String grpcTarget) { return channelManager.createStub( grpcTarget, () -> managedChannelSupplier.apply(grpcTarget), ExecTestGrpcStub::new); } + + public DeviceOpsStub getDeviceOpsStub(String grpcTarget) { + return channelManager.createStub( + grpcTarget, () -> managedChannelSupplier.apply(grpcTarget), DeviceOpsGrpcStub::new); + } } diff --git a/src/java/com/google/devtools/mobileharness/infra/lab/rpc/stub/DeviceOpsStub.java b/src/java/com/google/devtools/mobileharness/infra/lab/rpc/stub/DeviceOpsStub.java index 098636458a..0adbd35b0b 100644 --- a/src/java/com/google/devtools/mobileharness/infra/lab/rpc/stub/DeviceOpsStub.java +++ b/src/java/com/google/devtools/mobileharness/infra/lab/rpc/stub/DeviceOpsStub.java @@ -19,6 +19,8 @@ import com.google.common.util.concurrent.ListenableFuture; import com.google.devtools.common.metrics.stability.rpc.RpcExceptionWithErrorId; import com.google.devtools.mobileharness.shared.constant.closeable.NonThrowingAutoCloseable; +import com.google.wireless.qa.mobileharness.lab.proto.DeviceOpsServ.ConnectToDefaultWifiRequest; +import com.google.wireless.qa.mobileharness.lab.proto.DeviceOpsServ.ConnectToDefaultWifiResponse; import com.google.wireless.qa.mobileharness.lab.proto.DeviceOpsServ.GetDeviceDebugInfoRequest; import com.google.wireless.qa.mobileharness.lab.proto.DeviceOpsServ.GetDeviceDebugInfoResponse; import com.google.wireless.qa.mobileharness.lab.proto.DeviceOpsServ.GetDeviceLogRequest; @@ -98,4 +100,25 @@ default RunTroubleshootScriptResponse runTroubleshootScript( /** Runs a troubleshoot script asynchronously. */ ListenableFuture runTroubleshootScriptAsync( RunTroubleshootScriptRequest request); + + default ConnectToDefaultWifiResponse connectToDefaultWifi(ConnectToDefaultWifiRequest request) + throws RpcExceptionWithErrorId { + throw new UnsupportedOperationException(); + } + + default ConnectToDefaultWifiResponse connectToDefaultWifi( + ConnectToDefaultWifiRequest request, @Nullable String impersonationUser) + throws RpcExceptionWithErrorId { + return connectToDefaultWifi(request); + } + + default ListenableFuture connectToDefaultWifiAsync( + ConnectToDefaultWifiRequest request) { + throw new UnsupportedOperationException(); + } + + default ListenableFuture connectToDefaultWifiAsync( + ConnectToDefaultWifiRequest request, boolean useClientRpcAuthority) { + return connectToDefaultWifiAsync(request); + } } diff --git a/src/java/com/google/devtools/mobileharness/infra/lab/rpc/stub/grpc/BUILD b/src/java/com/google/devtools/mobileharness/infra/lab/rpc/stub/grpc/BUILD index d46ce77bcf..7122628a27 100644 --- a/src/java/com/google/devtools/mobileharness/infra/lab/rpc/stub/grpc/BUILD +++ b/src/java/com/google/devtools/mobileharness/infra/lab/rpc/stub/grpc/BUILD @@ -64,6 +64,7 @@ java_library( "//src/java/com/google/wireless/qa/mobileharness/lab/proto:device_ops_serv_grpc", "//src/java/com/google/wireless/qa/mobileharness/lab/proto:device_ops_serv_java_proto", "@grpc-java//core", + "@maven//:com_google_code_findbugs_jsr305", "@maven//:com_google_guava_guava", ], ) diff --git a/src/java/com/google/devtools/mobileharness/infra/lab/rpc/stub/grpc/DeviceOpsGrpcStub.java b/src/java/com/google/devtools/mobileharness/infra/lab/rpc/stub/grpc/DeviceOpsGrpcStub.java index 7dc3b0c418..40e0fd170e 100644 --- a/src/java/com/google/devtools/mobileharness/infra/lab/rpc/stub/grpc/DeviceOpsGrpcStub.java +++ b/src/java/com/google/devtools/mobileharness/infra/lab/rpc/stub/grpc/DeviceOpsGrpcStub.java @@ -22,6 +22,8 @@ import com.google.devtools.mobileharness.api.model.error.InfraErrorId; import com.google.devtools.mobileharness.infra.lab.rpc.stub.DeviceOpsStub; import com.google.devtools.mobileharness.shared.util.comm.stub.GrpcDirectTargetConfigures; +import com.google.wireless.qa.mobileharness.lab.proto.DeviceOpsServ.ConnectToDefaultWifiRequest; +import com.google.wireless.qa.mobileharness.lab.proto.DeviceOpsServ.ConnectToDefaultWifiResponse; import com.google.wireless.qa.mobileharness.lab.proto.DeviceOpsServ.GetDeviceDebugInfoRequest; import com.google.wireless.qa.mobileharness.lab.proto.DeviceOpsServ.GetDeviceDebugInfoResponse; import com.google.wireless.qa.mobileharness.lab.proto.DeviceOpsServ.GetDeviceLogRequest; @@ -32,6 +34,7 @@ import com.google.wireless.qa.mobileharness.lab.proto.DeviceOpsServ.TakeScreenshotResponse; import com.google.wireless.qa.mobileharness.lab.proto.DeviceOpsServiceGrpc; import io.grpc.Channel; +import javax.annotation.Nullable; /** gRPC stub of {@code DeviceOpsService}. */ public class DeviceOpsGrpcStub implements DeviceOpsStub { @@ -141,6 +144,39 @@ public ListenableFuture runTroubleshootScriptAsyn return futureStub.runTroubleshootScript(request); } + @Override + public ConnectToDefaultWifiResponse connectToDefaultWifi(ConnectToDefaultWifiRequest request) + throws RpcExceptionWithErrorId { + return GrpcStubUtil.invoke( + stub::connectToDefaultWifi, + request, + InfraErrorId.LAB_RPC_DEVICE_OPS_CONNECT_TO_DEFAULT_WIFI_GRPC_ERROR, + "Failed to connect to default wifi"); + } + + @Override + public ConnectToDefaultWifiResponse connectToDefaultWifi( + ConnectToDefaultWifiRequest request, @Nullable String impersonationUser) + throws RpcExceptionWithErrorId { + return connectToDefaultWifi(request); + } + + @Override + public ListenableFuture connectToDefaultWifiAsync( + ConnectToDefaultWifiRequest request) { + return futureStub.connectToDefaultWifi(request); + } + + @Override + public ListenableFuture connectToDefaultWifiAsync( + ConnectToDefaultWifiRequest request, boolean useClientRpcAuthority) { + if (useClientRpcAuthority) { + throw new UnsupportedOperationException( + "useClientRpcAuthority is not supported in gRPC stub"); + } + return connectToDefaultWifiAsync(request); + } + @Override public void close() { // This stub is not responsible for managing lifecycle of the channel. @@ -155,6 +191,8 @@ public static interface BlockingInterface { GetDeviceDebugInfoResponse getDeviceDebugInfo(GetDeviceDebugInfoRequest request); RunTroubleshootScriptResponse runTroubleshootScript(RunTroubleshootScriptRequest request); + + ConnectToDefaultWifiResponse connectToDefaultWifi(ConnectToDefaultWifiRequest request); } /** Interface for {@link DeviceOpsServiceFutureStub} */ @@ -168,6 +206,9 @@ ListenableFuture getDeviceDebugInfo( ListenableFuture runTroubleshootScript( RunTroubleshootScriptRequest request); + + ListenableFuture connectToDefaultWifi( + ConnectToDefaultWifiRequest request); } public static BlockingInterface newBlockingInterface(Channel channel) { diff --git a/src/java/com/google/wireless/qa/mobileharness/lab/proto/device_ops_serv.proto b/src/java/com/google/wireless/qa/mobileharness/lab/proto/device_ops_serv.proto index 985d6bd185..640c9331b6 100644 --- a/src/java/com/google/wireless/qa/mobileharness/lab/proto/device_ops_serv.proto +++ b/src/java/com/google/wireless/qa/mobileharness/lab/proto/device_ops_serv.proto @@ -93,6 +93,15 @@ message RunTroubleshootScriptResponse { optional string stderr = 3; } +message ConnectToDefaultWifiRequest { + optional string device_id = 1; +} + +message ConnectToDefaultWifiResponse { + optional bool success = 1; + optional string error_message = 2; +} + // Lab service for FE to take the screenshot. service DeviceOpsService { // Sends the screenshot file info to the lab. @@ -109,4 +118,8 @@ service DeviceOpsService { // Runs a troubleshooting script on the lab host. rpc RunTroubleshootScript(RunTroubleshootScriptRequest) returns (RunTroubleshootScriptResponse) {} + + // Connects the device to the default lab wifi network. + rpc ConnectToDefaultWifi(ConnectToDefaultWifiRequest) + returns (ConnectToDefaultWifiResponse) {} } diff --git a/src/java/com/google/wireless/qa/mobileharness/shared/api/decorator/AndroidSetWifiDecorator.java b/src/java/com/google/wireless/qa/mobileharness/shared/api/decorator/AndroidSetWifiDecorator.java index dcc96137b8..c24628ea45 100644 --- a/src/java/com/google/wireless/qa/mobileharness/shared/api/decorator/AndroidSetWifiDecorator.java +++ b/src/java/com/google/wireless/qa/mobileharness/shared/api/decorator/AndroidSetWifiDecorator.java @@ -17,20 +17,24 @@ package com.google.wireless.qa.mobileharness.shared.api.decorator; import com.google.common.annotations.VisibleForTesting; -import com.google.common.base.Ascii; import com.google.common.base.Strings; import com.google.common.flogger.FluentLogger; +import com.google.devtools.common.metrics.stability.rpc.RpcExceptionWithErrorId; import com.google.devtools.mobileharness.api.model.error.AndroidErrorId; import com.google.devtools.mobileharness.api.model.error.MobileHarnessException; +import com.google.devtools.mobileharness.infra.client.api.util.stub.GrpcStubManager; +import com.google.devtools.mobileharness.infra.lab.rpc.stub.DeviceOpsStub; import com.google.devtools.mobileharness.platform.android.lightning.networkconnector.NetworkConnector; import com.google.devtools.mobileharness.platform.android.lightning.networkconnector.WifiConnectArgs; +import com.google.devtools.mobileharness.shared.util.flags.Flags; +import com.google.wireless.qa.mobileharness.lab.proto.DeviceOpsServ.ConnectToDefaultWifiRequest; +import com.google.wireless.qa.mobileharness.lab.proto.DeviceOpsServ.ConnectToDefaultWifiResponse; import com.google.wireless.qa.mobileharness.shared.api.annotation.DecoratorAnnotation; import com.google.wireless.qa.mobileharness.shared.api.decorator.base.LifecycleDecorator.SetupContext; import com.google.wireless.qa.mobileharness.shared.api.decorator.base.LifecycleDecorator.SetupResult; import com.google.wireless.qa.mobileharness.shared.api.decorator.base.SetupOnlyDecorator; import com.google.wireless.qa.mobileharness.shared.api.device.Device; import com.google.wireless.qa.mobileharness.shared.api.driver.Driver; -import com.google.wireless.qa.mobileharness.shared.constant.PropertyName.Test; import com.google.wireless.qa.mobileharness.shared.model.job.TestInfo; import com.google.wireless.qa.mobileharness.shared.model.job.in.spec.SpecConfigable; import com.google.wireless.qa.mobileharness.shared.proto.spec.decorator.AndroidSetWifiDecoratorSpec; @@ -61,6 +65,11 @@ public AndroidSetWifiDecorator(Driver decoratedDriver, TestInfo testInfo) { this.networkConnector = networkConnector; } + @VisibleForTesting + DeviceOpsStub getDeviceOpsStub(String labIp, int rpcPort) { + return GrpcStubManager.getInstance().getDeviceOpsStub(labIp + ":" + rpcPort); + } + @Override protected SetupResult setUp(SetupContext context) throws MobileHarnessException, InterruptedException { @@ -74,39 +83,32 @@ protected SetupResult setUp(SetupContext context) int retryNum = spec.getWifiRetryNum(); boolean wifiSsidOptional = spec.getWifiSsidOptional(); if (spec.getUseDefaultSsid()) { - // Get the wifi config from the device property. - wifiSsid = - device.getProperty( - Ascii.toLowerCase(Test.AndroidSetWifiDecorator.DEFAULT_WIFI_SSID.name())); - wifiPsk = - device.getProperty( - Ascii.toLowerCase(Test.AndroidSetWifiDecorator.DEFAULT_WIFI_PSK.name())); - if (Strings.isNullOrEmpty(wifiSsid)) { - if (!wifiSsidOptional) { - // Failed to get default ssid for the device. - testInfo - .log() - .atInfo() - .alsoTo(logger) - .log( - "Could not get default ssid for the device %s from device properties. Have you" - + " set the default ssid in the device config? If you have reset the default" - + " wifi recently, it may have not taken effective.", - deviceId); + // Direct the lab server to connect to default wifi via gRPC. + String labIp = "localhost"; + int rpcPort = Flags.rpcPort.getNonNull(); + if (rpcPort > 0 && labIp != null && !labIp.isEmpty()) { + try { + DeviceOpsStub stub = getDeviceOpsStub(labIp, rpcPort); + ConnectToDefaultWifiResponse response = + stub.connectToDefaultWifi( + ConnectToDefaultWifiRequest.newBuilder().setDeviceId(deviceId).build()); + if (response.getSuccess()) { + return SetupResult.continueDecorated(); + } else { + throw new MobileHarnessException( + AndroidErrorId.ANDROID_SET_WIFI_DECORATOR_WIFI_CONNECT_ERROR, + "Failed to connect to default Wi-Fi: Lab Server returned failure."); + } + } catch (RpcExceptionWithErrorId e) { throw new MobileHarnessException( - AndroidErrorId.ANDROID_SET_WIFI_DECORATOR_GET_DEFAULT_SSID_ERROR, - "Failed to get default SSID for the device " + deviceId); - } else { - testInfo - .log() - .atWarning() - .alsoTo(logger) - .log( - "Could not get default ssid for the device %s from device properties. Skipping" - + " wifi setup.", - deviceId); - return SetupResult.continueDecorated(); + AndroidErrorId.ANDROID_SET_WIFI_DECORATOR_WIFI_CONNECT_ERROR, + "Failed to connect to default lab WiFi via gRPC for device " + deviceId, + e); } + } else { + throw new MobileHarnessException( + AndroidErrorId.ANDROID_SET_WIFI_DECORATOR_WIFI_CONNECT_ERROR, + "Lab server IP or RPC port is unavailable for use_default_ssid on device " + deviceId); } } else { // Get the wifi config from the spec. diff --git a/src/java/com/google/wireless/qa/mobileharness/shared/api/decorator/BUILD b/src/java/com/google/wireless/qa/mobileharness/shared/api/decorator/BUILD index e6d49b13af..71e9c1c72e 100644 --- a/src/java/com/google/wireless/qa/mobileharness/shared/api/decorator/BUILD +++ b/src/java/com/google/wireless/qa/mobileharness/shared/api/decorator/BUILD @@ -677,15 +677,19 @@ java_library( name = "android_set_wifi_decorator", srcs = ["AndroidSetWifiDecorator.java"], deps = [ + "//src/java/com/google/devtools/common/metrics/stability/rpc:exception", "//src/java/com/google/devtools/mobileharness/api/model/error", + "//src/java/com/google/devtools/mobileharness/infra/client/api/util/stub:grpc_stub_manager", + "//src/java/com/google/devtools/mobileharness/infra/lab/rpc/stub:device_ops", "//src/java/com/google/devtools/mobileharness/platform/android/lightning/networkconnector", + "//src/java/com/google/devtools/mobileharness/shared/util/flags", "//src/java/com/google/devtools/mobileharness/shared/util/logging:google_logger", + "//src/java/com/google/wireless/qa/mobileharness/lab/proto:device_ops_serv_java_proto", "//src/java/com/google/wireless/qa/mobileharness/shared/api/annotation", "//src/java/com/google/wireless/qa/mobileharness/shared/api/decorator/base:lifecycle_decorator", "//src/java/com/google/wireless/qa/mobileharness/shared/api/decorator/base:setup_only_decorator", "//src/java/com/google/wireless/qa/mobileharness/shared/api/device", "//src/java/com/google/wireless/qa/mobileharness/shared/api/driver", - "//src/java/com/google/wireless/qa/mobileharness/shared/constant:property", "//src/java/com/google/wireless/qa/mobileharness/shared/model/job", "//src/java/com/google/wireless/qa/mobileharness/shared/model/job/in/spec", "//src/java/com/google/wireless/qa/mobileharness/shared/proto/spec:android_set_wifi_decorator_spec_java_proto", diff --git a/src/javatests/com/google/devtools/mobileharness/shared/size/BinarySizeTest.java b/src/javatests/com/google/devtools/mobileharness/shared/size/BinarySizeTest.java index b5771dd268..82c4e65fec 100644 --- a/src/javatests/com/google/devtools/mobileharness/shared/size/BinarySizeTest.java +++ b/src/javatests/com/google/devtools/mobileharness/shared/size/BinarySizeTest.java @@ -59,9 +59,9 @@ public class BinarySizeTest { "ats_olc_server", 40_050_000L, "ats_olc_server_local_mode", - 45_250_000L, + 46_350_000L, "lab_server", - 46_550_000L, + 46_750_000L, "ats_console", 22_550_000L, "persistent_cache_manager", diff --git a/src/javatests/com/google/wireless/qa/mobileharness/shared/api/decorator/AndroidSetWifiDecoratorTest.java b/src/javatests/com/google/wireless/qa/mobileharness/shared/api/decorator/AndroidSetWifiDecoratorTest.java index 55e342758b..d786a31490 100644 --- a/src/javatests/com/google/wireless/qa/mobileharness/shared/api/decorator/AndroidSetWifiDecoratorTest.java +++ b/src/javatests/com/google/wireless/qa/mobileharness/shared/api/decorator/AndroidSetWifiDecoratorTest.java @@ -19,22 +19,27 @@ import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.assertThrows; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoInteractions; import static org.mockito.Mockito.when; -import com.google.common.base.Ascii; +import com.google.devtools.common.metrics.stability.rpc.RpcExceptionWithErrorId; import com.google.devtools.mobileharness.api.model.error.AndroidErrorId; import com.google.devtools.mobileharness.api.model.error.MobileHarnessException; import com.google.devtools.mobileharness.api.model.job.out.Result; import com.google.devtools.mobileharness.api.model.job.out.Warnings; +import com.google.devtools.mobileharness.infra.lab.rpc.stub.DeviceOpsStub; import com.google.devtools.mobileharness.platform.android.lightning.networkconnector.NetworkConnector; import com.google.devtools.mobileharness.platform.android.lightning.networkconnector.WifiConnectArgs; +import com.google.wireless.qa.mobileharness.lab.proto.DeviceOpsServ.ConnectToDefaultWifiResponse; import com.google.wireless.qa.mobileharness.shared.api.device.AndroidRealDevice; import com.google.wireless.qa.mobileharness.shared.api.driver.Driver; -import com.google.wireless.qa.mobileharness.shared.constant.PropertyName; import com.google.wireless.qa.mobileharness.shared.model.job.JobInfo; import com.google.wireless.qa.mobileharness.shared.model.job.TestInfo; import com.google.wireless.qa.mobileharness.shared.model.job.out.Log; @@ -69,8 +74,10 @@ public class AndroidSetWifiDecoratorTest { private AndroidSetWifiDecorator decorator; + @Mock private DeviceOpsStub mockDeviceOpsStub; + @Before - public void setUp() throws MobileHarnessException, InterruptedException { + public void setUp() throws MobileHarnessException, InterruptedException, RpcExceptionWithErrorId { when(decoratedDriver.getDevice()).thenReturn(device); when(device.getDeviceId()).thenReturn(DEVICE_ID); when(testInfo.jobInfo()).thenReturn(jobInfo); @@ -78,7 +85,12 @@ public void setUp() throws MobileHarnessException, InterruptedException { when(testInfo.resultWithCause()).thenReturn(result); when(testInfo.warnings()).thenReturn(warnings); - decorator = new AndroidSetWifiDecorator(decoratedDriver, testInfo, networkConnector); + decorator = spy(new AndroidSetWifiDecorator(decoratedDriver, testInfo, networkConnector)); + + ConnectToDefaultWifiResponse successResponse = + ConnectToDefaultWifiResponse.newBuilder().setSuccess(true).build(); + when(mockDeviceOpsStub.connectToDefaultWifi(any())).thenReturn(successResponse); + doReturn(mockDeviceOpsStub).when(decorator).getDeviceOpsStub(anyString(), anyInt()); } @Test @@ -148,47 +160,33 @@ public void testSetWifi_notSpecifiSsid_throwException() @Test public void testSetWifi_defaultConfig_success() - throws MobileHarnessException, InterruptedException { - String ssid = "ssid1"; - String psk = "psk1"; + throws MobileHarnessException, InterruptedException, RpcExceptionWithErrorId { AndroidSetWifiDecoratorSpec spec = AndroidSetWifiDecoratorSpec.newBuilder().setUseDefaultSsid(true).build(); when(jobInfo.combinedSpec(decorator, DEVICE_ID)).thenReturn(spec); - when(device.getProperty( - Ascii.toLowerCase(PropertyName.Test.AndroidSetWifiDecorator.DEFAULT_WIFI_SSID.name()))) - .thenReturn(ssid); - when(device.getProperty( - Ascii.toLowerCase(PropertyName.Test.AndroidSetWifiDecorator.DEFAULT_WIFI_PSK.name()))) - .thenReturn(psk); decorator.run(testInfo); - verify(networkConnector) - .connectToWifi( - device, - WifiConnectArgs.builder() - .setWifiSsid("ssid1") - .setWifiPsk("psk1") - .setScanSsid(false) - .setWaitTimeout(Duration.ofMinutes(5)) - .setRetryNum(3) - .build(), - log); + verify(mockDeviceOpsStub).connectToDefaultWifi(any()); verify(decoratedDriver).run(testInfo); } @Test public void testSetWifi_defaultConfigWithEmptySsid_throwException() - throws MobileHarnessException, InterruptedException { + throws MobileHarnessException, InterruptedException, RpcExceptionWithErrorId { AndroidSetWifiDecoratorSpec spec = AndroidSetWifiDecoratorSpec.newBuilder().setUseDefaultSsid(true).build(); when(jobInfo.combinedSpec(decorator, DEVICE_ID)).thenReturn(spec); + ConnectToDefaultWifiResponse failResponse = + ConnectToDefaultWifiResponse.newBuilder().setSuccess(false).build(); + when(mockDeviceOpsStub.connectToDefaultWifi(any())).thenReturn(failResponse); + MobileHarnessException e = assertThrows(MobileHarnessException.class, () -> decorator.run(testInfo)); assertThat(e.getErrorId()) - .isEqualTo(AndroidErrorId.ANDROID_SET_WIFI_DECORATOR_GET_DEFAULT_SSID_ERROR); + .isEqualTo(AndroidErrorId.ANDROID_SET_WIFI_DECORATOR_WIFI_CONNECT_ERROR); } @Test @@ -237,14 +235,11 @@ public void testRun_missingDefaultSsid_wifiSsidOptional_skipsAndRunsDecorated() .setWifiSsidOptional(true) .build(); when(jobInfo.combinedSpec(decorator, DEVICE_ID)).thenReturn(spec); - when(device.getProperty( - Ascii.toLowerCase(PropertyName.Test.AndroidSetWifiDecorator.DEFAULT_WIFI_SSID.name()))) - .thenReturn(null); decorator.run(testInfo); verify(decoratedDriver).run(testInfo); - verifyNoInteractions(networkConnector); + verify(mockDeviceOpsStub).connectToDefaultWifi(any()); } @Test diff --git a/src/javatests/com/google/wireless/qa/mobileharness/shared/api/decorator/BUILD b/src/javatests/com/google/wireless/qa/mobileharness/shared/api/decorator/BUILD index 9f2d815733..811e56e538 100644 --- a/src/javatests/com/google/wireless/qa/mobileharness/shared/api/decorator/BUILD +++ b/src/javatests/com/google/wireless/qa/mobileharness/shared/api/decorator/BUILD @@ -29,11 +29,13 @@ java_library( "//src/devtools/mobileharness/platform/android/logcat/proto:logcat_monitoring_report_java_proto", "//src/devtools/mobileharness/platform/android/runtimestats:runtime_stats_report_java_proto", "//src/devtools/mobileharness/platform/android/video/proto:video_output_java_proto", + "//src/java/com/google/devtools/common/metrics/stability/rpc:exception", "//src/java/com/google/devtools/deviceinfra/platform/android/lightning/internal/sdk/adb", "//src/java/com/google/devtools/mobileharness/api/model/error", "//src/java/com/google/devtools/mobileharness/api/model/job/out", "//src/java/com/google/devtools/mobileharness/api/testrunner/device/cache:device_cache", "//src/java/com/google/devtools/mobileharness/infra/ats/common:session_handler_helper", + "//src/java/com/google/devtools/mobileharness/infra/lab/rpc/stub:device_ops", "//src/java/com/google/devtools/mobileharness/platform/android/dropbox", "//src/java/com/google/devtools/mobileharness/platform/android/file:android_file_util", "//src/java/com/google/devtools/mobileharness/platform/android/instrumentation:android_instrumentation_util", @@ -62,6 +64,7 @@ java_library( "//src/java/com/google/devtools/mobileharness/shared/util/path", "//src/java/com/google/devtools/mobileharness/shared/util/time:count_down_timer", "//src/java/com/google/devtools/mobileharness/shared/util/time:sleeper", + "//src/java/com/google/wireless/qa/mobileharness/lab/proto:device_ops_serv_java_proto", "//src/java/com/google/wireless/qa/mobileharness/shared/api/decorator:android_aflags_decorator", "//src/java/com/google/wireless/qa/mobileharness/shared/api/decorator:android_ats_dynamic_config_pusher_setup_only_decorator", "//src/java/com/google/wireless/qa/mobileharness/shared/api/decorator:android_ats_dynamic_config_pusher_teardown_only_decorator",