You'll need to a -DTESTING
compile flag to the configuration you use for testing to enable this extension only for test builds.
Last active
May 2, 2016 16:47
-
-
Save koenpunt/cf8130bdfc432d31136b to your computer and use it in GitHub Desktop.
UIRefreshControl override so that app correctly idles while UI testing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// UIRefreshControl+testing.swift | |
// Kakhiel | |
// | |
// Created by Koen Punt on 08-02-16. | |
// Copyright © 2016 Fetch!. All rights reserved. | |
// | |
import Foundation | |
import UIKit | |
/** testing Extends UIRefreshControl | |
*/ | |
#if TESTING | |
extension UIRefreshControl { | |
public override class func initialize() { | |
struct Static { | |
static var token: dispatch_once_t = 0 | |
} | |
// make sure this isn't a subclass | |
if self !== UIRefreshControl.self { | |
return | |
} | |
dispatch_once(&Static.token) { | |
let originalSelector = Selector("_setRefreshControlState:notify:") | |
let swizzledSelector = Selector("kp__setRefreshControlState:notify:") | |
let originalMethod = class_getInstanceMethod(self, originalSelector) | |
let swizzledMethod = class_getInstanceMethod(self, swizzledSelector) | |
let didAddMethod = class_addMethod(self, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod)) | |
if didAddMethod { | |
class_replaceMethod(self, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod)) | |
} else { | |
method_exchangeImplementations(originalMethod, swizzledMethod) | |
} | |
} | |
} | |
// MARK: - Method Swizzling | |
// Overrides so that app idles correctly when running UITests | |
func kp__setRefreshControlState(state: Int, notify: Bool) { | |
print("state: \(state) notify: \(notify)") | |
} | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
do you have any idea how to do this with the Swift 2.2 selector syntax?