Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Adonis Peralta committed Sep 9, 2018
1 parent 38e2a18 commit f1331c2
Show file tree
Hide file tree
Showing 6 changed files with 455 additions and 49 deletions.
18 changes: 14 additions & 4 deletions RMessage.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
/* Begin PBXBuildFile section */
7A43CAF12144E8A300A2A0D1 /* RMessage.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 7A71D40E2144DA8E0050BD01 /* RMessage.framework */; };
7A43CAF22144E8A300A2A0D1 /* RMessage.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 7A71D40E2144DA8E0050BD01 /* RMessage.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
7A7045AF21450EFF0086370D /* TestHelpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A7045AE21450EFF0086370D /* TestHelpers.swift */; };
7A7045B021450EFF0086370D /* TestHelpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A7045AE21450EFF0086370D /* TestHelpers.swift */; };
7A71D4122144DA8E0050BD01 /* RMessage.h in Headers */ = {isa = PBXBuildFile; fileRef = 7A71D4102144DA8E0050BD01 /* RMessage.h */; settings = {ATTRIBUTES = (Public, ); }; };
7A71D41B2144DBDB0050BD01 /* UIWindow+ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A7AB90B2115191C00A9D5C1 /* UIWindow+ViewController.swift */; };
7A71D41C2144DBDB0050BD01 /* SlideAnimator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A2DD59421192A6B00025ADE /* SlideAnimator.swift */; };
Expand All @@ -31,10 +33,11 @@
7AB3842B211386C700DA8076 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 7AB3842A211386C700DA8076 /* Assets.xcassets */; };
7AB3842E211386C700DA8076 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 7AB3842C211386C700DA8076 /* LaunchScreen.storyboard */; };
7AB38439211386C700DA8076 /* RMessageTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7AB38438211386C700DA8076 /* RMessageTests.swift */; };
7AB38444211386C700DA8076 /* RMessageUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7AB38443211386C700DA8076 /* RMessageUITests.swift */; };
7AB38444211386C700DA8076 /* NavigationControllerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7AB38443211386C700DA8076 /* NavigationControllerTests.swift */; };
7AB384562113883700DA8076 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 7AB384552113883700DA8076 /* Main.storyboard */; };
7ADB44922144EAD1009C5782 /* HexColors.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 7A8E69052144E9C9007D0F50 /* HexColors.framework */; };
7ADB44932144EAF1009C5782 /* HexColors.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 7A8E69052144E9C9007D0F50 /* HexColors.framework */; };
7ADB44952144EFA9009C5782 /* ViewControllerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7ADB44942144EFA9009C5782 /* ViewControllerTests.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -80,6 +83,7 @@
7A2DD592211929E500025ADE /* RMAnimator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RMAnimator.swift; sourceTree = "<group>"; };
7A2DD59421192A6B00025ADE /* SlideAnimator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SlideAnimator.swift; sourceTree = "<group>"; };
7A5EDAC62114401500C2BFE3 /* RMessageSpec.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RMessageSpec.swift; sourceTree = "<group>"; };
7A7045AE21450EFF0086370D /* TestHelpers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestHelpers.swift; sourceTree = "<group>"; };
7A71D40E2144DA8E0050BD01 /* RMessage.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = RMessage.framework; sourceTree = BUILT_PRODUCTS_DIR; };
7A71D4102144DA8E0050BD01 /* RMessage.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RMessage.h; sourceTree = "<group>"; };
7A71D4112144DA8E0050BD01 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
Expand All @@ -97,7 +101,7 @@
7AB38438211386C700DA8076 /* RMessageTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RMessageTests.swift; sourceTree = "<group>"; };
7AB3843A211386C700DA8076 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
7AB3843F211386C700DA8076 /* RMessageUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = RMessageUITests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
7AB38443211386C700DA8076 /* RMessageUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RMessageUITests.swift; sourceTree = "<group>"; };
7AB38443211386C700DA8076 /* NavigationControllerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NavigationControllerTests.swift; sourceTree = "<group>"; };
7AB38445211386C700DA8076 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
7AB384552113883700DA8076 /* Main.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = Main.storyboard; sourceTree = "<group>"; };
7AB3845E2113887800DA8076 /* RMessage.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = RMessage.xib; sourceTree = "<group>"; };
Expand All @@ -107,6 +111,7 @@
7AB3847621138F7500DA8076 /* RMControllerDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RMControllerDelegate.swift; sourceTree = "<group>"; };
7AB3DA8121150CFC0061F5B4 /* RMPresenterDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RMPresenterDelegate.swift; sourceTree = "<group>"; };
7AC67916211CF8F00096B3B4 /* RMShowOperation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RMShowOperation.swift; sourceTree = "<group>"; };
7ADB44942144EFA9009C5782 /* ViewControllerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewControllerTests.swift; sourceTree = "<group>"; };
7AE10D73211805D900543F68 /* RMessage+Design.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "RMessage+Design.swift"; sourceTree = "<group>"; };
7AE10D7D21180CBF00543F68 /* RMPresenter+TouchCompletion.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "RMPresenter+TouchCompletion.swift"; sourceTree = "<group>"; };
/* End PBXFileReference section */
Expand Down Expand Up @@ -260,7 +265,9 @@
7AB38442211386C700DA8076 /* UITests */ = {
isa = PBXGroup;
children = (
7AB38443211386C700DA8076 /* RMessageUITests.swift */,
7A7045AE21450EFF0086370D /* TestHelpers.swift */,
7ADB44942144EFA9009C5782 /* ViewControllerTests.swift */,
7AB38443211386C700DA8076 /* NavigationControllerTests.swift */,
7AB38445211386C700DA8076 /* Info.plist */,
);
path = UITests;
Expand Down Expand Up @@ -588,14 +595,17 @@
buildActionMask = 2147483647;
files = (
7AB38439211386C700DA8076 /* RMessageTests.swift in Sources */,
7A7045AF21450EFF0086370D /* TestHelpers.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
7AB3843B211386C700DA8076 /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
7AB38444211386C700DA8076 /* RMessageUITests.swift in Sources */,
7ADB44952144EFA9009C5782 /* ViewControllerTests.swift in Sources */,
7AB38444211386C700DA8076 /* NavigationControllerTests.swift in Sources */,
7A7045B021450EFF0086370D /* TestHelpers.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
18 changes: 7 additions & 11 deletions Tests/RMessageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,21 @@
import XCTest

class RMessageTests: XCTestCase {
let rControl = RMController()

override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
}

override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}

func testExample() {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct results.
}

func testPerformanceExample() {
// This is an example of a performance test case.
measure {
// Put the code you want to measure the time of here.
/// Test that RMessage does not attempt to present if there is no window hierarchy present.
func testQueuedMessages() {
for _ in 1 ... 6 {
rControl.showMessage(withSpec: DefaultRMessageSpec(), title: "Test")
}
XCTAssertTrue(rControl.queueCount == 0)
}
}
190 changes: 190 additions & 0 deletions UITests/NavigationControllerTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
//
// RMessageUITests.swift
// RMessageUITests
//
// Created by Adonis Peralta on 8/2/18.
// Copyright © 2018 None. All rights reserved.
//

import Foundation
import XCTest

class NavigationControllerTests: XCTestCase {
let app = XCUIApplication()
lazy var navBarElement = app.navigationBars.element
lazy var navBarFrame = navBarElement.frame
lazy var windowElement = app.windows.element.firstMatch
lazy var mainWindowFrame = windowElement.frame
lazy var notHittablePredicate = NSPredicate(format: "hittable == FALSE")
lazy var hittablePredicate = NSPredicate(format: "hittable == TRUE")

// Check for message y position to hundredths place
let kMsgYPositionScale = 2

override func setUp() {
super.setUp()
continueAfterFailure = false
XCUIApplication().launch()
}

func showMessageFromTopByPressingButton(withName buttonName: String, hidingNavBar: Bool, timeToShow displayTimeout: TimeInterval, timeToHide dismissTimeout: TimeInterval) {
if hidingNavBar {
app.buttons["Toggle NavBar"].tap()
}

app.buttons[buttonName].tap()

let displayedMessage = app.otherElements["RMessage"]
let padding = springAnimationPadding(forHeight: Float(displayedMessage.frame.size.height))
let expectedMsgYPosition = hidingNavBar ? padding : Float(navBarFrame.size.height + navBarFrame.origin.y) + padding

let messageDisplayed = displayedMessage.waitForExistence(timeout: displayTimeout)

XCTAssert(messageDisplayed, "\(buttonName) message failed to display")

let expectedMessagePositionValid = validateFloatsToScale(
Float(displayedMessage.frame.origin.y),
expectedMsgYPosition, scale: kMsgYPositionScale
)
XCTAssert(expectedMessagePositionValid, "\(buttonName) message displayed in the wrong position")

let exp = expectation(for: notHittablePredicate, evaluatedWith: displayedMessage, handler: nil)
wait(for: [exp], timeout: dismissTimeout)
}

func showBottomMessage(hidingNavBar: Bool, withTimeout displayTimeout: TimeInterval, timeToHide dismissTimeout: TimeInterval) {
if hidingNavBar {
app.buttons["Toggle NavBar"].tap()
}

let buttonName = "Bottom"

app.buttons[buttonName].tap()

let displayedMessage = app.otherElements["RMessage"]
let padding = springAnimationPadding(forHeight: Float(displayedMessage.frame.size.height))
let expectedMsgYPosition = Float(mainWindowFrame.size.height - displayedMessage.frame.size.height) - padding
let messageDisplayed = displayedMessage.waitForExistence(timeout: displayTimeout)

XCTAssert(messageDisplayed, "\(buttonName) message failed to display")

let expectedMessagePositionValid = validateFloatsToScale(
Float(displayedMessage.frame.origin.y),
expectedMsgYPosition, scale: kMsgYPositionScale
)

XCTAssert(expectedMessagePositionValid, "\(buttonName) message displayed in the wrong position")
let exp = expectation(for: notHittablePredicate, evaluatedWith: displayedMessage, handler: nil)
wait(for: [exp], timeout: dismissTimeout)
}

func showEndlessMessage(hidingNavBar: Bool, withTimeout displayTimeout: TimeInterval) {
if hidingNavBar {
app.buttons["Toggle NavBar"].tap()
}

let buttonName = "Endless"

app.buttons[buttonName].tap()

let displayedMessage = app.otherElements["RMessage"]

// The spring animation padding is calculated on the message size prior to accounting for the safe area layout
// guides so to properly determine the position of the view we must remove any safe layout are guide sizing from
// height of the view prior to attempting to calculate the spring animation padding.
let padding = springAnimationPadding(forHeight: Float(displayedMessage.frame.size.height - 20.0))
let expectedMsgYPosition = hidingNavBar ? padding : Float(navBarFrame.size.height + navBarFrame.origin.y) + padding
let messageDisplayed = displayedMessage.waitForExistence(timeout: displayTimeout)

XCTAssert(messageDisplayed, "\(buttonName) message failed to display")

let expectedMessagePositionValid = validateFloatsToScale(
Float(displayedMessage.frame.origin.y),
expectedMsgYPosition, scale: kMsgYPositionScale
)
XCTAssert(expectedMessagePositionValid, "\(buttonName) message displayed in the wrong position")

sleep(20)
XCTAssert(displayedMessage.isHittable, "\(buttonName) message no longer on screen")
}

func testErrorMessage() {
showMessageFromTopByPressingButton(withName: "Error", hidingNavBar: false, timeToShow: 3.0, timeToHide: 8.0)
}

func testNormalMessage() {
showMessageFromTopByPressingButton(withName: "Message", hidingNavBar: false, timeToShow: 3.0, timeToHide: 8.0)
}

func testWarningMessage() {
showMessageFromTopByPressingButton(withName: "Warning", hidingNavBar: false, timeToShow: 3.0, timeToHide: 8.0)
}

func testSuccessMessage() {
showMessageFromTopByPressingButton(withName: "Success", hidingNavBar: false, timeToShow: 3.0, timeToHide: 8.0)
}

func testLongMessage() {
showMessageFromTopByPressingButton(withName: "Text", hidingNavBar: false, timeToShow: 3.0, timeToHide: 12.0)
}

func testButtonMessage() {
showMessageFromTopByPressingButton(withName: "Button", hidingNavBar: false, timeToShow: 3.0, timeToHide: 8.0)
}

func testBottomMessage() {
showBottomMessage(hidingNavBar: false, withTimeout: 3.0, timeToHide: 8.0)
}

func testCustomMessage() {
showMessageFromTopByPressingButton(withName: "Custom design", hidingNavBar: false, timeToShow: 3.0, timeToHide: 8.0)
}

func testImageMessage() {
showMessageFromTopByPressingButton(withName: "Custom image", hidingNavBar: false, timeToShow: 3.0, timeToHide: 8.0)
}

func testEndlessMessage() {
showEndlessMessage(hidingNavBar: false, withTimeout: 3.0)
}

func testErrorMessageNoNavBar() {
showMessageFromTopByPressingButton(withName: "Error", hidingNavBar: true, timeToShow: 3.0, timeToHide: 8.0)
}

func testNormalMessageNoNavBar() {
showMessageFromTopByPressingButton(withName: "Message", hidingNavBar: true, timeToShow: 3.0, timeToHide: 8.0)
}

func testWarningMessageNoNavBar() {
showMessageFromTopByPressingButton(withName: "Warning", hidingNavBar: true, timeToShow: 3.0, timeToHide: 8.0)
}

func testSuccessMessageNoNavBar() {
showMessageFromTopByPressingButton(withName: "Success", hidingNavBar: true, timeToShow: 3.0, timeToHide: 8.0)
}

func testLongMessageNoNavBar() {
showMessageFromTopByPressingButton(withName: "Text", hidingNavBar: true, timeToShow: 3.0, timeToHide: 12.0)
}

func testButtonMessageNoNavBar() {
showMessageFromTopByPressingButton(withName: "Button", hidingNavBar: true, timeToShow: 3.0, timeToHide: 8.0)
}

func testBottomMessageNoNavBar() {
showBottomMessage(hidingNavBar: false, withTimeout: 3.0, timeToHide: 8.0)
}

func testCustomMessageNoNavBar() {
showMessageFromTopByPressingButton(withName: "Custom design", hidingNavBar: false, timeToShow: 3.0, timeToHide: 8.0)
}

func testImageMessageNoNavBar() {
showMessageFromTopByPressingButton(withName: "Custom image", hidingNavBar: false, timeToShow: 3.0, timeToHide: 8.0)
}

func testEndlessMessageNoNavBar() {
showEndlessMessage(hidingNavBar: false, withTimeout: 3.0)
}
}
34 changes: 0 additions & 34 deletions UITests/RMessageUITests.swift

This file was deleted.

22 changes: 22 additions & 0 deletions UITests/TestHelpers.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// TestHelpers.swift
// RMessage
//
// Created by Adonis Peralta on 9/9/18.
// Copyright © 2018 None. All rights reserved.
//

import Foundation

// Returns true or false if the floats are equal to each other after truncating all numbers
// after the scale decimal places
func validateFloatsToScale(_ f1: Float, _ f2: Float, scale: Int) -> Bool {
let truncFactor = powf(10.0, Float(scale))
let tFloat1 = truncf(f1 * truncFactor) / truncFactor
let tFloat2 = truncf(f2 * truncFactor) / truncFactor
return tFloat1 == tFloat2
}

func springAnimationPadding(forHeight height: Float) -> Float {
return ceilf(height / 120) * -5.0
}
Loading

0 comments on commit f1331c2

Please sign in to comment.