2015-09-25 12:37:33 +08:00
|
|
|
#if os(iOS) || os(tvOS)
|
2014-07-25 12:24:17 +08:00
|
|
|
import UIKit
|
2015-03-24 06:39:41 +08:00
|
|
|
typealias View = UIView
|
|
|
|
extension View {
|
2015-06-17 19:09:54 +08:00
|
|
|
var snp_constraints: [AnyObject] { return self.constraints }
|
2015-03-24 06:39:41 +08:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
import AppKit
|
|
|
|
typealias View = NSView
|
|
|
|
extension View {
|
|
|
|
var snp_constraints: [AnyObject] { return self.constraints }
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-06-06 11:42:44 +08:00
|
|
|
import XCTest
|
2015-04-15 19:07:50 +08:00
|
|
|
import SnapKit
|
2014-06-06 11:42:44 +08:00
|
|
|
|
2015-04-15 19:07:50 +08:00
|
|
|
class SnapKitTests: XCTestCase {
|
2014-06-06 11:42:44 +08:00
|
|
|
|
2015-03-24 06:39:41 +08:00
|
|
|
let container = View()
|
2015-02-14 18:37:27 +08:00
|
|
|
|
2014-06-06 11:42:44 +08:00
|
|
|
override func setUp() {
|
|
|
|
super.setUp()
|
|
|
|
// Put setup code here. This method is called before the invocation of each test method in the class.
|
|
|
|
}
|
|
|
|
|
|
|
|
override func tearDown() {
|
|
|
|
// Put teardown code here. This method is called after the invocation of each test method in the class.
|
|
|
|
super.tearDown()
|
|
|
|
}
|
|
|
|
|
2015-05-30 15:54:43 +08:00
|
|
|
func testLayoutGuideConstraints() {
|
2015-09-25 12:37:33 +08:00
|
|
|
#if os(iOS) || os(tvOS)
|
2015-05-30 15:54:43 +08:00
|
|
|
let vc = UIViewController()
|
|
|
|
vc.view = UIView(frame: CGRectMake(0, 0, 300, 300))
|
|
|
|
|
|
|
|
vc.view.addSubview(self.container)
|
|
|
|
|
|
|
|
self.container.snp_makeConstraints { (make) -> Void in
|
|
|
|
make.top.equalTo(vc.snp_topLayoutGuideBottom)
|
|
|
|
make.bottom.equalTo(vc.snp_bottomLayoutGuideTop)
|
|
|
|
}
|
|
|
|
|
|
|
|
XCTAssertEqual(vc.view.snp_constraints.count, 6, "Should have 6 constraints installed")
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-02-14 18:37:27 +08:00
|
|
|
func testMakeConstraints() {
|
2015-03-24 06:39:41 +08:00
|
|
|
let v1 = View()
|
|
|
|
let v2 = View()
|
2015-02-14 18:37:27 +08:00
|
|
|
self.container.addSubview(v1)
|
|
|
|
self.container.addSubview(v2)
|
|
|
|
|
|
|
|
v1.snp_makeConstraints { (make) -> Void in
|
|
|
|
make.top.equalTo(v2.snp_top).offset(50)
|
|
|
|
make.left.equalTo(v2.snp_top).offset(50)
|
|
|
|
return
|
|
|
|
}
|
2015-02-17 06:12:03 +08:00
|
|
|
|
2015-03-24 06:39:41 +08:00
|
|
|
XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints installed")
|
2015-02-17 06:12:03 +08:00
|
|
|
|
2015-02-14 18:37:27 +08:00
|
|
|
v2.snp_makeConstraints { (make) -> Void in
|
|
|
|
make.edges.equalTo(v1)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-03-24 06:39:41 +08:00
|
|
|
XCTAssertEqual(self.container.snp_constraints.count, 6, "Should have 6 constraints installed")
|
2015-02-17 06:12:03 +08:00
|
|
|
|
2015-02-14 18:37:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func testUpdateConstraints() {
|
2015-03-24 06:39:41 +08:00
|
|
|
let v1 = View()
|
|
|
|
let v2 = View()
|
2015-02-14 18:37:27 +08:00
|
|
|
self.container.addSubview(v1)
|
|
|
|
self.container.addSubview(v2)
|
|
|
|
|
|
|
|
v1.snp_makeConstraints { (make) -> Void in
|
|
|
|
make.top.equalTo(v2.snp_top).offset(50)
|
|
|
|
make.left.equalTo(v2.snp_top).offset(50)
|
|
|
|
return
|
|
|
|
}
|
2015-02-17 06:12:03 +08:00
|
|
|
|
2015-03-24 06:39:41 +08:00
|
|
|
XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints installed")
|
2015-02-17 06:12:03 +08:00
|
|
|
|
2015-02-14 18:37:27 +08:00
|
|
|
v1.snp_updateConstraints { (make) -> Void in
|
|
|
|
make.top.equalTo(v2.snp_top).offset(15)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-03-24 06:39:41 +08:00
|
|
|
XCTAssertEqual(self.container.snp_constraints.count, 2, "Should still have 2 constraints installed")
|
2015-02-17 06:12:03 +08:00
|
|
|
|
2015-02-14 18:37:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func testRemakeConstraints() {
|
2015-03-24 06:39:41 +08:00
|
|
|
let v1 = View()
|
|
|
|
let v2 = View()
|
2015-02-14 18:37:27 +08:00
|
|
|
self.container.addSubview(v1)
|
|
|
|
self.container.addSubview(v2)
|
|
|
|
|
|
|
|
v1.snp_makeConstraints { (make) -> Void in
|
|
|
|
make.top.equalTo(v2.snp_top).offset(50)
|
|
|
|
make.left.equalTo(v2.snp_top).offset(50)
|
|
|
|
return
|
|
|
|
}
|
2015-02-17 06:12:03 +08:00
|
|
|
|
2015-03-24 06:39:41 +08:00
|
|
|
XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints installed")
|
2015-02-17 06:12:03 +08:00
|
|
|
|
2015-02-14 18:37:27 +08:00
|
|
|
v1.snp_remakeConstraints { (make) -> Void in
|
|
|
|
make.edges.equalTo(v2)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-03-24 06:39:41 +08:00
|
|
|
XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints installed")
|
2015-02-17 06:12:03 +08:00
|
|
|
|
2014-06-06 11:42:44 +08:00
|
|
|
}
|
|
|
|
|
2015-02-14 18:37:27 +08:00
|
|
|
func testRemoveConstraints() {
|
2015-03-24 06:39:41 +08:00
|
|
|
let v1 = View()
|
|
|
|
let v2 = View()
|
2015-02-14 18:37:27 +08:00
|
|
|
self.container.addSubview(v1)
|
|
|
|
self.container.addSubview(v2)
|
|
|
|
|
|
|
|
v1.snp_makeConstraints { (make) -> Void in
|
|
|
|
make.top.equalTo(v2.snp_top).offset(50)
|
|
|
|
make.left.equalTo(v2.snp_top).offset(50)
|
|
|
|
return
|
2014-06-06 11:42:44 +08:00
|
|
|
}
|
2015-02-17 06:12:03 +08:00
|
|
|
|
2015-03-24 06:39:41 +08:00
|
|
|
XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints installed")
|
2015-02-17 06:12:03 +08:00
|
|
|
|
2015-02-14 18:37:27 +08:00
|
|
|
v1.snp_removeConstraints()
|
|
|
|
|
2015-03-24 06:39:41 +08:00
|
|
|
XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints installed")
|
2015-02-17 06:12:03 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func testPrepareConstraints() {
|
2015-03-24 06:39:41 +08:00
|
|
|
let v1 = View()
|
|
|
|
let v2 = View()
|
2015-02-17 06:12:03 +08:00
|
|
|
self.container.addSubview(v1)
|
|
|
|
self.container.addSubview(v2)
|
|
|
|
|
|
|
|
let constraints = v1.snp_prepareConstraints { (make) -> Void in
|
|
|
|
make.edges.equalTo(v2)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-03-24 06:39:41 +08:00
|
|
|
XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints installed")
|
2015-02-17 06:12:03 +08:00
|
|
|
|
|
|
|
for constraint in constraints {
|
|
|
|
constraint.install()
|
|
|
|
}
|
|
|
|
|
2015-03-24 06:39:41 +08:00
|
|
|
XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints installed")
|
2015-02-17 06:12:03 +08:00
|
|
|
|
|
|
|
for constraint in constraints {
|
|
|
|
constraint.uninstall()
|
|
|
|
}
|
|
|
|
|
2015-03-24 06:39:41 +08:00
|
|
|
XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints installed")
|
2015-02-17 06:12:03 +08:00
|
|
|
|
2014-06-06 11:42:44 +08:00
|
|
|
}
|
|
|
|
|
2015-02-17 08:41:45 +08:00
|
|
|
func testReinstallConstraints() {
|
2015-03-24 06:39:41 +08:00
|
|
|
let v1 = View()
|
|
|
|
let v2 = View()
|
2015-02-17 08:41:45 +08:00
|
|
|
self.container.addSubview(v1)
|
|
|
|
self.container.addSubview(v2)
|
|
|
|
|
|
|
|
let constraints = v1.snp_prepareConstraints { (make) -> Void in
|
|
|
|
make.edges.equalTo(v2)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-04-12 18:21:02 +08:00
|
|
|
|
2015-03-24 06:39:41 +08:00
|
|
|
XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints installed")
|
2015-02-17 08:41:45 +08:00
|
|
|
|
|
|
|
for constraint in constraints {
|
|
|
|
constraint.install()
|
|
|
|
}
|
|
|
|
|
2015-03-24 06:39:41 +08:00
|
|
|
XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints installed")
|
2015-02-17 08:41:45 +08:00
|
|
|
|
|
|
|
for constraint in constraints {
|
|
|
|
constraint.install()
|
|
|
|
}
|
|
|
|
|
2015-03-24 06:39:41 +08:00
|
|
|
XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 0 constraints installed")
|
2015-02-17 08:41:45 +08:00
|
|
|
}
|
|
|
|
|
2015-04-11 17:50:44 +08:00
|
|
|
func testActivateDeactivateConstraints() {
|
|
|
|
let v1 = View()
|
|
|
|
let v2 = View()
|
|
|
|
self.container.addSubview(v1)
|
|
|
|
self.container.addSubview(v2)
|
|
|
|
|
|
|
|
var c1: Constraint? = nil
|
|
|
|
var c2: Constraint? = nil
|
|
|
|
|
|
|
|
v1.snp_prepareConstraints { (make) -> Void in
|
2015-04-11 19:39:12 +08:00
|
|
|
c1 = make.top.equalTo(v2.snp_top).offset(50).constraint
|
|
|
|
c2 = make.left.equalTo(v2.snp_top).offset(50).constraint
|
2015-04-11 17:50:44 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints")
|
|
|
|
|
|
|
|
c1?.activate()
|
|
|
|
c2?.activate()
|
|
|
|
|
|
|
|
XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints")
|
|
|
|
|
|
|
|
c1?.deactivate()
|
|
|
|
c2?.deactivate()
|
|
|
|
|
|
|
|
XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints")
|
|
|
|
|
|
|
|
c1?.uninstall()
|
|
|
|
c2?.uninstall()
|
|
|
|
|
|
|
|
XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints")
|
|
|
|
|
|
|
|
c1?.activate()
|
|
|
|
c2?.activate()
|
|
|
|
|
|
|
|
XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-01-26 19:14:18 +08:00
|
|
|
func testSizeConstraints() {
|
|
|
|
let view = View()
|
|
|
|
self.container.addSubview(view)
|
|
|
|
|
|
|
|
view.snp_makeConstraints { (make) -> Void in
|
|
|
|
make.size.equalTo(CGSizeMake(50, 50))
|
|
|
|
make.left.top.equalTo(self.container)
|
|
|
|
}
|
|
|
|
|
|
|
|
XCTAssertEqual(view.snp_constraints.count, 2, "Should have 2 constraints")
|
|
|
|
|
|
|
|
XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints")
|
|
|
|
|
|
|
|
|
|
|
|
let constraints = view.snp_constraints as! [NSLayoutConstraint]
|
|
|
|
|
|
|
|
XCTAssertEqual(constraints[0].firstAttribute, NSLayoutAttribute.Width, "Should be width")
|
|
|
|
XCTAssertEqual(constraints[1].firstAttribute, NSLayoutAttribute.Height, "Should be height")
|
|
|
|
XCTAssertEqual(constraints[0].constant, 50, "Should be 50")
|
|
|
|
XCTAssertEqual(constraints[1].constant, 50, "Should be 50")
|
|
|
|
}
|
|
|
|
|
2016-03-22 07:04:23 +08:00
|
|
|
func testConstraintIdentifier() {
|
|
|
|
let identifier = "Test-Identifier"
|
|
|
|
let view = View()
|
|
|
|
self.container.addSubview(view)
|
|
|
|
|
|
|
|
view.snp_makeConstraints { (make) -> Void in
|
2016-03-22 07:53:30 +08:00
|
|
|
make.top.equalTo(self.container.snp_top).labeled(identifier)
|
2016-03-22 07:04:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
let constraints = container.snp_constraints as! [NSLayoutConstraint]
|
|
|
|
XCTAssertEqual(constraints[0].identifier, identifier, "Identifier should be 'Test'")
|
|
|
|
}
|
|
|
|
|
2014-06-06 11:42:44 +08:00
|
|
|
}
|