From 395b1342a9a3ec85569fbe60e5fb4ceff505cacc Mon Sep 17 00:00:00 2001 From: Jesse Squires Date: Fri, 19 Dec 2014 17:41:19 -0500 Subject: [PATCH] unit tests and refinements --- JSQMessages.xcodeproj/project.pbxproj | 12 +++ .../ModelTests/JSQLocationMediaItemTests.m | 64 +++++++++++++++ .../ModelTests/JSQMessageMediaTests.m | 20 ++--- .../ModelTests/JSQMessageTextTests.m | 2 +- .../ModelTests/JSQPhotoMediaItemTests.m | 76 ++++++++++++++++++ .../ModelTests/JSQVideoMediaItemTests.m | 77 +++++++++++++++++++ .../Model/JSQLocationMediaItem.m | 2 +- .../Model/JSQPhotoMediaItem.m | 13 +--- .../Model/JSQVideoMediaItem.m | 2 +- 9 files changed, 243 insertions(+), 25 deletions(-) create mode 100644 JSQMessagesTests/ModelTests/JSQLocationMediaItemTests.m create mode 100644 JSQMessagesTests/ModelTests/JSQPhotoMediaItemTests.m create mode 100644 JSQMessagesTests/ModelTests/JSQVideoMediaItemTests.m diff --git a/JSQMessages.xcodeproj/project.pbxproj b/JSQMessages.xcodeproj/project.pbxproj index d37960109..1cd44c479 100644 --- a/JSQMessages.xcodeproj/project.pbxproj +++ b/JSQMessages.xcodeproj/project.pbxproj @@ -99,6 +99,9 @@ 88A2601919D8E18400924534 /* JSQMessagesTypingIndicatorFooterViewTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 88A2600019D8E18400924534 /* JSQMessagesTypingIndicatorFooterViewTests.m */; }; 88A2601B19D8E45600924534 /* Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = 88A2601A19D8E45600924534 /* Info.plist */; }; 88A901B619F618B100F99777 /* JSQMediaItem.m in Sources */ = {isa = PBXBuildFile; fileRef = 88A901B519F618B100F99777 /* JSQMediaItem.m */; }; + 88C00A4E1A44D4C600B004B3 /* JSQLocationMediaItemTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 88C00A4D1A44D4C600B004B3 /* JSQLocationMediaItemTests.m */; }; + 88C00A501A44D4D800B004B3 /* JSQPhotoMediaItemTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 88C00A4F1A44D4D800B004B3 /* JSQPhotoMediaItemTests.m */; }; + 88C00A521A44D4E500B004B3 /* JSQVideoMediaItemTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 88C00A511A44D4E500B004B3 /* JSQVideoMediaItemTests.m */; }; 88C4583019F5F7A0008FD427 /* JSQMessagesMediaViewBubbleImageMasker.m in Sources */ = {isa = PBXBuildFile; fileRef = 88C4582F19F5F7A0008FD427 /* JSQMessagesMediaViewBubbleImageMasker.m */; }; 88E4D7131A0DBD6B000CC061 /* JSQMessages.strings in Resources */ = {isa = PBXBuildFile; fileRef = 8841B88719F4988800EA16B6 /* JSQMessages.strings */; }; 94A4FA20C2FBD0D62614D5A8 /* libPods.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 97E6750B77E8A7042BA0754B /* libPods.a */; }; @@ -268,6 +271,9 @@ 88A2601A19D8E45600924534 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 88A901B419F618B100F99777 /* JSQMediaItem.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSQMediaItem.h; sourceTree = ""; }; 88A901B519F618B100F99777 /* JSQMediaItem.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = JSQMediaItem.m; sourceTree = ""; }; + 88C00A4D1A44D4C600B004B3 /* JSQLocationMediaItemTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = JSQLocationMediaItemTests.m; sourceTree = ""; }; + 88C00A4F1A44D4D800B004B3 /* JSQPhotoMediaItemTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = JSQPhotoMediaItemTests.m; sourceTree = ""; }; + 88C00A511A44D4E500B004B3 /* JSQVideoMediaItemTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = JSQVideoMediaItemTests.m; sourceTree = ""; }; 88C4582E19F5F7A0008FD427 /* JSQMessagesMediaViewBubbleImageMasker.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSQMessagesMediaViewBubbleImageMasker.h; sourceTree = ""; }; 88C4582F19F5F7A0008FD427 /* JSQMessagesMediaViewBubbleImageMasker.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = JSQMessagesMediaViewBubbleImageMasker.m; sourceTree = ""; }; 97E6750B77E8A7042BA0754B /* libPods.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libPods.a; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -602,10 +608,13 @@ 88A25FF219D8E18400924534 /* ModelTests */ = { isa = PBXGroup; children = ( + 88C00A4D1A44D4C600B004B3 /* JSQLocationMediaItemTests.m */, 88A25FF319D8E18400924534 /* JSQMessageMediaTests.m */, 88A25FF419D8E18400924534 /* JSQMessagesAvatarImageTests.m */, 88A25FF519D8E18400924534 /* JSQMessagesBubbleImageTests.m */, 88A25FF619D8E18400924534 /* JSQMessageTextTests.m */, + 88C00A4F1A44D4D800B004B3 /* JSQPhotoMediaItemTests.m */, + 88C00A511A44D4E500B004B3 /* JSQVideoMediaItemTests.m */, ); path = ModelTests; sourceTree = ""; @@ -865,6 +874,7 @@ files = ( 88A2600219D8E18400924534 /* JSQMessagesUIColorTests.m in Sources */, 88A2601819D8E18400924534 /* JSQMessagesToolbarContentViewTests.m in Sources */, + 88C00A521A44D4E500B004B3 /* JSQVideoMediaItemTests.m in Sources */, 88A2601519D8E18400924534 /* JSQMessagesInputToolbarTests.m in Sources */, 88A2601719D8E18400924534 /* JSQMessagesLoadEarlierHeaderViewTests.m in Sources */, 88A2601219D8E18400924534 /* JSQMessagesCollectionViewCellTests.m in Sources */, @@ -881,9 +891,11 @@ 88A2601319D8E18400924534 /* JSQMessagesCollectionViewTests.m in Sources */, 88A2600119D8E18400924534 /* JSQMessagesNSStringTests.m in Sources */, 88A2600A19D8E18400924534 /* JSQMessagesToolbarButtonFactoryTests.m in Sources */, + 88C00A501A44D4D800B004B3 /* JSQPhotoMediaItemTests.m in Sources */, 88324C3419F6301C00BC732D /* JSQMessagesMediaViewBubbleImageMaskerTests.m in Sources */, 88A2601919D8E18400924534 /* JSQMessagesTypingIndicatorFooterViewTests.m in Sources */, 88A2600319D8E18400924534 /* JSQMessagesUIImageTests.m in Sources */, + 88C00A4E1A44D4C600B004B3 /* JSQLocationMediaItemTests.m in Sources */, 88A2600C19D8E18400924534 /* JSQMessagesCollectionViewLayoutAttributesTests.m in Sources */, 88A2600619D8E18400924534 /* JSQMessagesViewControllerTests.m in Sources */, 88A2600519D8E18400924534 /* JSQMessagesKeyboardControllerTests.m in Sources */, diff --git a/JSQMessagesTests/ModelTests/JSQLocationMediaItemTests.m b/JSQMessagesTests/ModelTests/JSQLocationMediaItemTests.m new file mode 100644 index 000000000..cf9c80a38 --- /dev/null +++ b/JSQMessagesTests/ModelTests/JSQLocationMediaItemTests.m @@ -0,0 +1,64 @@ +// +// Created by Jesse Squires +// http://www.jessesquires.com +// +// +// MIT License +// Copyright (c) 2014 Jesse Squires +// http://opensource.org/licenses/MIT +// + +#import + +#import "JSQLocationMediaItem.h" + + +@interface JSQLocationMediaItemTests : XCTestCase + +@property (strong, nonatomic) CLLocation *location; + +@end + + +@implementation JSQLocationMediaItemTests + +- (void)setUp +{ + [super setUp]; + self.location = [[CLLocation alloc] initWithLatitude:37.795313 longitude:-122.393757]; +} + +- (void)tearDown +{ + self.location = nil; + [super tearDown]; +} + +- (void)testLocationItemInit +{ + JSQLocationMediaItem *item = [[JSQLocationMediaItem alloc] initWithLocation:self.location]; + XCTAssertNotNil(item); +} + +- (void)testMediaDataProtocol +{ + JSQLocationMediaItem *item = [[JSQLocationMediaItem alloc] init]; + + XCTAssertTrue(!CGSizeEqualToSize([item mediaViewDisplaySize], CGSizeZero)); + XCTAssertNotNil([item mediaPlaceholderView]); + XCTAssertNil([item mediaView], @"Media view should be nil if location is nil"); + + XCTestExpectation *expectation = [self expectationWithDescription:[NSString stringWithFormat:@"%s", __PRETTY_FUNCTION__]]; + + [item setLocation:self.location withCompletionHandler:^{ + [expectation fulfill]; + }]; + + [self waitForExpectationsWithTimeout:5 handler:^(NSError *error) { + XCTAssertNil(error, @"Expectation should not error"); + }]; + + XCTAssertNotNil([item mediaView], @"Media view should NOT be nil once item has media data"); +} + +@end diff --git a/JSQMessagesTests/ModelTests/JSQMessageMediaTests.m b/JSQMessagesTests/ModelTests/JSQMessageMediaTests.m index 880b2ad89..94110930a 100644 --- a/JSQMessagesTests/ModelTests/JSQMessageMediaTests.m +++ b/JSQMessagesTests/ModelTests/JSQMessageMediaTests.m @@ -68,9 +68,9 @@ - (void)tearDown - (void)testMediaMessageInit { JSQMessage *msg = [[JSQMessage alloc] initWithSenderId:self.senderId - senderDisplayName:self.senderDisplayName - date:self.date - media:self.mockMediaData]; + senderDisplayName:self.senderDisplayName + date:self.date + media:self.mockMediaData]; XCTAssertNotNil(msg, @"Message should not be nil"); } @@ -83,24 +83,24 @@ - (void)testMediaMessageInvalidInit - (void)testMediaMessageIsEqual { JSQMessage *msg = [[JSQMessage alloc] initWithSenderId:self.senderId - senderDisplayName:self.senderDisplayName - date:self.date - media:self.mockMediaData]; + senderDisplayName:self.senderDisplayName + date:self.date + media:self.mockMediaData]; JSQMessage *copy = [msg copy]; XCTAssertEqualObjects(msg, copy, @"Copied messages should be equal"); + XCTAssertEqual([msg hash], [copy hash], @"Copied messages hashes should be equal"); - XCTAssertEqualObjects(msg, copy, @"Copied messages should be equal"); XCTAssertEqualObjects(msg, msg, @"Messages should be equal to itself"); } - (void)testMediaMessageArchiving { JSQMessage *msg = [[JSQMessage alloc] initWithSenderId:self.senderId - senderDisplayName:self.senderDisplayName - date:self.date - media:[FakeMedia new]]; + senderDisplayName:self.senderDisplayName + date:self.date + media:[FakeMedia new]]; NSData *msgData = [NSKeyedArchiver archivedDataWithRootObject:msg]; diff --git a/JSQMessagesTests/ModelTests/JSQMessageTextTests.m b/JSQMessagesTests/ModelTests/JSQMessageTextTests.m index 1bbf61ecc..74783d17f 100644 --- a/JSQMessagesTests/ModelTests/JSQMessageTextTests.m +++ b/JSQMessagesTests/ModelTests/JSQMessageTextTests.m @@ -73,9 +73,9 @@ - (void)testTextMessageIsEqual JSQMessage *copy = [msg copy]; XCTAssertEqualObjects(msg, copy, @"Copied messages should be equal"); + XCTAssertEqual([msg hash], [copy hash], @"Copied messages hashes should be equal"); - XCTAssertEqualObjects(msg, copy, @"Copied messages should be equal"); XCTAssertEqualObjects(msg, msg, @"Messages should be equal to itself"); } diff --git a/JSQMessagesTests/ModelTests/JSQPhotoMediaItemTests.m b/JSQMessagesTests/ModelTests/JSQPhotoMediaItemTests.m new file mode 100644 index 000000000..3e568b7b5 --- /dev/null +++ b/JSQMessagesTests/ModelTests/JSQPhotoMediaItemTests.m @@ -0,0 +1,76 @@ +// +// Created by Jesse Squires +// http://www.jessesquires.com +// +// +// MIT License +// Copyright (c) 2014 Jesse Squires +// http://opensource.org/licenses/MIT +// + +#import + +#import "JSQPhotoMediaItem.h" + + +@interface JSQPhotoMediaItemTests : XCTestCase + +@end + + +@implementation JSQPhotoMediaItemTests + +- (void)setUp +{ + [super setUp]; +} + +- (void)tearDown +{ + [super tearDown]; +} + +- (void)testPhotoItemInit +{ + JSQPhotoMediaItem *item = [[JSQPhotoMediaItem alloc] initWithImage:[UIImage new]]; + XCTAssertNotNil(item); +} + +- (void)testPhotoItemIsEqual +{ + JSQPhotoMediaItem *item = [[JSQPhotoMediaItem alloc] initWithImage:[UIImage imageNamed:@"demo_avatar_jobs"]]; + + JSQPhotoMediaItem *copy = [item copy]; + + XCTAssertEqualObjects(item, copy, @"Copied items should be equal"); + + XCTAssertEqual([item hash], [copy hash], @"Copied item hashes should be equal"); + + XCTAssertEqualObjects(item, item, @"Item should be equal to itself"); +} + +- (void)testPhotoItemArchiving +{ + JSQPhotoMediaItem *item = [[JSQPhotoMediaItem alloc] initWithImage:[UIImage new]]; + + NSData *data = [NSKeyedArchiver archivedDataWithRootObject:item]; + + JSQPhotoMediaItem *unarchivedItem = [NSKeyedUnarchiver unarchiveObjectWithData:data]; + + XCTAssertEqualObjects(item, unarchivedItem); +} + +- (void)testMediaDataProtocol +{ + JSQPhotoMediaItem *item = [[JSQPhotoMediaItem alloc] initWithImage:nil]; + + XCTAssertTrue(!CGSizeEqualToSize([item mediaViewDisplaySize], CGSizeZero)); + XCTAssertNotNil([item mediaPlaceholderView]); + XCTAssertNil([item mediaView], @"Media view should be nil if image is nil"); + + item.image = [UIImage imageNamed:@"demo_avatar_jobs"]; + + XCTAssertNotNil([item mediaView], @"Media view should NOT be nil once item has media data"); +} + +@end diff --git a/JSQMessagesTests/ModelTests/JSQVideoMediaItemTests.m b/JSQMessagesTests/ModelTests/JSQVideoMediaItemTests.m new file mode 100644 index 000000000..2f164cff6 --- /dev/null +++ b/JSQMessagesTests/ModelTests/JSQVideoMediaItemTests.m @@ -0,0 +1,77 @@ +// +// Created by Jesse Squires +// http://www.jessesquires.com +// +// +// MIT License +// Copyright (c) 2014 Jesse Squires +// http://opensource.org/licenses/MIT +// + +#import + +#import "JSQVideoMediaItem.h" + + +@interface JSQVideoMediaItemTests : XCTestCase + +@end + + +@implementation JSQVideoMediaItemTests + +- (void)setUp +{ + [super setUp]; +} + +- (void)tearDown +{ + [super tearDown]; +} + +- (void)testVideoMediaItemInit +{ + JSQVideoMediaItem *item = [[JSQVideoMediaItem alloc] initWithFileURL:[NSURL URLWithString:@"file://"] isReadyToPlay:NO]; + XCTAssertNotNil(item); +} + +- (void)testVideoItemIsEqual +{ + JSQVideoMediaItem *item = [[JSQVideoMediaItem alloc] initWithFileURL:[NSURL URLWithString:@"file://"] isReadyToPlay:YES]; + + JSQVideoMediaItem *copy = [item copy]; + + XCTAssertEqualObjects(item, copy, @"Copied items should be equal"); + + XCTAssertEqual([item hash], [copy hash], @"Copied item hashes should be equal"); + + XCTAssertEqualObjects(item, item, @"Item should be equal to itself"); +} + +- (void)testVideoItemArchiving +{ + JSQVideoMediaItem *item = [[JSQVideoMediaItem alloc] initWithFileURL:[NSURL URLWithString:@"file://"] isReadyToPlay:YES]; + + NSData *data = [NSKeyedArchiver archivedDataWithRootObject:item]; + + JSQVideoMediaItem *unarchivedItem = [NSKeyedUnarchiver unarchiveObjectWithData:data]; + + XCTAssertEqualObjects(item, unarchivedItem); +} + +- (void)testMediaDataProtocol +{ + JSQVideoMediaItem *item = [[JSQVideoMediaItem alloc] init]; + + XCTAssertTrue(!CGSizeEqualToSize([item mediaViewDisplaySize], CGSizeZero)); + XCTAssertNotNil([item mediaPlaceholderView]); + XCTAssertNil([item mediaView], @"Media view should be nil if fileURL is nil, and readyToPlay is NO"); + + item.fileURL = [NSURL URLWithString:@"file://"]; + item.isReadyToPlay = YES; + + XCTAssertNotNil([item mediaView], @"Media view should NOT be nil once item has media data"); +} + +@end diff --git a/JSQMessagesViewController/Model/JSQLocationMediaItem.m b/JSQMessagesViewController/Model/JSQLocationMediaItem.m index 6b0065277..4b6a6595e 100644 --- a/JSQMessagesViewController/Model/JSQLocationMediaItem.m +++ b/JSQMessagesViewController/Model/JSQLocationMediaItem.m @@ -170,7 +170,7 @@ - (BOOL)isEqual:(id)object - (NSUInteger)hash { - return self.location.hash; + return super.hash ^ self.location.hash; } - (NSString *)description diff --git a/JSQMessagesViewController/Model/JSQPhotoMediaItem.m b/JSQMessagesViewController/Model/JSQPhotoMediaItem.m index fca82e28a..50c3a0385 100644 --- a/JSQMessagesViewController/Model/JSQPhotoMediaItem.m +++ b/JSQMessagesViewController/Model/JSQPhotoMediaItem.m @@ -86,20 +86,9 @@ - (UIView *)mediaView #pragma mark - NSObject -- (BOOL)isEqual:(id)object -{ - if (![super isEqual:object]) { - return NO; - } - - JSQPhotoMediaItem *photoItem = (JSQPhotoMediaItem *)object; - - return [self.image isEqual:photoItem.image]; -} - - (NSUInteger)hash { - return self.image.hash; + return super.hash ^ self.image.hash; } - (NSString *)description diff --git a/JSQMessagesViewController/Model/JSQVideoMediaItem.m b/JSQMessagesViewController/Model/JSQVideoMediaItem.m index db8abb82d..5fd93c9d2 100644 --- a/JSQMessagesViewController/Model/JSQVideoMediaItem.m +++ b/JSQMessagesViewController/Model/JSQVideoMediaItem.m @@ -112,7 +112,7 @@ - (BOOL)isEqual:(id)object - (NSUInteger)hash { - return self.fileURL.hash; + return super.hash ^ self.fileURL.hash; } - (NSString *)description