Commits

Dave Dribin  committed d020290

Add testLocationFormatterDelegateUpdatesUI and convert tabs to spaces

  • Participants
  • Parent commits ebb5992

Comments (0)

Files changed (6)

File WhereIsMyMac-WithUnitTests/Classes/CoreLocationFormatter.h

 
 - (void)locationFormatter:(CoreLocationFormatter *)formatter
  didUpdateFormattedString:(NSString *)formattedString
-			locationLabel:(NSString *)locationLabel
-		   accuractyLabel:(NSString *)accuracyLabel;
+            locationLabel:(NSString *)locationLabel
+           accuractyLabel:(NSString *)accuracyLabel;
 
 @end
 
 @interface CoreLocationFormatter : NSObject <CLLocationManagerDelegate>
 {
-	id<CoreLocationFormatterDelegate> _delegate;
-	NSString * _formatString;
+    id<CoreLocationFormatterDelegate> _delegate;
+    NSString * _formatString;
 }
 
 @property (nonatomic, assign, readwrite) id<CoreLocationFormatterDelegate> delegate;
 @property (nonatomic, copy, readonly) NSString * formatString;
 
 - (id)initWithDelegate:(id<CoreLocationFormatterDelegate>)delegate
-		  formatString:(NSString *)htmlFormatString;
-
-- (void)locationManager:(CLLocationManager *)manager
-	didUpdateToLocation:(CLLocation *)newLocation
-		   fromLocation:(CLLocation *)oldLocation;
-
-- (void)locationManager:(CLLocationManager *)manager
-	   didFailWithError:(NSError *)error;
+          formatString:(NSString *)htmlFormatString;
 
 - (NSURL *)googleMapsUrlForLocation:(CLLocation *)currentLocation;
 
+- (void)locationManager:(CLLocationManager *)manager
+    didUpdateToLocation:(CLLocation *)newLocation
+           fromLocation:(CLLocation *)oldLocation;
+
+- (void)locationManager:(CLLocationManager *)manager
+       didFailWithError:(NSError *)error;
+
 @end

File WhereIsMyMac-WithUnitTests/Classes/CoreLocationFormatter.m

 
 + (double)latitudeRangeForLocation:(CLLocation *)aLocation
 {
-	const double M = 6367000.0; // approximate average meridional radius of curvature of earth
-	const double metersToLatitude = 1.0 / ((M_PI / 180.0) * M);
-	const double accuracyToWindowScale = 2.0;
-	
-	return aLocation.horizontalAccuracy * metersToLatitude * accuracyToWindowScale;
+    const double M = 6367000.0; // approximate average meridional radius of curvature of earth
+    const double metersToLatitude = 1.0 / ((M_PI / 180.0) * M);
+    const double accuracyToWindowScale = 2.0;
+    
+    return aLocation.horizontalAccuracy * metersToLatitude * accuracyToWindowScale;
 }
 
 + (double)longitudeRangeForLocation:(CLLocation *)aLocation
 {
-	double latitudeRange =
-	[self latitudeRangeForLocation:aLocation];
-	
-	return latitudeRange * cos(aLocation.coordinate.latitude * M_PI / 180.0);
+    double latitudeRange =
+    [self latitudeRangeForLocation:aLocation];
+    
+    return latitudeRange * cos(aLocation.coordinate.latitude * M_PI / 180.0);
 }
 
 - (id)initWithDelegate:(id<CoreLocationFormatterDelegate>)delegate
-		  formatString:(NSString *)htmlFormatString;
+          formatString:(NSString *)htmlFormatString;
 {
-	self = [super init];
-	if (self == nil) {
-		return nil;
-	}
-	
-	_delegate = delegate;
-	_formatString = [htmlFormatString copy];
-	
-	return self;
+    self = [super init];
+    if (self == nil) {
+        return nil;
+    }
+    
+    _delegate = delegate;
+    _formatString = [htmlFormatString copy];
+    
+    return self;
 }
 
 - (void)dealloc
 {
-	[_formatString release];
-	[super dealloc];
+    [_formatString release];
+    [super dealloc];
 }
 
 - (void)locationManager:(CLLocationManager *)manager
