在 kotlin 单元测试中创建 MKMapView 时崩溃

Crash when creating MKMapView in kotlin unit test

我正在尝试向使用 Mapbox 的 kotlin 多平台库部分添加测试,但我无法实例化 MKMapView 的实例。

即使像这样简单的测试也会崩溃。

@Test
fun `test creating view`() {
    MKMapView(CGRectMake(0.0, 0.0, 512.0, 512.0))
}

输出为

2020-07-14 09:31:53.667 test.kexe[9090:10700111] Error creating notification handler for simulator graphics quality override: 3
2020-07-14 09:31:53.670 test.kexe[9090:10700111] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'contentScale is unreasonable (NaN or Inf)'
*** First throw call stack:
(
        0   CoreFoundation                      0x00007fff23e3cf0e __exceptionPreprocess + 350
        1   libobjc.A.dylib                     0x00007fff50ba89b2 objc_exception_throw + 48
        2   CoreFoundation                      0x00007fff23e3cad9 -[NSException raise] + 9
        3   VectorKit                           0x00007fff49c14143 _Z25checkForBogusContentScaled + 115
        4   VectorKit                           0x00007fff49c14252 -[VKMapView initShouldRasterize:inBackground:contentScale:auditToken:] + 62
        5   MapKit                              0x00007fff2782009a -[MKBasicMapView initWithFrame:andGlobe:shouldRasterize:] + 419
        6   MapKit                              0x00007fff27755b85 -[MKMapView _commonInitFromIB:gestureRecognizerHostView:showsAttribution:showsAppleLogo:] + 1331
        7   MapKit                              0x00007fff27756698 -[MKMapView initWithFrame:] + 253
        8   test.kexe                           0x000000010e346dd0 _70616e6765612d6170706c655f74657374_knbridge2 + 64
        9   test.kexe                           0x000000010e33fa8b kfun:com.weather.pangea.apple.AppleViewportTest.test creating view() + 987
        10  test.kexe                           0x000000010e3410a6 kfun:com.weather.pangea.apple.$AppleViewportTest$test[=11=].$test creating view$FUNCTION_REFERENCE.invoke#internal + 70
        11  test.kexe                           0x000000010e34111f kfun:com.weather.pangea.apple.$AppleViewportTest$test[=11=].$test creating view$FUNCTION_REFERENCE.$<bridge-UNNN>invoke(P1)#internal + 95
        12  test.kexe                           0x000000010e3e0fa7 kfun:kotlin.native.internal.test.BaseClassSuite.TestCase.run() + 1287
        13  test.kexe                           0x000000010e3d9256 kfun:kotlin.native.internal.test.TestRunner.run#internal + 2406
        14  test.kexe                           0x000000010e3da3e4 kfun:kotlin.native.internal.test.TestRunner.runIteration#internal + 2916
        15  test.kexe                           0x000000010e3dacee kfun:kotlin.native.internal.test.TestRunner.run()kotlin.Int + 958
        16  test.kexe                           0x000000010e3ce29d kfun:kotlin.native.internal.test.testLauncherEntryPoint(kotlin.Array<kotlin.String>)kotlin.Int + 301
        17  test.kexe                           0x000000010e3ce387 kfun:kotlin.native.internal.test.main(kotlin.Array<kotlin.String>) + 55
        18  test.kexe                           0x000000010e341315 Konan_start + 165
        19  test.kexe                           0x000000010e34ba1b Init_and_run_start + 107
        20  libdyld.dylib                       0x00007fff51a231fd start + 1

当 运行在模拟器上运行一个真实的应用程序时,我能够正确地创建 MKMapView。我猜这与 kotlin 测试用于 运行 测试的环境有关,但我不知道如何配置它。

我正在使用 Kotlin 1.3.72 和 gradle 插件的内置 iOS 测试任务。

这里的根本原因是 Kotlin/Native 利用 xcrun simctl spawn CLI 工具进行 运行ning 测试。 此命令 运行 是模拟器上的给定测试,不是作为应用程序,而是作为可执行文件。这使测试更加直接,但某些极端情况可能会出错。可能在这里我们遇到了负责图形的设备服务的问题。在这个假设中,我依赖于 1) Error creating notification handler for simulator graphics quality override: 3 错误消息和 2) initShouldRasterize 在堆栈跟踪中,这对我来说似乎很可疑。


为了证明问题与Kotlin/Native没有直接关系,我制作了两个代码片段:map.swift和map.kt。 像这样编译它们中的每一个: swiftc map.swift -sdk /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator13.5.sdk -target x86_64-apple-ios13.5 ~/.konan/kotlin-native-macos-1.3.72/bin/konanc -tr -g -ea map.kt -target ios_x64 -o map 创建两个可执行文件:mapmap.kexe。在那之后,我 运行 他们都使用 simctl spawn:

