forked from ParisiLabs/wire-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextMessageCellTests.swift
212 lines (175 loc) · 8.42 KB
/
TextMessageCellTests.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
211
212
//
// 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/.
//
@testable import Wire
class TextMessageCellTests: ZMSnapshotTestCase {
var sut: TextMessageCell!
var layoutProperties: ConversationCellLayoutProperties {
let layoutProperties = ConversationCellLayoutProperties()
layoutProperties.showSender = true
layoutProperties.showBurstTimestamp = false
layoutProperties.showUnreadMarker = false
return layoutProperties
}
override func setUp() {
super.setUp()
Settings.shared().likeTutorialCompleted = true
snapshotBackgroundColor = UIColor.white
accentColor = .strongBlue
sut = TextMessageCell(style: .default, reuseIdentifier: name!)
sut.layer.speed = 0
[Message.shortVersionDateFormatter(), Message.longVersionTimeFormatter()].forEach {
$0.locale = NSLocale(localeIdentifier: "en_US") as Locale!
$0.timeZone = NSTimeZone(forSecondsFromGMT: 0) as TimeZone!
}
}
func testThatItRendersATextMessage_Sent() {
sut.setSelected(true, animated: false)
sut.configure(for: mockMessage(state: .sent), layoutProperties: layoutProperties)
verify(view: sut.prepareForSnapshot())
}
func testThatItRendersATextMessage_Obfuscated() {
sut.configure(for: mockMessage(state: .sent, obfuscated: true), layoutProperties: layoutProperties)
verify(view: sut.prepareForSnapshot())
}
func testThatItRendersATextMessage_Delivered() {
sut.setSelected(true, animated: false)
sut.configure(for: mockMessage(state: .delivered), layoutProperties: layoutProperties)
verify(view: sut.prepareForSnapshot())
}
func testThatItRendersATextMessage_Expired() {
sut.configure(for: mockMessage(state: .failedToSend), layoutProperties: layoutProperties)
verify(view: sut.prepareForSnapshot())
}
func testThatItRendersATextMessage_Selected() {
sut.setSelected(true, animated: false)
sut.configure(for: mockMessage(), layoutProperties: layoutProperties)
verify(view: sut.prepareForSnapshot())
}
func testThatItRendersATextMessage_Pending_Selected() {
sut.setSelected(true, animated: false)
sut.configure(for: mockMessage(state: .pending), layoutProperties: layoutProperties)
verify(view: sut.prepareForSnapshot())
}
func testThatItRendersATextMessage_LongText() {
let text = "".padding(toLength: 71, withPad: "Hello ", startingAt: 0)
sut.configure(for: mockMessage(text), layoutProperties: layoutProperties)
verify(view: sut.prepareForSnapshot())
}
func testThatItRendersEditedTimestampCorrectly_Selected() {
sut.setSelected(true, animated: false)
sut.configure(for: mockMessage(edited: true), layoutProperties: layoutProperties)
verify(view: sut.prepareForSnapshot())
}
func testThatItRendersEditedTimestampCorrectly_Selected_LongText() {
let text = "".padding(toLength: 70, withPad: "Hello ", startingAt: 0)
sut.setSelected(true, animated: false)
sut.configure(for: mockMessage(text, edited: true), layoutProperties: layoutProperties)
verify(view: sut.prepareForSnapshot())
}
func testThatItRendersEditedTimestampCorrectly_Selected_LongText_Pending() {
let text = "".padding(toLength: 70, withPad: "Hello ", startingAt: 0)
sut.setSelected(true, animated: false)
sut.configure(for: mockMessage(text, edited: true, state: .pending), layoutProperties: layoutProperties)
verify(view: sut.prepareForSnapshot())
}
func testThatRenderLastSentMessageWithoutLikeIcon() {
let layoutProperties = self.layoutProperties
layoutProperties.alwaysShowDeliveryState = true
sut.configure(for: mockMessage(state: .sent), layoutProperties: layoutProperties)
verify(view: sut.prepareForSnapshot())
}
func testThatRenderLastSentMessageWithLikeIcon_whenSelected() {
let layoutProperties = self.layoutProperties
layoutProperties.alwaysShowDeliveryState = true
sut.setSelected(true, animated: false)
sut.configure(for: mockMessage(state: .sent), layoutProperties: layoutProperties)
verify(view: sut.prepareForSnapshot())
}
func testThatItRendersATextMessage_LikedReceiver() {
let message = mockMessage(state: .sent)
message.backingUsersReaction = [ZMMessageReaction.Like.rawValue: [otherUsers.first!]]
sut.configure(for: message, layoutProperties: layoutProperties)
verify(view: sut.prepareForSnapshot())
}
func testThatItRendersATextMessage_LikedSender() {
let message = mockMessage(state: .sent)
message.backingUsersReaction = [ZMMessageReaction.Like.rawValue: [selfUser]]
sut.configure(for: message, layoutProperties: layoutProperties)
verify(view: sut.prepareForSnapshot())
}
func testThatItRendersATextMessage_LikedSelected() {
let message = mockMessage(state: .sent)
message.backingUsersReaction = [ZMMessageReaction.Like.rawValue: [selfUser]]
sut.setSelected(true, animated: false)
sut.configure(for: message, layoutProperties: layoutProperties)
verify(view: sut.prepareForSnapshot())
}
func testThatItRendersATextMessage_LikedByTwoPeople() {
let message = mockMessage(state: .sent)
message.backingUsersReaction = [ZMMessageReaction.Like.rawValue: Array(otherUsers[0..<2])]
sut.configure(for: message, layoutProperties: layoutProperties)
verify(view: sut.prepareForSnapshot())
}
func testThatItRendersATextMessage_LikedByTwoPeopleIncludingSelf() {
let message = mockMessage(state: .sent)
message.backingUsersReaction = [ZMMessageReaction.Like.rawValue: [selfUser] + [otherUsers.first!]]
sut.configure(for: message, layoutProperties: layoutProperties)
verify(view: sut.prepareForSnapshot())
}
func testThatItRendersATextMessage_LikedByALotOfPeople() {
let message = mockMessage(state: .sent)
message.backingUsersReaction = [ZMMessageReaction.Like.rawValue: [selfUser] + otherUsers]
sut.configure(for: message, layoutProperties: layoutProperties)
verify(view: sut.prepareForSnapshot())
}
func testThatItRendersATextMessage_LikeTooltipNotShownForSelf() {
Settings.shared().likeTutorialCompleted = false
sut.setSelected(true, animated: false)
sut.configure(for: mockMessage(), layoutProperties: layoutProperties)
verify(view: sut.prepareForSnapshot())
}
func testThatItRendersATextMessage_LikeTooltipShownForOther() {
Settings.shared().likeTutorialCompleted = false
let message = mockMessage()
message.sender = self.otherUsers.first
sut.setSelected(true, animated: false)
sut.configure(for: message, layoutProperties: layoutProperties)
verify(view: sut.prepareForSnapshot())
}
// MARK: - Helper
func mockMessage(_ text: String? = "Hello World", edited: Bool = false, state: ZMDeliveryState = .delivered, obfuscated: Bool = false) -> MockMessage {
let message = MockMessageFactory.textMessage(withText: text)
message?.deliveryState = state
message?.isObfuscated = obfuscated
message?.serverTimestamp = Date(timeIntervalSince1970: 1234567230)
message?.updatedAt = edited ? Date(timeIntervalSince1970: 0) : nil
return message!
}
var selfUser: ZMUser {
return (MockUser.mockSelf() as AnyObject) as! ZMUser
}
var otherUsers: [ZMUser] {
return MockUser.mockUsers().map { $0 }
}
}
private extension TextMessageCell {
func prepareForSnapshot() -> UIView {
self.backgroundColor = .clear
return self.wrapInTableView()
}
}