-	didUpdateToLocation:(CLLocation *)newLocation
-		   fromLocation:(CLLocation *)oldLocation;
+    didUpdateToLocation:(CLLocation *)newLocation
+           fromLocation:(CLLocation *)oldLocation;
 {
-	// Ignore updates where nothing we care about changed
-	if (newLocation.coordinate.longitude == oldLocation.coordinate.longitude &&
-		newLocation.coordinate.latitude == oldLocation.coordinate.latitude &&
-		newLocation.horizontalAccuracy == oldLocation.horizontalAccuracy)
-	{
-		return;
-	}
-	
-	NSString * formattedString = [NSString stringWithFormat:_formatString,
-								  newLocation.coordinate.latitude,
-								  newLocation.coordinate.longitude,
-								  [[self class] latitudeRangeForLocation:newLocation],
-								  [[self class] longitudeRangeForLocation:newLocation]];
-	
-	NSString * locationLabel = [NSString stringWithFormat:@"%f, %f",
-								newLocation.coordinate.latitude, newLocation.coordinate.longitude];
-	NSString * accuracyLabel = [NSString stringWithFormat:@"%f",
-								newLocation.horizontalAccuracy];
+    // Ignore updates where nothing we care about changed
+    if (newLocation.coordinate.longitude == oldLocation.coordinate.longitude &&
+        newLocation.coordinate.latitude == oldLocation.coordinate.latitude &&
+        newLocation.horizontalAccuracy == oldLocation.horizontalAccuracy)
+    {
+        return;
+    }
+    
+    NSString * formattedString = [NSString stringWithFormat:_formatString,
+                                  newLocation.coordinate.latitude,
+                                  newLocation.coordinate.longitude,
+                                  [[self class] latitudeRangeForLocation:newLocation],
+                                  [[self class] longitudeRangeForLocation:newLocation]];
+    
+    NSString * locationLabel = [NSString stringWithFormat:@"%f, %f",
+                                newLocation.coordinate.latitude, newLocation.coordinate.longitude];
+    NSString * accuracyLabel = [NSString stringWithFormat:@"%f",
+                                newLocation.horizontalAccuracy];
 
-	[_delegate locationFormatter:self
-		didUpdateFormattedString:formattedString
-				   locationLabel:locationLabel
-				  accuractyLabel:accuracyLabel];
+    [_delegate locationFormatter:self
+        didUpdateFormattedString:formattedString
+                   locationLabel:locationLabel
+                  accuractyLabel:accuracyLabel];
 }
 
 - (void)locationManager:(CLLocationManager *)manager
-	   didFailWithError:(NSError *)error;
+       didFailWithError:(NSError *)error;
 {
-	NSString * formattedString = [NSString stringWithFormat:
-								  NSLocalizedString(@"Location manager failed with error: %@", nil),
-								  [error localizedDescription]];
-	[_delegate locationFormatter:self
-		didUpdateFormattedString:formattedString
-				   locationLabel:@""
-				  accuractyLabel:@""];
+    NSString * formattedString = [NSString stringWithFormat:
+                                  NSLocalizedString(@"Location manager failed with error: %@", nil),
+                                  [error localizedDescription]];
+    [_delegate locationFormatter:self
+        didUpdateFormattedString:formattedString
+                   locationLabel:@""
+                  accuractyLabel:@""];
 }
 
 - (NSURL *)googleMapsUrlForLocation:(CLLocation *)location;
 {
-	NSURL * googleMapsUrl = [NSURL URLWithString:[NSString stringWithFormat:
-												  @"http://maps.google.com/maps?ll=%f,%f&amp;spn=%f,%f",
-												  location.coordinate.latitude,
-												  location.coordinate.longitude,
-												  [[self class] latitudeRangeForLocation:location],
-												  [[self class] longitudeRangeForLocation:location]]];
-	return googleMapsUrl;
+    NSURL * googleMapsUrl = [NSURL URLWithString:[NSString stringWithFormat:
+                                                  @"http://maps.google.com/maps?ll=%f,%f&amp;spn=%f,%f",
+                                                  location.coordinate.latitude,
+                                                  location.coordinate.longitude,
+                                                  [[self class] latitudeRangeForLocation:location],
+                                                  [[self class] longitudeRangeForLocation:location]]];
+    return googleMapsUrl;
 }
 
 @end

