Tests.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. #if os(iOS) || os(tvOS)
  2. import UIKit
  3. typealias View = UIView
  4. extension View {
  5. var snp_constraints: [AnyObject] { return self.constraints }
  6. }
  7. #else
  8. import AppKit
  9. typealias View = NSView
  10. extension View {
  11. var snp_constraints: [AnyObject] { return self.constraints }
  12. }
  13. #endif
  14. import XCTest
  15. import SnapKit
  16. class SnapKitTests: XCTestCase {
  17. let container = View()
  18. override func setUp() {
  19. super.setUp()
  20. // Put setup code here. This method is called before the invocation of each test method in the class.
  21. }
  22. override func tearDown() {
  23. // Put teardown code here. This method is called after the invocation of each test method in the class.
  24. super.tearDown()
  25. }
  26. @available(iOS 9.0, *)
  27. func testReadableLayoutGuide() {
  28. #if os(iOS) || os(tvOS)
  29. let view = UIView(frame: CGRectMake(0, 0, 300, 300))
  30. view.addSubview(self.container)
  31. let guide = view.readableContentGuide
  32. self.container.snp_makeConstraints { (make) -> Void in
  33. make.top.equalTo(guide)
  34. make.bottom.equalTo(guide)
  35. }
  36. print(view.snp_constraints)
  37. XCTAssertEqual(view.snp_constraints.count, 12, "Should have 12 constraints installed")
  38. #endif
  39. }
  40. func testLayoutGuideConstraints() {
  41. #if os(iOS) || os(tvOS)
  42. let vc = UIViewController()
  43. vc.view = UIView(frame: CGRectMake(0, 0, 300, 300))
  44. vc.view.addSubview(self.container)
  45. self.container.snp_makeConstraints { (make) -> Void in
  46. make.top.equalTo(vc.snp_topLayoutGuideBottom)
  47. make.bottom.equalTo(vc.snp_bottomLayoutGuideTop)
  48. }
  49. XCTAssertEqual(vc.view.snp_constraints.count, 6, "Should have 6 constraints installed")
  50. #endif
  51. }
  52. func testMakeConstraints() {
  53. let v1 = View()
  54. let v2 = View()
  55. self.container.addSubview(v1)
  56. self.container.addSubview(v2)
  57. v1.snp_makeConstraints { (make) -> Void in
  58. make.top.equalTo(v2.snp_top).offset(50)
  59. make.left.equalTo(v2.snp_top).offset(50)
  60. return
  61. }
  62. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints installed")
  63. v2.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 testUpdateConstraints() {
  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(v2.snp_top).offset(50)
  76. make.left.equalTo(v2.snp_top).offset(50)
  77. return
  78. }
  79. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints installed")
  80. v1.snp_updateConstraints { (make) -> Void in
  81. make.top.equalTo(v2.snp_top).offset(15)
  82. return
  83. }
  84. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should still have 2 constraints installed")
  85. }
  86. func testRemakeConstraints() {
  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_remakeConstraints { (make) -> Void in
  98. make.edges.equalTo(v2)
  99. return
  100. }
  101. XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints installed")
  102. }
  103. func testRemoveConstraints() {
  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_removeConstraints()
  115. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints installed")
  116. }
  117. func testPrepareConstraints() {
  118. let v1 = View()
  119. let v2 = View()
  120. self.container.addSubview(v1)
  121. self.container.addSubview(v2)
  122. let constraints = v1.snp_prepareConstraints { (make) -> Void in
  123. make.edges.equalTo(v2)
  124. return
  125. }
  126. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints installed")
  127. for constraint in constraints {
  128. constraint.install()
  129. }
  130. XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints installed")
  131. for constraint in constraints {
  132. constraint.uninstall()
  133. }
  134. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints installed")
  135. }
  136. func testReinstallConstraints() {
  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.install()
  148. }
  149. XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints installed")
  150. for constraint in constraints {
  151. constraint.install()
  152. }
  153. XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 0 constraints installed")
  154. }
  155. func testActivateDeactivateConstraints() {
  156. let v1 = View()
  157. let v2 = View()
  158. self.container.addSubview(v1)
  159. self.container.addSubview(v2)
  160. var c1: Constraint? = nil
  161. var c2: Constraint? = nil
  162. v1.snp_prepareConstraints { (make) -> Void in
  163. c1 = make.top.equalTo(v2.snp_top).offset(50).constraint
  164. c2 = make.left.equalTo(v2.snp_top).offset(50).constraint
  165. return
  166. }
  167. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints")
  168. c1?.activate()
  169. c2?.activate()
  170. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints")
  171. c1?.deactivate()
  172. c2?.deactivate()
  173. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints")
  174. c1?.uninstall()
  175. c2?.uninstall()
  176. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints")
  177. c1?.activate()
  178. c2?.activate()
  179. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints")
  180. }
  181. func testSizeConstraints() {
  182. let view = View()
  183. self.container.addSubview(view)
  184. view.snp_makeConstraints { (make) -> Void in
  185. make.size.equalTo(CGSizeMake(50, 50))
  186. make.left.top.equalTo(self.container)
  187. }
  188. XCTAssertEqual(view.snp_constraints.count, 2, "Should have 2 constraints")
  189. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints")
  190. let constraints = view.snp_constraints as! [NSLayoutConstraint]
  191. XCTAssertEqual(constraints[0].firstAttribute, NSLayoutAttribute.Width, "Should be width")
  192. XCTAssertEqual(constraints[1].firstAttribute, NSLayoutAttribute.Height, "Should be height")
  193. XCTAssertEqual(constraints[0].constant, 50, "Should be 50")
  194. XCTAssertEqual(constraints[1].constant, 50, "Should be 50")
  195. }
  196. func testConstraintIdentifier() {
  197. let identifier = "Test-Identifier"
  198. let view = View()
  199. self.container.addSubview(view)
  200. view.snp_makeConstraints { (make) -> Void in
  201. make.top.equalTo(self.container.snp_top).labeled(identifier)
  202. }
  203. let constraints = container.snp_constraints as! [NSLayoutConstraint]
  204. XCTAssertEqual(constraints[0].identifier, identifier, "Identifier should be 'Test'")
  205. }
  206. func testSuperviewConstraints() {
  207. let view = View()
  208. container.addSubview(view)
  209. view.snp_makeConstraints { (make) -> Void in
  210. make.top.equalToSuperview().inset(10)
  211. make.bottom.equalToSuperview().inset(10)
  212. }
  213. XCTAssertEqual(container.snp_constraints.count, 2, "Should have 2 constraints")
  214. let constraints = container.snp_constraints as! [NSLayoutConstraint]
  215. XCTAssertEqual(constraints[0].firstAttribute, NSLayoutAttribute.Top, "Should be top")
  216. XCTAssertEqual(constraints[1].firstAttribute, NSLayoutAttribute.Bottom, "Should be bottom")
  217. XCTAssertEqual(constraints[0].secondAttribute, NSLayoutAttribute.Top, "Should be top")
  218. XCTAssertEqual(constraints[1].secondAttribute, NSLayoutAttribute.Bottom, "Should be bottom")
  219. XCTAssertEqual(constraints[0].firstItem as? View, view, "Should be added subview")
  220. XCTAssertEqual(constraints[1].firstItem as? View, view, "Should be added subview")
  221. XCTAssertEqual(constraints[0].secondItem as? View, container, "Should be containerView")
  222. XCTAssertEqual(constraints[1].secondItem as? View, container, "Should be containerView")
  223. XCTAssertEqual(constraints[0].constant, 10, "Should be 10")
  224. XCTAssertEqual(constraints[1].constant, -10, "Should be 10")
  225. }
  226. func testNativeConstraints() {
  227. let view = View()
  228. container.addSubview(view)
  229. var topNativeConstraints: [LayoutConstraint]!
  230. var topNativeConstraint: LayoutConstraint?
  231. var sizeNativeConstraints: [LayoutConstraint]!
  232. view.snp_makeConstraints { (make) -> Void in
  233. let topConstraint = make.top.equalToSuperview().inset(10).constraint
  234. topNativeConstraints = topConstraint.layoutConstraints
  235. topNativeConstraint = topConstraint.layoutConstraints.first
  236. let sizeConstraints = make.size.equalTo(50).constraint
  237. sizeNativeConstraints = sizeConstraints.layoutConstraints
  238. }
  239. XCTAssertEqual(topNativeConstraints.count, 1, "make.top should creates one native constraint")
  240. XCTAssertEqual(topNativeConstraint?.constant, 10, "topNativeConstraint.constant is set to 10")
  241. XCTAssertEqual(sizeNativeConstraints.count, 2, "make.tosize should create two native constraint")
  242. XCTAssertEqual(sizeNativeConstraints[0].constant, 50, "sizeNativeConstraints should set size[0] to 50")
  243. XCTAssertEqual(sizeNativeConstraints[1].constant, 50, "sizeNativeConstraints should set size[1] to 50")
  244. }
  245. }