diff --git a/SUUpdater.h b/SUUpdater.h index 99b615a8..aaefffb4 100644 --- a/SUUpdater.h +++ b/SUUpdater.h @@ -35,7 +35,12 @@ @property NSTimeInterval updateCheckInterval; -@property (copy) NSURL *feedURL; // *** MUST BE CALLED ON MAIN THREAD *** +/*! + * The URL of the appcast used to download update information. + * + * This property must be called on the main thread. + */ +@property (copy) NSURL *feedURL; @property (nonatomic, copy) NSString *userAgentString; diff --git a/SUUpdater.m b/SUUpdater.m index f8623042..16dd23e5 100644 --- a/SUUpdater.m +++ b/SUUpdater.m @@ -441,17 +441,23 @@ static NSString * const SUUpdaterDefaultsObservationContext = @"SUUpdaterDefault - (void)setFeedURL:(NSURL *)feedURL { + if (![NSThread isMainThread]) + [NSException raise:@"SUThreadException" format:@"This method must be called on the main thread"]; + [host setObject:[feedURL absoluteString] forUserDefaultsKey:SUFeedURLKey]; } -- (NSURL *)feedURL // *** MUST BE CALLED ON MAIN THREAD *** +- (NSURL *)feedURL { + if (![NSThread isMainThread]) + [NSException raise:@"SUThreadException" format:@"This method must be called on the main thread"]; + // A value in the user defaults overrides one in the Info.plist (so preferences panels can be created wherein users choose between beta / release feeds). NSString *appcastString = [host objectForKey:SUFeedURLKey]; if( [delegate respondsToSelector: @selector(feedURLStringForUpdater:)] ) appcastString = [delegate feedURLStringForUpdater: self]; if (!appcastString) // Can't find an appcast string! - [NSException raise:@"SUNoFeedURL" format:@"You must specify the URL of the appcast as the SUFeedURL key in either the Info.plist or the user defaults!"]; + [NSException raise:@"SUNoFeedURL" format:@"You must specify the URL of the appcast as the %@ key in either the Info.plist or the user defaults!", SUFeedURLKey]; NSCharacterSet* quoteSet = [NSCharacterSet characterSetWithCharactersInString: @"\"\'"]; // Some feed publishers add quotes; strip 'em. NSString* castUrlStr = [appcastString stringByTrimmingCharactersInSet:quoteSet]; if( !castUrlStr || [castUrlStr length] == 0 ) diff --git a/Sparkle.xcodeproj/project.pbxproj b/Sparkle.xcodeproj/project.pbxproj index 26efd971..665067db 100644 --- a/Sparkle.xcodeproj/project.pbxproj +++ b/Sparkle.xcodeproj/project.pbxproj @@ -31,6 +31,7 @@ 14950071195FCE3D00BC5B5B /* AppKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 0867D6A5FE840307C02AAC07 /* AppKit.framework */; }; 14950072195FCE4B00BC5B5B /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 0867D69BFE84028FC02AAC07 /* Foundation.framework */; }; 14950073195FCE4E00BC5B5B /* AppKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 0867D6A5FE840307C02AAC07 /* AppKit.framework */; }; + 14950075195FDF5900BC5B5B /* SUUpdaterTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 14950074195FDF5900BC5B5B /* SUUpdaterTest.m */; }; 3772FEA913DE0B6B00F79537 /* SUVersionDisplayProtocol.h in Headers */ = {isa = PBXBuildFile; fileRef = 3772FEA813DE0B6B00F79537 /* SUVersionDisplayProtocol.h */; settings = {ATTRIBUTES = (Public, ); }; }; 550347011370023A00AE7681 /* Sparkle.icns in Resources */ = {isa = PBXBuildFile; fileRef = 550347001370023A00AE7681 /* Sparkle.icns */; }; 55C14BD4136EEFCE00649790 /* Autoupdate.m in Sources */ = {isa = PBXBuildFile; fileRef = 55C14BD3136EEFCE00649790 /* Autoupdate.m */; }; @@ -245,6 +246,7 @@ 0263187714FEBB42005EBF43 /* uk */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = uk; path = uk.lproj/SUUpdatePermissionPrompt.xib; sourceTree = ""; }; 0867D69BFE84028FC02AAC07 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; }; 0867D6A5FE840307C02AAC07 /* AppKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AppKit.framework; path = System/Library/Frameworks/AppKit.framework; sourceTree = SDKROOT; }; + 14950074195FDF5900BC5B5B /* SUUpdaterTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = SUUpdaterTest.m; path = Tests/SUUpdaterTest.m; sourceTree = ""; }; 3772FEA813DE0B6B00F79537 /* SUVersionDisplayProtocol.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SUVersionDisplayProtocol.h; sourceTree = ""; }; 4607BEA21948443800EF8DA4 /* nb */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = nb; path = nb.lproj/Sparkle.strings; sourceTree = ""; }; 4607BEA31948443800EF8DA4 /* nb */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = nb; path = nb.lproj/SUAutomaticUpdateAlert.xib; sourceTree = ""; }; @@ -653,6 +655,7 @@ children = ( 612279DA0DB5470200AB99EA /* Sparkle Unit Tests-Info.plist */, 61227A150DB548B800AB99EA /* SUVersionComparisonTest.m */, + 14950074195FDF5900BC5B5B /* SUUpdaterTest.m */, ); name = Tests; sourceTree = ""; @@ -1183,6 +1186,7 @@ buildActionMask = 2147483647; files = ( 61227A160DB548B800AB99EA /* SUVersionComparisonTest.m in Sources */, + 14950075195FDF5900BC5B5B /* SUUpdaterTest.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/Tests/SUUpdaterTest.m b/Tests/SUUpdaterTest.m new file mode 100644 index 00000000..c93dc334 --- /dev/null +++ b/Tests/SUUpdaterTest.m @@ -0,0 +1,79 @@ +// +// SUUpdaterTest.m +// Sparkle +// +// Created by Jake Petroules on 2014-06-29. +// Copyright (c) 2014 Sparkle Project. All rights reserved. +// + +#import +#import "SUConstants.h" +#import "SUUpdater.h" + +@interface SUUpdaterTest : XCTestCase + +@end + +@implementation SUUpdaterTest +{ + NSOperationQueue *queue; + SUUpdater *updater; +} + +- (void)setUp +{ + [super setUp]; + queue = [[NSOperationQueue alloc] init]; + updater = [[SUUpdater alloc] init]; + updater.delegate = self; +} + +- (void)tearDown +{ + [updater release]; + updater = nil; + [queue release]; + queue = nil; + [super tearDown]; +} + +- (NSString *)feedURLStringForUpdater:(SUUpdater *) __unused updater +{ + return @""; +} + +- (void)testFeedURL +{ + [updater feedURL]; // this WON'T throw + + [queue addOperationWithBlock:^{ + XCTAssertTrue(![NSThread isMainThread]); + @try { + [updater feedURL]; + XCTFail(@"feedURL did not throw an exception when called on a secondary thread"); + } + @catch (NSException *exception) { + NSLog(@"%@", exception); + } + }]; + [queue waitUntilAllOperationsAreFinished]; +} + +- (void)setTestFeedURL +{ + [updater setFeedURL:[NSURL URLWithString:@""]]; // this WON'T throw + + [queue addOperationWithBlock:^{ + XCTAssertTrue(![NSThread isMainThread]); + @try { + [updater setFeedURL:[NSURL URLWithString:@""]]; + XCTFail(@"setFeedURL: did not throw an exception when called on a secondary thread"); + } + @catch (NSException *exception) { + NSLog(@"%@", exception); + } + }]; + [queue waitUntilAllOperationsAreFinished]; +} + +@end