Skip to content

Commit

Permalink
Merge pull request #153 from DJBen/ui-testing
Browse files Browse the repository at this point in the history
Add testings for the main app and eliminate test warnings
  • Loading branch information
DJBen authored Feb 10, 2018
2 parents 425cd25 + f3e515f commit 7e5a2a6
Show file tree
Hide file tree
Showing 17 changed files with 805 additions and 55 deletions.
285 changes: 284 additions & 1 deletion Graviton.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions Graviton.xcodeproj/xcshareddata/xcschemes/Graviton.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@
ReferencedContainer = "container:Graviton.xcodeproj">
</BuildableReference>
</TestableReference>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "FCFE7DF5202BBC7C00760633"
BuildableName = "GravitonTests.xctest"
BlueprintName = "GravitonTests"
ReferencedContainer = "container:Graviton.xcodeproj">
</BuildableReference>
</TestableReference>
</Testables>
<MacroExpansion>
<BuildableReference
Expand Down
52 changes: 9 additions & 43 deletions Graviton/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@

import UIKit
import Orbits
import RealmSwift
import SwiftyBeaver

let logger = SwiftyBeaver.self

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
Expand All @@ -20,24 +16,25 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
configureLogging()
migrateRealmIfNeeded()

RealmManager.default.migrateRealmIfNeeded()
UINavigationBar.configureNavigationBarStyles()
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSAttributedStringKey.foregroundColor.rawValue: UIColor.white]

// Disable online fetching in unit tests
let isInTest = ProcessInfo.processInfo.environment["XCTestConfigurationFilePath"] != nil
// Disable online fetching in unit tests or UI tests
let isInTest = ProcessInfo.processInfo.environment["XCTestConfigurationFilePath"] != nil || CommandLine.arguments.contains("--ui-testing")
DispatchQueue.global().asyncAfter(deadline: DispatchTime.now() + DispatchTimeInterval.seconds(2)) {
EphemerisManager.default.fetch(mode: isInTest ? .localOnly : .mixed)
}
RiseTransitSetManager.globalMode = isInTest ? .localOnly : .preferLocal
CelestialBodyObserverInfoManager.globalMode = isInTest ? .localOnly : .preferLocal
LocationManager.default.startLocationService()

DispatchQueue.main.async {
self.displaySceneKitBrokenWarning()
if CommandLine.arguments.contains("--ui-testing") {
// Set up UI Testing environment
LocationManager.default.locationOverride = UITestingConstants.location
RealmManager.default.clearRedundantRealm(daysAgo: 0)
} else {
RealmManager.default.clearRedundantRealm()
}

return true
}

Expand All @@ -57,37 +54,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
}
}

private func configureLogging() {
let console = ConsoleDestination()
console.minLevel = .verbose
logger.addDestination(console)
}

private func migrateRealmIfNeeded() {
let config = Realm.Configuration(
// Set the new schema version. This must be greater than the previously used
// version (if you've never set a schema version before, the version is 0).
schemaVersion: 1,

// Set the block which will be called automatically when opening a Realm with
// a schema version lower than the one set above
migrationBlock: { _, oldSchemaVersion in
// We haven’t migrated anything yet, so oldSchemaVersion == 0
if oldSchemaVersion < 1 {
// Nothing to do!
// Realm will automatically detect new properties and removed properties
// And will update the schema on disk automatically
}
})

// Tell Realm to use this new configuration object for the default Realm
Realm.Configuration.defaultConfiguration = config

// Now that we've told Realm how to handle the schema change, opening the file
// will automatically perform the migration
_ = try! Realm()
}

func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
Expand Down
45 changes: 45 additions & 0 deletions Graviton/Common/Managers/RealmManager.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//
// RealmManager.swift
// Graviton
//
// Created by Sihao Lu on 12/13/17.
// Copyright © 2017 Ben Lu. All rights reserved.
//

import RealmSwift
import Orbits

class RealmManager {
static let `default` = RealmManager()

func migrateRealmIfNeeded() {
let config = Realm.Configuration(
// Set the new schema version. This must be greater than the previously used
// version (if you've never set a schema version before, the version is 0).
schemaVersion: 1,

// Set the block which will be called automatically when opening a Realm with
// a schema version lower than the one set above
migrationBlock: { _, oldSchemaVersion in
// We haven’t migrated anything yet, so oldSchemaVersion == 0
if oldSchemaVersion < 1 {
// Nothing to do!
// Realm will automatically detect new properties and removed properties
// And will update the schema on disk automatically
}
})

// Tell Realm to use this new configuration object for the default Realm
Realm.Configuration.defaultConfiguration = config

// Now that we've told Realm how to handle the schema change, opening the file
// will automatically perform the migration
_ = try! Realm()
}

func clearRedundantRealm(daysAgo: Double = 3) {
// Remove all cached realm objects more than a few days ago
CelestialBodyObserverInfo.clearOutdatedInfo(daysAgo: daysAgo)
RiseTransitSetElevation.clearOutdatedInfo(daysAgo: daysAgo)
}
}
5 changes: 5 additions & 0 deletions Graviton/Common/Utils/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import UIKit
import CoreLocation

