github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/updater/osx/UpdaterTests/PromptTests.m (about) 1 // 2 // PromptTests.m 3 // UpdaterTests 4 // 5 // Created by Gabriel on 4/13/16. 6 // Copyright © 2016 Gabriel Handford. All rights reserved. 7 // 8 9 #import <XCTest/XCTest.h> 10 11 #import "Prompt.h" 12 13 @interface PromptTests : XCTestCase 14 @end 15 16 @implementation PromptTests 17 18 - (void)assertOutput:(NSData *)output action:(NSString *)action autoUpdate:(BOOL)autoUpdate { 19 NSString *stringOutput = [[NSString alloc] initWithData:output encoding:NSUTF8StringEncoding]; 20 NSLog(@"Checking output: %@", stringOutput); 21 NSString *expected = [NSString stringWithFormat:@"{\"action\":\"%@\",\"autoUpdate\":%@}", action, (autoUpdate ? @"true" : @"false")]; 22 XCTAssertEqualObjects(expected, stringOutput); 23 } 24 25 - (void)testUpdatePrompt { 26 NSData *data = [NSJSONSerialization dataWithJSONObject:@{@"title": @"Title", @"message": @"", @"description": @"", @"autoUpdate": @NO} options:0 error:nil]; 27 NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 28 [Prompt showPromptWithInputString:str presenter:^NSModalResponse(NSAlert *alert) { 29 return NSAlertFirstButtonReturn; 30 } completion:^(NSError *error, NSData *output) { 31 NSLog(@"Error: %@", error); 32 XCTAssertNil(error); 33 [self assertOutput:output action:@"apply" autoUpdate:NO]; 34 }]; 35 } 36 37 - (void)testUpdatePromptAutoUpdate { 38 NSData *data = [NSJSONSerialization dataWithJSONObject:@{@"title": @"Title", @"message": @"", @"description": @"", @"autoUpdate": @YES} options:0 error:nil]; 39 NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 40 [Prompt showPromptWithInputString:str presenter:^NSModalResponse(NSAlert *alert) { 41 return NSAlertSecondButtonReturn; 42 } completion:^(NSError *error, NSData *output) { 43 NSLog(@"Error: %@", error); 44 XCTAssertNil(error); 45 [self assertOutput:output action:@"snooze" autoUpdate:YES]; 46 }]; 47 } 48 49 - (void)testUpdatePromptNoSettings { 50 [Prompt showPromptWithInputString:@"" presenter:^NSModalResponse(NSAlert *alert) { 51 return NSAlertSecondButtonReturn; 52 } completion:^(NSError *error, NSData *output) { 53 NSLog(@"Error: %@", error); 54 XCTAssertNil(error); 55 [self assertOutput:output action:@"snooze" autoUpdate:NO]; 56 }]; 57 } 58 59 - (void)testUpdatePromptInvalidJSONInput { 60 NSData *data = [NSJSONSerialization dataWithJSONObject:@{@"title": @{}, @"message": @{}, @"description": @{}, @"autoUpdate": @{}} options:0 error:nil]; 61 NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 62 [Prompt showPromptWithInputString:str presenter:^NSModalResponse(NSAlert *alert) { 63 return NSAlertSecondButtonReturn; 64 } completion:^(NSError *error, NSData *output) { 65 [self assertOutput:output action:@"snooze" autoUpdate:NO]; 66 }]; 67 } 68 69 - (void)testUpdatePromptInvalidJSONRoot { 70 NSData *data = [NSJSONSerialization dataWithJSONObject:@[] options:0 error:nil]; 71 NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 72 [Prompt showPromptWithInputString:str presenter:^NSModalResponse(NSAlert *alert) { 73 return NSAlertSecondButtonReturn; 74 } completion:^(NSError *error, NSData *output) { 75 NSLog(@"Error: %@", error); 76 XCTAssertNil(error); 77 [self assertOutput:output action:@"snooze" autoUpdate:NO]; 78 }]; 79 } 80 81 - (void)testUpdatePromptBadJSON { 82 [Prompt showPromptWithInputString:@"badjson" presenter:^NSModalResponse(NSAlert *alert) { 83 return NSAlertFirstButtonReturn; 84 } completion:^(NSError *error, NSData *output) { 85 NSLog(@"Error: %@", error); 86 NSLog(@"Output: %@", output); 87 XCTAssertNil(error); 88 }]; 89 } 90 91 @end