PixelTest icon indicating copy to clipboard operation
PixelTest copied to clipboard

Add support for testing multiple sizes

Open Jon889 opened this issue 6 years ago • 4 comments

Currently we wrap the verify method and loop through an array of sizes to run the snapshot as if they were on different width devices. It would be great if this was integrated into the framework.

Also we have almost implemented testing for different accessibility sizes on each device size, which has caught a few issues. The code is a bit ugly (no public API to change the text size) but I think it would be worth integrating into the framework?

Jon889 avatar Dec 04 '18 13:12 Jon889

I like the idea of different sizes being built in.

In relation to the multiple text sizes, I wrote a post on how to do this without using private APIs: https://edit.theappbusiness.com/snapshot-testing-dynamic-type-74119ee36042

Does depend on how you set stuff up though. I'd be interested to see what you did!

KaneCheshire avatar Dec 04 '18 13:12 KaneCheshire

I did some work on an app where I created a Device struct and an extension function so I could verify snapshots on specific device sizes, like verifyWithFixedLayoutStyle(myView, forDevice: .iPhoneSE).

Is that what you're referring to? Or do you mean having the ability to specify any size? @Jon889

theblixguy avatar Dec 04 '18 13:12 theblixguy

It's probably TAB code then :) Here is it adapted to PixelTest (also changed to struct instead of enum)

extension PixelTestCase {
	
	public struct Device {
		let width: CGFloat
		let height: CGFloat
		public static let iPhone5S = Device(width: 320, height: 568)
		public static let iPhoneX = Device(width: 375, height: 736)
		public static let iPhone7Plus = Device(width: 414, height: 812)
	}
	
	func verify(_ view: UIView, devices: [Device] = [.iPhone5S, .iPhoneX, .iPhone7Plus], dynamicHeight: Bool = true,
					scale: Scale = .native, file: StaticString = #file, function: StaticString = #function, line: UInt = #line) {
		devices.forEach {
			let layoutStyle = dynamicHeight ? LayoutStyle.dynamicHeight(fixedWidth: $0.width) : LayoutStyle.fixed(width: $0.width, height: $0.height)
			verify(view, layoutStyle: layoutStyle, scale: scale, file: file, function: function, line: line)
		}
	}
}

Jon889 avatar Dec 04 '18 13:12 Jon889

Oh yeah I forget about that, the problem is though that we have to support iOS 10 😢 so we can't use UIFontMetrics instead we have UILabel/etc subclasses that reapply the font when the text size changes.

Jon889 avatar Dec 04 '18 13:12 Jon889