From c62b444272645f428abfef314e607e24e0deea2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Spie=C3=9F?= Date: Fri, 28 Mar 2014 12:30:45 +0100 Subject: [PATCH 01/25] start refactoring AlertView delegate --- Classes/BITCrashManager.h | 14 ++++++++++ Classes/BITCrashManager.m | 55 ++++++++++++++++++++------------------- 2 files changed, 42 insertions(+), 27 deletions(-) diff --git a/Classes/BITCrashManager.h b/Classes/BITCrashManager.h index bf651da527..a524c2b6ab 100644 --- a/Classes/BITCrashManager.h +++ b/Classes/BITCrashManager.h @@ -58,6 +58,16 @@ typedef NS_ENUM(NSUInteger, BITCrashManagerStatus) { BITCrashManagerStatusAutoSend = 2 }; +typedef NS_ENUM(NSUInteger, BITCrashManagerUserInput) { + + BITCrashManagerUserInputDontSend, + + BITCrashManagerUserInputSend, + + BITCrashManagerUserInputAlwaysSend + +}; + @protocol BITCrashManagerDelegate; @@ -163,6 +173,10 @@ typedef NS_ENUM(NSUInteger, BITCrashManagerStatus) { * the debugger during runtime, this may cause issues the Mach exception handler is enabled! * @see isDebuggerAttached */ + +@property (nonatomic, assign) BITCrashManagerUserInput crashManagerUserInput; + + @property (nonatomic, assign, getter=isMachExceptionHandlerEnabled) BOOL enableMachExceptionHandler; diff --git a/Classes/BITCrashManager.m b/Classes/BITCrashManager.m index 18b3aaf270..be655b91f6 100644 --- a/Classes/BITCrashManager.m +++ b/Classes/BITCrashManager.m @@ -930,34 +930,35 @@ NSString *const kBITCrashManagerStatus = @"BITCrashManagerStatus"; #pragma mark - UIAlertView Delegate - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { - switch (buttonIndex) { - case 0: - if (self.delegate != nil && [self.delegate respondsToSelector:@selector(crashManagerWillCancelSendingCrashReport:)]) { - [self.delegate crashManagerWillCancelSendingCrashReport:self]; - } - - _sendingInProgress = NO; - [self cleanCrashReports]; - break; - case 1: - [self sendCrashReports]; - break; - case 2: { - _crashManagerStatus = BITCrashManagerStatusAutoSend; - [[NSUserDefaults standardUserDefaults] setInteger:_crashManagerStatus forKey:kBITCrashManagerStatus]; - [[NSUserDefaults standardUserDefaults] synchronize]; - if (self.delegate != nil && [self.delegate respondsToSelector:@selector(crashManagerWillSendCrashReportsAlways:)]) { - [self.delegate crashManagerWillSendCrashReportsAlways:self]; - } - - [self sendCrashReports]; - break; + _crashManagerUserInput = buttonIndex; + [self handleUserInput]; +} + +- (void)handleUserInput { + switch (_crashManagerUserInput) { + case BITCrashManagerUserInputDontSend: + if (self.delegate != nil && [self.delegate respondsToSelector:@selector(crashManagerWillCancelSendingCrashReport:)]) { + [self.delegate crashManagerWillCancelSendingCrashReport:self]; + } + + _sendingInProgress = NO; + [self cleanCrashReports]; + break; + case BITCrashManagerUserInputSend: + [self sendCrashReports]; + break; + case BITCrashManagerUserInputAlwaysSend: + _crashManagerStatus = BITCrashManagerStatusAutoSend; + [[NSUserDefaults standardUserDefaults] setInteger:_crashManagerStatus forKey:kBITCrashManagerStatus]; + [[NSUserDefaults standardUserDefaults] synchronize]; + if (self.delegate != nil && [self.delegate respondsToSelector:@selector(crashManagerWillSendCrashReportsAlways:)]) { + [self.delegate crashManagerWillSendCrashReportsAlways:self]; + } + + [self sendCrashReports]; + break; } - default: - _sendingInProgress = NO; - [self cleanCrashReports]; - break; - } + } From 50b0eb9bd1591d625a2c6c48c9a10823a50d122e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Spie=C3=9F?= Date: Fri, 28 Mar 2014 12:39:06 +0100 Subject: [PATCH 02/25] remove unnecessary property --- Classes/BITCrashManager.h | 4 ---- Classes/BITCrashManager.m | 7 +++---- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/Classes/BITCrashManager.h b/Classes/BITCrashManager.h index a524c2b6ab..dfff2f67f4 100644 --- a/Classes/BITCrashManager.h +++ b/Classes/BITCrashManager.h @@ -173,10 +173,6 @@ typedef NS_ENUM(NSUInteger, BITCrashManagerUserInput) { * the debugger during runtime, this may cause issues the Mach exception handler is enabled! * @see isDebuggerAttached */ - -@property (nonatomic, assign) BITCrashManagerUserInput crashManagerUserInput; - - @property (nonatomic, assign, getter=isMachExceptionHandlerEnabled) BOOL enableMachExceptionHandler; diff --git a/Classes/BITCrashManager.m b/Classes/BITCrashManager.m index be655b91f6..8dfff2863b 100644 --- a/Classes/BITCrashManager.m +++ b/Classes/BITCrashManager.m @@ -930,12 +930,11 @@ NSString *const kBITCrashManagerStatus = @"BITCrashManagerStatus"; #pragma mark - UIAlertView Delegate - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { - _crashManagerUserInput = buttonIndex; - [self handleUserInput]; + [self handleUserInput:buttonIndex]; } -- (void)handleUserInput { - switch (_crashManagerUserInput) { +- (void)handleUserInput:(BITCrashManagerUserInput)userInput { + switch (userInput) { case BITCrashManagerUserInputDontSend: if (self.delegate != nil && [self.delegate respondsToSelector:@selector(crashManagerWillCancelSendingCrashReport:)]) { [self.delegate crashManagerWillCancelSendingCrashReport:self]; From 9f9f44916a1b6bb706da292c672b37ce1bee3a6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Spie=C3=9F?= Date: Wed, 16 Apr 2014 12:50:45 +0200 Subject: [PATCH 03/25] adds public method to handle user input from an alert view --- Classes/BITCrashManager.h | 5 +++ Classes/BITCrashManager.m | 69 ++++++++++++++++++++++++--------------- 2 files changed, 47 insertions(+), 27 deletions(-) diff --git a/Classes/BITCrashManager.h b/Classes/BITCrashManager.h index dfff2f67f4..97d39c214a 100644 --- a/Classes/BITCrashManager.h +++ b/Classes/BITCrashManager.h @@ -247,6 +247,11 @@ typedef NS_ENUM(NSUInteger, BITCrashManagerUserInput) { */ @property (nonatomic, readonly) BOOL didCrashInLastSession; +/** +Provides an interface to handle user input from a custom alert + @return BOOl if the input is a valid option + */ +- (BOOL)handleUserInput: (BITCrashManagerUserInput) userInput; /** Provides the time between startup and crash in seconds diff --git a/Classes/BITCrashManager.m b/Classes/BITCrashManager.m index 8dfff2863b..1bc77ea7a9 100644 --- a/Classes/BITCrashManager.m +++ b/Classes/BITCrashManager.m @@ -487,6 +487,36 @@ NSString *const kBITCrashManagerStatus = @"BITCrashManagerStatus"; } } +- (BOOL)handleUserInput:(BITCrashManagerUserInput)userInput { + switch (userInput) { + case BITCrashManagerUserInputDontSend: + if (self.delegate != nil && [self.delegate respondsToSelector:@selector(crashManagerWillCancelSendingCrashReport:)]) { + [self.delegate crashManagerWillCancelSendingCrashReport:self]; + } + + [self cleanCrashReports]; + YES; + + case BITCrashManagerUserInputSend: + [self sendCrashReports]; + YES; + + case BITCrashManagerUserInputAlwaysSend: + _crashManagerStatus = BITCrashManagerStatusAutoSend; + [[NSUserDefaults standardUserDefaults] setInteger:_crashManagerStatus forKey:kBITCrashManagerStatus]; + [[NSUserDefaults standardUserDefaults] synchronize]; + if (self.delegate != nil && [self.delegate respondsToSelector:@selector(crashManagerWillSendCrashReportsAlways:)]) { + [self.delegate crashManagerWillSendCrashReportsAlways:self]; + } + + [self sendCrashReports]; + return YES; + + default: + return NO; + } + +} #pragma mark - PLCrashReporter @@ -930,35 +960,20 @@ NSString *const kBITCrashManagerStatus = @"BITCrashManagerStatus"; #pragma mark - UIAlertView Delegate - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { - [self handleUserInput:buttonIndex]; + switch (buttonIndex) { + case 0: + [self handleUserInput:BITCrashManagerUserInputDontSend]; + break; + case 1: + [self handleUserInput:BITCrashManagerUserInputSend]; + break; + case 2: + [self handleUserInput:BITCrashManagerUserInputAlwaysSend]; + break; + } } -- (void)handleUserInput:(BITCrashManagerUserInput)userInput { - switch (userInput) { - case BITCrashManagerUserInputDontSend: - if (self.delegate != nil && [self.delegate respondsToSelector:@selector(crashManagerWillCancelSendingCrashReport:)]) { - [self.delegate crashManagerWillCancelSendingCrashReport:self]; - } - - _sendingInProgress = NO; - [self cleanCrashReports]; - break; - case BITCrashManagerUserInputSend: - [self sendCrashReports]; - break; - case BITCrashManagerUserInputAlwaysSend: - _crashManagerStatus = BITCrashManagerStatusAutoSend; - [[NSUserDefaults standardUserDefaults] setInteger:_crashManagerStatus forKey:kBITCrashManagerStatus]; - [[NSUserDefaults standardUserDefaults] synchronize]; - if (self.delegate != nil && [self.delegate respondsToSelector:@selector(crashManagerWillSendCrashReportsAlways:)]) { - [self.delegate crashManagerWillSendCrashReportsAlways:self]; - } - - [self sendCrashReports]; - break; - } - -} + #pragma mark - Networking From 68c7fd95b4845e5aa3eb28857143d1ae4d791ffd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Spie=C3=9F?= Date: Wed, 16 Apr 2014 12:51:51 +0200 Subject: [PATCH 04/25] makes enum more explicit --- Classes/BITCrashManager.h | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/Classes/BITCrashManager.h b/Classes/BITCrashManager.h index 97d39c214a..c20cce6834 100644 --- a/Classes/BITCrashManager.h +++ b/Classes/BITCrashManager.h @@ -57,14 +57,22 @@ typedef NS_ENUM(NSUInteger, BITCrashManagerStatus) { */ BITCrashManagerStatusAutoSend = 2 }; - +/** + * Crash Manager alert user input + */ typedef NS_ENUM(NSUInteger, BITCrashManagerUserInput) { - - BITCrashManagerUserInputDontSend, - - BITCrashManagerUserInputSend, - - BITCrashManagerUserInputAlwaysSend + /** + * User chose not to send the crash report + */ + BITCrashManagerUserInputDontSend = 0, + /** + * User wants the crash report to be sent + */ + BITCrashManagerUserInputSend = 1, + /** + * User chose to always send crash reports + */ + BITCrashManagerUserInputAlwaysSend = 2 }; From 954e5c9ac2d54a35238c90f0e4f1281463b90009 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Spie=C3=9F?= Date: Wed, 16 Apr 2014 15:29:54 +0200 Subject: [PATCH 05/25] add tests for handleUserInput method --- Support/HockeySDKTests/BITCrashManagerTests.m | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/Support/HockeySDKTests/BITCrashManagerTests.m b/Support/HockeySDKTests/BITCrashManagerTests.m index 099b608e1d..34510ba35d 100644 --- a/Support/HockeySDKTests/BITCrashManagerTests.m +++ b/Support/HockeySDKTests/BITCrashManagerTests.m @@ -123,6 +123,40 @@ [verifyCount(delegateMock, times(1)) userEmailForHockeyManager:hm componentManager:_sut]; } +#pragma mark - Handle User Input + +- (void)testHandleUserInputDontSend { + id delegateMock = mockProtocol(@protocol(BITCrashManagerDelegate)); + _sut.delegate = delegateMock; + + assertThatBool([_sut handleUserInput:BITCrashManagerUserInputDontSend], equalToBool(YES)); + + [verify(delegateMock) crashManagerWillCancelSendingCrashReport:_sut]; + +} + +- (void)testHandleUserInputSend { + assertThatBool([_sut handleUserInput:BITCrashManagerUserInputSend], equalToBool(YES)); +} + +- (void)testHandleUserInputAlwaysSend { + id delegateMock = mockProtocol(@protocol(BITCrashManagerDelegate)); + _sut.delegate = delegateMock; + NSUserDefaults *mockUserDefaults = mock([NSUserDefaults class]); + + //Test if CrashManagerStatus is unset + [given([mockUserDefaults integerForKey:@"BITCrashManagerStatus"]) willReturn:nil]; + + //Test if method runs through + assertThatBool([_sut handleUserInput:BITCrashManagerUserInputAlwaysSend], equalToBool(YES)); + + //Test if correct CrashManagerStatus is now set + [given([mockUserDefaults integerForKey:@"BITCrashManagerStauts"]) willReturnInt:BITCrashManagerStatusAutoSend]; + + //Verify that delegate method has been called + [verify(delegateMock) crashManagerWillSendCrashReportsAlways:_sut]; + +} #pragma mark - Debugger From 91698b85a3e4a7d2dc9edcc655c805e9d06653ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Spie=C3=9F?= Date: Wed, 16 Apr 2014 17:12:17 +0200 Subject: [PATCH 06/25] updates formatting and documentation --- Classes/BITCrashManager.h | 12 +++++++++--- Classes/BITCrashManager.m | 6 +++--- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/Classes/BITCrashManager.h b/Classes/BITCrashManager.h index c20cce6834..f52d37346f 100644 --- a/Classes/BITCrashManager.h +++ b/Classes/BITCrashManager.h @@ -57,6 +57,8 @@ typedef NS_ENUM(NSUInteger, BITCrashManagerStatus) { */ BITCrashManagerStatusAutoSend = 2 }; + + /** * Crash Manager alert user input */ @@ -220,7 +222,7 @@ typedef NS_ENUM(NSUInteger, BITCrashManagerUserInput) { * * @param callbacks A pointer to an initialized PLCrashReporterCallback structure, see https://www.plcrashreporter.org/documentation/api/v1.2-rc2/struct_p_l_crash_reporter_callbacks.html */ -- (void)setCrashCallbacks: (PLCrashReporterCallbacks *) callbacks; +- (void)setCrashCallbacks:(PLCrashReporterCallbacks *)callbacks; /** @@ -256,10 +258,14 @@ typedef NS_ENUM(NSUInteger, BITCrashManagerUserInput) { @property (nonatomic, readonly) BOOL didCrashInLastSession; /** -Provides an interface to handle user input from a custom alert + Provides an interface to handle user input from a custom alert + + On this input depends, whether crash reports are sent, always sent or not sent and deleted. + @return BOOl if the input is a valid option + @see BITCrashManagerUserInput */ -- (BOOL)handleUserInput: (BITCrashManagerUserInput) userInput; +- (BOOL)handleUserInput:(BITCrashManagerUserInput)userInput; /** Provides the time between startup and crash in seconds diff --git a/Classes/BITCrashManager.m b/Classes/BITCrashManager.m index 1bc77ea7a9..0dbca1cc0a 100644 --- a/Classes/BITCrashManager.m +++ b/Classes/BITCrashManager.m @@ -174,9 +174,9 @@ NSString *const kBITCrashManagerStatus = @"BITCrashManagerStatus"; NSString *errorString = nil; NSMutableDictionary *rootObj = [NSMutableDictionary dictionaryWithCapacity:2]; - if (_approvedCrashReports && [_approvedCrashReports count] > 0) + if (_approvedCrashReports && [_approvedCrashReports count] > 0) { [rootObj setObject:_approvedCrashReports forKey:kBITCrashApprovedReports]; - + } NSData *plist = [NSPropertyListSerialization dataFromPropertyList:(id)rootObj format:NSPropertyListBinaryFormat_v1_0 errorDescription:&errorString]; @@ -438,7 +438,7 @@ NSString *const kBITCrashManagerStatus = @"BITCrashManagerStatus"; #pragma mark - Public -- (void)setCrashCallbacks: (PLCrashReporterCallbacks *) callbacks { +- (void)setCrashCallbacks:(PLCrashReporterCallbacks *)callbacks { _crashCallBacks = callbacks; } From d22fb02ec20a3ee7530e9a7cb7b0a57aa3dc9177 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Spie=C3=9F?= Date: Tue, 22 Apr 2014 17:52:14 +0200 Subject: [PATCH 07/25] Add ability to add a description to a crash report --- Classes/BITCrashManager.h | 2 +- Classes/BITCrashManager.m | 27 ++++++++++++++++++++------- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/Classes/BITCrashManager.h b/Classes/BITCrashManager.h index f52d37346f..0682dffd85 100644 --- a/Classes/BITCrashManager.h +++ b/Classes/BITCrashManager.h @@ -265,7 +265,7 @@ typedef NS_ENUM(NSUInteger, BITCrashManagerUserInput) { @return BOOl if the input is a valid option @see BITCrashManagerUserInput */ -- (BOOL)handleUserInput:(BITCrashManagerUserInput)userInput; +- (BOOL)handleUserInput:(BITCrashManagerUserInput)userInput withCrashMetaDescription:metaDescription; /** Provides the time between startup and crash in seconds diff --git a/Classes/BITCrashManager.m b/Classes/BITCrashManager.m index 0dbca1cc0a..c1110b568a 100644 --- a/Classes/BITCrashManager.m +++ b/Classes/BITCrashManager.m @@ -55,6 +55,7 @@ #define kBITCrashMetaUserID @"BITCrashMetaUserID" #define kBITCrashMetaApplicationLog @"BITCrashMetaApplicationLog" #define kBITCrashMetaAttachment @"BITCrashMetaAttachment" +#define kBITCrashMetaDescription @"BITCrashMetaDescription" // internal keys NSString *const KBITAttachmentDictIndex = @"index"; @@ -74,6 +75,7 @@ NSString *const kBITCrashManagerStatus = @"BITCrashManagerStatus"; NSMutableArray *_crashFiles; NSString *_crashesDir; + NSString *_lastCrashFilename; NSString *_settingsFile; NSString *_analyzerInProgressFile; NSFileManager *_fileManager; @@ -487,7 +489,7 @@ NSString *const kBITCrashManagerStatus = @"BITCrashManagerStatus"; } } -- (BOOL)handleUserInput:(BITCrashManagerUserInput)userInput { +- (BOOL)handleUserInput:(BITCrashManagerUserInput)userInput withCrashMetaDescription:(NSString *)metaDescription{ switch (userInput) { case BITCrashManagerUserInputDontSend: if (self.delegate != nil && [self.delegate respondsToSelector:@selector(crashManagerWillCancelSendingCrashReport:)]) { @@ -498,6 +500,11 @@ NSString *const kBITCrashManagerStatus = @"BITCrashManagerStatus"; YES; case BITCrashManagerUserInputSend: + if (!metaDescription && [metaDescription length] > 0) { + NSError *error; + [metaDescription writeToFile:[NSString stringWithFormat:@"%@_description.meta", [_crashesDir stringByAppendingPathComponent: _lastCrashFilename]] atomically:YES encoding:NSUTF8StringEncoding error:&error]; + BITHockeyLog(@"ERROR: Writing crash meta description failed. %@", error); + } [self sendCrashReports]; YES; @@ -591,6 +598,7 @@ NSString *const kBITCrashManagerStatus = @"BITCrashManagerStatus"; } else { BITHockeyLog(@"ERROR: Writing crash meta data failed. %@", error); } + _lastCrashFilename = cacheFilename; } } } @@ -626,7 +634,7 @@ NSString *const kBITCrashManagerStatus = @"BITCrashManagerStatus"; /** * Check if there are any new crash reports that are not yet processed * - * @return `YES` if ther eis at least one new crash report found, `NO` otherwise + * @return `YES` if there is at least one new crash report found, `NO` otherwise */ - (BOOL)hasPendingCrashReport { if (_crashManagerStatus == BITCrashManagerStatusDisabled) return NO; @@ -902,7 +910,8 @@ NSString *const kBITCrashManagerStatus = @"BITCrashManagerStatus"; useremail = [self stringValueFromKeychainForKey:[NSString stringWithFormat:@"%@.%@", cacheFilename, kBITCrashMetaUserEmail]] ?: @""; userid = [self stringValueFromKeychainForKey:[NSString stringWithFormat:@"%@.%@", cacheFilename, kBITCrashMetaUserID]] ?: @""; applicationLog = [metaDict objectForKey:kBITCrashMetaApplicationLog] ?: @""; - + description = [NSString stringWithContentsOfFile:[NSString stringWithFormat:@"%@_description.meta", [_crashesDir stringByAppendingPathComponent: _lastCrashFilename]] encoding:NSUTF8StringEncoding error:&error]; + BITCrashAttachment *attachment = [self attachmentForCrashReport:filename]; if (attachment) { NSDictionary *attachmentDict = @{KBITAttachmentDictIndex: @(i), @@ -914,7 +923,11 @@ NSString *const kBITCrashManagerStatus = @"BITCrashManagerStatus"; } if ([applicationLog length] > 0) { - description = [NSString stringWithFormat:@"%@", applicationLog]; + if ([description length] > 0) { + description = [NSString stringWithFormat:@"%@\n\nLog:\n%@", description, applicationLog]; + } else { + description = [NSString stringWithFormat:@"Log:\n%@", applicationLog]; + } } [crashes appendFormat:@"%s%@%@%@%@%@%@%@%@%@%@%@", @@ -962,13 +975,13 @@ NSString *const kBITCrashManagerStatus = @"BITCrashManagerStatus"; - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { switch (buttonIndex) { case 0: - [self handleUserInput:BITCrashManagerUserInputDontSend]; + [self handleUserInput:BITCrashManagerUserInputDontSend withCrashMetaDescription:nil]; break; case 1: - [self handleUserInput:BITCrashManagerUserInputSend]; + [self handleUserInput:BITCrashManagerUserInputSend withCrashMetaDescription:nil]; break; case 2: - [self handleUserInput:BITCrashManagerUserInputAlwaysSend]; + [self handleUserInput:BITCrashManagerUserInputAlwaysSend withCrashMetaDescription:nil]; break; } } From 7a5077e7c15d587751f574c4ba99dd81cce26198 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Spie=C3=9F?= Date: Wed, 23 Apr 2014 16:09:23 +0200 Subject: [PATCH 08/25] semi-working prototype of custom Alert --- Classes/BITCrashManager.h | 6 ++++++ Classes/BITCrashManager.m | 31 ++++++++++++++++++++----------- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/Classes/BITCrashManager.h b/Classes/BITCrashManager.h index 0682dffd85..0b7c2a7582 100644 --- a/Classes/BITCrashManager.h +++ b/Classes/BITCrashManager.h @@ -267,6 +267,12 @@ typedef NS_ENUM(NSUInteger, BITCrashManagerUserInput) { */ - (BOOL)handleUserInput:(BITCrashManagerUserInput)userInput withCrashMetaDescription:metaDescription; +/** + Property that lets you set a custom block which handles showing a custom UI and asking the user + whether he wants to send the crash report. Needs to call the `handleUserInput` method. + */ +@property (nonatomic, copy, setter = setAlertViewHandler:) void (^alertViewHandler) (); + /** Provides the time between startup and crash in seconds diff --git a/Classes/BITCrashManager.m b/Classes/BITCrashManager.m index c1110b568a..1e89ce5106 100644 --- a/Classes/BITCrashManager.m +++ b/Classes/BITCrashManager.m @@ -116,6 +116,7 @@ NSString *const kBITCrashManagerStatus = @"BITCrashManagerStatus"; _timeintervalCrashInLastSessionOccured = -1; _approvedCrashReports = [[NSMutableDictionary alloc] init]; + _alertViewHandler = nil; _fileManager = [[NSFileManager alloc] init]; _crashFiles = [[NSMutableArray alloc] init]; @@ -489,6 +490,10 @@ NSString *const kBITCrashManagerStatus = @"BITCrashManagerStatus"; } } +- (void)setAlertViewHandler:(void (^)())alertViewHandler{ + _alertViewHandler = alertViewHandler; +} + - (BOOL)handleUserInput:(BITCrashManagerUserInput)userInput withCrashMetaDescription:(NSString *)metaDescription{ switch (userInput) { case BITCrashManagerUserInputDontSend: @@ -500,7 +505,7 @@ NSString *const kBITCrashManagerStatus = @"BITCrashManagerStatus"; YES; case BITCrashManagerUserInputSend: - if (!metaDescription && [metaDescription length] > 0) { + if (metaDescription && [metaDescription length] > 0) { NSError *error; [metaDescription writeToFile:[NSString stringWithFormat:@"%@_description.meta", [_crashesDir stringByAppendingPathComponent: _lastCrashFilename]] atomically:YES encoding:NSUTF8StringEncoding error:&error]; BITHockeyLog(@"ERROR: Writing crash meta description failed. %@", error); @@ -730,17 +735,21 @@ NSString *const kBITCrashManagerStatus = @"BITCrashManagerStatus"; alertDescription = [NSString stringWithFormat:BITHockeyLocalizedString(@"CrashDataFoundDescription"), appName]; } - UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:BITHockeyLocalizedString(@"CrashDataFoundTitle"), appName] - message:alertDescription - delegate:self - cancelButtonTitle:BITHockeyLocalizedString(@"CrashDontSendReport") - otherButtonTitles:BITHockeyLocalizedString(@"CrashSendReport"), nil]; - - if (self.shouldShowAlwaysButton) { - [alertView addButtonWithTitle:BITHockeyLocalizedString(@"CrashSendReportAlways")]; + if (_alertViewHandler) { + _alertViewHandler(); + } else { + UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:BITHockeyLocalizedString(@"CrashDataFoundTitle"), appName] + message:alertDescription + delegate:self + cancelButtonTitle:BITHockeyLocalizedString(@"CrashDontSendReport") + otherButtonTitles:BITHockeyLocalizedString(@"CrashSendReport"), nil]; + + if (self.shouldShowAlwaysButton) { + [alertView addButtonWithTitle:BITHockeyLocalizedString(@"CrashSendReportAlways")]; + } + + [alertView show]; } - - [alertView show]; } else { [self sendCrashReports]; } From 15582b773481791ded907290736f1fe06296ba12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Spie=C3=9F?= Date: Thu, 24 Apr 2014 12:32:01 +0200 Subject: [PATCH 09/25] Cleanup and small logic error fix --- Classes/BITCrashManager.m | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/Classes/BITCrashManager.m b/Classes/BITCrashManager.m index 1e89ce5106..d39c597784 100644 --- a/Classes/BITCrashManager.m +++ b/Classes/BITCrashManager.m @@ -445,6 +445,11 @@ NSString *const kBITCrashManagerStatus = @"BITCrashManagerStatus"; _crashCallBacks = callbacks; } + +- (void)setAlertViewHandler:(void (^)())alertViewHandler{ + _alertViewHandler = alertViewHandler; +} + /** * Check if the debugger is attached * @@ -490,9 +495,6 @@ NSString *const kBITCrashManagerStatus = @"BITCrashManagerStatus"; } } -- (void)setAlertViewHandler:(void (^)())alertViewHandler{ - _alertViewHandler = alertViewHandler; -} - (BOOL)handleUserInput:(BITCrashManagerUserInput)userInput withCrashMetaDescription:(NSString *)metaDescription{ switch (userInput) { @@ -508,7 +510,6 @@ NSString *const kBITCrashManagerStatus = @"BITCrashManagerStatus"; if (metaDescription && [metaDescription length] > 0) { NSError *error; [metaDescription writeToFile:[NSString stringWithFormat:@"%@_description.meta", [_crashesDir stringByAppendingPathComponent: _lastCrashFilename]] atomically:YES encoding:NSUTF8StringEncoding error:&error]; - BITHockeyLog(@"ERROR: Writing crash meta description failed. %@", error); } [self sendCrashReports]; YES; @@ -555,6 +556,7 @@ NSString *const kBITCrashManagerStatus = @"BITCrashManagerStatus"; NSData *crashData = [[NSData alloc] initWithData:[self.plCrashReporter loadPendingCrashReportDataAndReturnError: &error]]; NSString *cacheFilename = [NSString stringWithFormat: @"%.0f", [NSDate timeIntervalSinceReferenceDate]]; + _lastCrashFilename = cacheFilename; if (crashData == nil) { BITHockeyLog(@"ERROR: Could not load crash report: %@", error); @@ -603,7 +605,6 @@ NSString *const kBITCrashManagerStatus = @"BITCrashManagerStatus"; } else { BITHockeyLog(@"ERROR: Writing crash meta data failed. %@", error); } - _lastCrashFilename = cacheFilename; } } } @@ -919,7 +920,7 @@ NSString *const kBITCrashManagerStatus = @"BITCrashManagerStatus"; useremail = [self stringValueFromKeychainForKey:[NSString stringWithFormat:@"%@.%@", cacheFilename, kBITCrashMetaUserEmail]] ?: @""; userid = [self stringValueFromKeychainForKey:[NSString stringWithFormat:@"%@.%@", cacheFilename, kBITCrashMetaUserID]] ?: @""; applicationLog = [metaDict objectForKey:kBITCrashMetaApplicationLog] ?: @""; - description = [NSString stringWithContentsOfFile:[NSString stringWithFormat:@"%@_description.meta", [_crashesDir stringByAppendingPathComponent: _lastCrashFilename]] encoding:NSUTF8StringEncoding error:&error]; + description = [NSString stringWithContentsOfFile:[NSString stringWithFormat:@"%@_description.meta", [_crashesDir stringByAppendingPathComponent: cacheFilename]] encoding:NSUTF8StringEncoding error:&error]; BITCrashAttachment *attachment = [self attachmentForCrashReport:filename]; if (attachment) { From a05e15463b15da16bd594c666c55b5bb4b052bcd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Spie=C3=9F?= Date: Thu, 24 Apr 2014 15:37:34 +0200 Subject: [PATCH 10/25] Update tests for new method syntax --- Support/HockeySDKTests/BITCrashManagerTests.m | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Support/HockeySDKTests/BITCrashManagerTests.m b/Support/HockeySDKTests/BITCrashManagerTests.m index 34510ba35d..4e1c09e754 100644 --- a/Support/HockeySDKTests/BITCrashManagerTests.m +++ b/Support/HockeySDKTests/BITCrashManagerTests.m @@ -129,14 +129,14 @@ id delegateMock = mockProtocol(@protocol(BITCrashManagerDelegate)); _sut.delegate = delegateMock; - assertThatBool([_sut handleUserInput:BITCrashManagerUserInputDontSend], equalToBool(YES)); + assertThatBool([_sut handleUserInput:BITCrashManagerUserInputDontSend crashMetaDescription:nil], equalToBool(YES)); [verify(delegateMock) crashManagerWillCancelSendingCrashReport:_sut]; } - (void)testHandleUserInputSend { - assertThatBool([_sut handleUserInput:BITCrashManagerUserInputSend], equalToBool(YES)); + assertThatBool([_sut handleUserInput:BITCrashManagerUserInputSend crashMetaDescription:nil], equalToBool(YES)); } - (void)testHandleUserInputAlwaysSend { @@ -148,7 +148,7 @@ [given([mockUserDefaults integerForKey:@"BITCrashManagerStatus"]) willReturn:nil]; //Test if method runs through - assertThatBool([_sut handleUserInput:BITCrashManagerUserInputAlwaysSend], equalToBool(YES)); + assertThatBool([_sut handleUserInput:BITCrashManagerUserInputAlwaysSend crashMetaDescription:nil], equalToBool(YES)); //Test if correct CrashManagerStatus is now set [given([mockUserDefaults integerForKey:@"BITCrashManagerStauts"]) willReturnInt:BITCrashManagerStatusAutoSend]; From 27cc84a84c8b96e40d432f5581192f7295f42951 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Spie=C3=9F?= Date: Thu, 24 Apr 2014 15:38:10 +0200 Subject: [PATCH 11/25] Maybe a switch case should return a value... --- Classes/BITCrashManager.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Classes/BITCrashManager.m b/Classes/BITCrashManager.m index d39c597784..e4561fac59 100644 --- a/Classes/BITCrashManager.m +++ b/Classes/BITCrashManager.m @@ -504,7 +504,7 @@ NSString *const kBITCrashManagerStatus = @"BITCrashManagerStatus"; } [self cleanCrashReports]; - YES; + return YES; case BITCrashManagerUserInputSend: if (metaDescription && [metaDescription length] > 0) { @@ -512,7 +512,7 @@ NSString *const kBITCrashManagerStatus = @"BITCrashManagerStatus"; [metaDescription writeToFile:[NSString stringWithFormat:@"%@_description.meta", [_crashesDir stringByAppendingPathComponent: _lastCrashFilename]] atomically:YES encoding:NSUTF8StringEncoding error:&error]; } [self sendCrashReports]; - YES; + return YES; case BITCrashManagerUserInputAlwaysSend: _crashManagerStatus = BITCrashManagerStatusAutoSend; From 4aee9f81b547046164bad62ad5a96de35d2e1270 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Spie=C3=9F?= Date: Thu, 24 Apr 2014 15:38:31 +0200 Subject: [PATCH 12/25] Small optimizations --- Classes/BITCrashManager.h | 2 +- Classes/BITCrashManager.m | 13 +++++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/Classes/BITCrashManager.h b/Classes/BITCrashManager.h index 0b7c2a7582..76dd986676 100644 --- a/Classes/BITCrashManager.h +++ b/Classes/BITCrashManager.h @@ -265,7 +265,7 @@ typedef NS_ENUM(NSUInteger, BITCrashManagerUserInput) { @return BOOl if the input is a valid option @see BITCrashManagerUserInput */ -- (BOOL)handleUserInput:(BITCrashManagerUserInput)userInput withCrashMetaDescription:metaDescription; +- (BOOL)handleUserInput:(BITCrashManagerUserInput)userInput crashMetaDescription:metaDescription; /** Property that lets you set a custom block which handles showing a custom UI and asking the user diff --git a/Classes/BITCrashManager.m b/Classes/BITCrashManager.m index e4561fac59..8fa17d2214 100644 --- a/Classes/BITCrashManager.m +++ b/Classes/BITCrashManager.m @@ -496,7 +496,7 @@ NSString *const kBITCrashManagerStatus = @"BITCrashManagerStatus"; } -- (BOOL)handleUserInput:(BITCrashManagerUserInput)userInput withCrashMetaDescription:(NSString *)metaDescription{ +- (BOOL)handleUserInput:(BITCrashManagerUserInput)userInput crashMetaDescription:(NSString *)metaDescription{ switch (userInput) { case BITCrashManagerUserInputDontSend: if (self.delegate != nil && [self.delegate respondsToSelector:@selector(crashManagerWillCancelSendingCrashReport:)]) { @@ -522,6 +522,11 @@ NSString *const kBITCrashManagerStatus = @"BITCrashManagerStatus"; [self.delegate crashManagerWillSendCrashReportsAlways:self]; } + if (metaDescription && [metaDescription length] > 0) { + NSError *error; + [metaDescription writeToFile:[NSString stringWithFormat:@"%@_description.meta", [_crashesDir stringByAppendingPathComponent: _lastCrashFilename]] atomically:YES encoding:NSUTF8StringEncoding error:&error]; + } + [self sendCrashReports]; return YES; @@ -985,13 +990,13 @@ NSString *const kBITCrashManagerStatus = @"BITCrashManagerStatus"; - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { switch (buttonIndex) { case 0: - [self handleUserInput:BITCrashManagerUserInputDontSend withCrashMetaDescription:nil]; + [self handleUserInput:BITCrashManagerUserInputDontSend crashMetaDescription:nil]; break; case 1: - [self handleUserInput:BITCrashManagerUserInputSend withCrashMetaDescription:nil]; + [self handleUserInput:BITCrashManagerUserInputSend crashMetaDescription:nil]; break; case 2: - [self handleUserInput:BITCrashManagerUserInputAlwaysSend withCrashMetaDescription:nil]; + [self handleUserInput:BITCrashManagerUserInputAlwaysSend crashMetaDescription:nil]; break; } } From 13118462413ff8e1583ce81d7626737e62fd029b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Spie=C3=9F?= Date: Thu, 24 Apr 2014 16:10:12 +0200 Subject: [PATCH 13/25] update method signature --- Classes/BITCrashManager.h | 4 ++-- Classes/BITCrashManager.m | 8 ++++---- Support/HockeySDKTests/BITCrashManagerTests.m | 6 +++--- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/Classes/BITCrashManager.h b/Classes/BITCrashManager.h index 76dd986676..476145969d 100644 --- a/Classes/BITCrashManager.h +++ b/Classes/BITCrashManager.h @@ -265,13 +265,13 @@ typedef NS_ENUM(NSUInteger, BITCrashManagerUserInput) { @return BOOl if the input is a valid option @see BITCrashManagerUserInput */ -- (BOOL)handleUserInput:(BITCrashManagerUserInput)userInput crashMetaDescription:metaDescription; +- (BOOL)handleUserInput:(BITCrashManagerUserInput)userInput withUserProvidedCrashDescription:(NSString*)metaDescription; /** Property that lets you set a custom block which handles showing a custom UI and asking the user whether he wants to send the crash report. Needs to call the `handleUserInput` method. */ -@property (nonatomic, copy, setter = setAlertViewHandler:) void (^alertViewHandler) (); +@property (nonatomic, copy, setter = setAlertViewHandler:) customAlertViewHandler alertViewHandler; /** Provides the time between startup and crash in seconds diff --git a/Classes/BITCrashManager.m b/Classes/BITCrashManager.m index 8fa17d2214..303c0d0912 100644 --- a/Classes/BITCrashManager.m +++ b/Classes/BITCrashManager.m @@ -496,7 +496,7 @@ NSString *const kBITCrashManagerStatus = @"BITCrashManagerStatus"; } -- (BOOL)handleUserInput:(BITCrashManagerUserInput)userInput crashMetaDescription:(NSString *)metaDescription{ +- (BOOL)handleUserInput:(BITCrashManagerUserInput)userInput withUserProvidedCrashDescription:(NSString *)metaDescription{ switch (userInput) { case BITCrashManagerUserInputDontSend: if (self.delegate != nil && [self.delegate respondsToSelector:@selector(crashManagerWillCancelSendingCrashReport:)]) { @@ -990,13 +990,13 @@ NSString *const kBITCrashManagerStatus = @"BITCrashManagerStatus"; - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { switch (buttonIndex) { case 0: - [self handleUserInput:BITCrashManagerUserInputDontSend crashMetaDescription:nil]; + [self handleUserInput:BITCrashManagerUserInputDontSend withUserProvidedCrashDescription:nil]; break; case 1: - [self handleUserInput:BITCrashManagerUserInputSend crashMetaDescription:nil]; + [self handleUserInput:BITCrashManagerUserInputSend withUserProvidedCrashDescription:nil]; break; case 2: - [self handleUserInput:BITCrashManagerUserInputAlwaysSend crashMetaDescription:nil]; + [self handleUserInput:BITCrashManagerUserInputAlwaysSend withUserProvidedCrashDescription:nil]; break; } } diff --git a/Support/HockeySDKTests/BITCrashManagerTests.m b/Support/HockeySDKTests/BITCrashManagerTests.m index 4e1c09e754..45f6c044db 100644 --- a/Support/HockeySDKTests/BITCrashManagerTests.m +++ b/Support/HockeySDKTests/BITCrashManagerTests.m @@ -129,14 +129,14 @@ id delegateMock = mockProtocol(@protocol(BITCrashManagerDelegate)); _sut.delegate = delegateMock; - assertThatBool([_sut handleUserInput:BITCrashManagerUserInputDontSend crashMetaDescription:nil], equalToBool(YES)); + assertThatBool([_sut handleUserInput:BITCrashManagerUserInputDontSend withUserProvidedCrashDescription:nil], equalToBool(YES)); [verify(delegateMock) crashManagerWillCancelSendingCrashReport:_sut]; } - (void)testHandleUserInputSend { - assertThatBool([_sut handleUserInput:BITCrashManagerUserInputSend crashMetaDescription:nil], equalToBool(YES)); + assertThatBool([_sut handleUserInput:BITCrashManagerUserInputSend withUserProvidedCrashDescription:nil], equalToBool(YES)); } - (void)testHandleUserInputAlwaysSend { @@ -148,7 +148,7 @@ [given([mockUserDefaults integerForKey:@"BITCrashManagerStatus"]) willReturn:nil]; //Test if method runs through - assertThatBool([_sut handleUserInput:BITCrashManagerUserInputAlwaysSend crashMetaDescription:nil], equalToBool(YES)); + assertThatBool([_sut handleUserInput:BITCrashManagerUserInputAlwaysSend withUserProvidedCrashDescription:nil], equalToBool(YES)); //Test if correct CrashManagerStatus is now set [given([mockUserDefaults integerForKey:@"BITCrashManagerStauts"]) willReturnInt:BITCrashManagerStatusAutoSend]; From f8561afacc4e47a00ceccee2f9d29908674386e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Spie=C3=9F?= Date: Thu, 24 Apr 2014 16:11:05 +0200 Subject: [PATCH 14/25] Updates documentation --- Classes/BITCrashManager.h | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Classes/BITCrashManager.h b/Classes/BITCrashManager.h index 476145969d..3afe47092b 100644 --- a/Classes/BITCrashManager.h +++ b/Classes/BITCrashManager.h @@ -260,16 +260,19 @@ typedef NS_ENUM(NSUInteger, BITCrashManagerUserInput) { /** Provides an interface to handle user input from a custom alert - On this input depends, whether crash reports are sent, always sent or not sent and deleted. + @param userInput On this input depends, whether crash reports are sent, always sent or not sent and deleted. + @param metaDescription The content of this string will be attached to the crash report as the description and allows to ask the user for e.g. additional comments or info - @return BOOl if the input is a valid option + @return Returns YES if the input is a valid option and successfully triggered further processing of the crash report @see BITCrashManagerUserInput */ - (BOOL)handleUserInput:(BITCrashManagerUserInput)userInput withUserProvidedCrashDescription:(NSString*)metaDescription; /** Property that lets you set a custom block which handles showing a custom UI and asking the user - whether he wants to send the crash report. Needs to call the `handleUserInput` method. + whether he wants to send the crash report. + + @warning Needs to call the `handleUserInput` method! */ @property (nonatomic, copy, setter = setAlertViewHandler:) customAlertViewHandler alertViewHandler; From 3fd8cdc7e0b4d4b3227302575b29dff2498e088c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Spie=C3=9F?= Date: Thu, 24 Apr 2014 16:11:34 +0200 Subject: [PATCH 15/25] replace ugly block notation with clean typedef --- Classes/BITCrashManager.h | 5 +++++ Classes/BITCrashManager.m | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/Classes/BITCrashManager.h b/Classes/BITCrashManager.h index 3afe47092b..9a6ba8a0f5 100644 --- a/Classes/BITCrashManager.h +++ b/Classes/BITCrashManager.h @@ -39,6 +39,11 @@ #import #endif +/** + * Custom block that handles the alert that prompts the user whether he wants to send crash reports + */ +typedef void(^customAlertViewHandler)(); + /** * Crash Manager status diff --git a/Classes/BITCrashManager.m b/Classes/BITCrashManager.m index 303c0d0912..f4cb6e1508 100644 --- a/Classes/BITCrashManager.m +++ b/Classes/BITCrashManager.m @@ -446,7 +446,7 @@ NSString *const kBITCrashManagerStatus = @"BITCrashManagerStatus"; } -- (void)setAlertViewHandler:(void (^)())alertViewHandler{ +- (void)setAlertViewHandler:(customAlertViewHandler)alertViewHandler{ _alertViewHandler = alertViewHandler; } From 10c5475a02b5387b006e119384e8f0ba69091d5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Spie=C3=9F?= Date: Thu, 24 Apr 2014 16:32:34 +0200 Subject: [PATCH 16/25] make method signature consistent and reflect those changes in documentation --- Classes/BITCrashManager.h | 4 ++-- Classes/BITCrashManager.m | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Classes/BITCrashManager.h b/Classes/BITCrashManager.h index 9a6ba8a0f5..825ec48913 100644 --- a/Classes/BITCrashManager.h +++ b/Classes/BITCrashManager.h @@ -266,12 +266,12 @@ typedef NS_ENUM(NSUInteger, BITCrashManagerUserInput) { Provides an interface to handle user input from a custom alert @param userInput On this input depends, whether crash reports are sent, always sent or not sent and deleted. - @param metaDescription The content of this string will be attached to the crash report as the description and allows to ask the user for e.g. additional comments or info + @param userProvidedCrashDescription The content of this string will be attached to the crash report as the description and allows to ask the user for e.g. additional comments or info @return Returns YES if the input is a valid option and successfully triggered further processing of the crash report @see BITCrashManagerUserInput */ -- (BOOL)handleUserInput:(BITCrashManagerUserInput)userInput withUserProvidedCrashDescription:(NSString*)metaDescription; +- (BOOL)handleUserInput:(BITCrashManagerUserInput)userInput withUserProvidedCrashDescription:(NSString*)userProvidedCrashDescription; /** Property that lets you set a custom block which handles showing a custom UI and asking the user diff --git a/Classes/BITCrashManager.m b/Classes/BITCrashManager.m index f4cb6e1508..8f9327649d 100644 --- a/Classes/BITCrashManager.m +++ b/Classes/BITCrashManager.m @@ -496,7 +496,7 @@ NSString *const kBITCrashManagerStatus = @"BITCrashManagerStatus"; } -- (BOOL)handleUserInput:(BITCrashManagerUserInput)userInput withUserProvidedCrashDescription:(NSString *)metaDescription{ +- (BOOL)handleUserInput:(BITCrashManagerUserInput)userInput withUserProvidedCrashDescription:(NSString *)userProvidedCrashDescription{ switch (userInput) { case BITCrashManagerUserInputDontSend: if (self.delegate != nil && [self.delegate respondsToSelector:@selector(crashManagerWillCancelSendingCrashReport:)]) { @@ -507,9 +507,9 @@ NSString *const kBITCrashManagerStatus = @"BITCrashManagerStatus"; return YES; case BITCrashManagerUserInputSend: - if (metaDescription && [metaDescription length] > 0) { + if (userProvidedCrashDescription && [userProvidedCrashDescription length] > 0) { NSError *error; - [metaDescription writeToFile:[NSString stringWithFormat:@"%@_description.meta", [_crashesDir stringByAppendingPathComponent: _lastCrashFilename]] atomically:YES encoding:NSUTF8StringEncoding error:&error]; + [userProvidedCrashDescription writeToFile:[NSString stringWithFormat:@"%@_description.meta", [_crashesDir stringByAppendingPathComponent: _lastCrashFilename]] atomically:YES encoding:NSUTF8StringEncoding error:&error]; } [self sendCrashReports]; return YES; @@ -522,9 +522,9 @@ NSString *const kBITCrashManagerStatus = @"BITCrashManagerStatus"; [self.delegate crashManagerWillSendCrashReportsAlways:self]; } - if (metaDescription && [metaDescription length] > 0) { + if (userProvidedCrashDescription && [userProvidedCrashDescription length] > 0) { NSError *error; - [metaDescription writeToFile:[NSString stringWithFormat:@"%@_description.meta", [_crashesDir stringByAppendingPathComponent: _lastCrashFilename]] atomically:YES encoding:NSUTF8StringEncoding error:&error]; + [userProvidedCrashDescription writeToFile:[NSString stringWithFormat:@"%@_description.meta", [_crashesDir stringByAppendingPathComponent: _lastCrashFilename]] atomically:YES encoding:NSUTF8StringEncoding error:&error]; } [self sendCrashReports]; From b5348f7dcbd33392164ae5e66fd085dbcab7f5b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Spie=C3=9F?= Date: Thu, 24 Apr 2014 16:38:23 +0200 Subject: [PATCH 17/25] Types should start with a capital letter --- Classes/BITCrashManager.h | 2 +- Classes/BITCrashManager.m | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Classes/BITCrashManager.h b/Classes/BITCrashManager.h index 825ec48913..8312702ae0 100644 --- a/Classes/BITCrashManager.h +++ b/Classes/BITCrashManager.h @@ -42,7 +42,7 @@ /** * Custom block that handles the alert that prompts the user whether he wants to send crash reports */ -typedef void(^customAlertViewHandler)(); +typedef void(^CustomAlertViewHandler)(); /** diff --git a/Classes/BITCrashManager.m b/Classes/BITCrashManager.m index 8f9327649d..3c546c4786 100644 --- a/Classes/BITCrashManager.m +++ b/Classes/BITCrashManager.m @@ -55,7 +55,6 @@ #define kBITCrashMetaUserID @"BITCrashMetaUserID" #define kBITCrashMetaApplicationLog @"BITCrashMetaApplicationLog" #define kBITCrashMetaAttachment @"BITCrashMetaAttachment" -#define kBITCrashMetaDescription @"BITCrashMetaDescription" // internal keys NSString *const KBITAttachmentDictIndex = @"index"; @@ -67,6 +66,7 @@ NSString *const kBITCrashManagerStatus = @"BITCrashManagerStatus"; @interface BITCrashManager () @property (nonatomic, strong) NSFileManager *fileManager; +@property (nonatomic, copy, setter = setAlertViewHandler:) CustomAlertViewHandler alertViewHandler; @end @@ -79,7 +79,7 @@ NSString *const kBITCrashManagerStatus = @"BITCrashManagerStatus"; NSString *_settingsFile; NSString *_analyzerInProgressFile; NSFileManager *_fileManager; - + PLCrashReporterCallbacks *_crashCallBacks; BOOL _crashIdenticalCurrentVersion; @@ -446,7 +446,7 @@ NSString *const kBITCrashManagerStatus = @"BITCrashManagerStatus"; } -- (void)setAlertViewHandler:(customAlertViewHandler)alertViewHandler{ +- (void)setAlertViewHandler:(CustomAlertViewHandler)alertViewHandler{ _alertViewHandler = alertViewHandler; } From e026e42664c009aed605e5eca22fafee395a0999 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Spie=C3=9F?= Date: Thu, 24 Apr 2014 16:41:07 +0200 Subject: [PATCH 18/25] expose setter rather than the whole property --- Classes/BITCrashManager.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Classes/BITCrashManager.h b/Classes/BITCrashManager.h index 8312702ae0..0e0c69960d 100644 --- a/Classes/BITCrashManager.h +++ b/Classes/BITCrashManager.h @@ -274,12 +274,12 @@ typedef NS_ENUM(NSUInteger, BITCrashManagerUserInput) { - (BOOL)handleUserInput:(BITCrashManagerUserInput)userInput withUserProvidedCrashDescription:(NSString*)userProvidedCrashDescription; /** - Property that lets you set a custom block which handles showing a custom UI and asking the user + Lets you set a custom block which handles showing a custom UI and asking the user whether he wants to send the crash report. - @warning Needs to call the `handleUserInput` method! + @warning Block needs to call the `handleUserInput:withUserProvidedCrashDescription` method! */ -@property (nonatomic, copy, setter = setAlertViewHandler:) customAlertViewHandler alertViewHandler; +- (void) setAlertViewHandler:(CustomAlertViewHandler)alertViewHandler; /** Provides the time between startup and crash in seconds From 3b2b76b34ec6bbfc46279598a8af1bce87db8eb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Spie=C3=9F?= Date: Thu, 24 Apr 2014 16:46:11 +0200 Subject: [PATCH 19/25] move persisting of userProvidedCrashDescription to own method --- Classes/BITCrashManager.m | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/Classes/BITCrashManager.m b/Classes/BITCrashManager.m index 3c546c4786..f6b414ae10 100644 --- a/Classes/BITCrashManager.m +++ b/Classes/BITCrashManager.m @@ -252,6 +252,13 @@ NSString *const kBITCrashManagerStatus = @"BITCrashManagerStatus"; [data writeToFile:attachmentFilename atomically:YES]; } +- (void)persistUserProvidedCrashDescription:(NSString *)userProvidedCrashDescription { + if (userProvidedCrashDescription && [userProvidedCrashDescription length] > 0) { + NSError *error; + [userProvidedCrashDescription writeToFile:[NSString stringWithFormat:@"%@.desc", [_crashesDir stringByAppendingPathComponent: _lastCrashFilename]] atomically:YES encoding:NSUTF8StringEncoding error:&error]; + } +} + /** * Read the attachment data from the stored file * @@ -507,10 +514,8 @@ NSString *const kBITCrashManagerStatus = @"BITCrashManagerStatus"; return YES; case BITCrashManagerUserInputSend: - if (userProvidedCrashDescription && [userProvidedCrashDescription length] > 0) { - NSError *error; - [userProvidedCrashDescription writeToFile:[NSString stringWithFormat:@"%@_description.meta", [_crashesDir stringByAppendingPathComponent: _lastCrashFilename]] atomically:YES encoding:NSUTF8StringEncoding error:&error]; - } + [self persistUserProvidedCrashDescription:userProvidedCrashDescription]; + [self sendCrashReports]; return YES; @@ -522,10 +527,7 @@ NSString *const kBITCrashManagerStatus = @"BITCrashManagerStatus"; [self.delegate crashManagerWillSendCrashReportsAlways:self]; } - if (userProvidedCrashDescription && [userProvidedCrashDescription length] > 0) { - NSError *error; - [userProvidedCrashDescription writeToFile:[NSString stringWithFormat:@"%@_description.meta", [_crashesDir stringByAppendingPathComponent: _lastCrashFilename]] atomically:YES encoding:NSUTF8StringEncoding error:&error]; - } + [self persistUserProvidedCrashDescription:userProvidedCrashDescription]; [self sendCrashReports]; return YES; From 5442b217eb2e355cfb75cbe0a9fe3229b53e3e8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Spie=C3=9F?= Date: Thu, 24 Apr 2014 16:47:47 +0200 Subject: [PATCH 20/25] Use .desc file extension for userProvidedCrashDescription --- Classes/BITCrashManager.m | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Classes/BITCrashManager.m b/Classes/BITCrashManager.m index f6b414ae10..e0e5e3b135 100644 --- a/Classes/BITCrashManager.m +++ b/Classes/BITCrashManager.m @@ -227,6 +227,8 @@ NSString *const kBITCrashManagerStatus = @"BITCrashManagerStatus"; [_fileManager removeItemAtPath:[_crashFiles objectAtIndex:i] error:&error]; [_fileManager removeItemAtPath:[[_crashFiles objectAtIndex:i] stringByAppendingString:@".data"] error:&error]; [_fileManager removeItemAtPath:[[_crashFiles objectAtIndex:i] stringByAppendingString:@".meta"] error:&error]; + [_fileManager removeItemAtPath:[[_crashFiles objectAtIndex:i] stringByAppendingString:@".desc"] error:&error]; + NSString *cacheFilename = [[_crashFiles objectAtIndex:i] lastPathComponent]; [self removeKeyFromKeychain:[NSString stringWithFormat:@"%@.%@", cacheFilename, kBITCrashMetaUserName]]; @@ -665,7 +667,8 @@ NSString *const kBITCrashManagerStatus = @"BITCrashManagerStatus"; ![file hasSuffix:@".analyzer"] && ![file hasSuffix:@".plist"] && ![file hasSuffix:@".data"] && - ![file hasSuffix:@".meta"]) { + ![file hasSuffix:@".meta"] && + ![file hasSuffix:@".desc"]) { [_crashFiles addObject:[_crashesDir stringByAppendingPathComponent: file]]; } } @@ -884,6 +887,7 @@ NSString *const kBITCrashManagerStatus = @"BITCrashManagerStatus"; [_fileManager removeItemAtPath:filename error:&error]; [_fileManager removeItemAtPath:[NSString stringWithFormat:@"%@.data", filename] error:&error]; [_fileManager removeItemAtPath:[NSString stringWithFormat:@"%@.meta", filename] error:&error]; + [_fileManager removeItemAtPath:[NSString stringWithFormat:@"%@.desc", filename] error:&error]; [self removeKeyFromKeychain:[NSString stringWithFormat:@"%@.%@", cacheFilename, kBITCrashMetaUserName]]; [self removeKeyFromKeychain:[NSString stringWithFormat:@"%@.%@", cacheFilename, kBITCrashMetaUserEmail]]; @@ -927,7 +931,7 @@ NSString *const kBITCrashManagerStatus = @"BITCrashManagerStatus"; useremail = [self stringValueFromKeychainForKey:[NSString stringWithFormat:@"%@.%@", cacheFilename, kBITCrashMetaUserEmail]] ?: @""; userid = [self stringValueFromKeychainForKey:[NSString stringWithFormat:@"%@.%@", cacheFilename, kBITCrashMetaUserID]] ?: @""; applicationLog = [metaDict objectForKey:kBITCrashMetaApplicationLog] ?: @""; - description = [NSString stringWithContentsOfFile:[NSString stringWithFormat:@"%@_description.meta", [_crashesDir stringByAppendingPathComponent: cacheFilename]] encoding:NSUTF8StringEncoding error:&error]; + description = [NSString stringWithContentsOfFile:[NSString stringWithFormat:@"%@.desc", [_crashesDir stringByAppendingPathComponent: cacheFilename]] encoding:NSUTF8StringEncoding error:&error]; BITCrashAttachment *attachment = [self attachmentForCrashReport:filename]; if (attachment) { From 080eb3c657aa5352d3f1e08d6e4fc96e0f1d3222 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Spie=C3=9F?= Date: Thu, 24 Apr 2014 16:58:31 +0200 Subject: [PATCH 21/25] More detailed documentation --- Classes/BITCrashManager.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Classes/BITCrashManager.h b/Classes/BITCrashManager.h index 0e0c69960d..fbcbcd958f 100644 --- a/Classes/BITCrashManager.h +++ b/Classes/BITCrashManager.h @@ -266,7 +266,7 @@ typedef NS_ENUM(NSUInteger, BITCrashManagerUserInput) { Provides an interface to handle user input from a custom alert @param userInput On this input depends, whether crash reports are sent, always sent or not sent and deleted. - @param userProvidedCrashDescription The content of this string will be attached to the crash report as the description and allows to ask the user for e.g. additional comments or info + @param userProvidedCrashDescription The content of this optional string will be attached to the crash report as the description and allows to ask the user for e.g. additional comments or info @return Returns YES if the input is a valid option and successfully triggered further processing of the crash report @see BITCrashManagerUserInput @@ -275,7 +275,9 @@ typedef NS_ENUM(NSUInteger, BITCrashManagerUserInput) { /** Lets you set a custom block which handles showing a custom UI and asking the user - whether he wants to send the crash report. + whether he wants to send the crash report. + + @param alertViewHandler A block that is responsible for loading, presenting and and dismissing your custom user interface which prompts the user if he wants to send crash reports. The block is also responsible for triggering further processing of the crash reports. @warning Block needs to call the `handleUserInput:withUserProvidedCrashDescription` method! */ From d232fe58f6cc09b9dc4814c7f1c7960537518a75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Spie=C3=9F?= Date: Thu, 24 Apr 2014 18:17:46 +0200 Subject: [PATCH 22/25] move cleanup to separate method --- Classes/BITCrashManager.m | 40 ++++++++++++++++----------------------- 1 file changed, 16 insertions(+), 24 deletions(-) diff --git a/Classes/BITCrashManager.m b/Classes/BITCrashManager.m index e0e5e3b135..fd7bda8e02 100644 --- a/Classes/BITCrashManager.m +++ b/Classes/BITCrashManager.m @@ -224,16 +224,11 @@ NSString *const kBITCrashManagerStatus = @"BITCrashManagerStatus"; NSError *error = NULL; for (NSUInteger i=0; i < [_crashFiles count]; i++) { - [_fileManager removeItemAtPath:[_crashFiles objectAtIndex:i] error:&error]; - [_fileManager removeItemAtPath:[[_crashFiles objectAtIndex:i] stringByAppendingString:@".data"] error:&error]; - [_fileManager removeItemAtPath:[[_crashFiles objectAtIndex:i] stringByAppendingString:@".meta"] error:&error]; - [_fileManager removeItemAtPath:[[_crashFiles objectAtIndex:i] stringByAppendingString:@".desc"] error:&error]; - - + NSString *filename = [_crashFiles objectAtIndex:i]; NSString *cacheFilename = [[_crashFiles objectAtIndex:i] lastPathComponent]; - [self removeKeyFromKeychain:[NSString stringWithFormat:@"%@.%@", cacheFilename, kBITCrashMetaUserName]]; - [self removeKeyFromKeychain:[NSString stringWithFormat:@"%@.%@", cacheFilename, kBITCrashMetaUserEmail]]; - [self removeKeyFromKeychain:[NSString stringWithFormat:@"%@.%@", cacheFilename, kBITCrashMetaUserID]]; + + [self cleanFilesAndKeychainWithFileName:filename CacheFilename:cacheFilename Error:error]; + } [_crashFiles removeAllObjects]; [_approvedCrashReports removeAllObjects]; @@ -241,6 +236,16 @@ NSString *const kBITCrashManagerStatus = @"BITCrashManagerStatus"; [self saveSettings]; } +- (void)cleanFilesAndKeychainWithFileName:(NSString *)filename CacheFilename:(NSString *)cacheFilename Error:(NSError *)error{ + [_fileManager removeItemAtPath:filename error:&error]; + [_fileManager removeItemAtPath:[NSString stringWithFormat:@"%@.data", filename] error:&error]; + [_fileManager removeItemAtPath:[NSString stringWithFormat:@"%@.meta", filename] error:&error]; + [_fileManager removeItemAtPath:[NSString stringWithFormat:@"%@.desc", filename] error:&error]; + + [self removeKeyFromKeychain:[NSString stringWithFormat:@"%@.%@", cacheFilename, kBITCrashMetaUserName]]; + [self removeKeyFromKeychain:[NSString stringWithFormat:@"%@.%@", cacheFilename, kBITCrashMetaUserEmail]]; + [self removeKeyFromKeychain:[NSString stringWithFormat:@"%@.%@", cacheFilename, kBITCrashMetaUserID]]; +} - (void)persistAttachment:(BITCrashAttachment *)attachment withFilename:(NSString *)filename { NSString *attachmentFilename = [filename stringByAppendingString:@".data"]; @@ -884,14 +889,7 @@ NSString *const kBITCrashManagerStatus = @"BITCrashManagerStatus"; if (report == nil) { BITHockeyLog(@"WARNING: Could not parse crash report"); // we cannot do anything with this report, so delete it - [_fileManager removeItemAtPath:filename error:&error]; - [_fileManager removeItemAtPath:[NSString stringWithFormat:@"%@.data", filename] error:&error]; - [_fileManager removeItemAtPath:[NSString stringWithFormat:@"%@.meta", filename] error:&error]; - [_fileManager removeItemAtPath:[NSString stringWithFormat:@"%@.desc", filename] error:&error]; - - [self removeKeyFromKeychain:[NSString stringWithFormat:@"%@.%@", cacheFilename, kBITCrashMetaUserName]]; - [self removeKeyFromKeychain:[NSString stringWithFormat:@"%@.%@", cacheFilename, kBITCrashMetaUserEmail]]; - [self removeKeyFromKeychain:[NSString stringWithFormat:@"%@.%@", cacheFilename, kBITCrashMetaUserID]]; + [self cleanFilesAndKeychainWithFileName:filename CacheFilename:cacheFilename Error:error]; continue; } @@ -972,13 +970,7 @@ NSString *const kBITCrashManagerStatus = @"BITCrashManagerStatus"; [_approvedCrashReports setObject:[NSNumber numberWithBool:YES] forKey:filename]; } else { // we cannot do anything with this report, so delete it - [_fileManager removeItemAtPath:filename error:&error]; - [_fileManager removeItemAtPath:[NSString stringWithFormat:@"%@.data", filename] error:&error]; - [_fileManager removeItemAtPath:[NSString stringWithFormat:@"%@.meta", filename] error:&error]; - - [self removeKeyFromKeychain:[NSString stringWithFormat:@"%@.%@", cacheFilename, kBITCrashMetaUserName]]; - [self removeKeyFromKeychain:[NSString stringWithFormat:@"%@.%@", cacheFilename, kBITCrashMetaUserEmail]]; - [self removeKeyFromKeychain:[NSString stringWithFormat:@"%@.%@", cacheFilename, kBITCrashMetaUserID]]; + [self cleanFilesAndKeychainWithFileName:filename CacheFilename:cacheFilename Error:error]; } } From 654620ee3f61cd672d836b738317470f45d8736f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Spie=C3=9F?= Date: Thu, 24 Apr 2014 18:18:09 +0200 Subject: [PATCH 23/25] add test for persistUserProvidedCrashDescription --- Classes/BITCrashManager.m | 7 +++++++ Classes/BITCrashManagerPrivate.h | 5 +++++ Support/HockeySDKTests/BITCrashManagerTests.m | 7 +++++++ 3 files changed, 19 insertions(+) diff --git a/Classes/BITCrashManager.m b/Classes/BITCrashManager.m index fd7bda8e02..5714696ebc 100644 --- a/Classes/BITCrashManager.m +++ b/Classes/BITCrashManager.m @@ -451,6 +451,13 @@ NSString *const kBITCrashManagerStatus = @"BITCrashManagerStatus"; return useremail; } +- (NSString *)getCrashesDir { + return _crashesDir; +} + +- (void)setLastCrashFilename:(NSString *)lastCrashFilename { + _lastCrashFilename = lastCrashFilename; +} #pragma mark - Public diff --git a/Classes/BITCrashManagerPrivate.h b/Classes/BITCrashManagerPrivate.h index f2231342f4..cf4296432d 100644 --- a/Classes/BITCrashManagerPrivate.h +++ b/Classes/BITCrashManagerPrivate.h @@ -61,9 +61,14 @@ - (BOOL)hasPendingCrashReport; - (BOOL)hasNonApprovedCrashReports; +- (void)persistUserProvidedCrashDescription:(NSString *)userProvidedCrashDescription; + - (void)invokeDelayedProcessing; - (void)sendCrashReports; +- (NSString *)getCrashesDir; +- (void)setLastCrashFilename:(NSString *)lastCrashFilename; + @end diff --git a/Support/HockeySDKTests/BITCrashManagerTests.m b/Support/HockeySDKTests/BITCrashManagerTests.m index 45f6c044db..8cecb1fb9c 100644 --- a/Support/HockeySDKTests/BITCrashManagerTests.m +++ b/Support/HockeySDKTests/BITCrashManagerTests.m @@ -71,6 +71,13 @@ [self startManager]; } +- (void)testPersistUserProvidedCrashDescription { + [_sut setLastCrashFilename:@"temp"]; + [_sut persistUserProvidedCrashDescription:@"Test string"]; + NSError *error; + NSString *description = [NSString stringWithContentsOfFile:[NSString stringWithFormat:@"%@.desc", [[_sut getCrashesDir] stringByAppendingPathComponent: @"temp"]] encoding:NSUTF8StringEncoding error:&error]; + assertThat(description, equalTo(@"Test string")); +} #pragma mark - Setup Tests From 535537aa739a5e5ee43cd7c75ff42a8a4275033a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Spie=C3=9F?= Date: Fri, 25 Apr 2014 14:23:18 +0200 Subject: [PATCH 24/25] add additional tests --- Classes/BITCrashManager.m | 4 --- Classes/BITCrashManagerPrivate.h | 3 ++ Support/HockeySDKTests/BITCrashManagerTests.m | 30 ++++++++++++++----- 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/Classes/BITCrashManager.m b/Classes/BITCrashManager.m index 5714696ebc..99ee061400 100644 --- a/Classes/BITCrashManager.m +++ b/Classes/BITCrashManager.m @@ -455,10 +455,6 @@ NSString *const kBITCrashManagerStatus = @"BITCrashManagerStatus"; return _crashesDir; } -- (void)setLastCrashFilename:(NSString *)lastCrashFilename { - _lastCrashFilename = lastCrashFilename; -} - #pragma mark - Public diff --git a/Classes/BITCrashManagerPrivate.h b/Classes/BITCrashManagerPrivate.h index cf4296432d..c12749e7df 100644 --- a/Classes/BITCrashManagerPrivate.h +++ b/Classes/BITCrashManagerPrivate.h @@ -38,6 +38,8 @@ @property (nonatomic, strong) BITPLCrashReporter *plCrashReporter; +@property (nonatomic) NSString *lastCrashFilename; + #if HOCKEYSDK_FEATURE_AUTHENTICATOR // Only set via BITAuthenticator @@ -62,6 +64,7 @@ - (BOOL)hasNonApprovedCrashReports; - (void)persistUserProvidedCrashDescription:(NSString *)userProvidedCrashDescription; +- (void)persistAttachment:(BITCrashAttachment *)attachment withFilename:(NSString *)filename; - (void)invokeDelayedProcessing; - (void)sendCrashReports; diff --git a/Support/HockeySDKTests/BITCrashManagerTests.m b/Support/HockeySDKTests/BITCrashManagerTests.m index 8cecb1fb9c..efae8c1c48 100644 --- a/Support/HockeySDKTests/BITCrashManagerTests.m +++ b/Support/HockeySDKTests/BITCrashManagerTests.m @@ -71,14 +71,6 @@ [self startManager]; } -- (void)testPersistUserProvidedCrashDescription { - [_sut setLastCrashFilename:@"temp"]; - [_sut persistUserProvidedCrashDescription:@"Test string"]; - NSError *error; - NSString *description = [NSString stringWithContentsOfFile:[NSString stringWithFormat:@"%@.desc", [[_sut getCrashesDir] stringByAppendingPathComponent: @"temp"]] encoding:NSUTF8StringEncoding error:&error]; - assertThat(description, equalTo(@"Test string")); -} - #pragma mark - Setup Tests - (void)testThatItInstantiates { @@ -88,6 +80,24 @@ #pragma mark - Persistence tests +- (void)testPersistUserProvidedCrashDescription { + NSString *tempCrashName = @"tempCrash"; + [_sut setLastCrashFilename:tempCrashName]; + [_sut persistUserProvidedCrashDescription:@"Test string"]; + + NSError *error; + NSString *description = [NSString stringWithContentsOfFile:[NSString stringWithFormat:@"%@.desc", [[_sut getCrashesDir] stringByAppendingPathComponent: tempCrashName]] encoding:NSUTF8StringEncoding error:&error]; + assertThat(description, equalTo(@"Test string")); +} + +- (void)testPersistAttachment { + BITCrashAttachment *attachment = mock([BITCrashAttachment class]); + NSString *filename = @"testAttachment"; + + [_sut persistAttachment:attachment withFilename:filename]; + + [given([NSData dataWithContentsOfFile:[filename stringByAppendingString:@".data"]]) willReturn:[NSMutableDictionary class]]; +} #pragma mark - Helper @@ -165,6 +175,10 @@ } +- (void)testHandleUserInputWithInvalidInput { + assertThatBool([_sut handleUserInput:3 withUserProvidedCrashDescription:nil], equalToBool(NO)); +} + #pragma mark - Debugger /** From 82b217f19d774e188d6161fe3b947bee167fa9be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Spie=C3=9F?= Date: Fri, 25 Apr 2014 16:23:39 +0200 Subject: [PATCH 25/25] Much better test for attachment persisting --- Classes/BITCrashManager.m | 1 - Classes/BITCrashManagerPrivate.h | 4 ++++ Support/HockeySDKTests/BITCrashManagerTests.m | 17 +++++++++++++---- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/Classes/BITCrashManager.m b/Classes/BITCrashManager.m index 99ee061400..de9fb5dbbe 100644 --- a/Classes/BITCrashManager.m +++ b/Classes/BITCrashManager.m @@ -65,7 +65,6 @@ NSString *const kBITCrashManagerStatus = @"BITCrashManagerStatus"; @interface BITCrashManager () -@property (nonatomic, strong) NSFileManager *fileManager; @property (nonatomic, copy, setter = setAlertViewHandler:) CustomAlertViewHandler alertViewHandler; @end diff --git a/Classes/BITCrashManagerPrivate.h b/Classes/BITCrashManagerPrivate.h index c12749e7df..36e8ce9607 100644 --- a/Classes/BITCrashManagerPrivate.h +++ b/Classes/BITCrashManagerPrivate.h @@ -36,6 +36,8 @@ @property (nonatomic) NSUncaughtExceptionHandler *exceptionHandler; +@property (nonatomic, strong) NSFileManager *fileManager; + @property (nonatomic, strong) BITPLCrashReporter *plCrashReporter; @property (nonatomic) NSString *lastCrashFilename; @@ -66,6 +68,8 @@ - (void)persistUserProvidedCrashDescription:(NSString *)userProvidedCrashDescription; - (void)persistAttachment:(BITCrashAttachment *)attachment withFilename:(NSString *)filename; +- (BITCrashAttachment *)attachmentForCrashReport:(NSString *)filename; + - (void)invokeDelayedProcessing; - (void)sendCrashReports; diff --git a/Support/HockeySDKTests/BITCrashManagerTests.m b/Support/HockeySDKTests/BITCrashManagerTests.m index efae8c1c48..b6d0240921 100644 --- a/Support/HockeySDKTests/BITCrashManagerTests.m +++ b/Support/HockeySDKTests/BITCrashManagerTests.m @@ -23,6 +23,7 @@ #import "BITTestHelper.h" +#define kBITCrashMetaAttachment @"BITCrashMetaAttachment" @interface BITCrashManagerTests : SenTestCase @@ -91,12 +92,20 @@ } - (void)testPersistAttachment { - BITCrashAttachment *attachment = mock([BITCrashAttachment class]); - NSString *filename = @"testAttachment"; + NSString *filename = @"TestAttachment"; + NSData *data = [[NSData alloc] initWithBase64Encoding:@"TestData"]; + NSString* type = @"text/plain"; - [_sut persistAttachment:attachment withFilename:filename]; + BITCrashAttachment *originalAttachment = [[BITCrashAttachment alloc] initWithFilename:filename attachmentData:data contentType:type]; + NSString *attachmentFilename = [_sut.getCrashesDir stringByAppendingPathComponent:@"testAttachment"]; - [given([NSData dataWithContentsOfFile:[filename stringByAppendingString:@".data"]]) willReturn:[NSMutableDictionary class]]; + [_sut persistAttachment:originalAttachment withFilename:attachmentFilename]; + + BITCrashAttachment *decodedAttachment = [_sut attachmentForCrashReport:attachmentFilename]; + + assertThat(decodedAttachment.filename, equalTo(filename)); + assertThat(decodedAttachment.attachmentData, equalTo(data)); + assertThat(decodedAttachment.contentType, equalTo(type)); } #pragma mark - Helper