forked from louisdh/panelkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StateTests.swift
139 lines (101 loc) · 3.41 KB
/
StateTests.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
//
// StateTests.swift
// PanelKitTests
//
// Created by Louis D'hauwe on 16/11/2017.
// Copyright © 2017 Silver Fox. All rights reserved.
//
import XCTest
import UIKit
@testable import PanelKit
class StateTests: XCTestCase {
var viewController: StateViewController!
var navigationController: UINavigationController!
override func setUp() {
super.setUp()
viewController = StateViewController()
navigationController = UINavigationController(rootViewController: viewController)
navigationController.view.frame = CGRect(origin: .zero, size: CGSize(width: 1024, height: 768))
let window = UIWindow(frame: UIScreen.main.bounds)
window.rootViewController = navigationController
window.makeKeyAndVisible()
XCTAssertNotNil(navigationController.view)
XCTAssertNotNil(viewController.view)
if UIDevice.current.userInterfaceIdiom == .phone {
XCTFail("Test does not work on an iPhone")
}
}
override func tearDown() {
super.tearDown()
// Put teardown code here. This method is called after the invocation of each test method in the class.
}
func testFloatPanel() {
viewController.float(viewController.panel1VC, at: CGRect(x: 200, y: 200, width: 300, height: 300))
XCTAssert(viewController.panel1VC.isFloating)
}
func testPinMultiplePanelsRight() {
viewController.pin(viewController.panel1VC, to: .right, atIndex: 0)
viewController.pin(viewController.panel2VC, to: .right, atIndex: 0)
XCTAssert(viewController.numberOfPanelsPinned(at: .right) == 2)
XCTAssert(viewController.panel1VC.isPinned)
XCTAssert(viewController.panel2VC.isPinned)
}
func testPinMultiplePanelsLeft() {
viewController.pin(viewController.panel1VC, to: .left, atIndex: 0)
viewController.pin(viewController.panel2VC, to: .left, atIndex: 0)
XCTAssert(viewController.numberOfPanelsPinned(at: .left) == 2)
XCTAssert(viewController.panel1VC.isPinned)
XCTAssert(viewController.panel2VC.isPinned)
}
func testEncodeStates() {
viewController.pin(viewController.panel1VC, to: .left, atIndex: 0)
viewController.pin(viewController.panel2VC, to: .right, atIndex: 0)
let states = viewController.panelStates
guard let state1 = states[1] else {
XCTFail("Expected state 1")
return
}
guard let state2 = states[2] else {
XCTFail("Expected state 2")
return
}
XCTAssert(state1.pinnedMetadata?.side == .left)
XCTAssert(state2.pinnedMetadata?.side == .right)
}
func testDecodeStates() {
let json = """
{
"2": {
"floatingState": {
"relativePosition": [0.4, 0.4],
"zIndex": 0
}
},
"1": {
"pinnedMetadata": {
"side": 0,
"index": 0,
"date": 532555376.97106999
}
}
}
"""
let decoder = JSONDecoder()
let states = try! decoder.decode([Int: PanelState].self, from: json.data(using: .utf8)!)
guard let state1 = states[1] else {
XCTFail("Expected state 1")
return
}
guard let state2 = states[2] else {
XCTFail("Expected state 2")
return
}
XCTAssert(state1.pinnedMetadata?.side == .left)
XCTAssert(state2.floatingState?.zIndex == 0)
viewController.restorePanelStates(states)
XCTAssert(viewController.numberOfPanelsPinned(at: .left) == 1)
XCTAssert(viewController.numberOfPanelsPinned(at: .right) == 0)
XCTAssert(viewController.panel1VC.isPinned)
XCTAssert(viewController.panel2VC.isFloating)
}
}