Tests.swift 18 KB

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