diff options
Diffstat (limited to 'macosx/HandBrake Tests')
-rw-r--r-- | macosx/HandBrake Tests/HBPresetsTests.m | 53 |
1 files changed, 52 insertions, 1 deletions
diff --git a/macosx/HandBrake Tests/HBPresetsTests.m b/macosx/HandBrake Tests/HBPresetsTests.m index 231f151a4..af7e48ddd 100644 --- a/macosx/HandBrake Tests/HBPresetsTests.m +++ b/macosx/HandBrake Tests/HBPresetsTests.m @@ -46,8 +46,59 @@ [self measureBlock:^{ [manager generateBuiltInPresets]; - }]; } +- (void)testSave +{ + NSURL *tempURL = [NSURL fileURLWithPath:NSTemporaryDirectory()]; + NSURL *presetsURL = [tempURL URLByAppendingPathComponent:@"test.json"]; + HBPresetsManager *manager = [[HBPresetsManager alloc] initWithURL:presetsURL]; + [manager savePresets]; + + XCTAssertTrue([[NSFileManager defaultManager] fileExistsAtPath:presetsURL.path]); + + // Remove the temp files. + [[NSFileManager defaultManager] removeItemAtURL:presetsURL error:NULL]; +} + +- (void)testUpgrade +{ + NSURL *tempURL = [NSURL fileURLWithPath:NSTemporaryDirectory()]; + NSURL *presetsURL = [tempURL URLByAppendingPathComponent:@"test.json"]; + NSURL *modifiedPresetsURL = [tempURL URLByAppendingPathComponent:@"test2.json"]; + + // Create a new presets manager with the defaults presets. + HBPresetsManager *manager = [[HBPresetsManager alloc] initWithURL:presetsURL]; + [manager savePresets]; + + // Read the json and change the version to the previous major + // so it will kick in the import routine. + NSData *data = [NSData dataWithContentsOfURL:presetsURL]; + NSMutableDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:NULL]; + dict[@"VersionMajor"] = @([dict[@"VersionMajor"] integerValue] - 1); + + + NSString *backupName = [NSString stringWithFormat:@"%@.%d.%d.%d.json", + modifiedPresetsURL.lastPathComponent.stringByDeletingPathExtension, + [dict[@"VersionMajor"] intValue], + [dict[@"VersionMinor"] intValue], + [dict[@"VersionMicro"] intValue]]; + NSURL *backupURL = [tempURL URLByAppendingPathComponent:backupName]; + + NSData *modifiedData = [NSJSONSerialization dataWithJSONObject:dict options:0 error:NULL]; + [modifiedData writeToURL:modifiedPresetsURL atomically:YES]; + + // Create a new manager and init it with the modified json. + HBPresetsManager *newManager = [[HBPresetsManager alloc] initWithURL:modifiedPresetsURL]; + + XCTAssert(newManager); + XCTAssertTrue([[NSFileManager defaultManager] fileExistsAtPath:backupURL.path]); + + // Remove the temp files. + [[NSFileManager defaultManager] removeItemAtURL:presetsURL error:NULL]; + [[NSFileManager defaultManager] removeItemAtURL:modifiedPresetsURL error:NULL]; + [[NSFileManager defaultManager] removeItemAtURL:backupURL error:NULL]; +} + @end |