|
| 1 | +// |
| 2 | +// ===----------------------------------------------------------------------===// |
| 3 | +// |
| 4 | +// LayoutViewTests.swift |
| 5 | +// |
| 6 | +// Created by Steven Grosmark on 08/10/2021 |
| 7 | +// Copyright © 2021 WW International, Inc. |
| 8 | +// |
| 9 | +// |
| 10 | +// This source file is part of the WWLayout open source project |
| 11 | +// |
| 12 | +// https://github.com/ww-tech/wwlayout |
| 13 | +// |
| 14 | +// Copyright © 2017-2021 WW International, Inc. |
| 15 | +// |
| 16 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 17 | +// you may not use this file except in compliance with the License. |
| 18 | +// You may obtain a copy of the License at |
| 19 | +// |
| 20 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 21 | +// |
| 22 | +// Unless required by applicable law or agreed to in writing, software |
| 23 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 24 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 25 | +// See the License for the specific language governing permissions and |
| 26 | +// limitations under the License. |
| 27 | +// |
| 28 | +// ===----------------------------------------------------------------------===// |
| 29 | +// |
| 30 | + |
| 31 | +import XCTest |
| 32 | +@testable import WWLayout |
| 33 | + |
| 34 | +class LayoutViewTests: XCTestCase { |
| 35 | + |
| 36 | + private var window: UIWindow! |
| 37 | + |
| 38 | + override func setUp() { |
| 39 | + super.setUp() |
| 40 | + |
| 41 | + window = UIWindow() |
| 42 | + window.rootViewController = UIViewController() |
| 43 | + } |
| 44 | + |
| 45 | + override func tearDown() { |
| 46 | + window = nil |
| 47 | + |
| 48 | + super.tearDown() |
| 49 | + } |
| 50 | + |
| 51 | + func test_creationInViewController() throws { |
| 52 | + // given |
| 53 | + let label = UILabel() |
| 54 | + window.rootViewController?.view.addSubview(label) |
| 55 | + |
| 56 | + // when |
| 57 | + label.layout(horizontalSize: .compact).fill(.superview) |
| 58 | + |
| 59 | + // then |
| 60 | + let layoutView = findLayoutView() |
| 61 | + XCTAssertNotNil(layoutView, "LayoutView should exist") |
| 62 | + XCTAssert(layoutView?.superview === window.rootViewController?.view, "LayoutView should be created at root of the view controller" ) |
| 63 | + } |
| 64 | + |
| 65 | + func test_creationInViewController_nestedViews() throws { |
| 66 | + // given |
| 67 | + let label = UILabel() |
| 68 | + window.rootViewController?.view.addSubview(UIView(subviews: UIView(subviews: label))) |
| 69 | + |
| 70 | + // when |
| 71 | + label.layout(horizontalSize: .compact).fill(.superview) |
| 72 | + |
| 73 | + // then |
| 74 | + let layoutView = findLayoutView() |
| 75 | + XCTAssertNotNil(layoutView, "LayoutView should exist") |
| 76 | + XCTAssert(layoutView?.superview === window.rootViewController?.view, "LayoutView should be created at root of the view controller" ) |
| 77 | + } |
| 78 | + |
| 79 | + func test_creationInViewController_nestedController() throws { |
| 80 | + // given |
| 81 | + let childController = UIViewController() |
| 82 | + let label = UILabel() |
| 83 | + |
| 84 | + childController.view.addSubview(UIView(subviews: UIView(subviews: label))) |
| 85 | + window.rootViewController?.addChild(childController) |
| 86 | + window.rootViewController?.view.addSubview(childController.view) |
| 87 | + |
| 88 | + // when |
| 89 | + label.layout(horizontalSize: .compact).fill(.superview) |
| 90 | + |
| 91 | + // then |
| 92 | + let layoutView = findLayoutView() |
| 93 | + XCTAssertNotNil(layoutView, "LayoutView should exist") |
| 94 | + XCTAssert(layoutView?.superview === childController.view, "LayoutView should be created at root of the _child_ view controller" ) |
| 95 | + } |
| 96 | + |
| 97 | + func test_creationInWindow() throws { |
| 98 | + // given |
| 99 | + let label = UILabel() |
| 100 | + let targetSubview = UIView(subviews: UIView(subviews: label)) |
| 101 | + window.addSubview(targetSubview) |
| 102 | + |
| 103 | + // when |
| 104 | + label.layout(horizontalSize: .compact).fill(.superview) |
| 105 | + |
| 106 | + // then |
| 107 | + let layoutView = findLayoutView() |
| 108 | + XCTAssertNotNil(layoutView, "LayoutView should exist") |
| 109 | + XCTAssert(layoutView?.superview === targetSubview, "LayoutView should be created in child of thw window" ) |
| 110 | + } |
| 111 | + |
| 112 | + private func findLayoutView() -> LayoutView? { |
| 113 | + var toCheck: [UIView] = [window, window.rootViewController?.view].compactMap { return $0 } |
| 114 | + while !toCheck.isEmpty { |
| 115 | + let candidate = toCheck.removeFirst() |
| 116 | + if let layoutView = candidate as? LayoutView { |
| 117 | + return layoutView |
| 118 | + } |
| 119 | + toCheck.append(contentsOf: candidate.subviews) |
| 120 | + } |
| 121 | + return nil |
| 122 | + } |
| 123 | + |
| 124 | +} |
0 commit comments