|
| 1 | +/* |
| 2 | + * Copyright 2022 Google LLC |
| 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 | + * https://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.google.devtools.mobileharness.shared.labinfo; |
| 18 | + |
| 19 | +import static com.google.common.truth.Truth.assertThat; |
| 20 | +import static org.mockito.Mockito.when; |
| 21 | + |
| 22 | +import com.google.common.collect.ImmutableListMultimap; |
| 23 | +import com.google.common.collect.ImmutableMap; |
| 24 | +import com.google.devtools.mobileharness.api.model.lab.LabLocator; |
| 25 | +import com.google.devtools.mobileharness.api.model.proto.Device.DeviceCondition; |
| 26 | +import com.google.devtools.mobileharness.api.model.proto.Device.DeviceFeature; |
| 27 | +import com.google.devtools.mobileharness.api.model.proto.Device.DeviceStatus; |
| 28 | +import com.google.devtools.mobileharness.api.model.proto.Device.DeviceStatusWithTimestamp; |
| 29 | +import com.google.devtools.mobileharness.api.query.proto.LabQueryProto.DeviceInfo; |
| 30 | +import com.google.devtools.mobileharness.api.query.proto.LabQueryProto.LabQueryResult.LabView; |
| 31 | +import com.google.devtools.mobileharness.infra.controller.device.DeviceStatusInfo; |
| 32 | +import com.google.devtools.mobileharness.infra.controller.device.LocalDeviceManager; |
| 33 | +import com.google.devtools.mobileharness.shared.labinfo.DeviceTempRequiredDimensionManager.DeviceKey; |
| 34 | +import com.google.devtools.mobileharness.shared.labinfo.DeviceTempRequiredDimensionManager.DeviceTempRequiredDimensions; |
| 35 | +import com.google.inject.Guice; |
| 36 | +import com.google.inject.testing.fieldbinder.Bind; |
| 37 | +import com.google.inject.testing.fieldbinder.BoundFieldModule; |
| 38 | +import com.google.wireless.qa.mobileharness.shared.api.device.Device; |
| 39 | +import java.time.Instant; |
| 40 | +import java.util.Optional; |
| 41 | +import javax.inject.Inject; |
| 42 | +import org.junit.Before; |
| 43 | +import org.junit.Rule; |
| 44 | +import org.junit.Test; |
| 45 | +import org.junit.runner.RunWith; |
| 46 | +import org.junit.runners.JUnit4; |
| 47 | +import org.mockito.Mock; |
| 48 | +import org.mockito.junit.MockitoJUnit; |
| 49 | +import org.mockito.junit.MockitoRule; |
| 50 | + |
| 51 | +@RunWith(JUnit4.class) |
| 52 | +public class LocalLabInfoProviderTest { |
| 53 | + |
| 54 | + @Rule public final MockitoRule mockito = MockitoJUnit.rule(); |
| 55 | + |
| 56 | + @Mock @Bind private LocalDeviceManager localDeviceManager; |
| 57 | + @Mock @Bind private DeviceTempRequiredDimensionManager tempRequiredDimensionManager; |
| 58 | + |
| 59 | + @Mock private Device device; |
| 60 | + |
| 61 | + @SuppressWarnings("DoNotMockAutoValue") |
| 62 | + @Mock |
| 63 | + private DeviceStatusInfo deviceStatusInfo; |
| 64 | + |
| 65 | + @Inject private LocalLabInfoProvider localLabInfoProvider; |
| 66 | + |
| 67 | + @Before |
| 68 | + public void setUp() throws Exception { |
| 69 | + Guice.createInjector(BoundFieldModule.of(this)).injectMembers(this); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void getLabInfos_withTempDimensions() throws Exception { |
| 74 | + String deviceUuid = "device_uuid"; |
| 75 | + when(device.getDeviceUuid()).thenReturn(deviceUuid); |
| 76 | + |
| 77 | + DeviceStatusWithTimestamp statusWithTimestamp = |
| 78 | + DeviceStatusWithTimestamp.newBuilder().setStatus(DeviceStatus.IDLE).build(); |
| 79 | + when(deviceStatusInfo.getDeviceStatusWithTimestamp()).thenReturn(statusWithTimestamp); |
| 80 | + |
| 81 | + when(localDeviceManager.getAllDeviceStatus(false)) |
| 82 | + .thenReturn(ImmutableMap.of(device, deviceStatusInfo)); |
| 83 | + |
| 84 | + DeviceFeature feature = DeviceFeature.getDefaultInstance(); |
| 85 | + when(device.toFeature()).thenReturn(feature); |
| 86 | + |
| 87 | + DeviceKey deviceKey = new DeviceKey(LabLocator.LOCALHOST.hostName(), deviceUuid); |
| 88 | + |
| 89 | + ImmutableListMultimap<String, String> dimensions = ImmutableListMultimap.of("key1", "value1"); |
| 90 | + Instant expireTime = Instant.ofEpochMilli(12345L); |
| 91 | + |
| 92 | + when(tempRequiredDimensionManager.getDimensions(deviceKey)) |
| 93 | + .thenReturn(Optional.of(new DeviceTempRequiredDimensions(dimensions, expireTime))); |
| 94 | + |
| 95 | + LabView labView = localLabInfoProvider.getLabInfos(null); |
| 96 | + |
| 97 | + assertThat(labView.getLabDataCount()).isEqualTo(1); |
| 98 | + assertThat(labView.getLabData(0).getDeviceList().getDeviceInfoCount()).isEqualTo(1); |
| 99 | + |
| 100 | + DeviceInfo deviceInfo = labView.getLabData(0).getDeviceList().getDeviceInfo(0); |
| 101 | + assertThat(deviceInfo.getDeviceStatus()).isEqualTo(DeviceStatus.IDLE); |
| 102 | + |
| 103 | + // Verify condition |
| 104 | + DeviceCondition condition = deviceInfo.getDeviceCondition(); |
| 105 | + assertThat(condition.getTempDimensionCount()).isEqualTo(1); |
| 106 | + assertThat(condition.getTempDimension(0).getDimension().getName()).isEqualTo("key1"); |
| 107 | + assertThat(condition.getTempDimension(0).getDimension().getValue()).isEqualTo("value1"); |
| 108 | + assertThat(condition.getTempDimension(0).getExpireTimestampMs()).isEqualTo(12345L); |
| 109 | + |
| 110 | + // Verify feature |
| 111 | + assertThat(deviceInfo.getDeviceFeature().getCompositeDimension().getRequiredDimensionCount()) |
| 112 | + .isEqualTo(1); |
| 113 | + assertThat( |
| 114 | + deviceInfo.getDeviceFeature().getCompositeDimension().getRequiredDimension(0).getName()) |
| 115 | + .isEqualTo("key1"); |
| 116 | + } |
| 117 | +} |
0 commit comments