From 70ddcfe869faea023289636fe7b9bb373062231f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thi=20Do=C3=A3n?= Date: Tue, 17 May 2022 16:27:19 +0900 Subject: [PATCH] Add support for setting system language of newly created simulator This adds a new `--simulator_language` argument that accepts a two-letter language code, which will be used to set the simulator's system language at startup time. Simulator language can be specified by passing `["--simulator_language", ""]` to `ios_unit_test`/`ios_ui_test`'s [`args`](https://bazel.build/reference/be/common-definitions#common-attributes-tests) attribute. Note that because this needs to kill all booted simulators' SpringBoard to reload the new language setting, avoid setting this if you run tests on multiple simulators simultanously. Based on https://gist.github.com/koke/e3106e4531e40d2ba423b76ad789caff. --- simulator_control/simulator_util.py | 18 ++++++++++++++++-- test_runner/ios_test_runner.py | 6 +++++- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/simulator_control/simulator_util.py b/simulator_control/simulator_util.py index 0f186fe..69ed88c 100644 --- a/simulator_control/simulator_util.py +++ b/simulator_control/simulator_util.py @@ -113,13 +113,20 @@ def device_plist_object(self): self._device_plist_object = plist_util.Plist(device_plist_path) return self._device_plist_object - def Boot(self): + def Boot(self, simulator_language=None): """Boots the simulator as asynchronously. + Args: + simulator_language: string, the language of the simulator at startup time, e.g. 'ja'. Returns: A subprocess.Popen object of the boot process. """ RunSimctlCommand(['xcrun', 'simctl', 'boot', self.simulator_id]) + if simulator_language: + RunSimctlCommand(['xcrun', 'simctl', 'spawn', self.simulator_id, + 'defaults', 'write', 'Apple Global Domain', 'AppleLanguages', + '-array', simulator_language]) + RespringAllSimulators() self.WaitUntilStateBooted() logging.info('The simulator %s is booted.', self.simulator_id) @@ -305,7 +312,7 @@ def GetSimulatorState(self): return _SIMULATOR_STATES_MAPPING[state_num] -def CreateNewSimulator(device_type=None, os_version=None, name_prefix=None): +def CreateNewSimulator(device_type=None, os_version=None, name_prefix=None, language=None): """Creates a new simulator according to arguments. If neither device_type nor os_version is given, will use the latest iOS @@ -663,6 +670,13 @@ def QuitSimulatorApp(): stderr=subprocess.STDOUT) +def RespringAllSimulators(): + """Restarts the SpringBoard.app in all booted simulators.""" + subprocess.Popen(['killall', '-HUP', 'SpringBoard'], + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT) + + def IsAppFailedToLaunchOnSim(sim_sys_log, app_bundle_id=''): """Checks if the app failed to launch on simulator. diff --git a/test_runner/ios_test_runner.py b/test_runner/ios_test_runner.py index c85800d..27307d6 100644 --- a/test_runner/ios_test_runner.py +++ b/test_runner/ios_test_runner.py @@ -191,7 +191,7 @@ def _RunSimulatorTest(args): hostless = args.app_under_test_path is None try: if not hostless: - simulator_obj.Boot() + simulator_obj.Boot(args.simulator_language) session.Prepare( app_under_test=args.app_under_test_path, test_bundle=args.test_bundle_path, @@ -238,6 +238,10 @@ def _SimulatorTest(args): 'The new simulator name will be the value of concatenating name ' 'prefix with simulator type and os version. ' 'E.g., New-iPhone 6 Plus-10.2.') + test_parser.add_argument( + '--simulator_language', + help='The system language of the simulator at creation time, e.g `ja`. ' + 'Note: Do not set this if you create multiple simulators simutaneously.') test_parser.set_defaults(func=_SimulatorTest)