forked from evgenyneu/Cosmos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CosmosLayersTests.swift
344 lines (245 loc) · 11.3 KB
/
CosmosLayersTests.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
import XCTest
@testable import Cosmos
class CosmosLayersTests: XCTestCase {
// MARK: - Position star layers
func testPositionStarLayers() {
let layerOne = CALayer()
layerOne.bounds = CGRect(origin: CGPoint(), size: CGSize(width: 20, height: 2))
let layerTwo = CALayer()
layerTwo.bounds = CGRect(origin: CGPoint(), size: CGSize(width: 20, height: 2))
let layerThree = CALayer()
layerThree.bounds = CGRect(origin: CGPoint(), size: CGSize(width: 20, height: 2))
let layers = [layerOne, layerTwo, layerThree]
CosmosLayers.positionStarLayers(layers, starMargin: 5)
XCTAssertEqual(0, layerOne.position.x)
XCTAssertEqual(25, layerTwo.position.x)
XCTAssertEqual(50, layerThree.position.x)
}
// MARK: - Create partially filled star
func testCreatePartiallyFilledStar_verifyTextAndStyles() {
var settings = CosmosSettings()
settings.starSize = 19
settings.emptyColor = UIColor.purple
settings.emptyBorderColor = UIColor.blue
settings.emptyBorderWidth = 1.31
settings.filledColor = UIColor.red
settings.filledBorderColor = UIColor.cyan
settings.filledBorderWidth = 1.72
let result = CosmosLayers.createPartialStar(0.8, settings: settings, isRightToLeft: false)
XCTAssertEqual(2, result.sublayers!.count)
// Check layer
// ------------------
XCTAssertEqual(19, result.bounds.size.width)
XCTAssertEqual(19, result.bounds.size.height)
// Check empty container layer
// ------------------
let emptyContainerLayer = result.sublayers![0] as CALayer
XCTAssertEqual(19, emptyContainerLayer.bounds.size.width)
XCTAssertEqual(19, emptyContainerLayer.bounds.size.height)
// Check empty shape layer
// ------------------
let emptyShapeLayer = emptyContainerLayer.sublayers![0] as! CAShapeLayer
XCTAssertEqual(UIColor.purple, UIColor(cgColor: emptyShapeLayer.fillColor!))
XCTAssertEqual(UIColor.blue, UIColor(cgColor: emptyShapeLayer.strokeColor!))
XCTAssertEqual(1.31, emptyShapeLayer.lineWidth)
// Check filled container layer
// ------------------
let filledContainerLayer = result.sublayers![1] as CALayer
XCTAssertEqual(19 * 0.8, filledContainerLayer.bounds.size.width)
XCTAssertEqual(19, filledContainerLayer.bounds.size.height)
// Check filled shape layer color
// ------------------
let filledShapeLayer = filledContainerLayer.sublayers![0] as! CAShapeLayer
XCTAssertEqual(UIColor.red, UIColor(cgColor: filledShapeLayer.fillColor!))
XCTAssertEqual(UIColor.cyan, UIColor(cgColor: filledShapeLayer.strokeColor!))
XCTAssertEqual(1.72, filledShapeLayer.lineWidth)
}
// MARK: - Create composite star layer
func testCreateCompositeStarLayer_fullStar() {
var settings = CosmosSettings()
settings.starSize = 12
settings.filledColor = UIColor.yellow
settings.filledBorderColor = UIColor.green
settings.emptyColor = UIColor.red
settings.emptyBorderColor = UIColor.gray
let result = CosmosLayers.createCompositeStarLayer(1, settings: settings, isRightToLeft: false) as CALayer
XCTAssertEqual(1, result.sublayers!.count)
// Check filled container layer
// ------------------
XCTAssertEqual(12, result.bounds.size.width)
XCTAssertEqual(12, result.bounds.size.height)
// Check filled shape layer color
// ------------------
let filledShapeLayer = result.sublayers![0] as! CAShapeLayer
XCTAssertEqual(UIColor.yellow, UIColor(cgColor: filledShapeLayer.fillColor!))
XCTAssertEqual(UIColor.green, UIColor(cgColor: filledShapeLayer.strokeColor!))
}
func testCreateCompositeStarLayer_emptyStar() {
var settings = CosmosSettings()
settings.starSize = 61
settings.emptyColor = UIColor.purple
settings.emptyBorderColor = UIColor.blue
settings.emptyBorderWidth = 1.31
let result = CosmosLayers.createCompositeStarLayer(0, settings: settings, isRightToLeft: false) as CALayer
XCTAssertEqual(1, result.sublayers!.count)
// Check empty container layer
// ------------------
XCTAssertEqual(61, result.bounds.size.width)
XCTAssertEqual(61, result.bounds.size.height)
// Check empty shape layer color
// ------------------
let emptyShapeLayer = result.sublayers![0] as! CAShapeLayer
XCTAssertEqual(UIColor.purple, UIColor(cgColor: emptyShapeLayer.fillColor!))
XCTAssertEqual(UIColor.blue, UIColor(cgColor: emptyShapeLayer.strokeColor!))
XCTAssertEqual(1.31, emptyShapeLayer.lineWidth)
}
func testCreateCompositeStarLayer_partiallyFilled() {
var settings = CosmosSettings()
settings.starSize = 14
settings.emptyColor = UIColor.purple
settings.emptyBorderColor = UIColor.blue
settings.emptyBorderWidth = 1.31
settings.filledColor = UIColor.yellow
settings.filledBorderColor = UIColor.magenta
settings.filledBorderWidth = 0.41
let result = CosmosLayers.createCompositeStarLayer(0.2, settings: settings, isRightToLeft: false)
XCTAssertEqual(2, result.sublayers!.count)
// Check layer
// ------------------
XCTAssertEqual(14, result.bounds.size.width)
XCTAssertEqual(14, result.bounds.size.height)
// Check empty container layer
// ------------------
let emptyContainerLayer = result.sublayers![0] as CALayer
XCTAssertEqual(14, emptyContainerLayer.bounds.size.width)
XCTAssertEqual(14, emptyContainerLayer.bounds.size.height)
// Check empty shape layer
// ------------------
let emptyShapeLayer = emptyContainerLayer.sublayers![0] as! CAShapeLayer
XCTAssertEqual(UIColor.purple, UIColor(cgColor: emptyShapeLayer.fillColor!))
XCTAssertEqual(UIColor.blue, UIColor(cgColor: emptyShapeLayer.strokeColor!))
XCTAssertEqual(1.31, emptyShapeLayer.lineWidth)
// Check filled container layer
// ------------------
let filledContainerLayer = result.sublayers![1] as CALayer
XCTAssertEqual(14 * 0.2, filledContainerLayer.bounds.size.width)
XCTAssertEqual(14, filledContainerLayer.bounds.size.height)
// Check filled shape layer color
// ------------------
let filledShapeLayer = filledContainerLayer.sublayers![0] as! CAShapeLayer
XCTAssertEqual(UIColor.yellow, UIColor(cgColor: filledShapeLayer.fillColor!))
XCTAssertEqual(UIColor.magenta, UIColor(cgColor: filledShapeLayer.strokeColor!))
XCTAssertEqual(0.41, filledShapeLayer.lineWidth)
}
// MARK: - Create star layers
func testCreateStarLayers() {
var settings = CosmosSettings()
settings.fillMode = .precise
settings.starSize = 17
settings.starMargin = 5
settings.totalStars = 100 // Crazy, huh? But still way less than 100,000,000,000 to 400,000,000,000 stars in our Milky Way galaxy. 🌌
let result = CosmosLayers.createStarLayers(3.7, settings: settings, isRightToLeft: false)
XCTAssertEqual(100, result.count)
XCTAssertEqual(17, result.last!.bounds.width)
// 100 * 10 (width of the stars)
// + 99 * 5 (margin between stars)
XCTAssertEqual(Double(99 * 17 + 99 * 5), Double(result.last!.position.x))
// Order is left-to-right, the fourth star is partial - contains two layers
XCTAssertEqual(2, result[3].sublayers!.count)
}
func testCreateStarLayers_rightToLeft() {
var settings = CosmosSettings()
settings.fillMode = .precise
settings.starSize = 17
settings.starMargin = 5
settings.totalStars = 100
let result = CosmosLayers.createStarLayers(3.7, settings: settings, isRightToLeft: true)
XCTAssertEqual(100, result.count)
XCTAssertEqual(17, result.last!.bounds.width)
// 100 * 10 (width of the stars)
// + 99 * 5 (margin between stars)
XCTAssertEqual(Double(99 * 17 + 99 * 5), Double(result.last!.position.x))
// Order is left-to-right, the fourth star from the end is partial - contains two layers
XCTAssertEqual(2, result[96].sublayers!.count)
}
// MARK: - Create star layers with images
func testCreateStarLayers_filledImage() {
var settings = CosmosSettings()
settings.fillMode = .precise
settings.starSize = 17
settings.starMargin = 5
settings.filledImage = CosmosTest.uiImageFromFile("35px.jpg")
let result = CosmosLayers.createStarLayers(3.7, settings: settings, isRightToLeft: false)
// First star
// --------
// The first star is filled - contains one layer with star from an image
var starLayer = result[0]
XCTAssertEqual(1, starLayer.sublayers?.count)
// The image layer should contain one sublayer that shows the image
var starImage = starLayer.sublayers?[0]
let image = starImage!.contents as! CGImage
// Check the image is shown
XCTAssertEqual(35, image.width)
// Last star
// --------
// The last star is empty - contains one layer with drawn star
starLayer = result[4]
XCTAssertEqual(1, starLayer.sublayers?.count)
// The image layer should contain one sublayer that draws the image
starImage = starLayer.sublayers?[0]
XCTAssertNil(starImage!.contents)
XCTAssert(starImage is CAShapeLayer)
}
func testCreateStarLayers_emptyImage() {
var settings = CosmosSettings()
settings.fillMode = .precise
settings.starSize = 17
settings.starMargin = 5
settings.emptyImage = CosmosTest.uiImageFromFile("67px.png")
let result = CosmosLayers.createStarLayers(3.7, settings: settings, isRightToLeft: false)
// First star
// --------
// The first star is filled - contains one layer that draws the image
var starLayer = result[0]
XCTAssertEqual(1, starLayer.sublayers?.count)
// The image layer should contain one sublayer that shows the image
var starImage = starLayer.sublayers?[0]
XCTAssertNil(starImage!.contents)
XCTAssert(starImage is CAShapeLayer)
// Last star
// --------
// The last star is empty - contains one layer
starLayer = result[4]
XCTAssertEqual(1, starLayer.sublayers?.count)
// The image layer should contain one sublayer that shows the image
starImage = starLayer.sublayers?[0]
let image = starImage!.contents as! CGImage
// Check the image is shown
XCTAssertEqual(67, image.width)
}
func testCreateStarLayers_drawStarIfImageIsNotSpecified() {
var settings = CosmosSettings()
settings.fillMode = .precise
settings.starSize = 17
settings.starMargin = 5
let result = CosmosLayers.createStarLayers(3.7, settings: settings, isRightToLeft: false)
// Filled star
// --------
// The first star is filled - contains one layer
let firstStar = result[0]
XCTAssertEqual(1, firstStar.sublayers?.count)
// The image layer should contain one sublayer that draws the image
let firstStarImage = firstStar.sublayers?[0]
XCTAssertNil(firstStarImage!.contents)
XCTAssert(firstStarImage is CAShapeLayer)
// Empty star
// --------
// The last star is empty - contains one layer
let lastStar = result[4]
XCTAssertEqual(1, lastStar.sublayers?.count)
// The image layer should contain one sublayer that draws the image
let lastStarImage = lastStar.sublayers?[0]
XCTAssertNil(lastStarImage!.contents)
XCTAssert(lastStarImage is CAShapeLayer)
}
}