Tests.swift 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. #if os(iOS) || os(tvOS)
  2. import UIKit
  3. typealias View = UIView
  4. extension View {
  5. var snp_constraints: [AnyObject] {
  6. return self.constraints
  7. .filter { $0 is LayoutConstraint }
  8. .filter { $0.isActive }
  9. }
  10. }
  11. #else
  12. import AppKit
  13. typealias View = NSView
  14. extension View {
  15. var snp_constraints: [AnyObject] {
  16. return self.constraints
  17. .filter { $0 is LayoutConstraint }
  18. .filter { $0.isActive }
  19. }
  20. }
  21. #endif
  22. import XCTest
  23. @testable import SnapKit
  24. class SnapKitTests: XCTestCase {
  25. let container = View()
  26. override func setUp() {
  27. super.setUp()
  28. // Put setup code here. This method is called before the invocation of each test method in the class.
  29. }
  30. override func tearDown() {
  31. // Put teardown code here. This method is called after the invocation of each test method in the class.
  32. super.tearDown()
  33. }
  34. func testMakeConstraints() {
  35. let v1 = View()
  36. let v2 = View()
  37. self.container.addSubview(v1)
  38. self.container.addSubview(v2)
  39. v1.snp.makeConstraints { (make) -> Void in
  40. make.top.equalTo(v2.snp.top).offset(50)
  41. make.left.equalTo(v2.snp.top).offset(50)
  42. return
  43. }
  44. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints installed")
  45. v2.snp.makeConstraints { (make) -> Void in
  46. make.edges.equalTo(v1)
  47. return
  48. }
  49. XCTAssertEqual(self.container.snp_constraints.count, 6, "Should have 6 constraints installed")
  50. }
  51. func testGuideMakeConstraints() {
  52. guard #available(iOS 9.0, OSX 10.11, *) else { return }
  53. let v1 = View()
  54. let g1 = ConstraintLayoutGuide()
  55. self.container.addSubview(v1)
  56. self.container.addLayoutGuide(g1)
  57. v1.snp.makeConstraints { (make) -> Void in
  58. make.top.equalTo(g1).offset(50)
  59. make.left.equalTo(g1.snp.top).offset(50)
  60. return
  61. }
  62. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints installed")
  63. g1.snp.makeConstraints { (make) -> Void in
  64. make.edges.equalTo(v1)
  65. return
  66. }
  67. XCTAssertEqual(self.container.snp_constraints.count, 6, "Should have 6 constraints installed")
  68. }
  69. func testMakeImpliedSuperviewConstraints() {
  70. let v1 = View()
  71. let v2 = View()
  72. self.container.addSubview(v1)
  73. self.container.addSubview(v2)
  74. v1.snp.makeConstraints { (make) -> Void in
  75. make.top.equalTo(50.0)
  76. make.left.equalTo(50.0)
  77. return
  78. }
  79. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints installed")
  80. v2.snp.makeConstraints { (make) -> Void in
  81. make.edges.equalTo(v1)
  82. return
  83. }
  84. XCTAssertEqual(self.container.snp_constraints.count, 6, "Should have 6 constraints installed")
  85. }
  86. func testUpdateConstraints() {
  87. let v1 = View()
  88. let v2 = View()
  89. self.container.addSubview(v1)
  90. self.container.addSubview(v2)
  91. v1.snp.makeConstraints { (make) -> Void in
  92. make.top.equalTo(v2.snp.top).offset(50)
  93. make.left.equalTo(v2.snp.top).offset(50)
  94. return
  95. }
  96. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints installed")
  97. v1.snp.updateConstraints { (make) -> Void in
  98. make.top.equalTo(v2.snp.top).offset(15)
  99. return
  100. }
  101. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should still have 2 constraints installed")
  102. }
  103. func testRemakeConstraints() {
  104. let v1 = View()
  105. let v2 = View()
  106. self.container.addSubview(v1)
  107. self.container.addSubview(v2)
  108. v1.snp.makeConstraints { (make) -> Void in
  109. make.top.equalTo(v2.snp.top).offset(50)
  110. make.left.equalTo(v2.snp.top).offset(50)
  111. return
  112. }
  113. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints installed")
  114. v1.snp.remakeConstraints { (make) -> Void in
  115. make.edges.equalTo(v2)
  116. return
  117. }
  118. XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints installed")
  119. }
  120. func testRemoveConstraints() {
  121. let v1 = View()
  122. let v2 = View()
  123. self.container.addSubview(v1)
  124. self.container.addSubview(v2)
  125. v1.snp.makeConstraints { (make) -> Void in
  126. make.top.equalTo(v2).offset(50)
  127. make.left.equalTo(v2).offset(50)
  128. return
  129. }
  130. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints installed")
  131. print(self.container.snp_constraints)
  132. v1.snp.removeConstraints()
  133. print(self.container.snp_constraints)
  134. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints installed")
  135. }
  136. func testPrepareConstraints() {
  137. let v1 = View()
  138. let v2 = View()
  139. self.container.addSubview(v1)
  140. self.container.addSubview(v2)
  141. let constraints = v1.snp.prepareConstraints { (make) -> Void in
  142. make.edges.equalTo(v2)
  143. return
  144. }
  145. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints installed")
  146. for constraint in constraints {
  147. constraint.activate()
  148. }
  149. XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints installed")
  150. for constraint in constraints {
  151. constraint.deactivate()
  152. }
  153. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints installed")
  154. }
  155. func testReactivateConstraints() {
  156. let v1 = View()
  157. let v2 = View()
  158. self.container.addSubview(v1)
  159. self.container.addSubview(v2)
  160. let constraints = v1.snp.prepareConstraints { (make) -> Void in
  161. make.edges.equalTo(v2)
  162. return
  163. }
  164. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints installed")
  165. for constraint in constraints {
  166. constraint.activate()
  167. }
  168. XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints installed")
  169. for constraint in constraints {
  170. constraint.deactivate()
  171. }
  172. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints installed")
  173. }
  174. func testActivateDeactivateConstraints() {
  175. let v1 = View()
  176. let v2 = View()
  177. self.container.addSubview(v1)
  178. self.container.addSubview(v2)
  179. var c1: Constraint? = nil
  180. var c2: Constraint? = nil
  181. v1.snp.prepareConstraints { (make) -> Void in
  182. c1 = make.top.equalTo(v2.snp.top).offset(50).constraint
  183. c2 = make.left.equalTo(v2.snp.top).offset(50).constraint
  184. return
  185. }
  186. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints")
  187. c1?.activate()
  188. c2?.activate()
  189. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints")
  190. c1?.deactivate()
  191. c2?.deactivate()
  192. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints")
  193. c1?.activate()
  194. c2?.activate()
  195. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints")
  196. }
  197. func testEdgeConstraints() {
  198. let view = View()
  199. self.container.addSubview(view)
  200. view.snp.makeConstraints { (make) -> Void in
  201. make.edges.equalTo(self.container).offset(50.0)
  202. }
  203. XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints")
  204. let constraints = self.container.snp_constraints as! [NSLayoutConstraint]
  205. XCTAssertEqual(constraints[0].constant, 50, "Should be 50")
  206. XCTAssertEqual(constraints[1].constant, 50, "Should be 50")
  207. XCTAssertEqual(constraints[2].constant, 50, "Should be 50")
  208. XCTAssertEqual(constraints[3].constant, 50, "Should be 50")
  209. }
  210. func testUpdateReferencedConstraints() {
  211. let v1 = View()
  212. let v2 = View()
  213. self.container.addSubview(v1)
  214. self.container.addSubview(v2)
  215. var c1: Constraint! = nil
  216. var c2: Constraint! = nil
  217. v1.snp.makeConstraints { (make) -> Void in
  218. c1 = make.top.equalTo(v2).offset(50).constraint
  219. c2 = make.bottom.equalTo(v2).offset(25).constraint
  220. return
  221. }
  222. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints")
  223. let constraints = (self.container.snp_constraints as! [NSLayoutConstraint]).sorted { $0.constant > $1.constant }
  224. XCTAssertEqual(constraints[0].constant, 50, "Should be 50")
  225. XCTAssertEqual(constraints[1].constant, 25, "Should be 25")
  226. c1.update(offset: 15)
  227. c2.update(offset: 20)
  228. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints")
  229. XCTAssertEqual(constraints[0].constant, 15, "Should be 15")
  230. XCTAssertEqual(constraints[1].constant, 20, "Should be 20")
  231. c1.update(inset: 15)
  232. c2.update(inset: 20)
  233. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints")
  234. XCTAssertEqual(constraints[0].constant, 15, "Should be 15")
  235. XCTAssertEqual(constraints[1].constant, -20, "Should be -20")
  236. }
  237. func testInsetsAsConstraintsConstant() {
  238. let view = View()
  239. self.container.addSubview(view)
  240. view.snp.makeConstraints { (make) -> Void in
  241. make.edges.equalTo(self.container).inset(50.0)
  242. }
  243. XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints")
  244. let constraints = (self.container.snp_constraints as! [NSLayoutConstraint]).sorted { $0.constant > $1.constant }
  245. XCTAssertEqual(constraints[0].constant, 50, "Should be 50")
  246. XCTAssertEqual(constraints[1].constant, 50, "Should be 50")
  247. XCTAssertEqual(constraints[2].constant, -50, "Should be -50")
  248. XCTAssertEqual(constraints[3].constant, -50, "Should be -50")
  249. }
  250. func testConstraintInsetsAsImpliedEqualToConstraints() {
  251. let view = View()
  252. self.container.addSubview(view)
  253. view.snp.makeConstraints { (make) -> Void in
  254. make.edges.equalTo(ConstraintInsets(top: 25, left: 25, bottom: 25, right: 25))
  255. }
  256. XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints")
  257. let constraints = (self.container.snp_constraints as! [NSLayoutConstraint]).sorted { $0.constant > $1.constant }
  258. XCTAssertEqual(constraints[0].constant, 25, "Should be 25")
  259. XCTAssertEqual(constraints[1].constant, 25, "Should be 25")
  260. XCTAssertEqual(constraints[2].constant, -25, "Should be -25")
  261. XCTAssertEqual(constraints[3].constant, -25, "Should be -25")
  262. }
  263. func testConstraintInsetsAsConstraintsConstant() {
  264. let view = View()
  265. self.container.addSubview(view)
  266. view.snp.makeConstraints { (make) -> Void in
  267. make.edges.equalTo(self.container).inset(ConstraintInsets(top: 25, left: 25, bottom: 25, right: 25))
  268. }
  269. XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints")
  270. let constraints = (self.container.snp_constraints as! [NSLayoutConstraint]).sorted { $0.constant > $1.constant }
  271. XCTAssertEqual(constraints[0].constant, 25, "Should be 25")
  272. XCTAssertEqual(constraints[1].constant, 25, "Should be 25")
  273. XCTAssertEqual(constraints[2].constant, -25, "Should be -25")
  274. XCTAssertEqual(constraints[3].constant, -25, "Should be -25")
  275. }
  276. func testSizeConstraints() {
  277. let view = View()
  278. self.container.addSubview(view)
  279. view.snp.makeConstraints { (make) -> Void in
  280. make.size.equalTo(CGSize(width: 50, height: 50))
  281. make.left.top.equalTo(self.container)
  282. }
  283. XCTAssertEqual(view.snp_constraints.count, 2, "Should have 2 constraints")
  284. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints")
  285. let constraints = view.snp_constraints as! [NSLayoutConstraint]
  286. // no guarantee which order the constraints are in, but we should test their couple
  287. let widthHeight = (NSLayoutAttribute.width.rawValue, NSLayoutAttribute.height.rawValue)
  288. let heightWidth = (widthHeight.1, widthHeight.0)
  289. let firstSecond = (constraints[0].firstAttribute.rawValue, constraints[1].firstAttribute.rawValue)
  290. // constraint values are correct in either width, height or height, width order
  291. XCTAssertTrue(firstSecond == widthHeight || firstSecond == heightWidth, "2 contraint values should match")
  292. XCTAssertEqual(constraints[0].constant, 50, "Should be 50")
  293. XCTAssertEqual(constraints[1].constant, 50, "Should be 50")
  294. }
  295. func testCenterConstraints() {
  296. let view = View()
  297. self.container.addSubview(view)
  298. view.snp.makeConstraints { (make) -> Void in
  299. make.center.equalTo(self.container).offset(50.0)
  300. }
  301. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints")
  302. let constraints = self.container.snp_constraints as! [NSLayoutConstraint]
  303. XCTAssertEqual(constraints[0].constant, 50, "Should be 50")
  304. XCTAssertEqual(constraints[1].constant, 50, "Should be 50")
  305. }
  306. func testConstraintIdentifier() {
  307. let identifier = "Test-Identifier"
  308. let view = View()
  309. self.container.addSubview(view)
  310. view.snp.makeConstraints { (make) -> Void in
  311. make.top.equalTo(self.container.snp.top).labeled(identifier)
  312. }
  313. let constraints = container.snp_constraints as! [NSLayoutConstraint]
  314. XCTAssertEqual(constraints[0].identifier, identifier, "Identifier should be 'Test'")
  315. }
  316. func testEdgesToEdges() {
  317. var fromAttributes = Set<NSLayoutAttribute>()
  318. var toAttributes = Set<NSLayoutAttribute>()
  319. let view = View()
  320. self.container.addSubview(view)
  321. view.snp.remakeConstraints { (make) -> Void in
  322. make.edges.equalTo(self.container.snp.edges)
  323. }
  324. XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints")
  325. for constraint in (container.snp_constraints as! [NSLayoutConstraint]) {
  326. fromAttributes.insert(constraint.firstAttribute)
  327. toAttributes.insert(constraint.secondAttribute)
  328. }
  329. XCTAssert(fromAttributes == [.top, .left, .bottom, .right])
  330. XCTAssert(toAttributes == [.top, .left, .bottom, .right])
  331. }
  332. #if os(iOS) || os(tvOS)
  333. func testEdgesToMargins() {
  334. var fromAttributes = Set<NSLayoutAttribute>()
  335. var toAttributes = Set<NSLayoutAttribute>()
  336. let view = View()
  337. self.container.addSubview(view)
  338. view.snp.remakeConstraints { (make) -> Void in
  339. make.edges.equalTo(self.container.snp.margins)
  340. }
  341. XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints")
  342. for constraint in (container.snp_constraints as! [NSLayoutConstraint]) {
  343. fromAttributes.insert(constraint.firstAttribute)
  344. toAttributes.insert(constraint.secondAttribute)
  345. }
  346. XCTAssert(fromAttributes == [.top, .left, .bottom, .right])
  347. XCTAssert(toAttributes == [.topMargin, .leftMargin, .bottomMargin, .rightMargin])
  348. fromAttributes.removeAll()
  349. toAttributes.removeAll()
  350. view.snp.remakeConstraints { (make) -> Void in
  351. make.margins.equalTo(self.container.snp.edges)
  352. }
  353. XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints")
  354. for constraint in (container.snp_constraints as! [NSLayoutConstraint]) {
  355. fromAttributes.insert(constraint.firstAttribute)
  356. toAttributes.insert(constraint.secondAttribute)
  357. }
  358. XCTAssert(toAttributes == [.top, .left, .bottom, .right])
  359. XCTAssert(fromAttributes == [.topMargin, .leftMargin, .bottomMargin, .rightMargin])
  360. }
  361. func testLayoutGuideConstraints() {
  362. let vc = UIViewController()
  363. vc.view = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
  364. vc.view.addSubview(self.container)
  365. self.container.snp.makeConstraints { (make) -> Void in
  366. make.top.equalTo(vc.topLayoutGuide.snp.bottom)
  367. make.bottom.equalTo(vc.bottomLayoutGuide.snp.top)
  368. }
  369. XCTAssertEqual(vc.view.snp_constraints.count, 2, "Should have 2 constraints installed")
  370. }
  371. #endif
  372. func testCanSetLabel() {
  373. self.container.snp.setLabel("Hello World")
  374. }
  375. }