-
Notifications
You must be signed in to change notification settings - Fork 220
/
FrameProviderTests.swift
574 lines (512 loc) · 25.2 KB
/
FrameProviderTests.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
// Created by Bryan Keller on 3/31/20.
// Copyright © 2020 Airbnb Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import XCTest
@testable import HorizonCalendar
// MARK: - FrameProviderTests
final class FrameProviderTests: XCTestCase {
// MARK: Internal
override func setUp() {
let size = CGSize(width: 320, height: 480)
let monthHeaderHeight = CGFloat(50)
let lowerBoundDate = calendar.date(from: DateComponents(year: 2020, month: 05, day: 15))!
let upperBoundDate = calendar.date(from: DateComponents(year: 2020, month: 07, day: 20))!
verticalFrameProvider = FrameProvider(
content: CalendarViewContent(
calendar: calendar,
visibleDateRange: Date.distantPast...Date.distantFuture,
monthsLayout: .vertical(options: VerticalMonthsLayoutOptions()))
.withMonthDayInsets(UIEdgeInsets(top: 5, left: 8, bottom: 5, right: 8))
.withInterMonthSpacing(20)
.withVerticalDayMargin(20)
.withHorizontalDayMargin(10),
size: size,
layoutMargins: .zero,
scale: 3,
monthHeaderHeight: monthHeaderHeight)
verticalPinnedDaysOfWeekFrameProvider = FrameProvider(
content: CalendarViewContent(
calendar: calendar,
visibleDateRange: Date.distantPast...Date.distantFuture,
monthsLayout: .vertical(options: VerticalMonthsLayoutOptions(pinDaysOfWeekToTop: true)))
.withMonthDayInsets(UIEdgeInsets(top: 5, left: 8, bottom: 5, right: 8))
.withInterMonthSpacing(20)
.withVerticalDayMargin(20)
.withHorizontalDayMargin(10),
size: size,
layoutMargins: .zero,
scale: 3,
monthHeaderHeight: monthHeaderHeight)
verticalPartialMonthFrameProvider = FrameProvider(
content: CalendarViewContent(
calendar: calendar,
visibleDateRange: lowerBoundDate...upperBoundDate,
monthsLayout: .vertical(
options: VerticalMonthsLayoutOptions(alwaysShowCompleteBoundaryMonths: false)))
.withMonthDayInsets(UIEdgeInsets(top: 5, left: 8, bottom: 5, right: 8))
.withInterMonthSpacing(20)
.withVerticalDayMargin(20)
.withHorizontalDayMargin(10),
size: size,
layoutMargins: .zero,
scale: 3,
monthHeaderHeight: monthHeaderHeight)
horizontalFrameProvider = FrameProvider(
content: CalendarViewContent(
calendar: calendar,
visibleDateRange: Date.distantPast...Date.distantFuture,
monthsLayout: .horizontal(monthWidth: 300))
.withMonthDayInsets(UIEdgeInsets(top: 5, left: 8, bottom: 5, right: 8))
.withInterMonthSpacing(20)
.withVerticalDayMargin(20)
.withHorizontalDayMargin(10),
size: size,
layoutMargins: .zero,
scale: 3,
monthHeaderHeight: monthHeaderHeight)
}
func testDaySize() {
let size1 = verticalFrameProvider.daySize.alignedToPixels(forScreenWithScale: 3)
let expectedSize1 = CGSize(width: 34.857142857142854, height: 34.857142857142854)
.alignedToPixels(forScreenWithScale: 3)
XCTAssert(size1 == expectedSize1, "Incorrect day size.")
let size2 = verticalPinnedDaysOfWeekFrameProvider.daySize.alignedToPixels(forScreenWithScale: 3)
let expectedSize2 = CGSize(width: 34.857142857142854, height: 34.857142857142854)
.alignedToPixels(forScreenWithScale: 3)
XCTAssert(size2 == expectedSize2, "Incorrect day size.")
let size3 = horizontalFrameProvider.daySize.alignedToPixels(forScreenWithScale: 3)
let expectedSize3 = CGSize(width: 32, height: 32).alignedToPixels(forScreenWithScale: 3)
XCTAssert(size3 == expectedSize3, "Incorrect day size.")
}
// MARK: Test initial month calculations
func testInitialMonthHeaderFrame() {
let frame1 = verticalFrameProvider.frameOfMonthHeader(inMonthWithOrigin: CGPoint(x: 0, y: 100))
.alignedToPixels(forScreenWithScale: 3)
let expectedFrame1 = CGRect(x: 0, y: 100, width: 320, height: 50)
.alignedToPixels(forScreenWithScale: 3)
XCTAssert(frame1 == expectedFrame1, "Incorrect initial month header frame.")
let frame2 = verticalPinnedDaysOfWeekFrameProvider.frameOfMonthHeader(
inMonthWithOrigin: CGPoint(x: 0, y: 100))
.alignedToPixels(forScreenWithScale: 3)
let expectedFrame2 = CGRect(x: 0, y: 100, width: 320, height: 50)
.alignedToPixels(forScreenWithScale: 3)
XCTAssert(frame2 == expectedFrame2, "Incorrect initial month header frame.")
let frame3 = horizontalFrameProvider.frameOfMonthHeader(
inMonthWithOrigin: CGPoint(x: 100, y: 0))
.alignedToPixels(forScreenWithScale: 3)
let expectedFrame3 = CGRect(x: 100, y: 0, width: 300, height: 50)
XCTAssert(frame3 == expectedFrame3, "Incorrect initial month header frame.")
}
func testMonthOriginContainingItem() {
let expectedOrigins1 = [
CGPoint(x: 0, y: 100).alignedToPixels(forScreenWithScale: 3),
CGPoint(x: 0, y: 100).alignedToPixels(forScreenWithScale: 3),
CGPoint(x: 0, y: 100).alignedToPixels(forScreenWithScale: 3),
CGPoint(x: 0, y: 100).alignedToPixels(forScreenWithScale: 3),
]
let expectedOrigins2 = [
CGPoint(x: -12.571428571428555, y: 145).alignedToPixels(forScreenWithScale: 3),
CGPoint(x: -12.571428571428555, y: 145).alignedToPixels(forScreenWithScale: 3),
CGPoint(x: -12.571428571428555, y: 145).alignedToPixels(forScreenWithScale: 3),
CGPoint(x: -4, y: 145).alignedToPixels(forScreenWithScale: 3),
]
let expectedOrigins3 = [
CGPoint(x: 147.14285714285714, y: 80.42857142857142).alignedToPixels(forScreenWithScale: 3),
CGPoint(x: 147.14285714285714, y: 135.28571428571428).alignedToPixels(forScreenWithScale: 3),
CGPoint(x: 147.14285714285714, y: 80.42857142857142).alignedToPixels(forScreenWithScale: 3),
CGPoint(x: 150, y: 89).alignedToPixels(forScreenWithScale: 3),
]
let expectedOrigins4 = [
CGPoint(x: 147.14285714285714, y: 25.571428571428584).alignedToPixels(forScreenWithScale: 3),
CGPoint(x: 147.14285714285714, y: 80.42857142857144).alignedToPixels(forScreenWithScale: 3),
CGPoint(x: 147.14285714285714, y: 135.28571428571428).alignedToPixels(forScreenWithScale: 3),
CGPoint(x: 150, y: 37).alignedToPixels(forScreenWithScale: 3),
]
let allFrameProviders: [FrameProvider] = [
verticalFrameProvider,
verticalPinnedDaysOfWeekFrameProvider,
verticalPartialMonthFrameProvider,
horizontalFrameProvider,
]
for (index, frameProvider) in zip(allFrameProviders.indices, allFrameProviders) {
let monthHeaderLayoutItem = LayoutItem(
itemType: .monthHeader(Month(era: 1, year: 2020, month: 01, isInGregorianCalendar: true)),
frame: CGRect(x: 0, y: 100, width: 320, height: 50))
let origin1 = frameProvider.originOfMonth(containing: monthHeaderLayoutItem)
.alignedToPixels(forScreenWithScale: 3)
XCTAssert(
origin1 == expectedOrigins1[index],
"Incorrect month origin containing month header.")
let dayOfWeekLayoutItem = LayoutItem(
itemType: .dayOfWeekInMonth(
position: .fourth,
month: Month(era: 1, year: 2020, month: 01, isInGregorianCalendar: true)),
frame: CGRect(x: 130, y: 200, width: 40, height: 40))
let origin2 = frameProvider.originOfMonth(containing: dayOfWeekLayoutItem)
.alignedToPixels(forScreenWithScale: 3)
XCTAssert(
origin2 == expectedOrigins2[index],
"Incorrect month origin containing day of week.")
let dayLayoutItem1 = LayoutItem(
itemType: .day(
Day(month: Month(era: 1, year: 2020, month: 02, isInGregorianCalendar: true), day: 10)),
frame: CGRect(x: 200, y: 300, width: 40, height: 40))
let origin3 = frameProvider.originOfMonth(containing: dayLayoutItem1)
.alignedToPixels(forScreenWithScale: 3)
XCTAssert(origin3 == expectedOrigins3[index], "Incorrect month origin containing day.")
let dayLayoutItem2 = LayoutItem(
itemType: .day(
Day(month: Month(era: 1, year: 2020, month: 05, isInGregorianCalendar: true), day: 18)),
frame: CGRect(x: 200, y: 300, width: 40, height: 40))
let origin4 = frameProvider.originOfMonth(containing: dayLayoutItem2)
.alignedToPixels(forScreenWithScale: 3)
XCTAssert(origin4 == expectedOrigins4[index], "Incorrect month origin containing day.")
}
}
func testPrecedingMonthOrigin() {
let origin1 = verticalFrameProvider.originOfMonth(
Month(era: 1, year: 2020, month: 01, isInGregorianCalendar: true),
beforeMonthWithOrigin: CGPoint(x: 0, y: 200))
.alignedToPixels(forScreenWithScale: 3)
let expectedOrigin1 = CGPoint(x: 0, y: -189.1428571428571)
.alignedToPixels(forScreenWithScale: 3)
XCTAssert(origin1 == expectedOrigin1, "Incorrect origin for preceding month.")
let origin2 = verticalPinnedDaysOfWeekFrameProvider.originOfMonth(
Month(era: 1, year: 2020, month: 01, isInGregorianCalendar: true),
beforeMonthWithOrigin: CGPoint(x: 0, y: 400))
.alignedToPixels(forScreenWithScale: 3)
let expectedOrigin2 = CGPoint(x: 0, y: 65.71428571428572).alignedToPixels(forScreenWithScale: 3)
XCTAssert(origin2 == expectedOrigin2, "Incorrect origin for preceding month.")
let origin3 = verticalPartialMonthFrameProvider.originOfMonth(
Month(era: 1, year: 2020, month: 05, isInGregorianCalendar: true),
beforeMonthWithOrigin: CGPoint(x: 0, y: 200))
.alignedToPixels(forScreenWithScale: 3)
let expectedOrigin3 = CGPoint(x: 0, y: -134.28571428571428)
.alignedToPixels(forScreenWithScale: 3)
XCTAssert(origin3 == expectedOrigin3, "Incorrect origin for preceding month.")
let origin4 = horizontalFrameProvider.originOfMonth(
Month(era: 1, year: 2020, month: 01, isInGregorianCalendar: true),
beforeMonthWithOrigin: CGPoint(x: 200, y: 0))
.alignedToPixels(forScreenWithScale: 3)
let expectedOrigin4 = CGPoint(x: -120, y: 0).alignedToPixels(forScreenWithScale: 3)
XCTAssert(origin4 == expectedOrigin4, "Incorrect origin for preceding month.")
}
func testSucceedingMonthOrigin() {
let origin1 = verticalFrameProvider.originOfMonth(
Month(era: 1, year: 2020, month: 01, isInGregorianCalendar: true),
afterMonthWithOrigin: CGPoint(x: 0, y: 200))
.alignedToPixels(forScreenWithScale: 3)
let expectedOrigin1 = CGPoint(x: 0, y: 589.1428571428571)
.alignedToPixels(forScreenWithScale: 3)
XCTAssert(origin1 == expectedOrigin1, "Incorrect origin for succeeding month.")
let origin2 = verticalPinnedDaysOfWeekFrameProvider.originOfMonth(
Month(era: 1, year: 2020, month: 01, isInGregorianCalendar: true),
afterMonthWithOrigin: CGPoint(x: 0, y: 400))
.alignedToPixels(forScreenWithScale: 3)
let expectedOrigin2 = CGPoint(x: 0, y: 734.2857142857142).alignedToPixels(forScreenWithScale: 3)
XCTAssert(origin2 == expectedOrigin2, "Incorrect origin for succeeding month.")
let origin3 = verticalPartialMonthFrameProvider.originOfMonth(
Month(era: 1, year: 2020, month: 06, isInGregorianCalendar: true),
afterMonthWithOrigin: CGPoint(x: 0, y: 400))
.alignedToPixels(forScreenWithScale: 3)
let expectedOrigin3 = CGPoint(x: 0, y: 734.2857142857142).alignedToPixels(forScreenWithScale: 3)
XCTAssert(origin3 == expectedOrigin3, "Incorrect origin for succeeding month.")
let origin4 = horizontalFrameProvider.originOfMonth(
Month(era: 1, year: 2020, month: 01, isInGregorianCalendar: true),
afterMonthWithOrigin: CGPoint(x: 200, y: 0))
.alignedToPixels(forScreenWithScale: 3)
let expectedOrigin4 = CGPoint(x: 520, y: 0).alignedToPixels(forScreenWithScale: 3)
XCTAssert(origin4 == expectedOrigin4, "Incorrect origin for succeeding month.")
}
func testFrameOfMonth() {
let frame1 = verticalFrameProvider.frameOfMonth(
Month(era: 1, year: 2020, month: 04, isInGregorianCalendar: true),
withOrigin: CGPoint(x: 0, y: 200))
.alignedToPixels(forScreenWithScale: 3)
let expectedFrame1 = CGRect(x: 0.0, y: 200.0, width: 320.0, height: 369.1428571428571)
.alignedToPixels(forScreenWithScale: 3)
XCTAssert(frame1 == expectedFrame1, "Incorrect frame for month.")
let frame2 = verticalPinnedDaysOfWeekFrameProvider.frameOfMonth(
Month(era: 1, year: 2020, month: 04, isInGregorianCalendar: true),
withOrigin: CGPoint(x: 0, y: 200))
.alignedToPixels(forScreenWithScale: 3)
let expectedFrame2 = CGRect(x: 0.0, y: 200.0, width: 320.0, height: 314.2857142857143)
.alignedToPixels(forScreenWithScale: 3)
XCTAssert(frame2 == expectedFrame2, "Incorrect frame for month.")
let frame3 = verticalPartialMonthFrameProvider.frameOfMonth(
Month(era: 1, year: 2020, month: 07, isInGregorianCalendar: true),
withOrigin: CGPoint(x: 0, y: 200))
.alignedToPixels(forScreenWithScale: 3)
let expectedFrame3 = CGRect(x: 0.0, y: 200.0, width: 320.0, height: 314.2857142857143)
.alignedToPixels(forScreenWithScale: 3)
XCTAssert(frame3 == expectedFrame3, "Incorrect frame for month.")
let frame4 = horizontalFrameProvider.frameOfMonth(
Month(era: 1, year: 2020, month: 04, isInGregorianCalendar: true),
withOrigin: CGPoint(x: 500, y: 0))
.alignedToPixels(forScreenWithScale: 3)
let expectedFrame4 = CGRect(x: 500.0, y: 0.0, width: 300.0, height: 352.0)
.alignedToPixels(forScreenWithScale: 3)
XCTAssert(frame4 == expectedFrame4, "Incorrect frame for month.")
}
// MARK: Core item frame calculations
func testMonthHeaderFrame() {
let frame1 = verticalFrameProvider.frameOfMonthHeader(inMonthWithOrigin: CGPoint(x: 0, y: 300))
.alignedToPixels(forScreenWithScale: 3)
let expectedFrame1 = CGRect(x: 0, y: 300, width: 320, height: 50)
.alignedToPixels(forScreenWithScale: 3)
XCTAssert(frame1 == expectedFrame1, "Incorrect frame for month header.")
let frame2 = verticalPinnedDaysOfWeekFrameProvider.frameOfMonthHeader(
inMonthWithOrigin: CGPoint(x: 0, y: 300))
.alignedToPixels(forScreenWithScale: 3)
let expectedFrame2 = CGRect(x: 0, y: 300, width: 320, height: 50)
.alignedToPixels(forScreenWithScale: 3)
XCTAssert(frame2 == expectedFrame2, "Incorrect frame for month header.")
let frame3 = horizontalFrameProvider.frameOfMonthHeader(
inMonthWithOrigin: CGPoint(x: 300, y: 0))
.alignedToPixels(forScreenWithScale: 3)
let expectedFrame3 = CGRect(x: 300, y: 0, width: 300, height: 50)
.alignedToPixels(forScreenWithScale: 3)
XCTAssert(frame3 == expectedFrame3, "Incorrect frame for month header.")
}
func testDayOfWeekFrame() {
let frame1 = verticalFrameProvider.frameOfDayOfWeek(
at: .fifth,
inMonthWithOrigin: CGPoint(x: 0, y: 200))
.alignedToPixels(forScreenWithScale: 3)
let expectedFrame1 = CGRect(
x: 187.42857142857142,
y: 255,
width: 34.857142857142854,
height: 34.857142857142854)
.alignedToPixels(forScreenWithScale: 3)
XCTAssert(frame1 == expectedFrame1, "Incorrect frame for day of week.")
let frame2 = verticalPinnedDaysOfWeekFrameProvider.frameOfDayOfWeek(
at: .first,
inMonthWithOrigin: CGPoint(x: 0, y: 200))
.alignedToPixels(forScreenWithScale: 3)
let expectedFrame2 = CGRect(x: 8, y: 255, width: 34.857142857142854, height: 34.857142857142854)
.alignedToPixels(forScreenWithScale: 3)
XCTAssert(frame2 == expectedFrame2, "Incorrect frame for day of week.")
let frame3 = horizontalFrameProvider.frameOfDayOfWeek(
at: .last,
inMonthWithOrigin: CGPoint(x: 200, y: 0))
.alignedToPixels(forScreenWithScale: 3)
let expectedFrame3 = CGRect(x: 460, y: 55, width: 32, height: 32)
.alignedToPixels(forScreenWithScale: 3)
XCTAssert(frame3 == expectedFrame3, "Incorrect frame for day of week.")
}
func testDayOfWeekBackgroundFrame() {
let frame1 = verticalFrameProvider.frameOfDayOfWeekBackground(
inMonthWithOrigin: CGPoint(x: 0, y: 200))
.alignedToPixels(forScreenWithScale: 3)
let expectedFrame1 = CGRect(x: 0, y: 255, width: 320, height: 34.857142857142854)
.alignedToPixels(forScreenWithScale: 3)
XCTAssert(frame1 == expectedFrame1, "Incorrect frame for day of week background.")
let frame2 = verticalPinnedDaysOfWeekFrameProvider.frameOfDayOfWeekBackground(
inMonthWithOrigin: CGPoint(x: 0, y: 150))
.alignedToPixels(forScreenWithScale: 3)
let expectedFrame2 = CGRect(x: 0, y: 205, width: 320, height: 34.857142857142854)
.alignedToPixels(forScreenWithScale: 3)
XCTAssert(frame2 == expectedFrame2, "Incorrect frame for day of week background.")
let frame3 = horizontalFrameProvider.frameOfDayOfWeekBackground(
inMonthWithOrigin: CGPoint(x: 200, y: 0))
.alignedToPixels(forScreenWithScale: 3)
let expectedFrame3 = CGRect(x: 200, y: 55, width: 300, height: 32)
.alignedToPixels(forScreenWithScale: 3)
XCTAssert(frame3 == expectedFrame3, "Incorrect frame for day of week background.")
}
func testDayFrameInMonth() {
let frame1 = verticalFrameProvider.frameOfDay(
Day(month: Month(era: 1, year: 2020, month: 04, isInGregorianCalendar: true), day: 20),
inMonthWithOrigin: CGPoint(x: 0, y: 69))
.alignedToPixels(forScreenWithScale: 3)
let expectedFrame1 = CGRect(
x: 52.857142857142854,
y: 343.42857142857144,
width: 34.857142857142854,
height: 34.857142857142854)
.alignedToPixels(forScreenWithScale: 3)
XCTAssert(frame1 == expectedFrame1, "Incorrect frame for day.")
let frame2 = verticalPinnedDaysOfWeekFrameProvider.frameOfDay(
Day(month: Month(era: 1, year: 1994, month: 01, isInGregorianCalendar: true), day: 01),
inMonthWithOrigin: CGPoint(x: 0, y: 130))
.alignedToPixels(forScreenWithScale: 3)
let expectedFrame2 = CGRect(
x: 277.1428571428571,
y: 185,
width: 34.857142857142854,
height: 34.857142857142854)
.alignedToPixels(forScreenWithScale: 3)
XCTAssert(frame2 == expectedFrame2, "Incorrect frame for day of week.")
let frame3 = verticalPartialMonthFrameProvider.frameOfDay(
Day(month: Month(era: 1, year: 2020, month: 05, isInGregorianCalendar: true), day: 29),
inMonthWithOrigin: CGPoint(x: 0, y: 69))
.alignedToPixels(forScreenWithScale: 3)
let expectedFrame3 = CGRect(
x: 232.28571428571428,
y: 288.57142857142856,
width: 34.857142857142854,
height: 34.857142857142854)
.alignedToPixels(forScreenWithScale: 3)
XCTAssert(frame3 == expectedFrame3, "Incorrect frame for day.")
let frame4 = horizontalFrameProvider.frameOfDay(
Day(month: Month(era: 1, year: 2072, month: 06, isInGregorianCalendar: true), day: 30),
inMonthWithOrigin: CGPoint(x: 300, y: 0))
.alignedToPixels(forScreenWithScale: 3)
let expectedFrame4 = CGRect(x: 476, y: 315, width: 32, height: 32)
.alignedToPixels(forScreenWithScale: 3)
XCTAssert(frame4 == expectedFrame4, "Incorrect frame for day of week.")
}
func testAdjacentDayFrame() {
let middleDay = Day(
month: Month(era: 1, year: 2020, month: 04, isInGregorianCalendar: true),
day: 22)
let middleDayMonthOrigin = CGPoint(x: 0, y: 100)
let middleDayFrame = verticalFrameProvider.frameOfDay(
middleDay,
inMonthWithOrigin: middleDayMonthOrigin)
.alignedToPixels(forScreenWithScale: 3)
let frameBeforeMiddleDay = verticalFrameProvider.frameOfDay(
calendar.day(byAddingDays: -1, to: middleDay),
adjacentTo: middleDay,
withFrame: middleDayFrame,
inMonthWithOrigin: middleDayMonthOrigin)
.alignedToPixels(forScreenWithScale: 3)
let frameAfterMiddleDay = verticalFrameProvider.frameOfDay(
calendar.day(byAddingDays: 1, to: middleDay),
adjacentTo: middleDay,
withFrame: middleDayFrame,
inMonthWithOrigin: middleDayMonthOrigin)
.alignedToPixels(forScreenWithScale: 3)
let expectedFrameBeforeMiddleDay = CGRect(
x: 97.8095238095238,
y: 374.3333333333333,
width: 34.857142857142854,
height: 34.857142857142854)
.alignedToPixels(forScreenWithScale: 3)
let expectedFrameAfterMiddleDay = CGRect(
x: 187.33333333333334,
y: 374.3333333333333,
width: 34.857142857142854,
height: 34.857142857142854)
.alignedToPixels(forScreenWithScale: 3)
XCTAssert(frameBeforeMiddleDay == expectedFrameBeforeMiddleDay, "Incorrect frame for day.")
XCTAssert(frameAfterMiddleDay == expectedFrameAfterMiddleDay, "Incorrect frame for day.")
let leftDay = Day(
month: Month(era: 1, year: 2020, month: 04, isInGregorianCalendar: true),
day: 19)
let leftDayMonthOrigin = CGPoint(x: 0, y: 200)
let leftDayFrame = verticalPinnedDaysOfWeekFrameProvider.frameOfDay(
leftDay,
inMonthWithOrigin: leftDayMonthOrigin)
.alignedToPixels(forScreenWithScale: 3)
let frameBeforeLeftDay = verticalPinnedDaysOfWeekFrameProvider.frameOfDay(
calendar.day(byAddingDays: -1, to: leftDay),
adjacentTo: leftDay,
withFrame: leftDayFrame,
inMonthWithOrigin: leftDayMonthOrigin)
.alignedToPixels(forScreenWithScale: 3)
let frameAfterLeftDay = verticalPinnedDaysOfWeekFrameProvider.frameOfDay(
calendar.day(byAddingDays: 1, to: leftDay),
adjacentTo: leftDay,
withFrame: leftDayFrame,
inMonthWithOrigin: leftDayMonthOrigin)
.alignedToPixels(forScreenWithScale: 3)
let expectedFrameBeforeLeftDay = CGRect(
x: 277.14285714285717,
y: 364.80952380952385,
width: 34.857142857142854,
height: 34.857142857142854)
.alignedToPixels(forScreenWithScale: 3)
let expectedFrameAfterLeftDay = CGRect(
x: 53.0,
y: 419.6666666666667,
width: 34.857142857142854,
height: 34.857142857142854)
.alignedToPixels(forScreenWithScale: 3)
XCTAssert(frameBeforeLeftDay == expectedFrameBeforeLeftDay, "Incorrect frame for day.")
XCTAssert(frameAfterLeftDay == expectedFrameAfterLeftDay, "Incorrect frame for day.")
let rightDay = Day(
month: Month(era: 1, year: 2020, month: 04, isInGregorianCalendar: true),
day: 25)
let rightDayMonthOrigin = CGPoint(x: 1000, y: 0)
let rightDayFrame = horizontalFrameProvider.frameOfDay(
rightDay,
inMonthWithOrigin: rightDayMonthOrigin)
.alignedToPixels(forScreenWithScale: 3)
let frameBeforeRightDay = horizontalFrameProvider.frameOfDay(
calendar.day(byAddingDays: -1, to: rightDay),
adjacentTo: rightDay,
withFrame: rightDayFrame,
inMonthWithOrigin: rightDayMonthOrigin)
.alignedToPixels(forScreenWithScale: 3)
let frameAfterRightDay = horizontalFrameProvider.frameOfDay(
calendar.day(byAddingDays: 1, to: rightDay),
adjacentTo: rightDay,
withFrame: rightDayFrame,
inMonthWithOrigin: rightDayMonthOrigin)
.alignedToPixels(forScreenWithScale: 3)
let expectedFrameBeforeRightDay = CGRect(
x: 1218.0,
y: 263.0,
width: 32.0,
height: 32.0)
.alignedToPixels(forScreenWithScale: 3)
let expectedFrameAfterRightDay = CGRect(
x: 1008.0,
y: 315.0,
width: 32.0,
height: 32.0)
.alignedToPixels(forScreenWithScale: 3)
XCTAssert(frameBeforeRightDay == expectedFrameBeforeRightDay, "Incorrect frame for day.")
XCTAssert(frameAfterRightDay == expectedFrameAfterRightDay, "Incorrect frame for day.")
}
// MARK: Misc item frame calculations
func testPinnedDayOfWeekFrame() {
let frame1 = verticalPinnedDaysOfWeekFrameProvider.frameOfPinnedDayOfWeek(
at: .second,
yContentOffset: 275)
.alignedToPixels(forScreenWithScale: 3)
let expectedFrame1 = CGRect(
x: 52.857142857142854,
y: 275,
width: 34.857142857142854,
height: 34.857142857142854)
.alignedToPixels(forScreenWithScale: 3)
XCTAssert(frame1 == expectedFrame1, "Incorrect frame for pinned day of week.")
}
func testPinnedDayOfWeekBackgroundFrame() {
let frame1 = verticalPinnedDaysOfWeekFrameProvider.frameOfPinnedDayOfWeekBackground(
yContentOffset: 140)
.alignedToPixels(forScreenWithScale: 3)
let expectedFrame1 = CGRect(x: 0, y: 140, width: 320, height: 34.857142857142854)
.alignedToPixels(forScreenWithScale: 3)
XCTAssert(frame1 == expectedFrame1, "Incorrect frame for pinned day of week background.")
}
// MARK: Private
private let calendar = Calendar(identifier: .gregorian)
private var verticalFrameProvider: FrameProvider!
private var verticalPinnedDaysOfWeekFrameProvider: FrameProvider!
private var verticalPartialMonthFrameProvider: FrameProvider!
private var horizontalFrameProvider: FrameProvider!
}
// MARK: CGSize Pixel Alignment
private extension CGSize {
// Rounds a `CGSize`'s origin `width` and `height` values so that they're aligned on pixel
// boundaries for a screen with the provided scale.
func alignedToPixels(forScreenWithScale scale: CGFloat) -> CGSize {
CGSize(
width: width.alignedToPixel(forScreenWithScale: scale),
height: height.alignedToPixel(forScreenWithScale: scale))
}
}