File WhereIsMyMac-WithUnitTests/Classes/CoreLocationFormatterTest.m

 
 @interface CoreLocationFormatterTest : SenTestCase
 {
-	id _mockDelegate;
-	CoreLocationFormatter * _formatter;
+    id _mockDelegate;
+    CoreLocationFormatter * _formatter;
 }
 @end
 
 #pragma mark Helpers
 
 - (CLLocation *)makeLocationWithLatitude:(CLLocationDegrees)latitude
-							   longitude:(CLLocationDegrees)longitutde
+                               longitude:(CLLocationDegrees)longitutde
 {
-	CLLocationCoordinate2D coord = {.latitude = latitude, .longitude = longitutde};
-	CLLocation *location = [[CLLocation alloc]
-							initWithCoordinate:coord
-							altitude:0
-							horizontalAccuracy:kCLLocationAccuracyBest
-							verticalAccuracy:kCLLocationAccuracyHundredMeters
-							timestamp:[NSDate date]];
-	
-	return [location autorelease];
+    CLLocationCoordinate2D coord = {.latitude = latitude, .longitude = longitutde};
+    CLLocation *location = [[CLLocation alloc]
+                            initWithCoordinate:coord
+                            altitude:0
+                            horizontalAccuracy:kCLLocationAccuracyBest
+                            verticalAccuracy:kCLLocationAccuracyHundredMeters
+                            timestamp:[NSDate date]];
+    
+    return [location autorelease];
 }
 
 - (NSError *)makeFakeErrorWithDescription:(NSString *)localizedErrorDescription
 {
-	NSError * error = [NSError
-					   errorWithDomain:@"TestDomain"
-					   code:1234
-					   userInfo:
-					   [NSDictionary
-						dictionaryWithObject:localizedErrorDescription
-						forKey:NSLocalizedDescriptionKey]];
-	return error;
+    NSError * error = [NSError
+                       errorWithDomain:@"TestDomain"
+                       code:1234
+                       userInfo:
+                       [NSDictionary
+                        dictionaryWithObject:localizedErrorDescription
+                        forKey:NSLocalizedDescriptionKey]];
+    return error;
 }
 
 #pragma mark -
 
 - (void)setUp
 {
-	// Setup
-	_mockDelegate = [OCMockObject mockForProtocol:@protocol(CoreLocationFormatterDelegate)];
-	_formatter = [[CoreLocationFormatter alloc] initWithDelegate:_mockDelegate
-													formatString:@"ll=%f,%f spn=%f,%f"];
+    // Setup
+    _mockDelegate = [OCMockObject mockForProtocol:@protocol(CoreLocationFormatterDelegate)];
+    _formatter = [[CoreLocationFormatter alloc] initWithDelegate:_mockDelegate
+                                                    formatString:@"ll=%f,%f spn=%f,%f"];
 }
 
 - (void)tearDown
 {
-	// Verify
-	[_mockDelegate verify];
-	
-	// Teardown
-	[_formatter release]; _formatter = nil;
+    // Verify
+    [_mockDelegate verify];
+    
+    // Teardown
+    [_formatter release]; _formatter = nil;
 }
 
 #pragma mark -
 
 - (void)testUpdateToNewLocationSendsUpdateToDelegate
 {
-	// Setup
-	CLLocation * location = [self makeLocationWithLatitude:-37.80996889 longitude:144.96326388];
-	[[_mockDelegate expect] locationFormatter:_formatter
-					 didUpdateFormattedString:@"ll=-37.809969,144.963264 spn=-0.000018,-0.000014"
-								locationLabel:@"-37.809969, 144.963264"
-							   accuractyLabel:[NSString stringWithFormat:@"%f", kCLLocationAccuracyBest]];
-	
-	// Execute
-	[_formatter locationManager:nil didUpdateToLocation:location fromLocation:nil];
+    // Setup
+    CLLocation * location = [self makeLocationWithLatitude:-37.80996889 longitude:144.96326388];
+    [[_mockDelegate expect] locationFormatter:_formatter
+                     didUpdateFormattedString:@"ll=-37.809969,144.963264 spn=-0.000018,-0.000014"
+                                locationLabel:@"-37.809969, 144.963264"
+                               accuractyLabel:[NSString stringWithFormat:@"%f", kCLLocationAccuracyBest]];
+    
+    // Execute
+    [_formatter locationManager:nil didUpdateToLocation:location fromLocation:nil];
 }
 
 - (void)testUpdateToSameLocationDoesNotSendUpdateToDelegate
 {
-	// Setup
-	CLLocation * location = [self makeLocationWithLatitude:-37.80996889 longitude:144.96326388];
-	
-	// Execute
-	[_formatter locationManager:nil didUpdateToLocation:location fromLocation:location];
+    // Setup
+    CLLocation * location = [self makeLocationWithLatitude:-37.80996889 longitude:144.96326388];
+    
+    // Execute
+    [_formatter locationManager:nil didUpdateToLocation:location fromLocation:location];
 }
 
 - (void)testFailedUpdateSendsUpdateToDelegate
 {
-	// Setup
-	NSError * error = [self makeFakeErrorWithDescription:@"Some error description"];
-	[[_mockDelegate expect] locationFormatter:_formatter
-					 didUpdateFormattedString:@"Location manager failed with error: Some error description"
-								locationLabel:@""
-							   accuractyLabel:@""];
-	
+    // Setup
+    NSError * error = [self makeFakeErrorWithDescription:@"Some error description"];
+    [[_mockDelegate expect] locationFormatter:_formatter
+                     didUpdateFormattedString:@"Location manager failed with error: Some error description"
+                                locationLabel:@""
+                               accuractyLabel:@""];
+    
 
-	// Execute
-	[_formatter locationManager:nil didFailWithError:error];
+    // Execute
+    [_formatter locationManager:nil didFailWithError:error];
 }
 
 - (void)testGoogleMapsUrl
 {
-	// Setup
-	CLLocation * location = [self makeLocationWithLatitude:-37.80996889 longitude:144.96326388];
-	
-	// Execute
-	NSURL * url = [_formatter googleMapsUrlForLocation:location];
-	
-	// Verify
-	NSURL * expectedUrl = [NSURL URLWithString:
-						   @"http://maps.google.com/maps?ll=-37.809969,144.963264&amp;spn=-0.000018,-0.000014"];
-	STAssertEqualObjects(url, expectedUrl, nil);
+    // Setup
+    CLLocation * location = [self makeLocationWithLatitude:-37.80996889 longitude:144.96326388];
+    
+    // Execute
+    NSURL * url = [_formatter googleMapsUrlForLocation:location];
+    
+    // Verify
+    NSURL * expectedUrl = [NSURL URLWithString:
+                           @"http://maps.google.com/maps?ll=-37.809969,144.963264&amp;spn=-0.000018,-0.000014"];
+    STAssertEqualObjects(url, expectedUrl, nil);
 }
 
 @end

