Advertisement
Guest User

Untitled

a guest
Feb 8th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. //
  2. // UIRefreshControl+testing.swift
  3. // Kakhiel
  4. //
  5. // Created by Koen Punt on 08-02-16.
  6. // Copyright © 2016 Fetch!. All rights reserved.
  7. //
  8.  
  9. import Foundation
  10. import UIKit
  11.  
  12. /** testing Extends UIRefreshControl
  13.  
  14. */
  15. extension UIRefreshControl {
  16.  
  17. public override class func initialize() {
  18. struct Static {
  19. static var token: dispatch_once_t = 0
  20. }
  21.  
  22. // make sure this isn't a subclass
  23. if self !== UIRefreshControl.self {
  24. return
  25. }
  26.  
  27. dispatch_once(&Static.token) {
  28. var originalSelector = Selector("beginRefreshing")
  29. var swizzledSelector = Selector("kp_beginRefreshing")
  30.  
  31. var originalMethod = class_getInstanceMethod(self, originalSelector)
  32. var swizzledMethod = class_getInstanceMethod(self, swizzledSelector)
  33.  
  34. var didAddMethod = class_addMethod(self, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod))
  35.  
  36. if didAddMethod {
  37. class_replaceMethod(self, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod))
  38. } else {
  39. method_exchangeImplementations(originalMethod, swizzledMethod)
  40. }
  41.  
  42.  
  43. originalSelector = Selector("endRefreshing")
  44. swizzledSelector = Selector("kp_endRefreshing")
  45.  
  46. originalMethod = class_getInstanceMethod(self, originalSelector)
  47. swizzledMethod = class_getInstanceMethod(self, swizzledSelector)
  48.  
  49. didAddMethod = class_addMethod(self, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod))
  50.  
  51. if didAddMethod {
  52. class_replaceMethod(self, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod))
  53. } else {
  54. method_exchangeImplementations(originalMethod, swizzledMethod)
  55. }
  56.  
  57. }
  58. }
  59.  
  60. // MARK: - Method Swizzling
  61.  
  62.  
  63. // Overrides so that app idles correctly when running UITests
  64. func kp_beginRefreshing() {
  65. print("Mock: Begin Refreshing")
  66. }
  67.  
  68. func kp_endRefreshing() {
  69. print("Mock: End Refreshing")
  70. }
  71.  
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement