Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 37 additions & 30 deletions Tests/FlexLayoutTests/FlexLayoutTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,28 @@
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

import XCTest
import Testing
import UIKit
@testable import FlexLayout

@MainActor
final class FlexLayoutTests: XCTestCase {
struct FlexLayoutTests {

func testRetainCycle() {
@Test
func flexDoesNotCreateRetainCycle() {
weak var weakView: UIView? = nil
_ = { _ in
let strongView = UIView()
strongView.flex.direction(.column)
weakView = strongView
}(())

XCTAssertNil(weakView, "Creation of flex should not lead to retain cycle")
#expect(weakView == nil, "Creation of flex should not lead to retain cycle")
}


func testRemoveViewDynamically() {
@Test
func removeViewDynamically() {
let rootFlexContainer = UIView()
rootFlexContainer.flex.addItem(UIView())
rootFlexContainer.flex.define { _ in }
Expand All @@ -38,10 +41,11 @@ final class FlexLayoutTests: XCTestCase {
rootFlexContainer.flex.markDirty()
rootFlexContainer.flex.layout()

XCTAssertTrue(rootFlexContainer.subviews.isEmpty)
#expect(rootFlexContainer.subviews.isEmpty)
}

func testLayoutPreservesNonFlexViewFrameAcrossLayouts() {
@Test
func layoutPreservesNonFlexViewFrameAcrossLayouts() {
let container = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
let originalFrame = CGRect(x: 30, y: 40, width: 50, height: 60)
let overlay = UIView(frame: originalFrame)
Expand All @@ -53,22 +57,23 @@ final class FlexLayoutTests: XCTestCase {

container.flex.layout()

XCTAssertEqual(overlay.frame, originalFrame)
XCTAssertFalse(overlay.isFlexEnabled)
XCTAssertEqual(flexView.frame, CGRect(x: 0, y: 0, width: 200, height: 20))
#expect(overlay.frame == originalFrame)
#expect(!overlay.isFlexEnabled)
#expect(flexView.frame == CGRect(x: 0, y: 0, width: 200, height: 20))

let updatedFrame = CGRect(x: 45, y: 55, width: 65, height: 75)
overlay.frame = updatedFrame
container.frame.size.width = 300

container.flex.layout()

XCTAssertEqual(overlay.frame, updatedFrame)
XCTAssertFalse(overlay.isFlexEnabled)
XCTAssertEqual(flexView.frame, CGRect(x: 0, y: 0, width: 300, height: 20))
#expect(overlay.frame == updatedFrame)
#expect(!overlay.isFlexEnabled)
#expect(flexView.frame == CGRect(x: 0, y: 0, width: 300, height: 20))
}

func testLayoutPreservesNonFlexSubtreeFrames() {
@Test
func layoutPreservesNonFlexSubtreeFrames() {
let container = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
container.flex.addItem().width(20).height(20)

Expand All @@ -82,12 +87,13 @@ final class FlexLayoutTests: XCTestCase {

container.flex.layout()

XCTAssertEqual(overlay.frame, overlayFrame)
XCTAssertEqual(child.frame, childFrame)
XCTAssertFalse(overlay.isFlexEnabled)
#expect(overlay.frame == overlayFrame)
#expect(child.frame == childFrame)
#expect(!overlay.isFlexEnabled)
}

func testLayoutUpdatesSubtreeWhenInclusionChanges() {
@Test
func layoutUpdatesSubtreeWhenInclusionChanges() {
let container = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
let excludedFrame = CGRect(x: 30, y: 40, width: 50, height: 60)
let excludedView = UIView(frame: excludedFrame)
Expand All @@ -100,28 +106,29 @@ final class FlexLayoutTests: XCTestCase {

container.flex.layout()

XCTAssertEqual(excludedView.frame, excludedFrame)
XCTAssertEqual(child.frame, childFrame)
XCTAssertEqual(flexView.frame, CGRect(x: 0, y: 0, width: 20, height: 20))
#expect(excludedView.frame == excludedFrame)
#expect(child.frame == childFrame)
#expect(flexView.frame == CGRect(x: 0, y: 0, width: 20, height: 20))

excludedView.flex.isIncludedInLayout = true
container.flex.layout()

XCTAssertEqual(excludedView.frame, CGRect(x: 0, y: 0, width: 100, height: 100))
XCTAssertEqual(child.frame, CGRect(x: 0, y: 0, width: 10, height: 10))
XCTAssertEqual(flexView.frame, CGRect(x: 0, y: 100, width: 20, height: 20))
#expect(excludedView.frame == CGRect(x: 0, y: 0, width: 100, height: 100))
#expect(child.frame == CGRect(x: 0, y: 0, width: 10, height: 10))
#expect(flexView.frame == CGRect(x: 0, y: 100, width: 20, height: 20))

excludedView.flex.isIncludedInLayout = false
excludedView.frame = excludedFrame
child.frame = childFrame
container.flex.layout()

XCTAssertEqual(excludedView.frame, excludedFrame)
XCTAssertEqual(child.frame, childFrame)
XCTAssertEqual(flexView.frame, CGRect(x: 0, y: 0, width: 20, height: 20))
#expect(excludedView.frame == excludedFrame)
#expect(child.frame == childFrame)
#expect(flexView.frame == CGRect(x: 0, y: 0, width: 20, height: 20))
}

func testLayoutAppliesFramesToIncludedFlexDescendants() {
@Test
func layoutAppliesFramesToIncludedFlexDescendants() {
let container = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
let nestedContainer = UIView()
let child = UIView()
Expand All @@ -130,7 +137,7 @@ final class FlexLayoutTests: XCTestCase {

container.flex.layout()

XCTAssertEqual(nestedContainer.frame, CGRect(x: 0, y: 0, width: 100, height: 100))
XCTAssertEqual(child.frame, CGRect(x: 10, y: 10, width: 20, height: 30))
#expect(nestedContainer.frame == CGRect(x: 0, y: 0, width: 100, height: 100))
#expect(child.frame == CGRect(x: 10, y: 10, width: 20, height: 30))
}
}
88 changes: 52 additions & 36 deletions Tests/FlexLayoutTests/InsetTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@
// POSSIBILITY OF SUCH DAMAGE.

import FlexLayout
import XCTest
import Testing
import UIKit

@MainActor
final class InsetTests: XCTestCase, Sendable {
struct InsetTests {

var viewController: UIViewController!
var rootFlexContainer: UIView!
var aView: UIView!

override func setUp() async throws {
try await super.setUp()
init() {

viewController = UIViewController()

Expand All @@ -32,137 +32,153 @@ final class InsetTests: XCTestCase, Sendable {
aView = UIView()
}

func test_adjust_the_aView_size_and_relative_position_with_left() {
@Test
func adjustTheAViewSizeAndRelativePositionWithLeft() {
rootFlexContainer.flex.define { flex in
flex.addItem(aView).position(.relative).grow(1).left(10)
}
rootFlexContainer.flex.layout()
XCTAssertEqual(aView.frame, CGRect(x: 10.0, y: 0.0, width: 400.0, height: 400.0))
#expect(aView.frame == CGRect(x: 10.0, y: 0.0, width: 400.0, height: 400.0))
}

func test_adjust_the_aView_size_and_relative_position_with_top() {
@Test
func adjustTheAViewSizeAndRelativePositionWithTop() {
rootFlexContainer.flex.define { flex in
flex.addItem(aView).position(.relative).grow(1).top(10)
}
rootFlexContainer.flex.layout()
XCTAssertEqual(aView.frame, CGRect(x: 0.0, y: 10.0, width: 400.0, height: 400.0))
#expect(aView.frame == CGRect(x: 0.0, y: 10.0, width: 400.0, height: 400.0))
}

func test_adjust_the_aView_size_and_relative_position_with_right() {
@Test
func adjustTheAViewSizeAndRelativePositionWithRight() {
rootFlexContainer.flex.define { flex in
flex.addItem(aView).position(.relative).grow(1).right(10)
}
rootFlexContainer.flex.layout()
XCTAssertEqual(aView.frame, CGRect(x: -10.0, y: 0.0, width: 400.0, height: 400.0))
#expect(aView.frame == CGRect(x: -10.0, y: 0.0, width: 400.0, height: 400.0))
}

func test_adjust_the_aView_size_and_relative_position_with_bottom() {
@Test
func adjustTheAViewSizeAndRelativePositionWithBottom() {
rootFlexContainer.flex.define { flex in
flex.addItem(aView).position(.relative).grow(1).bottom(10)
}
rootFlexContainer.flex.layout()
XCTAssertEqual(aView.frame, CGRect(x: 0.0, y: -10.0, width: 400.0, height: 400.0))
#expect(aView.frame == CGRect(x: 0.0, y: -10.0, width: 400.0, height: 400.0))
}

func test_adjust_the_aView_size_and_relative_position_with_horizontally() {
@Test
func adjustTheAViewSizeAndRelativePositionWithHorizontally() {
rootFlexContainer.flex.define { flex in
flex.addItem(aView).position(.relative).grow(1).horizontally(10)
}
rootFlexContainer.flex.layout()
XCTAssertEqual(aView.frame, CGRect(x: 10.0, y: 0.0, width: 400.0, height: 400.0))
#expect(aView.frame == CGRect(x: 10.0, y: 0.0, width: 400.0, height: 400.0))
}

func test_adjust_the_aView_size_and_relative_position_with_vertically() {
@Test
func adjustTheAViewSizeAndRelativePositionWithVertically() {
rootFlexContainer.flex.define { flex in
flex.addItem(aView).position(.relative).grow(1).vertically(10)
}
rootFlexContainer.flex.layout()
XCTAssertEqual(aView.frame, CGRect(x: 0.0, y: 10.0, width: 400.0, height: 400.0))
#expect(aView.frame == CGRect(x: 0.0, y: 10.0, width: 400.0, height: 400.0))
}

func test_adjust_the_aView_size_absolute_position_with_left_right() {
@Test
func adjustTheAViewSizeAbsolutePositionWithLeftRight() {
rootFlexContainer.flex.define { flex in
flex.addItem(aView).position(.absolute).left(10).right(10).width(300).height(200)
}

rootFlexContainer.flex.layout()
XCTAssertEqual(aView.frame, CGRect(x: 10.0, y: 0.0, width: 300.0, height: 200.0))
#expect(aView.frame == CGRect(x: 10.0, y: 0.0, width: 300.0, height: 200.0))
}

func test_adjust_the_aView_size_absolute_position_with_right_and_reset_left_later() {
@Test
func adjustTheAViewSizeAbsolutePositionWithRightAndResetLeftLater() {
rootFlexContainer.flex.define { flex in
flex.addItem(aView).position(.absolute).left(10).right(10).width(300).height(200)
}

// Later reset the aspectRatio
aView.flex.left(0)
rootFlexContainer.flex.layout()
XCTAssertEqual(aView.frame, CGRect(x: 0.0, y: 0.0, width: 300.0, height: 200.0))
#expect(aView.frame == CGRect(x: 0.0, y: 0.0, width: 300.0, height: 200.0))
}

func test_adjust_the_aView_size_absolute_position_with_horizontally_vertically() {
@Test
func adjustTheAViewSizeAbsolutePositionWithHorizontallyVertically() {
rootFlexContainer.flex.define { flex in
flex.addItem(aView).position(.absolute).horizontally(15).vertically(20)
}

rootFlexContainer.flex.layout()
XCTAssertEqual(aView.frame, CGRect(x: 15.0, y: 20.0, width: 370.0, height: 360.0))
#expect(aView.frame == CGRect(x: 15.0, y: 20.0, width: 370.0, height: 360.0))
}

func test_adjust_the_aView_size_absolute_position_with_all() {
@Test
func adjustTheAViewSizeAbsolutePositionWithAll() {
rootFlexContainer.flex.define { flex in
flex.addItem(aView).position(.absolute).all(45)
}

rootFlexContainer.flex.layout()
XCTAssertEqual(aView.frame, CGRect(x: 45.0, y: 45.0, width: 310.0, height: 310.0))
#expect(aView.frame == CGRect(x: 45.0, y: 45.0, width: 310.0, height: 310.0))
}

func test_adjust_the_aView_size_and_relative_static_with_left() {
@Test
func adjustTheAViewSizeAndRelativeStaticWithLeft() {
rootFlexContainer.flex.define { flex in
flex.addItem(aView).position(.static).grow(1).left(10)
}
rootFlexContainer.flex.layout()
XCTAssertEqual(aView.frame, CGRect(x: 0.0, y: 0.0, width: 400.0, height: 400.0))
#expect(aView.frame == CGRect(x: 0.0, y: 0.0, width: 400.0, height: 400.0))
}

func test_adjust_the_aView_size_and_static_position_with_top() {
@Test
func adjustTheAViewSizeAndStaticPositionWithTop() {
rootFlexContainer.flex.define { flex in
flex.addItem(aView).position(.static).grow(1).top(10)
}
rootFlexContainer.flex.layout()
XCTAssertEqual(aView.frame, CGRect(x: 0.0, y: 0.0, width: 400.0, height: 400.0))
#expect(aView.frame == CGRect(x: 0.0, y: 0.0, width: 400.0, height: 400.0))
}

func test_adjust_the_aView_size_and_static_position_with_right() {
@Test
func adjustTheAViewSizeAndStaticPositionWithRight() {
rootFlexContainer.flex.define { flex in
flex.addItem(aView).position(.static).grow(1).right(10)
}
rootFlexContainer.flex.layout()
XCTAssertEqual(aView.frame, CGRect(x: 0.0, y: 0.0, width: 400.0, height: 400.0))
#expect(aView.frame == CGRect(x: 0.0, y: 0.0, width: 400.0, height: 400.0))
}

func test_adjust_the_aView_size_and_static_position_with_bottom() {
@Test
func adjustTheAViewSizeAndStaticPositionWithBottom() {
rootFlexContainer.flex.define { flex in
flex.addItem(aView).position(.static).grow(1).bottom(10)
}
rootFlexContainer.flex.layout()
XCTAssertEqual(aView.frame, CGRect(x: 0.0, y: 0.0, width: 400.0, height: 400.0))
#expect(aView.frame == CGRect(x: 0.0, y: 0.0, width: 400.0, height: 400.0))
}

func test_adjust_the_aView_size_and_static_position_with_horizontally() {
@Test
func adjustTheAViewSizeAndStaticPositionWithHorizontally() {
rootFlexContainer.flex.define { flex in
flex.addItem(aView).position(.static).grow(1).horizontally(10)
}
rootFlexContainer.flex.layout()
XCTAssertEqual(aView.frame, CGRect(x: 0.0, y: 0.0, width: 400.0, height: 400.0))
#expect(aView.frame == CGRect(x: 0.0, y: 0.0, width: 400.0, height: 400.0))
}

func test_adjust_the_aView_size_and_static_position_with_vertically() {
@Test
func adjustTheAViewSizeAndStaticPositionWithVertically() {
rootFlexContainer.flex.define { flex in
flex.addItem(aView).position(.static).grow(1).vertically(10)
}
rootFlexContainer.flex.layout()
XCTAssertEqual(aView.frame, CGRect(x: 0.0, y: 0.0, width: 400.0, height: 400.0))
#expect(aView.frame == CGRect(x: 0.0, y: 0.0, width: 400.0, height: 400.0))
}
}
Loading