From 1122cfbbde889612270cbab55523881f1a16ab85 Mon Sep 17 00:00:00 2001 From: michael starke Date: Sun, 23 Feb 2014 20:22:59 +0100 Subject: [PATCH] Code cleanup --- KeePassKit | 2 +- MacPass.xcodeproj/project.pbxproj | 2 + MacPass/ContextBar.xib | 107 +++++++++++++++------------- MacPass/EntryView.xib | 14 ++-- MacPass/MPInspectorViewController.m | 10 ++- MacPassTests/KPKIconLoading.m | 1 - MacPassTests/KPKTestNSCoding.m | 73 +++++++++++++++++-- 7 files changed, 140 insertions(+), 69 deletions(-) diff --git a/KeePassKit b/KeePassKit index 9cbd9939..09201a81 160000 --- a/KeePassKit +++ b/KeePassKit @@ -1 +1 @@ -Subproject commit 9cbd9939cd5497b1a10bf6ad0d9373fdc09f6803 +Subproject commit 09201a8103900a3819e3b8c03b3c74d242c996b1 diff --git a/MacPass.xcodeproj/project.pbxproj b/MacPass.xcodeproj/project.pbxproj index 4500a478..a5343370 100644 --- a/MacPass.xcodeproj/project.pbxproj +++ b/MacPass.xcodeproj/project.pbxproj @@ -824,6 +824,7 @@ 4CEE46DC181C301D006BF1E5 /* MPAutotypeDaemon.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MPAutotypeDaemon.m; sourceTree = ""; }; 4CEED1C417D7BD0E007180F1 /* NSError+Messages.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSError+Messages.h"; sourceTree = ""; }; 4CEED1C517D7BD0E007180F1 /* NSError+Messages.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSError+Messages.m"; sourceTree = ""; }; + 4CEFC2F118BA7644007D2565 /* KeePassKit.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = KeePassKit.h; sourceTree = ""; }; 4CF29BF317879D0000851B60 /* 26_FileSaveTemplate.pdf */ = {isa = PBXFileReference; lastKnownFileType = image.pdf; path = 26_FileSaveTemplate.pdf; sourceTree = ""; }; 4CF5CF7218B7C81600B0C026 /* NSWindow+Shake.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "NSWindow+Shake.h"; path = "Categories/NSWindow+Shake.h"; sourceTree = ""; }; 4CF5CF7318B7C81600B0C026 /* NSWindow+Shake.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "NSWindow+Shake.m"; path = "Categories/NSWindow+Shake.m"; sourceTree = ""; }; @@ -1600,6 +1601,7 @@ 4CD3ABAD178F71B50073F5C5 /* KeePassKit */ = { isa = PBXGroup; children = ( + 4CEFC2F118BA7644007D2565 /* KeePassKit.h */, 4C0104B817C37BFC00173EF3 /* Utilites */, 4C1842D6179C716100E2F5BC /* Format */, 4CD3ABAF178F71B50073F5C5 /* Categories */, diff --git a/MacPass/ContextBar.xib b/MacPass/ContextBar.xib index 90f4d2d1..76f38ef0 100644 --- a/MacPass/ContextBar.xib +++ b/MacPass/ContextBar.xib @@ -9,10 +9,7 @@ - - - @@ -24,6 +21,10 @@ + + + + @@ -35,7 +36,7 @@ - + @@ -44,65 +45,71 @@ - - - + + + + + - - - - - - - - - + + + + + - - - - + + + + - - - diff --git a/MacPass/EntryView.xib b/MacPass/EntryView.xib index 4f82d068..bc11ac8f 100644 --- a/MacPass/EntryView.xib +++ b/MacPass/EntryView.xib @@ -48,11 +48,11 @@ - + - + @@ -86,7 +86,7 @@ - + @@ -136,11 +136,11 @@ - + - + @@ -174,7 +174,7 @@ - + @@ -212,7 +212,7 @@ - + diff --git a/MacPass/MPInspectorViewController.m b/MacPass/MPInspectorViewController.m index 3d28aab5..940665ec 100644 --- a/MacPass/MPInspectorViewController.m +++ b/MacPass/MPInspectorViewController.m @@ -104,7 +104,7 @@ typedef NS_ENUM(NSUInteger, MPContentTab) { [[self view] layoutSubtreeIfNeeded]; - [self _updateItemBindings:nil]; + [self _updateBindings:nil]; } - (void)setupNotifications:(NSWindowController *)windowController { @@ -220,7 +220,7 @@ typedef NS_ENUM(NSUInteger, MPContentTab) { #pragma mark - #pragma mark Bindings -- (void)_updateItemBindings:(id)item { +- (void)_updateBindings:(id)item { if(!item) { [self.itemNameTextField unbind:NSValueBinding]; [self.itemNameTextField setHidden:YES]; @@ -240,6 +240,10 @@ typedef NS_ENUM(NSUInteger, MPContentTab) { } [self.itemImageView setHidden:NO]; [self.itemNameTextField setHidden:NO]; + + if([item respondsToSelector:@selector(notes)]) { + + } } #pragma mark - @@ -261,7 +265,7 @@ typedef NS_ENUM(NSUInteger, MPContentTab) { self.activeTab = MPEntryTab; } } - [self _updateItemBindings:document.selectedItem]; + [self _updateBindings:document.selectedItem]; /* disable the entry text fields whenever the entry selection changes */ //[_entryViewController endEditing]; diff --git a/MacPassTests/KPKIconLoading.m b/MacPassTests/KPKIconLoading.m index 87424e1f..09ad5758 100644 --- a/MacPassTests/KPKIconLoading.m +++ b/MacPassTests/KPKIconLoading.m @@ -44,7 +44,6 @@ NSBitmapImageRep *bitmapRep = (NSBitmapImageRep *)imageRep; NSData *pngData = [bitmapRep representationUsingType:NSPNGFileType properties:nil]; XCTAssertTrue([pngData isEqualToData:_imageData], @"Image and PNG data shoudl be identical"); - } @end diff --git a/MacPassTests/KPKTestNSCoding.m b/MacPassTests/KPKTestNSCoding.m index ea8fe075..a4b22f91 100644 --- a/MacPassTests/KPKTestNSCoding.m +++ b/MacPassTests/KPKTestNSCoding.m @@ -9,9 +9,13 @@ #import #import "KPKEntry.h" +#import "KPKGroup.h" #import "KPKBinary.h" #import "KPKAttribute.h" #import "KPKXmlElements.h" +#import "KPKIcon.h" + +#import "NSData+Random.h" @interface KPKTestNSCoding : XCTestCase @@ -30,7 +34,16 @@ } - (void)testBinaryCoding { - XCTFail(@"Not Tested"); + KPKBinary *binary = [[KPKBinary alloc] init]; + binary.name = @"Binary"; + binary.data = [NSData dataWithRandomBytes:1*1024*1024]; + + NSData *data = [self encode:binary]; + KPKBinary *decodedBinary = [self decode:data ofClass:[KPKBinary class]]; + + + XCTAssertTrue([decodedBinary.data isEqualToData:binary.data]); + XCTAssertTrue([decodedBinary.name isEqualToString:binary.name]); } - (void)testEntryCoding { @@ -50,7 +63,7 @@ [entry addBinary:binary]; [entry addCustomAttribute:[[KPKAttribute alloc] initWithKey:@"Custom" value:kKPKXmlValue isProtected:NO]]; - + NSData *encodedData = [self encode:entry]; KPKEntry *copyEntry = [self decode:encodedData ofClass:[KPKEntry class]]; @@ -58,17 +71,58 @@ XCTAssertTrue([copyEntry.title isEqualToString:entry.title], @"Titles should match"); XCTAssertTrue([copyEntry.url isEqualToString:entry.url], @"URLS should match"); XCTAssertTrue([copyEntry.binaries count] == 1, @"Binareis should be copied"); - + KPKBinary *copiedBinary = [copyEntry.binaries lastObject]; XCTAssertTrue([copiedBinary.data isEqualToData:binary.data], @"Binary data should match"); XCTAssertTrue([copiedBinary.name isEqualToString:binary.name], @"Binary names should macht"); } - -- (void)testGroupCoding { - XCTFail(@"Not Implemented"); +- (void)testIconCoding { + NSBundle *myBundle = [NSBundle bundleForClass:[self class]]; + NSURL *imageURL = [myBundle URLForImageResource:@"image.png"]; + KPKIcon *icon = [[KPKIcon alloc] initWithImageAtURL:imageURL]; + NSData *data = [self encode:icon]; + KPKIcon *decodedIcon = [self decode:data ofClass:[KPKIcon class]]; + NSBitmapImageRep *originalRep = [[icon.image representations] lastObject]; + NSBitmapImageRep *decodedRep = [[decodedIcon.image representations] lastObject]; + XCTAssertTrue([originalRep isKindOfClass:[NSBitmapImageRep class]]); + XCTAssertTrue([decodedRep isKindOfClass:[NSBitmapImageRep class]]); + /* + We cannot assert bit depth since TIFF conversion might just strip a full white alpha channel + XCTAssertEqual([originalRep bitsPerPixel], [decodedRep bitsPerPixel]); + */ + XCTAssertEqual([originalRep pixelsHigh], [decodedRep pixelsHigh]); + XCTAssertEqual([originalRep pixelsWide], [decodedRep pixelsWide]); + + NSData *originalData = [icon.image TIFFRepresentation]; + NSData *decodedData = [decodedIcon.image TIFFRepresentation]; + XCTAssertTrue([originalData isEqualToData:decodedData]); } +- (void)testGroupCoding { + KPKGroup *group = [[KPKGroup alloc] init]; + group.name = @"A Group"; + group.iconId = 50; + group.notes = @"Some notes"; + KPKEntry *entry = [[KPKEntry alloc] init]; + entry.title = @"Entry"; + entry.url = @"www.url.com"; + [group addEntry:entry]; + + NSData *data = [self encode:group]; + KPKGroup *decodedGroup = [self decode:data ofClass:[KPKGroup class]]; + + XCTAssertTrue([group.uuid isEqual:decodedGroup.uuid]); + XCTAssertTrue([group.name isEqualToString:decodedGroup.name]); + XCTAssertEqual([group.entries count], [decodedGroup.entries count]); + XCTAssertEqual(group.iconId, decodedGroup.iconId); + XCTAssertTrue([group.notes isEqualToString:decodedGroup.notes]); + + KPKEntry *decodedEntry = [decodedGroup entryForUUID:entry.uuid]; + XCTAssertNotNil(decodedEntry); + XCTAssertEqualObjects(decodedEntry.parent, decodedGroup); + XCTAssertTrue([decodedEntry isEqualToEntry:entry]); +} - (NSData *)encode:(id)object { NSMutableData *data = [[NSMutableData alloc] initWithCapacity:500]; @@ -78,11 +132,16 @@ return data; } -- (id)decode:(NSData *)data ofClass:(Class)class { +- (id)decode:(NSData *)data ofClass:(Class)class usingSecureCoding:(BOOL)secureCoding { NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data]; id object = [[class alloc] initWithCoder:unarchiver]; [unarchiver finishDecoding]; return object; } + +- (id)decode:(NSData *)data ofClass:(Class)class { + return [self decode:data ofClass:class usingSecureCoding:NO]; +} + @end