mirror of https://github.com/oxen-io/session-ios
				
				
				
			
			You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
	
	
		
			112 lines
		
	
	
		
			3.7 KiB
		
	
	
	
		
			Swift
		
	
		
		
			
		
	
	
			112 lines
		
	
	
		
			3.7 KiB
		
	
	
	
		
			Swift
		
	
| 
											7 years ago
										 | // | ||
| 
											7 years ago
										 | //  Copyright (c) 2019 Open Whisper Systems. All rights reserved. | ||
| 
											7 years ago
										 | // | ||
|  | 
 | ||
|  | import XCTest | ||
|  | @testable import Signal | ||
|  | @testable import SignalMessaging | ||
|  | 
 | ||
| 
											7 years ago
										 | extension ImageEditorModel { | ||
|  |     func itemIds() -> [String] { | ||
|  |         return items().map { (item) in | ||
|  |             item.itemId | ||
|  |         } | ||
|  |     } | ||
|  | } | ||
|  | 
 | ||
| 
											7 years ago
										 | class ImageEditorTest: SignalBaseTest { | ||
|  | 
 | ||
|  |     override func setUp() { | ||
|  |         super.setUp() | ||
|  |     } | ||
|  | 
 | ||
|  |     override func tearDown() { | ||
|  |         // Put teardown code here. This method is called after the invocation of each test method in the class. | ||
|  |         super.tearDown() | ||
|  |     } | ||
|  | 
 | ||
|  |     func testImageEditorContents() { | ||
| 
											7 years ago
										 |         let contents = ImageEditorContents() | ||
| 
											7 years ago
										 |         XCTAssertEqual(0, contents.itemMap.count) | ||
|  | 
 | ||
|  |         let item = ImageEditorItem(itemType: .test) | ||
| 
											7 years ago
										 |         contents.append(item: item) | ||
|  |         XCTAssertEqual(1, contents.itemMap.count) | ||
|  | 
 | ||
|  |         let contentsCopy = contents.clone() | ||
|  |         XCTAssertEqual(1, contents.itemMap.count) | ||
|  |         XCTAssertEqual(1, contentsCopy.itemMap.count) | ||
|  | 
 | ||
|  |         contentsCopy.remove(item: item) | ||
|  |         XCTAssertEqual(1, contents.itemMap.count) | ||
|  |         XCTAssertEqual(0, contentsCopy.itemMap.count) | ||
| 
											7 years ago
										 | 
 | ||
| 
											7 years ago
										 |         let modifiedItem = ImageEditorItem(itemId: item.itemId, itemType: item.itemType) | ||
| 
											7 years ago
										 |         contents.replace(item: modifiedItem) | ||
|  |         XCTAssertEqual(1, contents.itemMap.count) | ||
|  |         XCTAssertEqual(0, contentsCopy.itemMap.count) | ||
|  |     } | ||
|  | 
 | ||
|  |     private func writeDummyImage() -> String { | ||
|  |         let image = UIImage.init(color: .red, size: CGSize(width: 1, height: 1)) | ||
|  |         guard let data = UIImagePNGRepresentation(image) else { | ||
|  |             owsFail("Couldn't export dummy image.") | ||
|  |         } | ||
|  |         let filePath = OWSFileSystem.temporaryFilePath(withFileExtension: "png") | ||
| 
											7 years ago
										 |         try! data.write(to: URL(fileURLWithPath: filePath)) | ||
| 
											7 years ago
										 |         return filePath | ||
|  |     } | ||
|  | 
 | ||
|  |     func testImageEditor() { | ||
|  |         let imagePath = writeDummyImage() | ||
|  | 
 | ||
| 
											7 years ago
										 |         let imageEditor = try! ImageEditorModel(srcImagePath: imagePath) | ||
| 
											7 years ago
										 |         XCTAssertFalse(imageEditor.canUndo()) | ||
|  |         XCTAssertFalse(imageEditor.canRedo()) | ||
|  |         XCTAssertEqual(0, imageEditor.itemCount()) | ||
|  | 
 | ||
| 
											7 years ago
										 |         let itemA = ImageEditorItem(itemType: .test) | ||
| 
											7 years ago
										 |         imageEditor.append(item: itemA) | ||
|  |         XCTAssertTrue(imageEditor.canUndo()) | ||
|  |         XCTAssertFalse(imageEditor.canRedo()) | ||
|  |         XCTAssertEqual(1, imageEditor.itemCount()) | ||
| 
											7 years ago
										 |         XCTAssertEqual([itemA.itemId], imageEditor.itemIds()) | ||
| 
											7 years ago
										 | 
 | ||
|  |         imageEditor.undo() | ||
|  |         XCTAssertFalse(imageEditor.canUndo()) | ||
|  |         XCTAssertTrue(imageEditor.canRedo()) | ||
|  |         XCTAssertEqual(0, imageEditor.itemCount()) | ||
|  | 
 | ||
|  |         imageEditor.redo() | ||
|  |         XCTAssertTrue(imageEditor.canUndo()) | ||
|  |         XCTAssertFalse(imageEditor.canRedo()) | ||
|  |         XCTAssertEqual(1, imageEditor.itemCount()) | ||
| 
											7 years ago
										 |         XCTAssertEqual([itemA.itemId], imageEditor.itemIds()) | ||
| 
											7 years ago
										 | 
 | ||
|  |         imageEditor.undo() | ||
|  |         XCTAssertFalse(imageEditor.canUndo()) | ||
|  |         XCTAssertTrue(imageEditor.canRedo()) | ||
|  |         XCTAssertEqual(0, imageEditor.itemCount()) | ||
|  | 
 | ||
| 
											7 years ago
										 |         let itemB = ImageEditorItem(itemType: .test) | ||
| 
											7 years ago
										 |         imageEditor.append(item: itemB) | ||
|  |         XCTAssertTrue(imageEditor.canUndo()) | ||
|  |         XCTAssertFalse(imageEditor.canRedo()) | ||
|  |         XCTAssertEqual(1, imageEditor.itemCount()) | ||
| 
											7 years ago
										 |         XCTAssertEqual([itemB.itemId], imageEditor.itemIds()) | ||
| 
											7 years ago
										 | 
 | ||
| 
											7 years ago
										 |         let itemC = ImageEditorItem(itemType: .test) | ||
| 
											7 years ago
										 |         imageEditor.append(item: itemC) | ||
|  |         XCTAssertTrue(imageEditor.canUndo()) | ||
|  |         XCTAssertFalse(imageEditor.canRedo()) | ||
|  |         XCTAssertEqual(2, imageEditor.itemCount()) | ||
| 
											7 years ago
										 |         XCTAssertEqual([itemB.itemId, itemC.itemId], imageEditor.itemIds()) | ||
| 
											7 years ago
										 | 
 | ||
|  |         imageEditor.undo() | ||
|  |         XCTAssertTrue(imageEditor.canUndo()) | ||
|  |         XCTAssertTrue(imageEditor.canRedo()) | ||
|  |         XCTAssertEqual(1, imageEditor.itemCount()) | ||
| 
											7 years ago
										 |         XCTAssertEqual([itemB.itemId], imageEditor.itemIds()) | ||
| 
											7 years ago
										 |     } | ||
|  | } |