Skip to content

Commit

Permalink
Add tests for seg_topViewController
Browse files Browse the repository at this point in the history
  • Loading branch information
humblehacker committed Jul 16, 2019
1 parent a323d5c commit 4099266
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 2 deletions.
6 changes: 6 additions & 0 deletions Analytics.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

/* Begin PBXBuildFile section */
3481C14AEC76E5A8DA64DA59 /* Pods_AnalyticsTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 89033CBF22319674E6CE7E61 /* Pods_AnalyticsTests.framework */; };
5AF0E8AE77F57B356DACCFE7 /* AutoScreenReportingTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5AF0E8457CCFC077382BF449 /* AutoScreenReportingTest.swift */; };
630FC8EA2107F2A500A759C5 /* SEGScreenReporting.h in Headers */ = {isa = PBXBuildFile; fileRef = 5AF0EDFEDC0EE79A0A0EB713 /* SEGScreenReporting.h */; settings = {ATTRIBUTES = (Public, ); }; };
6E265C791FB1178C0030E08E /* IntegrationsManagerTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6E265C781FB1178C0030E08E /* IntegrationsManagerTest.swift */; };
6EEC1C712017EA370089C478 /* EndToEndTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6EEC1C702017EA370089C478 /* EndToEndTests.swift */; };
Expand Down Expand Up @@ -90,7 +91,9 @@

