Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 583
niklassaers Avatar asked Oct 26 '15 07:10

niklassaers


1 Answers

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).

like image 83
Carlos Macasaet Avatar answered Sep 29 '22 08:09

Carlos Macasaet