File WhereIsMyMac-WithUnitTests/Classes/WhereIsMyMacWindowController.h

 
 @interface WhereIsMyMacWindowController : NSWindowController <CoreLocationFormatterDelegate>
 {
-	WebView *webView;
-	NSTextField *locationLabel;
-	NSTextField *accuracyLabel;
-	NSButton *openInBrowserButton;
-	
-	CLLocationManager * _locationManager;
-	CoreLocationFormatter * _locationFormatter;
-	NSWorkspace * _workspace;
+    WebView *webView;
+    NSTextField *locationLabel;
+    NSTextField *accuracyLabel;
+    NSButton *openInBrowserButton;
+    
+    CLLocationManager * _locationManager;
+    CoreLocationFormatter * _locationFormatter;
+    NSWorkspace * _workspace;
 }
 
 @property (assign) IBOutlet WebView *webView;
 
 - (id)init;
 - (id)initWithLocationManager:(CLLocationManager *)locationManager
-			locationFormatter:(CoreLocationFormatter *)locationFormatter
-					workspace:(NSWorkspace *)workspace;
+            locationFormatter:(CoreLocationFormatter *)locationFormatter
+                    workspace:(NSWorkspace *)workspace;
 
 - (IBAction)openInDefaultBrowser:(id)sender;
 
 - (void)locationFormatter:(CoreLocationFormatter *)formatter
  didUpdateFormattedString:(NSString *)formattedString
