Skip to content

Commit d95ee2e

Browse files
committed
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", "<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.
1 parent 24ba151 commit d95ee2e

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

simulator_control/simulator_util.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,20 @@ def device_plist_object(self):
113113
self._device_plist_object = plist_util.Plist(device_plist_path)
114114
return self._device_plist_object
115115

116-
def Boot(self):
116+
def Boot(self, simulator_language=None):
117117
"""Boots the simulator as asynchronously.
118118
119+
Args:
120+
simulator_language: string, the language of the simulator at startup time, e.g. 'ja'.
119121
Returns:
120122
A subprocess.Popen object of the boot process.
121123
"""
122124
RunSimctlCommand(['xcrun', 'simctl', 'boot', self.simulator_id])
125+
if simulator_language:
126+
RunSimctlCommand(['xcrun', 'simctl', 'spawn', self.simulator_id,
127+
'defaults', 'write', 'Apple Global Domain', 'AppleLanguages',
128+
'-array', simulator_language])
129+
RespringAllSimulators()
123130
self.WaitUntilStateBooted()
124131
logging.info('The simulator %s is booted.', self.simulator_id)
125132

@@ -305,7 +312,7 @@ def GetSimulatorState(self):
305312
return _SIMULATOR_STATES_MAPPING[state_num]
306313

307314

308-
def CreateNewSimulator(device_type=None, os_version=None, name_prefix=None):
315+
def CreateNewSimulator(device_type=None, os_version=None, name_prefix=None, language=None):
309316
"""Creates a new simulator according to arguments.
310317
311318
If neither device_type nor os_version is given, will use the latest iOS
@@ -663,6 +670,13 @@ def QuitSimulatorApp():
663670
stderr=subprocess.STDOUT)
664671

665672

673+
def RespringAllSimulators():
674+
"""Restarts the SpringBoard.app in all booted simulator."""
675+
subprocess.Popen(['killall', '-HUP', 'SpringBoard'],
676+
stdout=subprocess.PIPE,
677+
stderr=subprocess.STDOUT)
678+
679+
666680
def IsAppFailedToLaunchOnSim(sim_sys_log, app_bundle_id=''):
667681
"""Checks if the app failed to launch on simulator.
668682

test_runner/ios_test_runner.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ def _RunSimulatorTest(args):
191191
hostless = args.app_under_test_path is None
192192
try:
193193
if not hostless:
194-
simulator_obj.Boot()
194+
simulator_obj.Boot(args.simulator_language)
195195
session.Prepare(
196196
app_under_test=args.app_under_test_path,
197197
test_bundle=args.test_bundle_path,
@@ -238,6 +238,10 @@ def _SimulatorTest(args):
238238
'The new simulator name will be the value of concatenating name '
239239
'prefix with simulator type and os version. '
240240
'E.g., New-iPhone 6 Plus-10.2.')
241+
test_parser.add_argument(
242+
'--simulator_language',
243+
help='The system language of the simulator at creation time, e.g `ja`. '
244+
'Note: Do not set this if you create multiple simulators simutaneously.')
241245
test_parser.set_defaults(func=_SimulatorTest)
242246

243247

0 commit comments

Comments
 (0)