Skip to content

Instantly share code, notes, and snippets.

@djs-code
Created October 3, 2018 00:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save djs-code/ce67b29e6af68d48b05e525fec81c8e0 to your computer and use it in GitHub Desktop.
Save djs-code/ce67b29e6af68d48b05e525fec81c8e0 to your computer and use it in GitHub Desktop.
import XCTest
var myExpectation: XCTestExpectation? = nil
class Label: UILabel {
override func invalidateIntrinsicContentSize() {
super.invalidateIntrinsicContentSize()
myExpectation = XCTestExpectation(description: "\(String(describing: text))")
}
override func layoutSubviews() {
super.layoutSubviews()
myExpectation!.fulfill()
}
}
class MyView1: UIView {
let iconView: UIView = UIView()
let label = Label()
let stackView = UIStackView()
override init(frame: CGRect = .zero) {
super.init(frame: frame)
stackView.axis = .horizontal
stackView.addArrangedSubview(iconView)
stackView.addArrangedSubview(label)
iconView.backgroundColor = .green
iconView.heightAnchor.constraint(equalToConstant: 30).isActive = true
iconView.widthAnchor.constraint(equalToConstant: 30).isActive = true
label.backgroundColor = .blue
addSubview(stackView)
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.topAnchor.constraint(equalTo: topAnchor).isActive = true
stackView.leftAnchor.constraint(equalTo: leftAnchor).isActive = true
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
class MyView2: UIView {
let iconView: UIView = UIView()
let label = Label()
override init(frame: CGRect = .zero) {
super.init(frame: frame)
addSubview(iconView)
addSubview(label)
iconView.backgroundColor = .green
iconView.translatesAutoresizingMaskIntoConstraints = false
iconView.heightAnchor.constraint(equalToConstant: 30).isActive = true
iconView.widthAnchor.constraint(equalToConstant: 30).isActive = true
iconView.topAnchor.constraint(equalTo: topAnchor).isActive = true
iconView.leftAnchor.constraint(equalTo: leftAnchor).isActive = true
label.translatesAutoresizingMaskIntoConstraints = false
label.topAnchor.constraint(equalTo: topAnchor).isActive = true
label.leftAnchor.constraint(equalTo: iconView.rightAnchor).isActive = true
label.heightAnchor.constraint(equalToConstant: 30).isActive = true
label.backgroundColor = .blue
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
class MyView3: UIView {
let iconView: UIView = UIView()
let label = Label()
override init(frame: CGRect = .zero) {
super.init(frame: frame)
addSubview(iconView)
addSubview(label)
iconView.backgroundColor = .green
label.backgroundColor = .blue
iconView.frame = CGRect(x: 0, y: 0, width: 30, height: 30).integral
}
override func layoutSubviews() {
super.layoutSubviews()
let labelSize = label.sizeThatFits(CGSize(width: CGFloat.infinity, height: CGFloat.infinity))
label.frame = CGRect(x: iconView.frame.maxX, y: 0, width: labelSize.width, height: 30).integral
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
class StackViewTest2Tests: XCTestCase {
let count = 10000
func testWithStackView() {
let myView = MyView1(frame: CGRect(x: 0, y: 0, width: 200, height: 50))
myView.backgroundColor = .red
let window = UIApplication.shared.keyWindow
window?.addSubview(myView)
for i in 0...count {
myView.label.text = "\(i)"
wait(for: [myExpectation!], timeout: 5)
}
myView.removeFromSuperview()
}
func testWithRegularConstraints() {
let myView = MyView2(frame: CGRect(x: 0, y: 0, width: 200, height: 50))
myView.backgroundColor = .red
let window = UIApplication.shared.keyWindow
window?.addSubview(myView)
for i in 0...count {
myView.label.text = "\(i)"
wait(for: [myExpectation!], timeout: 5)
}
myView.removeFromSuperview()
}
func testWithManualLayout() {
let myView = MyView3(frame: CGRect(x: 0, y: 0, width: 200, height: 50))
myView.backgroundColor = .red
let window = UIApplication.shared.keyWindow!
window.addSubview(myView)
for i in 0...count {
myView.label.text = "\(i)"
myView.setNeedsLayout()
wait(for: [myExpectation!], timeout: 5)
}
myView.removeFromSuperview()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment