Skip to content

Commit

Permalink
Set up and added some unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
dehli committed Mar 27, 2016
1 parent 4b96869 commit 081a559
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 14 deletions.
4 changes: 4 additions & 0 deletions TouchDraw.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
89F60F6F1BC9580000BCDD93 /* TouchDraw.h in Headers */ = {isa = PBXBuildFile; fileRef = 89F60F6E1BC9580000BCDD93 /* TouchDraw.h */; settings = {ATTRIBUTES = (Public, ); }; };
89F60F761BC9580000BCDD93 /* TouchDraw.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 89F60F6B1BC9580000BCDD93 /* TouchDraw.framework */; };
89F60F7B1BC9580000BCDD93 /* TouchDrawTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 89F60F7A1BC9580000BCDD93 /* TouchDrawTests.swift */; };
89F70C161CA729BA00133F03 /* TouchDrawViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 89F70C151CA729BA00133F03 /* TouchDrawViewController.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand All @@ -31,6 +32,7 @@
89F60F751BC9580000BCDD93 /* TouchDrawTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = TouchDrawTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
89F60F7A1BC9580000BCDD93 /* TouchDrawTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TouchDrawTests.swift; sourceTree = "<group>"; };
89F60F7C1BC9580000BCDD93 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
89F70C151CA729BA00133F03 /* TouchDrawViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TouchDrawViewController.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -83,6 +85,7 @@
89F60F791BC9580000BCDD93 /* TouchDrawTests */ = {
isa = PBXGroup;
children = (
89F70C151CA729BA00133F03 /* TouchDrawViewController.swift */,
89F60F7A1BC9580000BCDD93 /* TouchDrawTests.swift */,
89F60F7C1BC9580000BCDD93 /* Info.plist */,
);
Expand Down Expand Up @@ -205,6 +208,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
89F70C161CA729BA00133F03 /* TouchDrawViewController.swift in Sources */,
89F60F7B1BC9580000BCDD93 /* TouchDrawTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
10 changes: 5 additions & 5 deletions TouchDraw/TouchDrawView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ private class BrushProperties {
}

/// a drawing stroke
private class Stroke {
internal class Stroke {
/// the points that make up the stroke
private var points: NSMutableArray!
/// the properties of the stroke
Expand All @@ -63,7 +63,7 @@ public class TouchDrawView: UIView {
public var delegate: TouchDrawViewDelegate!

/// used to keep track of all the strokes
private var stack: [Stroke]!
internal var stack: [Stroke]!
private var pointsArray: NSMutableArray!

private var lastPoint = CGPoint.zero
Expand Down Expand Up @@ -209,7 +209,7 @@ public class TouchDrawView: UIView {

checkClearState()

if !undoManager!.canRedo {
if undoManager != nil && !undoManager!.canRedo {
if redoEnabled {
delegate.redoDisabled()
redoEnabled = false
Expand Down Expand Up @@ -250,7 +250,7 @@ public class TouchDrawView: UIView {

/// if possible, it will undo the last stroke
public func undo() -> Void {
if undoManager!.canUndo {
if undoManager != nil && undoManager!.canUndo {
undoManager?.undo()

if !redoEnabled {
Expand All @@ -271,7 +271,7 @@ public class TouchDrawView: UIView {

/// if possible, it will redo the last undone stroke
public func redo() -> Void {
if undoManager!.canRedo {
if undoManager != nil && undoManager!.canRedo {
undoManager?.redo()

if !undoEnabled {
Expand Down
33 changes: 24 additions & 9 deletions TouchDrawTests/TouchDrawTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,45 @@
//

import XCTest
//@testable import TouchDraw
@testable import TouchDraw

class TouchDrawTests: XCTestCase {

var viewController: TouchDrawViewController!

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

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.
/// Tests whether the TouchDrawView enables undoing after one point is drawn
func testEnableUndo() {
var touches = Set<UITouch>()
touches.insert(UITouch())

viewController.touchDrawView.touchesBegan(touches, withEvent: nil)
viewController.touchDrawView.touchesEnded(touches, withEvent: nil)

XCTAssertTrue(viewController.undoIsEnabled, "Undo should be enabled")
}

func testPerformanceExample() {
// This is an example of a performance test case.
self.measureBlock {
// Put the code you want to measure the time of here.
}
/// Tests whether you clearing empties the strokes
func testClearing() {
var touches = Set<UITouch>()
touches.insert(UITouch())

viewController.touchDrawView.touchesBegan(touches, withEvent: nil)
viewController.touchDrawView.touchesEnded(touches, withEvent: nil)

XCTAssert(viewController.touchDrawView.stack.count > 0, "Should have strokes on view")
viewController.touchDrawView.clearDrawing()
XCTAssert(viewController.touchDrawView.stack.count == 0, "Should not have strokes on view")
}

}
67 changes: 67 additions & 0 deletions TouchDrawTests/TouchDrawViewController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//
// TouchDrawViewController.swift
// TouchDraw
//
// Created by Christian Paul Dehli on 3/26/16.
// Copyright © 2016 Christian Paul Dehli. All rights reserved.
//

@testable import TouchDraw

class TouchDrawViewController: UIViewController, TouchDrawViewDelegate {

internal var undoIsEnabled: Bool!
internal var redoIsEnabled: Bool!
internal var clearIsEnabled: Bool!

internal var touchDrawView: TouchDrawView!

init() {
super.init(nibName: nil, bundle: nil)
self.addTouchDrawView()
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.addTouchDrawView()
}

private func addTouchDrawView() {
self.touchDrawView = TouchDrawView(frame: self.view.frame)
self.view.addSubview(touchDrawView)
touchDrawView.delegate = self

self.undoIsEnabled = false
self.redoIsEnabled = false
self.clearIsEnabled = false

print(self.touchDrawView.undoManager)

}

// MARK: - TouchDrawViewDelegate

func undoEnabled() {
self.undoIsEnabled = true
}

func undoDisabled() {
self.undoIsEnabled = false
}

func redoEnabled() {
self.redoIsEnabled = true
}

func redoDisabled() {
self.redoIsEnabled = false
}

func clearEnabled() {
self.clearIsEnabled = true
}

func clearDisabled() {
self.clearIsEnabled = false
}
}

0 comments on commit 081a559

Please sign in to comment.