-			locationLabel:(NSString *)locationLabel
-		   accuractyLabel:(NSString *)accuracyLabel;
+            locationLabel:(NSString *)locationLabel
+           accuractyLabel:(NSString *)accuracyLabel;
 
 @end

File WhereIsMyMac-WithUnitTests/Classes/WhereIsMyMacWindowController.m

 
 - (id)init
 {
-	CLLocationManager * locationManager = [[[CLLocationManager alloc] init] autorelease];
+    CLLocationManager * locationManager = [[[CLLocationManager alloc] init] autorelease];
 
-	NSString * formatString = [NSString 
-							   stringWithContentsOfFile:
-							   [[NSBundle bundleForClass:[self class]]
-								pathForResource:@"HTMLFormatString" ofType:@"html"]
-							   encoding:NSUTF8StringEncoding
-							   error:NULL];
-	CoreLocationFormatter * locationFormatter =
-		[[[CoreLocationFormatter alloc] initWithDelegate:self
-											formatString:formatString] autorelease];
-	return [self initWithLocationManager:locationManager
-					   locationFormatter:locationFormatter
-							   workspace:[NSWorkspace sharedWorkspace]];
+    NSString * formatString = [NSString 
+                               stringWithContentsOfFile:
+                               [[NSBundle bundleForClass:[self class]]
+                                pathForResource:@"HTMLFormatString" ofType:@"html"]
+                               encoding:NSUTF8StringEncoding
+                               error:NULL];
+    CoreLocationFormatter * locationFormatter =
+        [[[CoreLocationFormatter alloc] initWithDelegate:self
+                                            formatString:formatString] autorelease];
+    return [self initWithLocationManager:locationManager
+                       locationFormatter:locationFormatter
+                               workspace:[NSWorkspace sharedWorkspace]];
 }
 
 - (id)initWithLocationManager:(CLLocationManager *)locationManager
