Skip to content

Instrumented tests for iOS #1786

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 18 commits into
base: jb-main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,16 @@

package androidx.build

import java.io.ByteArrayOutputStream
import javax.inject.Inject
import org.gradle.api.Project
import org.gradle.api.attributes.Attribute
import org.gradle.api.tasks.Copy
import org.gradle.api.tasks.Exec
import org.gradle.kotlin.dsl.creating
import org.gradle.kotlin.dsl.dependencies
import org.gradle.kotlin.dsl.getValue
import org.gradle.kotlin.dsl.getByName
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
import org.jetbrains.kotlin.gradle.plugin.KotlinJsCompilerType
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTargetWithSimulatorTests
import org.jetbrains.kotlin.gradle.targets.native.DefaultSimulatorTestRun
import org.jetbrains.kotlin.konan.target.KonanTarget
import org.tomlj.Toml

Expand Down Expand Up @@ -283,7 +278,17 @@ open class AndroidXComposeMultiplatformExtensionImpl @Inject constructor(
addAll(darwinFlags)
if (isIOS) addAll(iosFlags)
}
it.freeCompilerArgs = it.freeCompilerArgs + flags

// TODO: Remove when the issue is fixed in KGP
// https://youtrack.jetbrains.com/issue/KT-74564
// it.freeCompilerArgs += flags
//
// Fixes problem when instrumented tests compilation is not properly applied to
// the framework configuration.
it.linkTaskProvider.configure {
@Suppress("DEPRECATION")
it.kotlinOptions.freeCompilerArgs += flags
}
}
}
multiplatformExtension.run {
Expand All @@ -295,68 +300,36 @@ open class AndroidXComposeMultiplatformExtensionImpl @Inject constructor(
}
}

