Skip to content

Commit 4318efe

Browse files
authored
Add OpenSwiftUI UITest target support (#80)
* Update Example project * Add OpenSwiftUIUITests
1 parent 84d5802 commit 4318efe

24 files changed

+874
-400
lines changed

Configurations/Example.xcconfig

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
PRODUCT_BUNDLE_IDENTIFIER=org.OpenSwiftUIProject.OpenSwiftUI.Example
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
PRODUCT_BUNDLE_IDENTIFIER=org.OpenSwiftUIProject.OpenSwiftUI.HostingExample
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
PRODUCT_BUNDLE_IDENTIFIER=org.OpenSwiftUIProject.OpenSwiftUI.OpenSwiftUIUITests
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#include "basic/OpenSwiftUI.xcconfig"
2+
#include "basic/debug.xcconfig"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#include "basic/OpenSwiftUI.xcconfig"
2+
#include "basic/release.xcconfig"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#include "basic/SwiftUI.xcconfig"
2+
#include "basic/debug.xcconfig"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#include "basic/SwiftUI.xcconfig"
2+
#include "basic/release.xcconfig"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
GCC_PREPROCESSOR_DEFINITIONS = $(inherited) OPENSWIFTUI=1
2+
SWIFT_ACTIVE_COMPILATION_CONDITIONS = $(inherited) OPENSWIFTUI

Configurations/Shared/basic/SwiftUI.xcconfig

Whitespace-only changes.

Configurations/Shared/basic/common.xcconfig

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
GCC_PREPROCESSOR_DEFINITIONS = $(inherited) DEBUG=1
2+
SWIFT_ACTIVE_COMPILATION_CONDITIONS = $(inherited) DEBUG

Configurations/Shared/basic/release.xcconfig

Whitespace-only changes.

Configurations/TestHost.xcconfig

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
PRODUCT_BUNDLE_IDENTIFIER=org.OpenSwiftUIProject.OpenSwiftUI.TestHost

Example/Example.xcodeproj/project.pbxproj

+304-26
Large diffs are not rendered by default.

Tests/OpenSwiftUIUITests/ContentView_FB13341321.swift

-105
This file was deleted.

Tests/OpenSwiftUIUITests/LaunchTests.swift

-23
This file was deleted.

Tests/OpenSwiftUIUITests/UITests.swift

-20
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
//
2+
// FB13341321.swift
3+
// Test
4+
//
5+
// Created by Kyle on 2023/11/6.
6+
//
7+
8+
// (4 toggles: C1T1 C1T2 C2T1 C2T2 - C1T1&C2T1 use the same truth, C1T2&C2T2 use the same truth )
9+
// macOS 14 behavior with SwiftUI
10+
// C1T1's UI will only be updated at most 1 time if we only tap T1.
11+
// C2T1 will always reflect the latest value in UI. A tap for T2 will make C1T1's UI up to data.
12+
// Tap on C1T2 or C2T2 will update both toggle at the same time. (Expected)
13+
//
14+
// iOS 17 behavior with SwiftUI
15+
// Tap on C2T1 will only update toggle C2T1 while the UI of C1T1 remains the same.
16+
// Tap on C1T1 will update both toggle(C1T1 & C2T1) at the same time. (Expected)
17+
// Tap on C1T2 or C2T2 will update both toggle at the same time. (Expected)
18+
//
19+
// iOS 15.5 behavior with SwiftUI
20+
// Tap on C1T1 or C2T1 will update both toggle at the same time. (Expected)
21+
// Tap on C1T2 or C2T2 will update both toggle at the same time. (Expected)
22+
// But one is with transactino and the other is not.
23+
24+
import SwiftUI
25+
import Observation
26+
27+
@available(iOS 14, *)
28+
enum FB13341321 {
29+
public struct Section: View {
30+
31+
public let content: AnyView
32+
33+
public init(
34+
@ViewBuilder content: @escaping () -> some View
35+
) {
36+
37+
self.content = content().eraseToAnyView()
38+
}
39+
40+
public var body: some View {
41+
content
42+
}
43+
}
44+
45+
@resultBuilder
46+
public struct SectionBuilder {
47+
public static func buildBlock(_ sections: Section...) -> [Section] {
48+
sections
49+
}
50+
}
51+
public struct Container: View {
52+
private let builder: () -> [Section]
53+
54+
public init(
55+
@SectionBuilder builder: @escaping () -> [Section]
56+
) {
57+
self.builder = builder
58+
}
59+
60+
public var body: some View {
61+
let sections = builder()
62+
return sections[0]
63+
}
64+
}
65+
66+
public struct Container2: View {
67+
private let sections: [Section]
68+
69+
public init(
70+
sections: [Section] = []
71+
) {
72+
self.sections = sections
73+
}
74+
75+
public var body: some View {
76+
return sections[0]
77+
}
78+
}
79+
80+
struct ContentView: View {
81+
@AppStorage("Test") private var toggle = false
82+
@State private var toggle2 = false
83+
var body: some View {
84+
Container {
85+
Section {
86+
Toggle("Demo Toggle", isOn: $toggle)
87+
Toggle("Demo Toggle2", isOn: $toggle2)
88+
}
89+
}
90+
Container2(sections: [
91+
Section {
92+
Toggle("Demo Toggle", isOn: $toggle)
93+
Toggle("Demo Toggle2", isOn: $toggle2)
94+
}
95+
])
96+
}
97+
}
98+
}
99+
100+
extension View {
101+
func eraseToAnyView() -> AnyView {
102+
AnyView(self)
103+
}
104+
}
105+
106+
@available(iOS 14, *)
107+
#Preview {
108+
FB13341321.ContentView()
109+
.padding(20)
110+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//
2+
// AnyViewTests.swift
3+
// OpenSwiftUIUITests
4+
5+
import XCTest
6+
7+
#if OPENSWIFTUI
8+
import OpenSwiftUI
9+
#else
10+
import SwiftUI
11+
#endif
12+
13+
#if os(iOS)
14+
import UIKit
15+
16+
#if !OPENSWIFTUI
17+
@available(iOS 15, *)
18+
#endif
19+
final class AnyViewTests: XCTestCase {
20+
// @Test("Attribute setter crash for basic AnyView", .bug("https://github.com/OpenSwiftUIProject/OpenGraph/issues/58", relationship: .verifiesFix))
21+
func testBasicAnyView() throws {
22+
struct ContentView: View {
23+
var body: some View {
24+
AnyView(EmptyView())
25+
}
26+
}
27+
let vc = UIHostingController(rootView: ContentView())
28+
let window = UIWindow(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
29+
window.rootViewController = vc
30+
window.makeKeyAndVisible()
31+
vc.view.layoutSubviews()
32+
}
33+
}
34+
#endif

0 commit comments

Comments
 (0)