1 /* 2 * Copyright (C) 2021 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 package android.time.cts.host; 17 18 import static android.app.time.cts.shell.DeviceConfigKeys.NAMESPACE_SYSTEM_TIME; 19 import static android.app.time.cts.shell.LocationTimeZoneManagerShellHelper.PRIMARY_PROVIDER_INDEX; 20 import static android.app.time.cts.shell.LocationTimeZoneManagerShellHelper.PROVIDER_MODE_SIMULATED; 21 import static android.app.time.cts.shell.LocationTimeZoneManagerShellHelper.SECONDARY_PROVIDER_INDEX; 22 23 import android.app.time.LocationTimeZoneManagerServiceStateProto; 24 import android.app.time.cts.shell.DeviceConfigShellHelper; 25 import android.app.time.cts.shell.DeviceShellCommandExecutor; 26 import android.app.time.cts.shell.LocationShellHelper; 27 import android.app.time.cts.shell.LocationTimeZoneManagerShellHelper; 28 import android.app.time.cts.shell.TimeZoneDetectorShellHelper; 29 import android.app.time.cts.shell.host.HostShellCommandExecutor; 30 31 import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test; 32 import com.google.protobuf.Parser; 33 34 import org.junit.After; 35 import org.junit.Before; 36 37 /** A base class for tests that interact with the location_time_zone_manager via adb. */ 38 public abstract class BaseLocationTimeZoneManagerHostTest extends BaseHostJUnit4Test { 39 40 private boolean mOriginalLocationEnabled; 41 42 private boolean mOriginalAutoDetectionEnabled; 43 44 private boolean mOriginalGeoDetectionEnabled; 45 46 protected TimeZoneDetectorShellHelper mTimeZoneDetectorShellHelper; 47 private LocationTimeZoneManagerShellHelper mLocationTimeZoneManagerShellHelper; 48 private LocationShellHelper mLocationShellHelper; 49 private DeviceConfigShellHelper mDeviceConfigShellHelper; 50 private DeviceConfigShellHelper.PreTestState mDeviceConfigPreTestState; 51 52 @Before setUp()53 public void setUp() throws Exception { 54 DeviceShellCommandExecutor shellCommandExecutor = new HostShellCommandExecutor(getDevice()); 55 mTimeZoneDetectorShellHelper = new TimeZoneDetectorShellHelper(shellCommandExecutor); 56 mLocationTimeZoneManagerShellHelper = 57 new LocationTimeZoneManagerShellHelper(shellCommandExecutor); 58 mLocationShellHelper = new LocationShellHelper(shellCommandExecutor); 59 mDeviceConfigShellHelper = new DeviceConfigShellHelper(shellCommandExecutor); 60 61 // Confirm the service being tested is present. It can be turned off, in which case there's 62 // nothing to test. 63 mLocationTimeZoneManagerShellHelper.assumeLocationTimeZoneManagerIsPresent(); 64 65 // All tests start with the location_time_zone_manager disabled so that providers can be 66 // configured. 67 stopLocationTimeZoneManagerService(); 68 69 mDeviceConfigPreTestState = mDeviceConfigShellHelper.setSyncModeForTest( 70 DeviceConfigShellHelper.SYNC_DISABLED_MODE_UNTIL_REBOOT, NAMESPACE_SYSTEM_TIME); 71 72 // Configure two simulated providers. At least one is needed to be able to turn on 73 // geo detection below. Tests may override these values for their own use. 74 setProviderModeOverride(PRIMARY_PROVIDER_INDEX, PROVIDER_MODE_SIMULATED); 75 setProviderModeOverride(SECONDARY_PROVIDER_INDEX, PROVIDER_MODE_SIMULATED); 76 77 // Make sure locations is enabled, otherwise the geo detection feature will be disabled 78 // whatever the geolocation detection setting is set to. 79 mOriginalLocationEnabled = mLocationShellHelper.isLocationEnabledForCurrentUser(); 80 if (!mOriginalLocationEnabled) { 81 mLocationShellHelper.setLocationEnabledForCurrentUser(true); 82 } 83 84 // Make sure automatic time zone detection is enabled, otherwise the geo detection feature 85 // will be disabled whatever the geolocation detection setting is set to 86 mOriginalAutoDetectionEnabled = mTimeZoneDetectorShellHelper.isAutoDetectionEnabled(); 87 if (!mOriginalAutoDetectionEnabled) { 88 mTimeZoneDetectorShellHelper.setAutoDetectionEnabled(true); 89 } 90 91 // Make sure geolocation time zone detection is enabled. 92 mOriginalGeoDetectionEnabled = mTimeZoneDetectorShellHelper.isGeoDetectionEnabled(); 93 if (!mOriginalGeoDetectionEnabled) { 94 mTimeZoneDetectorShellHelper.setGeoDetectionEnabled(true); 95 } 96 } 97 98 @After tearDown()99 public void tearDown() throws Exception { 100 if (!mLocationTimeZoneManagerShellHelper.isLocationTimeZoneManagerPresent()) { 101 // Setup didn't do anything, no need to tearDown. 102 return; 103 } 104 // Turn off the service before we reset configuration, otherwise it will restart itself 105 // repeatedly. 106 stopLocationTimeZoneManagerService(); 107 108 // Reset settings and server flags as best we can. 109 mTimeZoneDetectorShellHelper.setGeoDetectionEnabled(mOriginalGeoDetectionEnabled); 110 mTimeZoneDetectorShellHelper.setAutoDetectionEnabled(mOriginalAutoDetectionEnabled); 111 mLocationShellHelper.setLocationEnabledForCurrentUser(mOriginalLocationEnabled); 112 setLocationTimeZoneManagerStateRecordingMode(false); 113 114 mDeviceConfigShellHelper.restoreDeviceConfigStateForTest(mDeviceConfigPreTestState); 115 116 // Attempt to start the service. It may not start if there are no providers configured, 117 // but that is ok. 118 startLocationTimeZoneManagerService(); 119 } 120 dumpLocationTimeZoneManagerServiceState()121 protected LocationTimeZoneManagerServiceStateProto dumpLocationTimeZoneManagerServiceState() 122 throws Exception { 123 byte[] protoBytes = mLocationTimeZoneManagerShellHelper.dumpState(); 124 Parser<LocationTimeZoneManagerServiceStateProto> parser = 125 LocationTimeZoneManagerServiceStateProto.parser(); 126 return parser.parseFrom(protoBytes); 127 } 128 setLocationTimeZoneManagerStateRecordingMode(boolean enabled)129 protected void setLocationTimeZoneManagerStateRecordingMode(boolean enabled) throws Exception { 130 mLocationTimeZoneManagerShellHelper.recordProviderStates(enabled); 131 } 132 startLocationTimeZoneManagerService()133 protected void startLocationTimeZoneManagerService() throws Exception { 134 mLocationTimeZoneManagerShellHelper.start(); 135 } 136 stopLocationTimeZoneManagerService()137 protected void stopLocationTimeZoneManagerService() throws Exception { 138 mLocationTimeZoneManagerShellHelper.stop(); 139 } 140 setProviderModeOverride(int providerIndex, String mode)141 protected void setProviderModeOverride(int providerIndex, String mode) throws Exception { 142 mLocationTimeZoneManagerShellHelper.setProviderModeOverride(providerIndex, mode); 143 } 144 simulateProviderSuggestion(int providerIndex, String... zoneIds)145 protected void simulateProviderSuggestion(int providerIndex, String... zoneIds) 146 throws Exception { 147 mLocationTimeZoneManagerShellHelper.simulateProviderSuggestion(providerIndex, zoneIds); 148 } 149 simulateProviderBind(int providerIndex)150 protected void simulateProviderBind(int providerIndex) throws Exception { 151 mLocationTimeZoneManagerShellHelper.simulateProviderBind(providerIndex); 152 } 153 } 154