Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test right-to-left language in iOS XCTest unit tests?

Is there a way to switch an XCTest unit test into the right-to-left mode to test Arabic version of the app where sentences are written from right to left of the screen? My app code logic behaves differently based on language direction. I would like to verify this functionality in a unit test. What I need to do is to switch the app into the right-to-left language mode from an XCTest unit test case.

One can run the app in the right-to-left mode by changing the Scheme's Application language settings to Right-to-left Pseudolanguage. Is there a way to do similar thing in a unit test?

My imperfect solution

I ended up changing semanticContentAttribute of a view under test to .ForceRightToLeft. It does what I need to do. It does not feel like a very clean approach though. Firstly, it only works in iOS 9. Secondly, it looks like I am tinkering with my app views on a low level from the unit test. Instead, I would prefer to switch the whole app's language to right-to-left if it is possible.

class MyTests: XCTestCase {
  func testRightToLeft() {
    if #available(iOS 9.0, *) {
      let view = UIView()
      view.semanticContentAttribute = .ForceRightToLeft
      // Test code involving the view
    }
  }
}
like image 871
Evgenii Avatar asked Aug 02 '15 06:08

Evgenii


2 Answers

There's no easy way to do this right now with testing/UI testing besides passing in environment flags or setting the semanticContentAttribute as you are doing now. Filing a bug to Apple is highly recommended.

like image 109
wakachamo Avatar answered Nov 15 '22 03:11

wakachamo


You can also change the device language & region in the scheme. This means you'll need separate schemes for the various LTR/RTL tests you want to run:

Xcode language & region settings

Xcode even provides pseudo-languages for extra-long string & RTL testing.

like image 23
Brian Gerstle Avatar answered Nov 15 '22 05:11

Brian Gerstle