Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Testing a constraint-based layout of UIView
Search
Shin Yamamoto
August 27, 2019
Programming
1
1.1k
Testing a constraint-based layout of UIView
Shin Yamamoto
August 27, 2019
Tweet
Share
More Decks by Shin Yamamoto
See All by Shin Yamamoto
Meet CoreDevice and devicectl
scenee
0
110
DocCのドキュメントをGithub Pagesで公開する
scenee
2
510
SwiftライブラリのObjC対応における落とし穴と回避策
scenee
0
250
Playing Sheet presentation style on iOS 13
scenee
0
530
Fluid Interfacesの実践
scenee
4
1.4k
Introducing Relax
scenee
0
490
Potatotips#7: Box C String Tip
scenee
0
2.7k
Other Decks in Programming
See All in Programming
PHP8の機能を使って堅牢にコードを書く
fendo181
6
2.6k
上手な探索的テストとその上達方法について
matsu802
4
660
DocC Tutorial と TCA におけるテスト機能の紹介
kalupas226
1
330
自作ソフト(VMagicMirror)がVRMA対応してる話+実装のTips
bakudreameater
0
110
チームでモデリングを育てるうえで 考えたこと・気づいたこと / Cultivating Modeling in Teams: Thoughts and Insights
mackey0225
5
2.4k
Open Source Swiftc Workshop
kitasuke
1
290
DDD, necessary but insufficient: physical design principles for microservices
cer
PRO
0
2.2k
MySQL のインデックスの種類をおさらいしよう! / overviewing indexes in MySQL
okashoi
0
170
ここ1~2年くらいで 使えるようになった(主要ブラウザーの最新版 がすべて対応した ) ウェブの新機能について ランダムに喋る!
myzkyy
9
6.5k
品質とスピードを両立: TypeScriptの柔軟な型システムをバックエンドで活用する
kosui
5
1.1k
両面どころかインフラもTSでできるよ ~ 全方位TypeScriptによるプロダクト開発 ~
myfinder
9
2.5k
導入から5年が経って見えた Datadog APM 運用の課題
bgpat
2
540
Featured
See All Featured
Code Reviewing Like a Champion
maltzj
512
39k
Mobile First: as difficult as doing things right
swwweet
215
8.5k
Facilitating Awesome Meetings
lara
39
5.5k
Build The Right Thing And Hit Your Dates
maggiecrowley
23
1.9k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
6
950
Java REST API Framework Comparison - PWX 2021
mraible
PRO
18
6.8k
Automating Front-end Workflow
addyosmani
1353
200k
A better future with KSS
kneath
230
16k
No one is an island. Learnings from fostering a developers community.
thoeni
14
2k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
153
14k
KATA
mclloyd
14
11k
Designing the Hi-DPI Web
ddemaree
275
33k
Transcript
Testing a constraint-based layout of UIView iOS Test Night #11
Shin Yamamoto @scenee #ios_test_night
今日は.. • AutoLayout(constraint-based)のレイアウトテスト • 前提と手法 • UIViewにフォーカス ◦ UIViewControllerは割愛しますが、ほぼ同じ
なぜUIViewレイアウトを テストしたいのか?
XCTestでレイアウトを ユニットテストしたいから
XCTestでレイアウトをユニットテストしたい • OSSのメンテンスなどで必要に QAで時間が溶ける!
XCTestでレイアウトをユニットテストしたい • OSSのメンテンスなどで必要に QAで時間が溶ける! • 安心して追加変更、デグレ防止
XCTestでレイアウトをユニットテストしたい • OSSのメンテンスなどで必要に QAで時間が溶ける! • 安心して追加変更、デグレ防止 • 素早い開発フィードバックループ Design Develop
Unit testing
どうしたらレイアウトを ユニットテストできるか?
SnapshotTesting with Jest
SnapshotTesting with Jest • 画面に描画する • Reactツリーを出力 • 比較 •
差分検出
ポイントは...
レンダラーを信頼する
レンダラーが あるレイアウトを期待通り描画する と仮定すること!
コントロールできない領域を テスト対象から除外する
UIKitのレンダラーを信頼する • あるレイアウト結果が、期待通りに描画されると想定 • レイアウト結果とは ◦ UIViewの各プロパティ値(frameなど) ◦ View hierarchy(Layer
tree) ◦ など
もう少し詳しく説明します
High Performance Auto Layout: https://developer.apple.com/videos/play/wwdc2018/220
High Performance Auto Layout: https://developer.apple.com/videos/play/wwdc2018/220 Layout pass Update pass Display
pass
High Performance Auto Layout: https://developer.apple.com/videos/play/wwdc2018/220 draw(_:) layoutSubviews(_:) updateConstraints(_:)
High Performance Auto Layout: https://developer.apple.com/videos/play/wwdc2018/220 draw(_:)はテストしない layoutSubviews(_:) updateConstraints(_:)
何が嬉しいのか?
レイアウトのパラメーター化 • レイアウトを各パラメーターの集合として処理 • 各パラメーターの変化をXCTestで評価
Pros • 気軽にViewのテストが書ける • Viewの振る舞いをテストで記述・保存 ◦ i.e. リグレッションテストが簡単に • Viewをテスト駆動で開発・リファクタ
◦ i.e. frame-based layoutからの移行が楽に
Cons • 描画結果が想定と異なるケースがある
この前提の上で...
早速テストを書いてみましょう! コード例ではわかりやすさのため使ってませんが、 swift-snaphost-testing が便利 https://github.com/pointfreeco/swift-snapshot-testing
MyView .containerView top bottom left right
MyView .containerView top bottom left right
レイアウト更新をテスト
レイアウト更新をテスト
レイアウト更新をテスト
レイアウト更新をテスト
❌ Failed: frame値が.zero
レイアウト更新をテスト
レイアウト更新をテスト
✅ Passed: frameが期待通り更新
なぜ?
Layout passを手動実行している
Layout passを手動実行している setNeedsLayoutはLayout passをlayoutIfNeededで実行するために
Mysteries of Auto Layout, Part 2: https://developer.apple.com/videos/play/wwdc2015/219
1. 制約の変更 2. setNeedsLayout() + layoutIfNeeded() 3. frameを評価 XCTestでのレイアウトテスト方法
• Intrinsic Content Sizeに応じたテスト可 • Unsatisfiable constraintsエラーもコンソールに出力 補足
簡単ですね! あくまで導入です!でも導入が大事。
✅ Passed: UIWindow + MainRunLoop ✏ Frameworkでは、Host Application追加不要
まとめ: XCTestでレイアウトテストをするには? • UIKitのレンダラーを信頼する • レイアウトをパラメーター化して評価 • レイアウト更新するにはLayout passを手動実行
References ◦ https://jestjs.io/docs/en/snapshot-testing Snapshot Testing · Jest ◦ https://github.com/pointfreeco/swift-snapshot-testing swift-snapshot-testing
◦ https://developer.apple.com/videos/play/wwdc2015/219 Mysteries of Auto Layout, Part 2 ◦ https://developer.apple.com/videos/play/wwdc2018/220 High Performance Auto Layout ◦ http://tech.gc.com/demystifying-ios-layout/ Demystifying iOS Layout
Happy Testing 質問があればぜひ懇親会で話かけてください
None