Skip to content

Commit

Permalink
Add support for setting system language of newly created simulator
Browse files Browse the repository at this point in the history
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", "<language-code>"]` 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 simulator's 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.
  • Loading branch information
thii committed May 17, 2022
1 parent 24ba151 commit d95ee2e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
18 changes: 16 additions & 2 deletions simulator_control/simulator_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -663,6 +670,13 @@ def QuitSimulatorApp():
stderr=subprocess.STDOUT)


def RespringAllSimulators():
"""Restarts the SpringBoard.app in all booted simulator."""
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.
Expand Down
6 changes: 5 additions & 1 deletion test_runner/ios_test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)


Expand Down

0 comments on commit d95ee2e

Please sign in to comment.