Skip to content
Merged
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
@@ -0,0 +1,158 @@
//
// ObservedObjectCompatibilityTests.swift
// OpenSwiftUICompatibilityTests

import Testing
import OpenSwiftUITestsSupport
#if OPENSWIFTUI_OPENCOMBINE
import OpenCombine
#else
import Combine
#endif

@MainActor
struct ObservedObjectCompatibilityTests {
@MainActor
enum Count {
static var contentBody: Int = 0
static var subviewBody: Int = 0
}

class Model: ObservableObject {
@Published var t1 = false
var t2 = false
}

@Observable
class Model2 {
var t1 = false
@ObservationIgnored var t2 = false
}

struct Subview: View {
var condition: Bool

var body: some View {
let _ = {
Count.subviewBody += 1
}()
Color(condition ? .red : .blue)
}
}

@Test
func observedObjectChangePublishValue() async throws {
defer {
Count.contentBody = 0
Count.subviewBody = 0
}
struct ContentView: View {
@ObservedObject private var model = Model()
var body: some View {
let _ = {
Count.contentBody += 1
}()
VStack {
Subview(condition: model.t1)
Subview(condition: model.t2)
}.onAppear {
model.t1.toggle()
}
}
}
try await triggerLayoutWithWindow(expectedCount: 0) { _ in
PlatformHostingController(
rootView: ContentView()
)
}
#expect(Count.contentBody == 2)
#expect(Count.subviewBody == 3)
}

@Test
func observedObjectChangeNonPublishValue() async throws {
defer {
Count.contentBody = 0
Count.subviewBody = 0
}
struct ContentView: View {
@ObservedObject private var model = Model()
var body: some View {
let _ = {
Count.contentBody += 1
}()
VStack {
Subview(condition: model.t1)
Subview(condition: model.t2)
}.onAppear {
model.t2.toggle()
}
}
}
try await triggerLayoutWithWindow(expectedCount: 0) { _ in
PlatformHostingController(
rootView: ContentView()
)
}
#expect(Count.contentBody == 1)
#expect(Count.subviewBody == 2)
}

@Test
func observableMacroTrackedValue() async throws {
defer {
Count.contentBody = 0
Count.subviewBody = 0
}
struct ContentView: View {
private let model = Model2()
var body: some View {
let _ = {
Count.contentBody += 1
}()
VStack {
Subview(condition: model.t1)
Subview(condition: model.t2)
}.onAppear {
model.t1.toggle()
}
}
}
try await triggerLayoutWithWindow(expectedCount: 0) { _ in
PlatformHostingController(
rootView: ContentView()
)
}
#expect(Count.contentBody == 2)
#expect(Count.subviewBody == 3)
}

@Test
func observableMacroIgnoredValue() async throws {
defer {
Count.contentBody = 0
Count.subviewBody = 0
}
struct ContentView: View {
private let model = Model2()
var body: some View {
let _ = {
Count.contentBody += 1
}()
VStack {
Subview(condition: model.t1)
Subview(condition: model.t2)
}.onAppear {
model.t2.toggle()
}
}
}
try await triggerLayoutWithWindow(expectedCount: 0) { _ in
PlatformHostingController(
rootView: ContentView()
)
}
#expect(Count.contentBody == 1)
#expect(Count.subviewBody == 2)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ struct StateCompatibilityTests {
@Test
func appear() async throws {
struct ContentView: View {
var confirmation: Confirmation
var continuation: UnsafeContinuation<Void, Never>

@State private var toggle = false

Expand All @@ -18,16 +18,16 @@ struct StateCompatibilityTests {
.onAppear {
toggle.toggle()
if toggle {
confirmation()
continuation.resume()
}
}
}
}

try await triggerLayoutWithWindow { confirmation in
try await triggerLayoutWithWindow { continuation in
PlatformHostingController(
rootView: ContentView(
confirmation: confirmation
continuation: continuation
)
)
}
Expand Down
Loading