-			locationFormatter:(CoreLocationFormatter *)locationFormatter
-					workspace:(NSWorkspace *)workspace;
+            locationFormatter:(CoreLocationFormatter *)locationFormatter
+                    workspace:(NSWorkspace *)workspace;
 {
-	self = [super init];
-	if (self == nil)
-		return nil;
-	
-	_locationManager = [locationManager retain];
-	_locationFormatter = [locationFormatter retain];
-	_workspace = [workspace retain];
-	
-	return self;
+    self = [super init];
+    if (self == nil)
+        return nil;
+    
+    _locationManager = [locationManager retain];
+    _locationFormatter = [locationFormatter retain];
+    _workspace = [workspace retain];
+    
+    return self;
 }
 
 - (void)dealloc
 {
-	[_locationManager release];
-	[_locationFormatter release];
-	[_workspace release];
-	
-	[super dealloc];
+    [_locationManager release];
+    [_locationFormatter release];
+    [_workspace release];
+    
+    [super dealloc];
 }
 
 - (void)close
 {
-	[_locationManager stopUpdatingLocation];
+    [_locationManager stopUpdatingLocation];
 }
 
 - (void)windowDidLoad
 {
-	_locationManager.delegate = _locationFormatter;
-	[_locationManager startUpdatingLocation];
+    _locationManager.delegate = _locationFormatter;
+    [_locationManager startUpdatingLocation];
 }
 
 - (NSString *)windowNibName
 {
-	return @"WhereIsMyMacWindow";
+    return @"WhereIsMyMacWindow";
 }
 
 - (IBAction)openInDefaultBrowser:(id)sender
 {
-	CLLocation *currentLocation = _locationManager.location;
-	NSURL *externalBrowserURL = [_locationFormatter googleMapsUrlForLocation:currentLocation];
+    CLLocation *currentLocation = _locationManager.location;
+    NSURL *externalBrowserURL = [_locationFormatter googleMapsUrlForLocation:currentLocation];
 
-	[_workspace openURL:externalBrowserURL];
+    [_workspace openURL:externalBrowserURL];
 }
 
 - (void)locationFormatter:(CoreLocationFormatter *)formatter
  didUpdateFormattedString:(NSString *)formattedString_
-			locationLabel:(NSString *)locationLabel_
-		   accuractyLabel:(NSString *)accuracyLabel_;
+            locationLabel:(NSString *)locationLabel_
+           accuractyLabel:(NSString *)accuracyLabel_;
 {
-	[[webView mainFrame] loadHTMLString:formattedString_ baseURL:nil];
-	[locationLabel setStringValue:locationLabel_];
-	[accuracyLabel setStringValue:accuracyLabel_];
+    [[webView mainFrame] loadHTMLString:formattedString_ baseURL:nil];
+    [locationLabel setStringValue:locationLabel_];
+    [accuracyLabel setStringValue:accuracyLabel_];
 }
 
 @end

File WhereIsMyMac-WithUnitTests/Classes/WhereIsMyMacWindowControllerTests.m

 	[_windowController windowDidLoad];
 }
 
+- (void)testLocationFormatterDelegateUpdatesUI
+{
+	// Setup
+	id mockWebView = [OCMockObject mockForClass:[WebView class]];
+	id mockWebFrame = [OCMockObject mockForClass:[WebFrame class]];
+	id mockLocationLabel = [OCMockObject mockForClass:[NSTextField class]];
+	id mockAccuracyLabel = [OCMockObject mockForClass:[NSTextField class]];
+	
+	_windowController.webView = mockWebView;
+	_windowController.locationLabel = mockLocationLabel;
+	_windowController.accuracyLabel = mockAccuracyLabel;
+	
+	[[[mockWebView stub] andReturn:mockWebFrame] mainFrame];
+	[[mockWebFrame expect] loadHTMLString:@"html string" baseURL:nil];
+	[[mockLocationLabel expect] setStringValue:@"location"];
+	[[mockAccuracyLabel expect] setStringValue:@"accuracy"];
+	
+	// Execute
+	[_windowController locationFormatter:_mockLocationFormatter
+				didUpdateFormattedString:@"html string"
+						   locationLabel:@"location"
+						  accuractyLabel:@"accuracy"];
+	
+	// Verify
+	[mockWebFrame verify];
+	[mockLocationLabel verify];
+	[mockAccuracyLabel verify];
+}
+
 - (void)testOpenInDefaultBrowserActionOpensGoogleMapsUrlInWorkspace
 {
 	// Setup