@@ -21,63 +21,101 @@ class IterableHtmlMessageViewControllerTests: XCTestCase {
21
21
super. tearDown ( )
22
22
}
23
23
24
- func testWebViewPositioning ( ) {
24
+ func testWebViewTopPositioning ( ) {
25
25
IterableAPI . initializeForTesting ( )
26
- struct MockViewCalculations : ViewCalculationsProtocol {
27
- func width( for _: UIView ) -> CGFloat {
28
- return 1234.0
29
- }
30
- }
31
- class MockWebView : WebViewProtocol {
32
- let view : UIView = UIView ( )
33
-
34
- func loadHTMLString( _: String , baseURL _: URL ? ) -> WKNavigation ? {
35
- return nil
36
- }
37
-
38
- func set( position: ViewPosition ) {
39
- self . position = position
40
- view. frame. size. width = position. width
41
- view. frame. size. height = position. height
42
- view. center = position. center
43
- }
44
-
45
- func set( navigationDelegate _: WKNavigationDelegate ? ) { }
46
-
47
- func evaluateJavaScript( _: String , completionHandler: ( ( Any ? , Error ? ) -> Void ) ? ) {
48
- completionHandler ? ( CGFloat ( 200.0 ) , nil )
49
- }
50
-
51
- func layoutSubviews( ) { }
52
-
53
- var position : ViewPosition ?
54
- }
55
- struct MockInjectedDependencyModule : InjectedDependencyModuleProtocol {
56
- static let shared = MockInjectedDependencyModule ( )
57
-
58
- let viewCalculations : ViewCalculationsProtocol = MockViewCalculations ( )
59
-
60
- let webView : WebViewProtocol = MockWebView ( )
61
-
62
- init ( ) { }
63
- }
26
+ checkPositioning ( viewPosition: ViewPosition ( width: 1234 , height: 400 , center: CGPoint ( x: 617.0 , y: 200.0 ) ) ,
27
+ safeAreaInsets: . zero,
28
+ inAppHeight: 200 ,
29
+ messageLocation: . top,
30
+ expectedWebViewPosition: ViewPosition ( width: 1234 , height: 200 , center: CGPoint ( x: 617.0 , y: 100.0 ) ) )
31
+ }
32
+
33
+ func testWebViewBottomPositioning( ) {
34
+ IterableAPI . initializeForTesting ( )
35
+ checkPositioning ( viewPosition: ViewPosition ( width: 1234 , height: 400 , center: CGPoint ( x: 617.0 , y: 200.0 ) ) ,
36
+ safeAreaInsets: . zero,
37
+ inAppHeight: 200 ,
38
+ messageLocation: . bottom,
39
+ expectedWebViewPosition: ViewPosition ( width: 1234 , height: 200 , center: CGPoint ( x: 617.0 , y: 300.0 ) ) )
40
+ }
41
+
42
+ func testWebViewCenterPositioning( ) {
43
+ IterableAPI . initializeForTesting ( )
44
+ checkPositioning ( viewPosition: ViewPosition ( width: 1234 , height: 400 , center: CGPoint ( x: 617.0 , y: 200.0 ) ) ,
45
+ safeAreaInsets: . zero,
46
+ inAppHeight: 200 ,
47
+ messageLocation: . center,
48
+ expectedWebViewPosition: ViewPosition ( width: 1234 , height: 200 , center: CGPoint ( x: 617.0 , y: 200.0 ) ) )
49
+ }
50
+
51
+ func testWebViewFullPositioning( ) {
52
+ IterableAPI . initializeForTesting ( )
53
+ checkPositioning ( viewPosition: ViewPosition ( width: 1234 , height: 400 , center: CGPoint ( x: 617.0 , y: 200.0 ) ) ,
54
+ safeAreaInsets: . zero,
55
+ inAppHeight: 200 ,
56
+ messageLocation: . full,
57
+ expectedWebViewPosition: ViewPosition ( width: 1234 , height: 400 , center: CGPoint ( x: 617.0 , y: 200.0 ) ) )
58
+ }
59
+
60
+ func testWebViewTopPositioningWithSafeAreaInsets( ) {
61
+ IterableAPI . initializeForTesting ( )
62
+ checkPositioning ( viewPosition: ViewPosition ( width: 1234 , height: 400 , center: CGPoint ( x: 617.0 , y: 200.0 ) ) ,
63
+ safeAreaInsets: UIEdgeInsets ( top: 25 , left: 0 , bottom: 30 , right: 0 ) ,
64
+ inAppHeight: 200 ,
65
+ messageLocation: . top,
66
+ expectedWebViewPosition: ViewPosition ( width: 1234 , height: 200 , center: CGPoint ( x: 617.0 , y: 125.0 ) ) )
67
+ }
68
+
69
+ func testWebViewBottomPositioningWithSafeAreaInsets( ) {
70
+ IterableAPI . initializeForTesting ( )
71
+ checkPositioning ( viewPosition: ViewPosition ( width: 1234 , height: 400 , center: CGPoint ( x: 617.0 , y: 200.0 ) ) ,
72
+ safeAreaInsets: UIEdgeInsets ( top: 25 , left: 0 , bottom: 30 , right: 0 ) ,
73
+ inAppHeight: 200 ,
74
+ messageLocation: . bottom,
75
+ expectedWebViewPosition: ViewPosition ( width: 1234 , height: 200 , center: CGPoint ( x: 617.0 , y: 270.0 ) ) )
76
+ }
77
+
78
+ private func checkPositioning( viewPosition: ViewPosition ,
79
+ safeAreaInsets: UIEdgeInsets ,
80
+ inAppHeight: CGFloat ,
81
+ messageLocation: IterableMessageLocation ,
82
+ expectedWebViewPosition: ViewPosition ) {
83
+ let viewCalculations = MockViewCalculations ( viewPosition: viewPosition, safeAreaInsets: safeAreaInsets)
84
+ let webView = MockWebView ( height: inAppHeight)
85
+ let dependencyModule = MockInjectedDependencyModule ( viewCalculations: viewCalculations, webView: webView)
64
86
65
87
InjectedDependencies . shared. set {
66
- MockInjectedDependencyModule . shared as InjectedDependencyModuleProtocol
88
+ dependencyModule as InjectedDependencyModuleProtocol
67
89
}
68
90
69
91
let viewController = IterableHtmlMessageViewController . create ( parameters: IterableHtmlMessageViewController . Parameters ( html: " " ,
70
- padding: UIEdgeInsets ( top : 0 , left : 0 , bottom : - 1 , right : 0 ) ,
92
+ padding: padding ( from : messageLocation ) ,
71
93
messageMetadata: nil ,
72
94
isModal: true ,
73
95
inboxSessionId: nil ) ) . viewController
74
96
viewController. loadView ( )
75
97
viewController. viewDidLayoutSubviews ( )
76
98
77
- let mockWebView = MockInjectedDependencyModule . shared. webView as! MockWebView
78
- XCTAssertEqual ( mockWebView. view. frame. width, 1234.0 )
79
- XCTAssertEqual ( mockWebView. view. frame. height, 200.0 )
80
- XCTAssertEqual ( mockWebView. view. center. x, 617.0 )
81
- XCTAssertEqual ( mockWebView. view. center. y, 100.0 )
99
+ checkPosition ( for: webView. view, expectedPosition: expectedWebViewPosition)
100
+ }
101
+
102
+ private func checkPosition( for view: UIView , expectedPosition: ViewPosition ) {
103
+ XCTAssertEqual ( view. frame. width, expectedPosition. width)
104
+ XCTAssertEqual ( view. frame. height, expectedPosition. height)
105
+ XCTAssertEqual ( view. center. x, expectedPosition. center. x)
106
+ XCTAssertEqual ( view. center. y, expectedPosition. center. y)
107
+ }
108
+
109
+ private func padding( from messageLocation: IterableMessageLocation ) -> UIEdgeInsets {
110
+ switch messageLocation {
111
+ case . top:
112
+ return UIEdgeInsets ( top: 0 , left: 0 , bottom: - 1 , right: 0 )
113
+ case . bottom:
114
+ return UIEdgeInsets ( top: - 1 , left: 0 , bottom: 0 , right: 0 )
115
+ case . center:
116
+ return UIEdgeInsets ( top: - 1 , left: 0 , bottom: - 1 , right: 0 )
117
+ case . full:
118
+ return UIEdgeInsets ( top: 0 , left: 0 , bottom: 0 , right: 0 )
119
+ }
82
120
}
83
121
}
0 commit comments