Skip to content

Commit

Permalink
Rename AndroidBridge to SkipAndroidBridge
Browse files Browse the repository at this point in the history
  • Loading branch information
marcprux committed Nov 4, 2024
1 parent d042dc7 commit 227851d
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 95 deletions.
8 changes: 4 additions & 4 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -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")]),
]
Expand Down
32 changes: 0 additions & 32 deletions Sources/AndroidBridge/AndroidBridge.swift

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -78,3 +82,5 @@ public class AndroidContext {
#endif
}
}

#endif
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# Configuration file for https://skip.tools project
skip:
mode: 'swift'

mode: 'kotlin'
50 changes: 50 additions & 0 deletions Sources/SkipAndroidBridge/Swift/AndroidBridgeToKotlin.swift
Original file line number Diff line number Diff line change
@@ -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
}

47 changes: 0 additions & 47 deletions Tests/AndroidBridgeTests/AndroidBridgeTests.swift

This file was deleted.

56 changes: 56 additions & 0 deletions Tests/SkipAndroidBridgeTests/AndroidBridgeTests.swift
Original file line number Diff line number Diff line change
@@ -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
}
}
File renamed without changes.
File renamed without changes.

0 comments on commit 227851d

Please sign in to comment.