From 9d1efb4ef3c8a95538976ec81fa780fa6b5f3800 Mon Sep 17 00:00:00 2001 From: michael starke Date: Mon, 14 Mar 2016 09:40:25 +0100 Subject: [PATCH] More experiments with NSProxy --- MacPass/MPEntryProxy.m | 25 ++++++++++++++++++++++++- MacPassTests/MPTextEntryProxy.m | 4 ++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/MacPass/MPEntryProxy.m b/MacPass/MPEntryProxy.m index 181dab60..f07628fd 100644 --- a/MacPass/MPEntryProxy.m +++ b/MacPass/MPEntryProxy.m @@ -12,6 +12,7 @@ @interface MPEntryProxy () @property (strong) KPKEntry *entry; +@property (strong) NSMutableDictionary *valueStore; @end @@ -22,12 +23,34 @@ @throw [NSException exceptionWithName:NSInvalidArgumentException reason:nil userInfo:nil]; } _entry = entry; + _valueStore = [[NSMutableDictionary alloc] init]; return self; } - (void)forwardInvocation:(NSInvocation *)invocation { NSLog(@"forwardInvocation: %@", NSStringFromSelector(invocation.selector)); - [invocation invokeWithTarget:self.entry]; + NSString *seletor = NSStringFromSelector(invocation.selector); + if([seletor hasPrefix:@"set"]) { + NSLog(@"forwardInvocation: setter detected"); + NSString *property = [seletor substringFromIndex:3].lowercaseString; // lowercase fist letter!!! + if(invocation.methodSignature.numberOfArguments == 3) { + id value; + [invocation getArgument:&value atIndex:2]; + NSLog(@"forwardInvocation: captured value %@", value); + if(value) { + self.valueStore[property] = value; + } + return; // captures getter, just return + } + } + id change = self.valueStore[seletor.lowercaseString]; + if(change) { + NSLog(@"forwardInvocation: hit cached value. Returning cache!"); + [invocation setReturnValue:&change]; + } + else { + [invocation invokeWithTarget:self.entry]; + } } - (NSMethodSignature *)methodSignatureForSelector:(SEL)sel { diff --git a/MacPassTests/MPTextEntryProxy.m b/MacPassTests/MPTextEntryProxy.m index fa31ed8d..6c35fc29 100644 --- a/MacPassTests/MPTextEntryProxy.m +++ b/MacPassTests/MPTextEntryProxy.m @@ -41,8 +41,8 @@ NSString *newPassword = @"new password"; NSString *newKeystrokes = @"{ENTER 3}"; [((id)self.proxy) setPassword:newPassword]; - XCTAssertEqualObjects(self.entry.password, newPassword, @"Proxy sets password on entry!"); - + XCTAssertNotEqualObjects(self.entry.password, newPassword, @"Proxy does not set password on entry!"); + [((id)self.proxy) autotype].defaultKeystrokeSequence= newKeystrokes; XCTAssertEqualObjects(self.entry.autotype.defaultKeystrokeSequence, newKeystrokes, @"Proxy sets default keystroke sequence on entry autotype!"); }