1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
@testable import Grain
import SwiftUI
import XCTest
/// The create sheet once photos have been chosen, and the profile on each of
/// its tabs. Both are most of a screen that only exists past a step a render
/// can't take — picking from the library, or tapping a tab.
@MainActor
final class CreateSheetRenderTests: GrainTestCase {
private var account: TestAccount!
/// Async overrides: the synchronous `setUp`/`tearDown` are nonisolated, so
/// touching main-actor state from them warns.
override func setUp() async throws {
try await super.setUp()
account = TestAccount()
MockURLProtocol.respondByPath(Fixtures.routes)
}
override func tearDown() async throws {
MockURLProtocol.handler = nil
account.restore()
try await super.tearDown()
}
private let settle: TimeInterval = 0.4
private func makeItems(_ count: Int, withExif: Bool = false, alt: Bool = false) -> [PhotoItem] {
let exif = ExifSummary(
camera: "Fujifilm X100V",
lens: "23mm f/2",
exposure: nil,
shutterSpeed: "1/500s",
iso: "ISO 400",
focalLength: "35mm",
aperture: "f/2"
)
return (0 ..< count).map { index in
let size = index.isMultiple(of: 2)
? CGSize(width: 120, height: 80)
: CGSize(width: 80, height: 120)
let image = UIGraphicsImageRenderer(size: size).image { context in
context.cgContext.setFillColor(red: 0.3, green: 0.5, blue: 0.7, alpha: 1)
context.cgContext.fill(CGRect(origin: .zero, size: size))
}
return PhotoItem(
thumbnail: image,
carouselPreview: image,
source: .camera(image, metadata: nil),
alt: alt ? "Alt text for photo \(index)" : "",
exifSummary: withExif && index.isMultiple(of: 2) ? exif : nil
)
}
}
private func renderSheet(_ items: [PhotoItem], env: TestEnvironment) {
ViewRender.render(
CreateGalleryView(client: env.client, onCreated: {}, photoItems: items)
.withTestEnvironment(env),
settle: settle
)
}
// MARK: - Create sheet
/// The state the sheet is actually used in: photos picked, editor up, Post
/// enabled.
func testRendersTheCreateSheetWithPhotosChosen() {
let env = TestEnvironment()
renderSheet(makeItems(4), env: env)
}
/// Exif and alt text each add a row under the carousel.
func testRendersTheCreateSheetWithExifAndAltText() {
let env = TestEnvironment()
renderSheet(makeItems(3, withExif: true, alt: true), env: env)
}
/// One photo has no strip to scroll; a full gallery does.
func testRendersTheCreateSheetAtBothEndsOfItsRange() {
let env = TestEnvironment()
renderSheet(makeItems(1), env: env)
renderSheet(makeItems(9), env: env)
}
/// Before the picker returns, which is the state it opens in.
func testRendersTheCreateSheetWithNothingChosenYet() {
let env = TestEnvironment()
renderSheet([], env: env)
}
/// A resumed publish puts the pending bar up over the sheet.
func testRendersTheCreateSheetWhileAGalleryIsStillGoingOut() {
let env = TestEnvironment()
let root = URL(fileURLWithPath: NSTemporaryDirectory())
.appendingPathComponent("createsheet-\(UUID().uuidString)", isDirectory: true)
defer { try? FileManager.default.removeItem(at: root) }
let store = GalleryDraftStore(root: root)
store.save(GalleryDraft(
repo: "did:plc:test",
title: "Still going",
description: "",
labels: [],
location: nil,
includeExif: false,
postToBluesky: false,
createdAt: DateFormatting.nowISO(),
galleryRkey: TID.next(),
blueskyRkey: TID.next(),
photos: []
))
env.uploadCenter.accountChanged(to: "did:plc:test")
renderSheet(makeItems(2), env: env)
}
// MARK: - Profile tabs
/// Each tab is a different grid with a different fetch behind it; the
/// profile opens on the first, so the others are otherwise unreachable.
func testRendersEachProfileTab() {
let env = TestEnvironment()
for mode in ProfileViewMode.allCases {
ViewRender.render(
ProfileView(client: env.client, did: "did:plc:test", isRoot: true, viewMode: mode)
.withTestEnvironment(env),
settle: settle
)
}
}
/// Someone else's profile has no archive of your own stories and a
/// different set of actions.
func testRendersEachTabOfSomeoneElsesProfile() {
let env = TestEnvironment()
for mode in ProfileViewMode.allCases {
ViewRender.render(
ProfileView(client: env.client, did: "did:plc:other", isRoot: false, viewMode: mode)
.withTestEnvironment(env),
settle: settle
)
}
}
/// A tab whose fetch fails shows its own empty state rather than blanking
/// the whole profile.
func testRendersAProfileTabWhoseContentFailsToLoad() {
MockURLProtocol.respondByPath(["actor.getProfile": Fixtures.profileDetailed], fallback: "{}")
let env = TestEnvironment()
for mode in ProfileViewMode.allCases {
ViewRender.render(
ProfileView(client: env.client, did: "did:plc:test", isRoot: true, viewMode: mode)
.withTestEnvironment(env),
settle: 0.3
)
}
}
/// The favorites tab is the one with the disk cache behind it, so it can
/// have content before its fetch lands.
func testRendersTheFavoritesTabForYourOwnProfile() {
let env = TestEnvironment()
ViewRender.render(
ProfileView(client: env.client, did: "did:plc:test", isRoot: true, viewMode: .favorites)
.withTestEnvironment(env),
settle: 0.5
)
}
func testRendersTheProfileModesRawValues() {
XCTAssertEqual(ProfileViewMode.allCases.map(\.rawValue), ["grid", "favorites", "stories"])
}
}