Commits

Anonymous committed 199c0df

Test openInDefaultBrowser: by mocking NSWorkspace

Comments (0)

Files changed (3)

WhereIsMyMac-WithUnitTests/Classes/WhereIsMyMacWindowController.h

 	
 	CLLocationManager * _locationManager;
 	CoreLocationFormatter * _locationFormatter;
+	NSWorkspace * _workspace;
 }
 
 @property (assign) IBOutlet WebView *webView;
 
 - (id)init;
 - (id)initWithLocationManager:(CLLocationManager *)locationManager
-			locationFormatter:(CoreLocationFormatter *)locationFormatter;
+			locationFormatter:(CoreLocationFormatter *)locationFormatter
+					workspace:(NSWorkspace *)workspace;
 
 - (IBAction)openInDefaultBrowser:(id)sender;
 

WhereIsMyMac-WithUnitTests/Classes/WhereIsMyMacWindowController.m

 	CoreLocationFormatter * locationFormatter =
 		[[[CoreLocationFormatter alloc] initWithDelegate:self
 											formatString:formatString] autorelease];
-	return [self initWithLocationManager:locationManager locationFormatter:locationFormatter];
+	return [self initWithLocationManager:locationManager
+					   locationFormatter:locationFormatter
+							   workspace:[NSWorkspace sharedWorkspace]];
 }
 
 - (id)initWithLocationManager:(CLLocationManager *)locationManager
 			locationFormatter:(CoreLocationFormatter *)locationFormatter
+					workspace:(NSWorkspace *)workspace;
 {
 	self = [super init];
 	if (self == nil)
 	
 	_locationManager = [locationManager retain];
 	_locationFormatter = [locationFormatter retain];
+	_workspace = [workspace retain];
 	
 	return self;
 }
 	[_locationManager stopUpdatingLocation];
 	[_locationManager release];
 	[_locationFormatter release];
+	[_workspace release];
 	
 	[super dealloc];
 }
 	CLLocation *currentLocation = _locationManager.location;
 	NSURL *externalBrowserURL = [_locationFormatter googleMapsUrlForLocation:currentLocation];
 
-	[[NSWorkspace sharedWorkspace] openURL:externalBrowserURL];
+	[_workspace openURL:externalBrowserURL];
 }
 
 - (void)locationFormatter:(CoreLocationFormatter *)formatter

WhereIsMyMac-WithUnitTests/Classes/WhereIsMyMacWindowControllerTests.m

 {
 	id _mockLocationManager;
 	id _mockLocationFormatter;
+	id _mockWorkspace;
 	WhereIsMyMacWindowController * _windowController;
 }
 
 	// Setup
 	_mockLocationManager = [OCMockObject mockForClass:[CLLocationManager class]];
 	_mockLocationFormatter = [OCMockObject mockForClass:[CoreLocationFormatter class]];
+	_mockWorkspace = [OCMockObject mockForClass:[NSWorkspace class]];
 	_windowController = [[WhereIsMyMacWindowController alloc]
 						 initWithLocationManager:_mockLocationManager
-						 locationFormatter:_mockLocationFormatter];
+						 locationFormatter:_mockLocationFormatter
+						 workspace:_mockWorkspace];
 }
 
 - (void)tearDown
 	// Verify
 	[_mockLocationManager verify];
 	[_mockLocationFormatter verify];
+	[_mockWorkspace verify];
 	
 	// Teardown
 	[_windowController close];
-	[_windowController release];
+	[_windowController release]; _windowController = nil;
 }
 
 - (void)testOutletConnectionsAfterLoadWindow
 	[_windowController windowDidLoad];
 }
 
+- (void)testOpenInDefaultBrowserActionOpensGoogleMapsUrlInWorkspace
+{
+	// Setup
+	[[_mockLocationManager stub] stopUpdatingLocation];
+	[[[_mockLocationManager stub] andReturn:nil] location];
+	NSURL * dummyUrl = [NSURL URLWithString:@"http://example.com/"];
+	[[[_mockLocationFormatter stub] andReturn:dummyUrl] googleMapsUrlForLocation:nil];
+	[[_mockWorkspace expect] openURL:dummyUrl];
+	
+	// Execute
+	[_windowController openInDefaultBrowser:nil];
+}
+
 - (void)testDeallocStopsLocationManager
 {
 	// Setup