From 227851d2d74d297a59271438784ab57c2ac83220 Mon Sep 17 00:00:00 2001 From: Marc Prud'hommeaux Date: Mon, 4 Nov 2024 11:01:12 -0500 Subject: [PATCH] Rename AndroidBridge to SkipAndroidBridge --- Package.swift | 8 +-- Sources/AndroidBridge/AndroidBridge.swift | 32 ----------- .../AndroidBridgeToSwift.swift} | 26 +++++---- .../Skip/skip.yml | 3 +- .../Swift/AndroidBridgeToKotlin.swift | 50 +++++++++++++++++ .../AndroidBridgeTests.swift | 47 ---------------- .../AndroidBridgeTests.swift | 56 +++++++++++++++++++ .../Skip/skip.yml | 0 .../XCSkipTests.swift | 0 9 files changed, 127 insertions(+), 95 deletions(-) delete mode 100644 Sources/AndroidBridge/AndroidBridge.swift rename Sources/{AndroidBridge/Kotlin/AndroidKotlinBridge.swift => SkipAndroidBridge/AndroidBridgeToSwift.swift} (83%) rename Sources/{AndroidBridge => SkipAndroidBridge}/Skip/skip.yml (77%) create mode 100644 Sources/SkipAndroidBridge/Swift/AndroidBridgeToKotlin.swift delete mode 100644 Tests/AndroidBridgeTests/AndroidBridgeTests.swift create mode 100644 Tests/SkipAndroidBridgeTests/AndroidBridgeTests.swift rename Tests/{AndroidBridgeTests => SkipAndroidBridgeTests}/Skip/skip.yml (100%) rename Tests/{AndroidBridgeTests => SkipAndroidBridgeTests}/XCSkipTests.swift (100%) diff --git a/Package.swift b/Package.swift index 2cb9174..bdca431 100644 --- a/Package.swift +++ b/Package.swift @@ -6,7 +6,7 @@ let package = Package( defaultLocalization: "en", platforms: [.iOS(.v16), .macOS(.v13), .tvOS(.v16), .watchOS(.v9), .macCatalyst(.v16)], products: [ - .library(name: "AndroidBridge", type: .dynamic, targets: ["AndroidBridge"]), + .library(name: "SkipAndroidBridge", type: .dynamic, targets: ["SkipAndroidBridge"]), ], dependencies: [ .package(url: "https://source.skip.tools/skip.git", from: "1.1.16"), @@ -15,13 +15,13 @@ let package = Package( .package(url: "https://source.skip.tools/swift-android-native.git", branch: "main") ], targets: [ - .target(name: "AndroidBridge", dependencies: [ + .target(name: "SkipAndroidBridge", dependencies: [ .product(name: "SkipBridge", package: "skip-bridge"), .product(name: "SkipFoundation", package: "skip-foundation"), .product(name: "AndroidNative", package: "swift-android-native"), ], plugins: [.plugin(name: "skipstone", package: "skip")]), - .testTarget(name: "AndroidBridgeTests", dependencies: [ - "AndroidBridge", + .testTarget(name: "SkipAndroidBridgeTests", dependencies: [ + "SkipAndroidBridge", .product(name: "SkipTest", package: "skip"), ], plugins: [.plugin(name: "skipstone", package: "skip")]), ] diff --git a/Sources/AndroidBridge/AndroidBridge.swift b/Sources/AndroidBridge/AndroidBridge.swift deleted file mode 100644 index 204d987..0000000 --- a/Sources/AndroidBridge/AndroidBridge.swift +++ /dev/null @@ -1,32 +0,0 @@ -// Copyright 2024 Skip -// -// This is free software: you can redistribute and/or modify it -// under the terms of the GNU Lesser General Public License 3.0 -// as published by the Free Software Foundation https://fsf.org - -//import Foundation -import SkipBridge - -// Current limitations on Roboelectric testing require us to go through a compiled wrapper in order to perform our -// tests of bridging Kotlin to Swift. - -// SKIP @BridgeToKotlin -func testSupport_isSkipMode() -> Int32 { - #if SKIP - fatalError("testSupport_isSkipMode should never be transpiled") - return -1 // this should NEVER be transpiled - #else - return isSkipMode() - #endif -} - -// SKIP @BridgeToKotlin -func testSupport_getJavaSystemProperty(_ name: String) -> String? { - getJavaSystemProperty(name) -} - -// SKIP @BridgeToKotlin -func testSupport_getAndroidContext() -> AndroidContext! { - AndroidContext.shared -} - diff --git a/Sources/AndroidBridge/Kotlin/AndroidKotlinBridge.swift b/Sources/SkipAndroidBridge/AndroidBridgeToSwift.swift similarity index 83% rename from Sources/AndroidBridge/Kotlin/AndroidKotlinBridge.swift rename to Sources/SkipAndroidBridge/AndroidBridgeToSwift.swift index 2d6c698..532c158 100644 --- a/Sources/AndroidBridge/Kotlin/AndroidKotlinBridge.swift +++ b/Sources/SkipAndroidBridge/AndroidBridgeToSwift.swift @@ -5,26 +5,30 @@ // as published by the Free Software Foundation https://fsf.org import Foundation -//#if canImport(AndroidNative) -//@_exported import AndroidNative -//#elseif canImport(OSLog) -//@_exported import OSLog -//#endif -// -//fileprivate let logger: Logger = Logger(subsystem: "AndroidBridge", category: "AndroidKotlinBridge") +#if !SKIP_BRIDGE // SKIP @BridgeToSwift -func isSkipMode() -> Int32 { +public func isAndroidBridgeToSwiftTranspiled() -> Int64 { #if SKIP + //if ({ true })() { return 1 } else { return 123 } return 1 #else - return 0 + return -2 #endif } // SKIP @BridgeToSwift -func getJavaSystemProperty(_ name: String) -> String? { +public func getJavaSystemProperties() -> String? { + #if SKIP + return java.lang.System.getProperties().toString() + #else + return nil + #endif +} + +// SKIP @BridgeToSwift +public func getJavaSystemProperty(_ name: String) -> String? { #if SKIP return java.lang.System.getProperty(name) #else @@ -78,3 +82,5 @@ public class AndroidContext { #endif } } + +#endif diff --git a/Sources/AndroidBridge/Skip/skip.yml b/Sources/SkipAndroidBridge/Skip/skip.yml similarity index 77% rename from Sources/AndroidBridge/Skip/skip.yml rename to Sources/SkipAndroidBridge/Skip/skip.yml index 39298a8..c35c5ec 100644 --- a/Sources/AndroidBridge/Skip/skip.yml +++ b/Sources/SkipAndroidBridge/Skip/skip.yml @@ -1,4 +1,3 @@ # Configuration file for https://skip.tools project skip: - mode: 'swift' - + mode: 'kotlin' diff --git a/Sources/SkipAndroidBridge/Swift/AndroidBridgeToKotlin.swift b/Sources/SkipAndroidBridge/Swift/AndroidBridgeToKotlin.swift new file mode 100644 index 0000000..a12b7fd --- /dev/null +++ b/Sources/SkipAndroidBridge/Swift/AndroidBridgeToKotlin.swift @@ -0,0 +1,50 @@ +// Copyright 2024 Skip +// +// This is free software: you can redistribute and/or modify it +// under the terms of the GNU Lesser General Public License 3.0 +// as published by the Free Software Foundation https://fsf.org + +//import Foundation +import SkipBridge + +// Current limitations on Robolectric testing require us to go through a compiled wrapper in order to perform our +// tests of bridging Kotlin to Swift. + +// FIXME: JNI method loading is logged for SkipBridgeSamples, but not for AndroidBridge, and the native method invocations do nothing (returns null or 0, but they don't crash) +// [3.972s][debug][jni,resolve] [Dynamic-linking native method skip.bridge.samples.BridgeToSwiftTestsSupportKt.Swift_testSupport_appendStrings ... JNI] + +// If we use the wrong name in SkopBridgeSamples, we get a good error: +// testAndroidBridge$SkipBridgeSamples_debugUnitTest java.lang.UnsatisfiedLinkError: 'java.lang.String skip.bridge.samples.BridgeToSwiftTestsSupportKt.Swift_testSupport_appendStrings(java.lang.String, java.lang.String)' + + + +// SKIP @BridgeToKotlin +func testSupport_appendStrings(_ a: String, _ b: String) -> String { + a + b +} + +// SKIP @BridgeToKotlin +func testSupport_isSkipMode() -> Int64 { + #if SKIP + fatalError("testSupport_isSkipMode should never be transpiled") + return -1 // this should NEVER be transpiled + #else + //fatalError("### testSupport_isSkipMode NEVER CALLED") + let mode = isAndroidBridgeToSwiftTranspiled() + if mode == 0 { + fatalError("### MODE WAS 0!") + } + return mode + #endif +} + +// SKIP @BridgeToKotlin +func testSupport_getJavaSystemProperty(_ name: String) -> String? { + getJavaSystemProperty(name) +} + +// SKIP @BridgeToKotlin +func testSupport_getAndroidContext() -> AndroidContext! { + AndroidContext.shared +} + diff --git a/Tests/AndroidBridgeTests/AndroidBridgeTests.swift b/Tests/AndroidBridgeTests/AndroidBridgeTests.swift deleted file mode 100644 index f567995..0000000 --- a/Tests/AndroidBridgeTests/AndroidBridgeTests.swift +++ /dev/null @@ -1,47 +0,0 @@ -// Copyright 2024 Skip -// -// This is free software: you can redistribute and/or modify it -// under the terms of the GNU Lesser General Public License 3.0 -// as published by the Free Software Foundation https://fsf.org - -import XCTest -import OSLog -import Foundation -import SkipBridge -@testable import AndroidBridge - -let logger: Logger = Logger(subsystem: "SkipAndroidBridge", category: "Tests") - -@available(macOS 13, *) -final class AndroidBridgeTests: XCTestCase { - override func setUp() { - #if SKIP - loadPeerLibrary(packageName: "skip-android-bridge", moduleName: "AndroidBridge") - #endif - } - - func testAndroidBridge() throws { - let mode = testSupport_isSkipMode() - let tmpdir = testSupport_getJavaSystemProperty("java.io.tmpdir") - #if SKIP - //XCTAssertEqual("/data/user/0/android.bridge.test/cache", tmpdir) - XCTAssertEqual(isRobolectric ? 0 : 1, mode, "@BridgeToSwift should be transpiled: \(mode)") - #else - //XCTAssertEqual(nil, tmpdir) - XCTAssertEqual(0, mode, "@BridgeToSwift should NOT be transpiled: \(mode)") - #endif - - logger.log("running testSkipAndroidBridge") - let context = testSupport_getAndroidContext() - #if !SKIP - XCTAssertNil(context) - #else - XCTAssertNotNil(ProcessInfo.processInfo.androidContext, "ProcessInfo.processInfo.androidContext was nil") - if !isRobolectric { - XCTAssertNotNil(context, "bridged context was nil") - XCTAssertEqual("/data/user/0/android.bridge.test/files", context.filesDir) - XCTAssertEqual("/data/user/0/android.bridge.test/cache", context.cacheDir) - } - #endif - } -} diff --git a/Tests/SkipAndroidBridgeTests/AndroidBridgeTests.swift b/Tests/SkipAndroidBridgeTests/AndroidBridgeTests.swift new file mode 100644 index 0000000..6170916 --- /dev/null +++ b/Tests/SkipAndroidBridgeTests/AndroidBridgeTests.swift @@ -0,0 +1,56 @@ +// Copyright 2024 Skip +// +// This is free software: you can redistribute and/or modify it +// under the terms of the GNU Lesser General Public License 3.0 +// as published by the Free Software Foundation https://fsf.org + +import XCTest +import OSLog +import Foundation +import SkipBridge +@testable import SkipAndroidBridge + +let logger: Logger = Logger(subsystem: "SkipAndroidBridge", category: "Tests") + +@available(macOS 13, *) +final class AndroidBridgeTests: XCTestCase { + override func setUp() { + #if SKIP + loadPeerLibrary(packageName: "skip-android-bridge", moduleName: "SkipAndroidBridge") + #endif + } + + func testSampleFunction() throws { + XCTAssertEqual("ABCXYZ", testSupport_appendStrings("ABC", "XYZ")) + } + + func testAndroidBridge() throws { + let mode = testSupport_isSkipMode() + XCTAssertEqual(isJava ? 1 : -2, mode, "@BridgeToSwift should be transpiled: \(mode)") + + #if SKIP + let tmpdir = testSupport_getJavaSystemProperty("java.io.tmpdir") + XCTAssertEqual(isRobolectric ? NSTemporaryDirectory() : "/data/user/0/skip.android.bridge.test/cache", tmpdir) + + XCTAssertNotNil(ProcessInfo.processInfo.androidContext, "ProcessInfo.processInfo.androidContext was nil") + + let context = AndroidContext.shared + + XCTAssertNotNil(context, "bridged context was nil") + + let filesDir = URL(fileURLWithPath: context.filesDir, isDirectory: true) + let cacheDir = URL(fileURLWithPath: context.cacheDir, isDirectory: true) + + // Robolectric's files folder is tough to predict /var/folders/zl/wkdjv4s1271fbm6w0plzknkh0000gn/T/robolectric-AndroidBridgeTests_testAndroidBridge_SkipAndroidBridge_debugUnitTest10131350412654065418/skip.android.bridge.test-dataDir/files + if !isRobolectric { + // …but Android is predictable + XCTAssertEqual("/data/user/0/skip.android.bridge.test/files", filesDir) + XCTAssertEqual("/data/user/0/skip.android.bridge.test/cache", cacheDir) + } + + // make sure we can read and write to the filesDir + try "ABC".write(to: filesDir.appendingPathComponent("test.txt"), atomically: true, encoding: .utf8) + try "XYZ".write(to: cacheDir.appendingPathComponent("test.txt"), atomically: true, encoding: .utf8) + #endif + } +} diff --git a/Tests/AndroidBridgeTests/Skip/skip.yml b/Tests/SkipAndroidBridgeTests/Skip/skip.yml similarity index 100% rename from Tests/AndroidBridgeTests/Skip/skip.yml rename to Tests/SkipAndroidBridgeTests/Skip/skip.yml diff --git a/Tests/AndroidBridgeTests/XCSkipTests.swift b/Tests/SkipAndroidBridgeTests/XCSkipTests.swift similarity index 100% rename from Tests/AndroidBridgeTests/XCSkipTests.swift rename to Tests/SkipAndroidBridgeTests/XCSkipTests.swift