xcrun simctl spawn -s "C01BDD64-19DC-49A0-9363-92EAFFD1B258" <absolute_path>/map
2020-07-15 12:49:57.262 map[8840:1053205] Error creating notification handler for simulator graphics quality override: 3
2020-07-15 12:49:57.264 map[8840:1053205] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'contentScale is unreasonable (NaN or Inf)'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff23e3cf0e __exceptionPreprocess + 350
    1   libobjc.A.dylib                     0x00007fff50ba89b2 objc_exception_throw + 48
    2   CoreFoundation                      0x00007fff23e3cad9 -[NSException raise] + 9
    3   VectorKit                           0x00007fff49c14143 _Z25checkForBogusContentScaled + 115
    4   VectorKit                           0x00007fff49c14252 -[VKMapView initShouldRasterize:inBackground:contentScale:auditToken:] + 62
    5   MapKit                              0x00007fff2782009a -[MKBasicMapView initWithFrame:andGlobe:shouldRasterize:] + 419
    6   MapKit                              0x00007fff27755b85 -[MKMapView _commonInitFromIB:gestureRecognizerHostView:showsAttribution:showsAppleLogo:] + 1331
    7   MapKit                              0x00007fff27756698 -[MKMapView initWithFrame:] + 253
    8   map                                 0x000000010b924f0d $sSo9MKMapViewC5frameABSo6CGRectV_tcfcTO + 77
    9   map                                 0x000000010b924eaa $sSo9MKMapViewC5frameABSo6CGRectV_tcfC + 74
    10  map                                 0x000000010b924e07 main + 183
    11  libdyld.dylib                       0x00007fff51a231fd start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Child process terminated with signal 6: Abort trap
xcrun simctl spawn -s "C01BDD64-19DC-49A0-9363-92EAFFD1B258" <absolute_path>/map.kexe
[==========] Running 1 tests from 1 test cases.
[----------] Global test environment set-up.
[----------] 1 tests from MapKt
[ RUN      ] MapKt.test creating view
2020-07-15 13:49:19.079 map.kexe[9117:1072135] Error creating notification handler for simulator graphics quality override: 3
2020-07-15 13:49:19.082 map.kexe[9117:1072135] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'contentScale is unreasonable (NaN or Inf)'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff23e3cf0e __exceptionPreprocess + 350
    1   libobjc.A.dylib                     0x00007fff50ba89b2 objc_exception_throw + 48
    2   CoreFoundation                      0x00007fff23e3cad9 -[NSException raise] + 9
    3   VectorKit                           0x00007fff49c14143 _Z25checkForBogusContentScaled + 115
    4   VectorKit                           0x00007fff49c14252 -[VKMapView initShouldRasterize:inBackground:contentScale:auditToken:] + 62
    5   MapKit                              0x00007fff2782009a -[MKBasicMapView initWithFrame:andGlobe:shouldRasterize:] + 419
    6   MapKit                              0x00007fff27755b85 -[MKMapView _commonInitFromIB:gestureRecognizerHostView:showsAttribution:showsAppleLogo:] + 1331
    7   MapKit                              0x00007fff27756698 -[MKMapView initWithFrame:] + 253
    8   map.kexe                            0x000000010e29c530 _6d6170_knbridge0 + 64
    9   map.kexe                            0x000000010e293605 kfun:test creating view() + 1077
    10  map.kexe                            0x000000010e2938f5 kfun:$test creating view$FUNCTION_REFERENCE[=11=].invoke#internal + 37
    11  map.kexe                            0x000000010e29394e kfun:$test creating view$FUNCTION_REFERENCE[=11=].$<bridge-UNN>invoke()#internal + 62
    12  map.kexe                            0x000000010e206218 kfun:kotlin.native.internal.test.TopLevelSuite.TestCase.run() + 1112
    13  map.kexe                            0x000000010e1fe73d kfun:kotlin.native.internal.test.TestRunner.run#internal + 2445
    14  map.kexe                            0x000000010e1ff8ea kfun:kotlin.native.internal.test.TestRunner.runIteration#internal + 2954
    15  map.kexe                            0x000000010e200215 kfun:kotlin.native.internal.test.TestRunner.run()kotlin.Int + 997
    16  map.kexe                            0x000000010e1f3826 kfun:kotlin.native.internal.test.testLauncherEntryPoint(kotlin.Array<kotlin.String>)kotlin.Int + 310
    17  map.kexe                            0x000000010e1f36ca kfun:kotlin.native.internal.test.main(kotlin.Array<kotlin.String>) + 58
    18  map.kexe                            0x000000010e1f3975 Konan_start + 165
    19  map.kexe                            0x000000010e2a13ab Init_and_run_start + 107
    20  libdyld.dylib                       0x00007fff51a231fd start + 1
    21  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Child process terminated with signal 6: Abort trap

不过,我同意应该解决这种不便,以使测试更加人性化。我可以在 Kotlin 错误跟踪器上提交问题,你同意吗?