/* Begin PBXFileReference section */
1DF03A6929EEFE7158913224 /* Pods-AnalyticsTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-AnalyticsTests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-AnalyticsTests/Pods-AnalyticsTests.debug.xcconfig"; sourceTree = "<group>"; };
5AF0E8457CCFC077382BF449 /* AutoScreenReportingTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AutoScreenReportingTest.swift; sourceTree = "<group>"; };
5AF0EDFEDC0EE79A0A0EB713 /* SEGScreenReporting.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SEGScreenReporting.h; sourceTree = "<group>"; };
63E090D722DD49C300DEC7EC /* UIViewController+SegScreenTest.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "UIViewController+SegScreenTest.h"; sourceTree = "<group>"; };
6E265C781FB1178C0030E08E /* IntegrationsManagerTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IntegrationsManagerTest.swift; sourceTree = "<group>"; };
6EEC1C702017EA370089C478 /* EndToEndTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EndToEndTests.swift; sourceTree = "<group>"; };
89033CBF22319674E6CE7E61 /* Pods_AnalyticsTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_AnalyticsTests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -268,6 +271,8 @@
EADEB8EC1DECD335005322DA /* AnalyticsTests-Bridging-Header.h */,
6E265C781FB1178C0030E08E /* IntegrationsManagerTest.swift */,
6EEC1C702017EA370089C478 /* EndToEndTests.swift */,
5AF0E8457CCFC077382BF449 /* AutoScreenReportingTest.swift */,
63E090D722DD49C300DEC7EC /* UIViewController+SegScreenTest.h */,
);
indentWidth = 2;
path = AnalyticsTests;
Expand Down Expand Up @@ -618,6 +623,7 @@
EADEB8EF1DECD335005322DA /* NSData+SEGGUNZIPP.m in Sources */,
6EEC1C712017EA370089C478 /* EndToEndTests.swift in Sources */,
EADEB8F01DECD335005322DA /* TestUtils.swift in Sources */,
5AF0E8AE77F57B356DACCFE7 /* AutoScreenReportingTest.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
13 changes: 13 additions & 0 deletions Analytics.xcodeproj/xcshareddata/xcschemes/AnalyticsTests.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForRunning = "YES"
buildForTesting = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "EADEB8691DECD0EF005322DA"
BuildableName = "AnalyticsTests.xctest"
BlueprintName = "AnalyticsTests"
ReferencedContainer = "container:Analytics.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
Expand Down
1 change: 1 addition & 0 deletions Analytics/Analytics.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ FOUNDATION_EXPORT const unsigned char AnalyticsVersionString[];
#import "SEGSegmentIntegrationFactory.h"
#import "SEGContext.h"
#import "SEGMiddleware.h"
#import "SEGScreenReporting.h"
2 changes: 1 addition & 1 deletion Analytics/Classes/Internal/UIViewController+SEGScreen.m
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ - (void)seg_viewDidAppear:(BOOL)animated
}
}

if ([top conformsToProtocol:@protocol(SEGScreenReporting)] && [top respondsToSelector:@selector(seg_trackScreen)]) {
if ([top conformsToProtocol:@protocol(SEGScreenReporting)] && [top respondsToSelector:@selector(seg_trackScreen:name:)]) {
__auto_type screenReporting = (UIViewController<SEGScreenReporting>*)top;
[screenReporting seg_trackScreen:top name:name];
return;
Expand Down
3 changes: 3 additions & 0 deletions AnalyticsTests/AnalyticsTests-Bridging-Header.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
#import <Analytics/SEGAnalyticsUtils.h>
#import <Analytics/SEGIntegrationsManager.h>
#import <Analytics/SEGUtils.h>
#import <Analytics/SEGScreenReporting.h>

#import "NSData+SEGGUNZIPP.h"
// Temp hack. We should fix the LSNocilla podspec to make this header publicly available
#import "LSMatcher.h"

#import "UIViewController+SegScreenTest.h"
136 changes: 136 additions & 0 deletions AnalyticsTests/AutoScreenReportingTest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
//
// Created by David Whetstone on 2018-11-04.
// Copyright (c) 2018 Segment. All rights reserved.
//

import Foundation
import Quick
import Nimble
import SwiftTryCatch
@testable import Analytics

class AutoScreenReportingTests: QuickSpec {

override func spec() {

var window: UIWindow!
var rootVC: UIViewController!

beforeEach {
let config = SEGAnalyticsConfiguration(writeKey: "foobar")
config.trackApplicationLifecycleEvents = true
config.recordScreenViews = true

window = UIWindow()
rootVC = UIViewController()
window.addSubview(rootVC.view)
}


describe("given a single view controller") {

it("seg_topViewController returns that view controller") {
let actualVC = UIViewController.seg_topViewController(rootVC)
expect(actualVC) === rootVC
}
}

describe("given a presented view controller") {

var expectedVC: UIViewController!

beforeEach {
expectedVC = UIViewController()
rootVC.present(expectedVC, animated: false)
}

it("seg_topViewController returns the presented view controller") {
let actualVC = UIViewController.seg_topViewController(rootVC)
expect(actualVC) === expectedVC
}
}

describe("given a pushed view controller") {

var expectedVC: UIViewController!

beforeEach {
expectedVC = UIViewController()
let nc = UINavigationController()
rootVC.present(nc, animated: false)
nc.pushViewController(expectedVC, animated: false)
}

it("seg_topViewController returns the pushed view controller") {
let actualVC = UIViewController.seg_topViewController(rootVC)
expect(actualVC) === expectedVC
}
}

describe("given a child of a UITabBarController") {

var expectedVC: UIViewController!

beforeEach {
expectedVC = UIViewController()
let tabBarController = UITabBarController()
rootVC.present(tabBarController, animated: false)
tabBarController.viewControllers = [UIViewController(), expectedVC, UIViewController()]
tabBarController.selectedIndex = 1
}

it("seg_topViewController returns the currently selected view controller") {
let actualVC = UIViewController.seg_topViewController(rootVC)
expect(actualVC) === expectedVC
}
}

describe("given a child of a custom container view controller conforming to SEGScreenReporting") {

class CustomContainerViewController: UIViewController, SEGScreenReporting {
var selectedIndex: Int = 0
var seg_mainViewController: UIViewController? {
return childViewControllers[selectedIndex]
}
}

var expectedVC: UIViewController!

beforeEach {
expectedVC = UIViewController()
let containerVC = CustomContainerViewController()
rootVC.present(containerVC, animated: false)
[UIViewController(), expectedVC, UIViewController()].forEach { child in
containerVC.addChildViewController(child)
}
containerVC.selectedIndex = 1
}

it("seg_topViewController returns the currently selected view controller") {
let actualVC = UIViewController.seg_topViewController(rootVC)
expect(actualVC) === expectedVC
}
}

describe("given a child of a container view controller NOT conforming to SEGScreenReporting") {

var expectedVC: UIViewController!

beforeEach {
expectedVC = UIViewController()
let containerVC = UIViewController()
rootVC.present(containerVC, animated: false)
[expectedVC, UIViewController(), UIViewController()].forEach { child in
containerVC.addChildViewController(child)
}
}

it("seg_topViewController returns the first child view controller") {
let actualVC = UIViewController.seg_topViewController(rootVC)
expect(actualVC) === expectedVC
}
}
}
}


21 changes: 21 additions & 0 deletions AnalyticsTests/UIViewController+SegScreenTest.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// UIViewController+SegScreenTest.h
// Analytics
//
// Created by David Whetstone on 7/15/19.
// Copyright © 2019 Segment. All rights reserved.
//

#ifndef UIViewController_SegScreenTest_h
#define UIViewController_SegScreenTest_h


@interface UIViewController (Testing)
/// We need to expose this normally private method to tests, as the public facing
/// `+ (UIViewController *)seg_topViewController` relies on the `application` property
/// of `SEGAnalyticsConfiguration`, which won't be set in these tests.
+ (UIViewController *)seg_topViewController:(UIViewController *)rootViewController;
@end


#endif /* UIViewController_SegScreenTest_h */
2 changes: 1 addition & 1 deletion AnalyticsTests/Utils/TestUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class JsonGzippedBody : LSMatcher, LSMatcheable {
}

func matchesJson(_ json: AnyObject) -> Bool {
let actualValue : () -> NSObject! = {
let actualValue : () -> NSObject = {
return json as! NSObject
}
let failureMessage = FailureMessage()
Expand Down

0 comments on commit 4099266

Please sign in to comment.