Dynamic Type & iOS Simulator: How can I set the value?
Asked Answered
P

1

7

I want to write my UI tests so that they test all seven states of dynamic type, from the smallest to the largest. How should I do this?

Can I set an environment variable for the simulator in my Scheme and then just make different schemes?

Or can I set the dynamic type variable programatically in my test?

I'd rather not make a DynamicTypeController and then make it say what type it is, because I'd risk forgetting to use this for some elements, and then not have the behaviour properly tested.

Cheers

Nik

Produce answered 26/10, 2015 at 7:33 Comment(0)
L
4

I apologise for the incompleteness of this answer, but it is too long for a comment and there would not be any code formatting.

The short answer is, this is not supported: https://forums.developer.apple.com/thread/21503 , but it probably warrants a radar.

The longer answer is, you might be able to hack something together. The setting for dynamic type in the simulator is available on your filesystem at: ~/Library/Developer/CoreSimulator/Devices/<device identifier>/data/Library/Preferences/com.apple.UIKit.plist. You can programmatically get the path to the simulator filesystem from inside an XCTest using:

    let environment = NSProcessInfo.processInfo().environment
    if let resourcesDir = environment[ "SIMULATOR_SHARED_RESOURCES_DIRECTORY" ]
    {
        print( "-- Simulator Shared Resources Directory: \(resourcesDir)" )
        let dictionary = NSMutableDictionary( contentsOfFile: "\(resourcesDir)/data/Library/Preferences/com.apple.UIKit.plist" )!
        print( "Dictionary: \(dictionary)" )
    }

However, the test is sandboxed from the simulator directory, so you cannot actually open or modify the preferences. That code snippet fails when trying to unwrap the dictionary. I also never tried this with Xcode Server.

You could modify the plist using with a shell script like so: plutil -replace UIPreferredContentSizeCategoryName -string UICTContentSizeCategoryAccessibilityXXXL com.apple.UIKit.plist. The simulator needs to restart after you modify the file so this may not fly in a shared build environment. Unfortunately, the path to the simulator's filesystem is not available to Pre-action scripts in Xcode. You could modify the setting for all the simulators, but again, might not fly in a shared build environment.

At the end of the day, you might be better off creating your DynamicTypeController and adding some process to ensure no one uses UIApplication.preferredContentSizeCategory.

This was all tested using Xcode 7.1 (7B91b).

Lisp answered 8/11, 2015 at 19:54 Comment(3)
Hey Carlos, no need to apologise, I think your answer is as complete as it gets. The approach I had started on was making different simulators, but now I'll absolutely switch to editing this plist. The Xcode server is used for the same team on the same project, so that should be no problem. Thanks a bunch! :-)Produce
I've added RADAR #23459874. PS, I'm sorry, I just noticed the bounty had expired before I got to mark the answer as correct. :-(Produce
Great find. Looks like you can add -UIPreferredContentSizeCategoryName UICTContentSizeCategoryAccessibilityXXXL as a launch argument (for example in an Xcode scheme). Thanks for this answer which really helped me.Doth

© 2022 - 2024 — McMap. All rights reserved.