https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
k

Kris Wong

07/31/2019, 6:08 PM
has anyone figured out how to test a view controller subclass implemented in kotlin MPP?
hot damn... calling
view
on the view controller will load the view and run
viewDidLoad
s

Sam

07/31/2019, 6:51 PM
Calling
view
will run through the following sequence. 1.
loadViewIfNecessary()
2.
loadView()
3.
viewDidLoad()
The best place to customize your view controller's view hierarchy is in
loadView()
. Be sure to call
super
first. Any configuration of the views should happen in
viewDidLoad()
. Note that your vc isn't actually attached to another view controller or window at this point so definitive size information is not available. Size customizations should be done in
UIContentContainer
overrides.
P.S. There is also a
viewIfLoaded
nullable property if you want the view but don't want to trigger loading. It is available in iOS 9.0+
k

Kris Wong

07/31/2019, 6:56 PM
thanks. all that is working great. now just trying to get an event callback to be triggered for a UISwitch. it's proving much trickier.
wow, this is frustrating. normal event dispatch through UIApplication does not work from a test. trying to call the selector directly using
performSelector
throws an error stating the selector is unrecognized, but I know it's valid, and it works fine just running the app.
s

Sam

08/01/2019, 12:46 PM
If you’re trying to test UI behavior, you need to do it within the context of a running app being driven by ui tests. It’s nearly impossible to synthesize touch events in iOS. Have a look at this article for a start. https://medium.com/@johnsundell/getting-started-with-xcode-ui-testing-in-swift-ac7b1f5101e5
k

Kris Wong

08/01/2019, 12:59 PM
will check that out. i guess the question is whether it's possible in Kotlin.
k

Kris Wong

08/01/2019, 1:20 PM
got it. so XCUIApplication is the key
makes sense because the call to UIApplication for the action is where it seemed to die
looks like there are no binding for the unit testing APIs
I didn't even check but I bet UIApplication.sharedApplication was null
so what I observed is, there is a UIApplication instance (so a 2nd can't be created), but
UIApplication.sharedApplication
is null (
UIApplicationMain
wasn't run, and can't be run)
so I can dispatch to the main queue, but control actions are a no-op
4 Views