struct Constants {
struct Menu {
Expand All @@ -32,3 +33,7 @@ struct Constants {
static let maximumDisplayMagnitude: Double = 5.3
}
}

struct UITestingConstants {
static let location = CLLocation(latitude: 37.775482, longitude: -122.417517)
}
16 changes: 9 additions & 7 deletions Graviton/Common/Utils/Formatters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,21 @@ class CoordinateFormatter: Formatter {
}
let lat = coordinate.latitude
let long = coordinate.longitude
func stripNegativeSign(_ dms: DegreeMinuteSecond) -> String {
func stripNegativeSign(_ dms: DegreeAngle) -> String {
if dms.value >= 0 {
return dms.description
return dms.compoundDescription
} else {
var str = dms.description
var str = dms.compoundDescription
str.remove(at: str.startIndex)
return str
}
}
let latDms = DegreeMinuteSecond(value: lat)
latDms.decimalNumberFormatter = Formatters.twoDecimalPointFormatter
let longDms = DegreeMinuteSecond(value: long)
longDms.decimalNumberFormatter = Formatters.twoDecimalPointFormatter
let latDms = DegreeAngle(lat)
latDms.wrapMode = .range_180
latDms.compoundDecimalNumberFormatter = Formatters.twoDecimalPointFormatter
let longDms = DegreeAngle(long)
longDms.wrapMode = .range_180
longDms.compoundDecimalNumberFormatter = Formatters.twoDecimalPointFormatter
let latStr = stripNegativeSign(latDms) + (lat >= 0 ? " N" : " S")
let longStr = stripNegativeSign(longDms) + (long >= 0 ? " E" : " W")
return "\(latStr), \(longStr)"
Expand Down
17 changes: 17 additions & 0 deletions Graviton/Common/Utils/Logging.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// Logging.swift
// Graviton
//
// Created by Sihao Lu on 2/7/18.
// Copyright © 2018 Ben Lu. All rights reserved.
//

import SwiftyBeaver

let logger = SwiftyBeaver.self

func configureLogging() {
let console = ConsoleDestination()
console.minLevel = .verbose
logger.addDestination(console)
}
28 changes: 28 additions & 0 deletions GravitonTests/FormattersTest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// FormattersTest.swift
// GravitonTests
//
// Created by Sihao Lu on 2/7/18.
// Copyright © 2018 Ben Lu. All rights reserved.
//

import XCTest
import CoreLocation
@testable import Graviton

class FormattersTest: XCTestCase {
func testCoordinateFormatter() {
let formatter = CoordinateFormatter()

let coordinate = CLLocationCoordinate2D(latitude: -32.1, longitude: 58.42)
let result = formatter.string(for: coordinate)
XCTAssertEqual(result, "32° 6′ 0″ S, 58° 25′ 12″ E")

let coordinate2 = CLLocationCoordinate2D(latitude: 173.94, longitude: -21.7)
let result2 = formatter.string(for: coordinate2)
XCTAssertEqual(result2, "173° 56′ 24″ N, 21° 41′ 60″ W")

let zero = CLLocationCoordinate2D()
XCTAssertEqual(formatter.string(for: zero), "0° 0′ 0″ N, 0° 0′ 0″ E")
}
}
22 changes: 22 additions & 0 deletions GravitonTests/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>BNDL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
</dict>
</plist>
39 changes: 39 additions & 0 deletions GravitonUITests/GravitonUITests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// GravitonUITests.swift
// GravitonUITests
//
// Created by Sihao Lu on 12/12/17.
// Copyright © 2017 Ben Lu. All rights reserved.
//

import XCTest

class GravitonUITests: XCTestCase {
var app: XCUIApplication!

override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
// In UI tests it is usually best to stop immediately when a failure occurs.
continueAfterFailure = false
app = XCUIApplication()
setupSnapshot(app)
app.launchArguments.append("--ui-testing")
}

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() {
app.launch()
app.navigationBars["Graviton.ObserverView"].buttons["menu icon settings"].tap()
let tablesQuery = app.tables
tablesQuery/*@START_MENU_TOKEN@*/.staticTexts["Debugging"]/*[[".cells.staticTexts[\"Debugging\"]",".staticTexts[\"Debugging\"]"],[[[-1,1],[-1,0]]],[0]]@END_MENU_TOKEN@*/.tap()
tablesQuery/*@START_MENU_TOKEN@*/.staticTexts["Horizontal Directions"]/*[[".cells.staticTexts[\"Horizontal Directions\"]",".staticTexts[\"Horizontal Directions\"]"],[[[-1,1],[-1,0]]],[0]]@END_MENU_TOKEN@*/.tap()
tablesQuery/*@START_MENU_TOKEN@*/.buttons["West"]/*[[".cells.buttons[\"West\"]",".buttons[\"West\"]"],[[[-1,1],[-1,0]]],[0]]@END_MENU_TOKEN@*/.tap()

// Use XCTAssert and related functions to verify your tests produce the correct results.
}
}
22 changes: 22 additions & 0 deletions GravitonUITests/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>BNDL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
</dict>
</plist>
Loading

0 comments on commit 7e5a2a6

Please sign in to comment.