// https://youtrack.jetbrains.com/issue/KT-55751/MPP-Gradle-Consumable-configurations-must-have-unique-attributes
private val instrumentedTestAttribute = Attribute.of("instrumentedTest", String::class.java)
private val instrumentedTestCompilationAttribute = Attribute.of("instrumentedTestCompilation", String::class.java)
override fun iosInstrumentedTest(): Unit =
override fun iosInstrumentedTest() {
multiplatformExtension.run {
fun getDeviceName(): String? {
return project.findProperty("iosSimulatorName") as? String
}

val bootTask = project.tasks.register("bootIosSimulator", Exec::class.java) { task ->
task.isIgnoreExitValue = true
task.errorOutput = ByteArrayOutputStream()
task.doFirst {
val simulatorName = getDeviceName()
?: error("Device is not provided. Use Use the -PiosSimulatorName=<Device name> flag to pass the device.")
task.commandLine("xcrun", "simctl", "boot", simulatorName)
}
task.doLast {
val result = task.executionResult.get()
if (result.exitValue != 148 && result.exitValue != 149) { // ignoring device already booted errors
result.assertNormalExitValue()
}
}
}
val uikitInstrumentedTest = sourceSets.create("uikitInstrumentedTest")

fun KotlinNativeTargetWithSimulatorTests.configureTestRun() {
attributes.attribute(instrumentedTestAttribute, "test")
testRuns.forEach {
(it as DefaultSimulatorTestRun).executionTask.configure { task ->
task.dependsOn(bootTask)
task.standalone.set(false)
task.device.set(getDeviceName())
val testCompilation = compilations.create("instrumentedTest") {
compilerOptions {
// Generate K/N test runner for kotlin.test @Test support
freeCompilerArgs.add("-tr")
}

it.associateWith(compilations.getByName("test"))
it.defaultSourceSet.dependsOn(uikitInstrumentedTest)
}
compilations.forEach {
it.attributes.attribute(instrumentedTestCompilationAttribute, "test")
binaries.framework("InstrumentedTest", setOf(DEBUG)) {
compilation = testCompilation
baseName = "InstrumentedTest"
isStatic = true
}
}

iosX64("uikitInstrumentedX64") {
configureTestRun()
}
// Testing on real iOS devices is not supported.
// iosArm64("uikitInstrumentedArm64") { ... }
iosSimulatorArm64("uikitInstrumentedSimArm64") {
configureTestRun()
}

val uikitMain = sourceSets.getByName("uikitMain")
val uikitInstrumentedMain = sourceSets.create("uikitInstrumentedMain")
val uikitInstrumentedX64Main = sourceSets.getByName("uikitInstrumentedX64Main")
val uikitInstrumentedSimArm64Main = sourceSets.getByName("uikitInstrumentedSimArm64Main")
uikitInstrumentedMain.dependsOn(uikitMain)
uikitInstrumentedX64Main.dependsOn(uikitInstrumentedMain)
uikitInstrumentedSimArm64Main.dependsOn(uikitInstrumentedMain)

val commonTest = sourceSets.getByName("commonTest")
val uikitInstrumentedTest = sourceSets.create("uikitInstrumentedTest")
val uikitInstrumentedX64Test = sourceSets.getByName("uikitInstrumentedX64Test")
val uikitInstrumentedSimArm64Test = sourceSets.getByName("uikitInstrumentedSimArm64Test")
uikitInstrumentedTest.dependsOn(commonTest)
uikitInstrumentedX64Test.dependsOn(uikitInstrumentedTest)
uikitInstrumentedSimArm64Test.dependsOn(uikitInstrumentedTest)
testableTargets.getByName(
"uikitX64",
KotlinNativeTargetWithSimulatorTests::class,
KotlinNativeTargetWithSimulatorTests::configureTestRun
)
testableTargets.getByName(
"uikitSimArm64",
KotlinNativeTargetWithSimulatorTests::class,
KotlinNativeTargetWithSimulatorTests::configureTestRun
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -842,7 +842,17 @@ class AndroidXImplPlugin @Inject constructor(val componentFactory: SoftwareCompo
kotlinTarget.binaries.all {
// Use std allocator to avoid the following warning:
// w: Mimalloc allocator isn't supported on target <target>. Used standard mode.
it.freeCompilerArgs += "-Xallocator=std"

// TODO: Remove when the issue is fixed in KGP
// https://youtrack.jetbrains.com/issue/KT-74564
// it.freeCompilerArgs += "-Xallocator=std"
//
// Fixes problem when instrumented tests compilation is not properly applied to
// the framework configuration.
it.linkTaskProvider.configure {
@Suppress("DEPRECATION")
it.kotlinOptions.freeCompilerArgs += "-Xallocator=std"
}
}
}
}
Expand Down
16 changes: 16 additions & 0 deletions compose/ui/ui-uikit/src/nativeInterop/cinterop/utils.def
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
#
# Copyright 2023 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

linkerOpts = -framework UIKit
package = androidx.compose.ui.uikit.utils
language = Objective-C
Expand Down
8 changes: 4 additions & 4 deletions compose/ui/ui/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,7 @@ if(AndroidXComposePlugin.isMultiplatformEnabled(project)) {
darwin()
js()
wasm()
// TODO: Migrate iosInstrumentedTest to kotlin 2.1.0
// https://youtrack.jetbrains.com/issue/CMP-7390/Migrate-iosInstrumentedTest-target-to-kotlin-2.1.0
// Kotlin 2.1.0 doesn't support declaring multiple targets of the same type
// iosInstrumentedTest()
iosInstrumentedTest()

configureDarwinFlags()
}
Expand Down Expand Up @@ -326,7 +323,10 @@ if(AndroidXComposePlugin.isMultiplatformEnabled(project)) {
}
uikitInstrumentedTest.dependencies {
implementation(project(":compose:material:material"))
implementation(project(":compose:material3:material3"))
implementation(project(":compose:foundation:foundation"))
implementation(project(":compose:ui:ui-test-junit4"))
implementation(project(":internal-testutils-xctest"))
}

desktopTest.dependencies {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2025 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package androidx.compose.ui

import androidx.compose.test.interaction.BasicInteractionTest
import androidx.compose.xctest.setupXCTestSuite
import kotlinx.cinterop.ExperimentalForeignApi
import platform.XCTest.XCTestSuite

@Suppress("unused")
@OptIn(ExperimentalForeignApi::class)
fun testSuite(): XCTestSuite = setupXCTestSuite(
// Run all test cases from the tests
// BasicInteractionTest::class,
// LayersAccessibilityTest::class,

// Run test cases from a test
BasicInteractionTest::testTextFieldCallout,
// LayersAccessibilityTest::testLayersAppearanceOrder
)
Loading