forked from ParisiLabs/wire-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCameraKeyboardViewControllerTests.swift
211 lines (177 loc) · 8.62 KB
/
CameraKeyboardViewControllerTests.swift
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
//
// Wire
// Copyright (C) 2016 Wire Swiss GmbH
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see http://www.gnu.org/licenses/.
//
import Foundation
import XCTest
import Photos
import Cartography
import AVFoundation
@testable import Wire
class CameraKeyboardViewControllerDelegateMock: CameraKeyboardViewControllerDelegate {
var cameraKeyboardWantsToOpenCameraRollHitCount: UInt = 0
@objc func cameraKeyboardViewControllerWantsToOpenCameraRoll(_ controller: CameraKeyboardViewController) {
cameraKeyboardWantsToOpenCameraRollHitCount = cameraKeyboardWantsToOpenCameraRollHitCount + 1
}
var cameraKeyboardWantsToOpenFullScreenCameraHitCount: UInt = 0
@objc func cameraKeyboardViewControllerWantsToOpenFullScreenCamera(_ controller: CameraKeyboardViewController) {
cameraKeyboardWantsToOpenFullScreenCameraHitCount = cameraKeyboardWantsToOpenFullScreenCameraHitCount + 1
}
var cameraKeyboardDidSelectVideoHitCount: UInt = 0
@objc func cameraKeyboardViewController(_ controller: CameraKeyboardViewController, didSelectVideo: URL, duration: TimeInterval) {
cameraKeyboardDidSelectVideoHitCount = cameraKeyboardDidSelectVideoHitCount + 1
}
var cameraKeyboardViewControllerDidSelectImageDataHitCount: UInt = 0
@objc func cameraKeyboardViewController(_ controller: CameraKeyboardViewController, didSelectImageData: Data, metadata: ImageMetadata) {
cameraKeyboardViewControllerDidSelectImageDataHitCount = cameraKeyboardViewControllerDidSelectImageDataHitCount + 1
}
}
@objc class SplitLayoutObservableMock: NSObject, SplitLayoutObservable {
@objc var layoutSize: SplitViewControllerLayoutSize = .compact
@objc var leftViewControllerWidth: CGFloat = 0
}
private final class MockAssetLibrary: AssetLibrary {
fileprivate override var count: UInt { return 5 }
fileprivate override func refetchAssets(synchronous: Bool) {
// no op
}
}
final class CameraKeyboardViewControllerTests: ZMSnapshotTestCase {
var sut: CameraKeyboardViewController!
var splitView: SplitLayoutObservableMock!
var delegateMock: CameraKeyboardViewControllerDelegateMock!
var assetLibrary: AssetLibrary!
override func setUp() {
super.setUp()
self.assetLibrary = MockAssetLibrary()
self.splitView = SplitLayoutObservableMock()
self.delegateMock = CameraKeyboardViewControllerDelegateMock()
}
@discardableResult func prepareForSnapshot(_ size: CGSize = CGSize(width: 320, height: 216)) -> UIView {
self.sut.beginAppearanceTransition(true, animated: false)
self.sut.endAppearanceTransition()
let container = UIView()
container.addSubview(self.sut.view)
container.backgroundColor = ColorScheme.default().color(withName: ColorSchemeColorTextForeground, variant: .light)
constrain(self.sut.view, container) { view, container in
container.height == size.height
container.width == size.width
view.top == container.top
view.bottom == container.bottom
view.left == container.left
view.right == container.right
}
container.setNeedsLayout()
container.layoutIfNeeded()
return container
}
func testThatFirstSectionContainsCameraCellOnly() {
// given
self.sut = CameraKeyboardViewController(splitLayoutObservable: self.splitView, assetLibrary: assetLibrary)
self.sut.delegate = self.delegateMock
self.prepareForSnapshot()
// when
let cameraCell = self.sut.collectionView.cellForItem(at: IndexPath(item: 0, section: 0))
// then
XCTAssertTrue(cameraCell is CameraCell)
XCTAssertEqual(self.sut.collectionView.numberOfSections, 2)
XCTAssertEqual(self.sut.collectionView.numberOfItems(inSection: 0), 1)
}
func testThatSecondSectionContainsCameraRollElements() {
// given
self.sut = CameraKeyboardViewController(splitLayoutObservable: self.splitView, assetLibrary: assetLibrary)
self.sut.delegate = self.delegateMock
self.prepareForSnapshot()
// when
let itemCell = self.sut.collectionView.cellForItem(at: IndexPath(item: 0, section: 1))
// then
XCTAssertTrue(itemCell is AssetCell)
XCTAssertEqual(self.sut.collectionView.numberOfSections, 2)
}
func testInitialStateLayoutSizeCompact() {
// given
self.splitView?.layoutSize = .compact
// when
self.sut = CameraKeyboardViewController(splitLayoutObservable: self.splitView, assetLibrary: assetLibrary)
// then
self.verify(view: self.prepareForSnapshot())
}
func testInitialStateLayoutSizeRegularPortrait() {
// given
self.splitView?.layoutSize = .regularPortrait
self.splitView?.leftViewControllerWidth = 216
// when
self.sut = CameraKeyboardViewController(splitLayoutObservable: self.splitView, assetLibrary: assetLibrary)
// then
self.verify(view: self.prepareForSnapshot(CGSize(width: 768, height: 264)))
}
func testInitialStateLayoutSizeRegularLandscape() {
// given
self.splitView?.layoutSize = .regularLandscape
self.splitView?.leftViewControllerWidth = 216
// when
self.sut = CameraKeyboardViewController(splitLayoutObservable: self.splitView, assetLibrary: assetLibrary)
// then
self.verify(view: self.prepareForSnapshot(CGSize(width: 1024, height: 352)))
}
func testCameraScrolledHorisontallySomePercent() {
// given
self.splitView?.layoutSize = .compact
self.sut = CameraKeyboardViewController(splitLayoutObservable: self.splitView, assetLibrary: assetLibrary)
self.prepareForSnapshot()
// when
self.sut.collectionView.scrollRectToVisible(CGRect(x: 300, y: 0, width: 160, height: 10), animated: false)
// then
self.verify(view: self.prepareForSnapshot())
}
func testCameraScrolledHorisontallyAwayPercent() {
// given
self.splitView?.layoutSize = .compact
self.sut = CameraKeyboardViewController(splitLayoutObservable: self.splitView, assetLibrary: assetLibrary)
self.prepareForSnapshot()
// when
self.sut.collectionView.scrollRectToVisible(CGRect(x: 320, y: 0, width: 160, height: 10), animated: false)
// then
self.verify(view: self.prepareForSnapshot())
}
func testThatItCallsDelegateCameraRollWhenCameraRollButtonPressed() {
// given
self.sut = CameraKeyboardViewController(splitLayoutObservable: self.splitView, assetLibrary: assetLibrary)
self.sut.delegate = self.delegateMock
self.prepareForSnapshot()
// when
self.sut.cameraRollButton.sendActions(for: .touchUpInside)
// then
XCTAssertEqual(self.delegateMock.cameraKeyboardWantsToOpenCameraRollHitCount, 1)
XCTAssertEqual(self.delegateMock.cameraKeyboardWantsToOpenFullScreenCameraHitCount, 0)
XCTAssertEqual(self.delegateMock.cameraKeyboardDidSelectVideoHitCount, 0)
XCTAssertEqual(self.delegateMock.cameraKeyboardViewControllerDidSelectImageDataHitCount, 0)
}
func testThatItCallsDelegateWhenWantsToOpenFullScreenCamera() {
// given
self.sut = CameraKeyboardViewController(splitLayoutObservable: self.splitView, assetLibrary: assetLibrary)
self.sut.delegate = self.delegateMock
self.prepareForSnapshot()
// when
let cameraCell = self.sut.collectionView.cellForItem(at: IndexPath(item: 0, section: 0)) as! CameraCell
cameraCell.expandButton.sendActions(for: .touchUpInside)
// then
XCTAssertEqual(self.delegateMock.cameraKeyboardWantsToOpenCameraRollHitCount, 0)
XCTAssertEqual(self.delegateMock.cameraKeyboardWantsToOpenFullScreenCameraHitCount, 1)
XCTAssertEqual(self.delegateMock.cameraKeyboardDidSelectVideoHitCount, 0)
XCTAssertEqual(self.delegateMock.cameraKeyboardViewControllerDidSelectImageDataHitCount, 0)
}
}