|
| 1 | +// |
| 2 | +// EPMapViewDecoratorTest.m |
| 3 | +// iOSMapExtension |
| 4 | +// |
| 5 | +// Created by epacces on 5/6/13. |
| 6 | +// Copyright (c) 2013 it.reply. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#import "EPMapViewDecoratorTest.h" |
| 10 | +#import "EPMapViewDecorator.h" |
| 11 | +#import <CoreLocation/CoreLocation.h> |
| 12 | +#import "EPPointOfInterest.h" |
| 13 | +#import "YAAnnotationView.h" |
| 14 | + |
| 15 | + |
| 16 | +#define ARC4RANDOM_MAX 0x100000000 |
| 17 | + |
| 18 | +static inline double RAND_IN_RANGE(double min, double max) { |
| 19 | + return min + ((double) arc4random() / ARC4RANDOM_MAX) * (max - min) ; |
| 20 | +} |
| 21 | + |
| 22 | +static inline CLLocationCoordinate2D EPRandomLocationCoordinateMake(void) { |
| 23 | + return CLLocationCoordinate2DMake(RAND_IN_RANGE(-90, 90), RAND_IN_RANGE(-180, 180)); |
| 24 | +} |
| 25 | + |
| 26 | +@interface AnnotationClass : EPPointOfInterest |
| 27 | + |
| 28 | +@property (copy, nonatomic) NSString *name; |
| 29 | +@property (nonatomic, readonly, copy) NSString *title; |
| 30 | + |
| 31 | +@end |
| 32 | + |
| 33 | +@implementation AnnotationClass |
| 34 | + |
| 35 | +- (CLLocationCoordinate2D)coordinate |
| 36 | +{ |
| 37 | + return EPRandomLocationCoordinateMake(); |
| 38 | +} |
| 39 | + |
| 40 | +- (NSString *)title |
| 41 | +{ |
| 42 | + return @"Jesus"; |
| 43 | +} |
| 44 | + |
| 45 | +@end |
| 46 | + |
| 47 | + |
| 48 | +@implementation EPMapViewDecoratorTest { |
| 49 | + EPMapViewDecorator *_mapDecorator; |
| 50 | + MKMapView *_mapView; |
| 51 | + AnnotationClass *_annotation; |
| 52 | + NSMutableArray *_pois; |
| 53 | +} |
| 54 | + |
| 55 | +- (void)setUp |
| 56 | +{ |
| 57 | + _mapDecorator = [[EPMapViewDecorator alloc] init]; |
| 58 | + _mapView = [[MKMapView alloc] init]; |
| 59 | + |
| 60 | + _annotation = [AnnotationClass new]; |
| 61 | + _pois = [[NSMutableArray alloc] initWithCapacity:5]; |
| 62 | + for (int i = 0; i < 5; i++) { |
| 63 | + [_pois addObject:[[EPPointOfInterest alloc] initWithCoordinate:EPRandomLocationCoordinateMake()]]; |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | + |
| 68 | +- (void)testMapViewDecoratorRegisterNonExistentAnnotationForClass |
| 69 | +{ |
| 70 | + [_mapView addAnnotation:_annotation]; |
| 71 | + STAssertThrows([_mapDecorator registerAnnotationViewForClass:[AnnotationClass class] annotationView:@"MKPinPoint"], |
| 72 | + @"should throw exception"); |
| 73 | +} |
| 74 | + |
| 75 | +- (void)testMapViewDecoratorRegisterAnnotationForClass |
| 76 | +{ |
| 77 | + [_mapView addAnnotation:_annotation]; |
| 78 | + [_mapDecorator registerAnnotationViewForClass:[AnnotationClass class] annotationView:@"MKPinAnnotationView"]; |
| 79 | + STAssertEquals([[_mapDecorator mapView:_mapView viewForAnnotation:_annotation] class], [MKPinAnnotationView class], |
| 80 | + @"Annotation class should be pinpoint"); |
| 81 | +} |
| 82 | + |
| 83 | +- (void)testMapViewDecoratorRegisterAnnotationForClassWithConfigurationBlock |
| 84 | +{ |
| 85 | + [_mapDecorator registerAnnotationViewForClass:[AnnotationClass class] annotationView:@"MKPinAnnotationView" |
| 86 | + configBlock:^(MKAnnotationView *av, id<MKAnnotation> annotation){ |
| 87 | + [av setCanShowCallout:YES]; |
| 88 | + }]; |
| 89 | + MKPinAnnotationView *pinAV = (MKPinAnnotationView *)[_mapDecorator mapView:_mapView viewForAnnotation:_annotation]; |
| 90 | + STAssertTrue([pinAV canShowCallout], @"annotation should show callout"); |
| 91 | +} |
| 92 | + |
| 93 | +- (void)testRegisterAnnotationViewForClassWithTraslationBlock |
| 94 | +{ |
| 95 | + [_mapView addAnnotation:_annotation]; |
| 96 | + [_mapDecorator registerAnnotationViewForClass:[AnnotationClass class] |
| 97 | + translationBlock:^NSString * (id<MKAnnotation> annotation) { |
| 98 | + if (![[(AnnotationClass *)annotation name] isEqualToString:@"Jesus"]) |
| 99 | + return @"YAAnnotationView"; |
| 100 | + else |
| 101 | + return @"YAJesusAnnotationView"; |
| 102 | + }]; |
| 103 | + STAssertEquals([[_mapDecorator mapView:_mapView viewForAnnotation:_annotation] class], [YAAnnotationView class], |
| 104 | + @"Annotation class should be YAAnotationView"); |
| 105 | + |
| 106 | + [_annotation setName:@"Jesus"]; |
| 107 | + STAssertEquals([[_mapDecorator mapView:_mapView viewForAnnotation:_annotation] class], [YAJesusAnnotationView class], |
| 108 | + @"Annotation class should be YAAnotationView"); |
| 109 | + |
| 110 | +} |
| 111 | + |
| 112 | + |
| 113 | +- (void)testSetConfigurationBlock |
| 114 | +{ |
| 115 | + [_mapDecorator registerAnnotationViewForClass:[AnnotationClass class] |
| 116 | + translationBlock:^NSString * (id<MKAnnotation> annotation) { |
| 117 | + if (![[(AnnotationClass *)annotation name] isEqualToString:@"Jesus"]) |
| 118 | + return @"YAAnnotationView"; |
| 119 | + else |
| 120 | + return @"YAJesusAnnotationView"; |
| 121 | + }]; |
| 122 | + |
| 123 | + [_mapDecorator setConfigBlockForClass:[AnnotationClass class] |
| 124 | + block:^(MKAnnotationView *av, id<MKAnnotation> annotation){ |
| 125 | + if([av class] == [YAAnnotationView class]) { |
| 126 | + [av setCanShowCallout:YES]; |
| 127 | + } else { |
| 128 | + [av setCanShowCallout:NO]; |
| 129 | + } |
| 130 | + }]; |
| 131 | + |
| 132 | + STAssertEquals([[_mapDecorator mapView:_mapView viewForAnnotation:_annotation] class], [YAAnnotationView class], |
| 133 | + @"Annotation class should be YAAnotationView"); |
| 134 | + YAAnnotationView *av = (YAAnnotationView *)[_mapDecorator mapView:_mapView viewForAnnotation:_annotation]; |
| 135 | + STAssertTrue([av canShowCallout], @"annotation view should show callout"); |
| 136 | + |
| 137 | + [_annotation setName:@"Jesus"]; |
| 138 | + STAssertEquals([[_mapDecorator mapView:_mapView viewForAnnotation:_annotation] class], [YAJesusAnnotationView class], |
| 139 | + @"Annotation class should be YAAnotationView"); |
| 140 | + YAJesusAnnotationView *jav = (YAJesusAnnotationView *)[_mapDecorator mapView:_mapView viewForAnnotation:_annotation]; |
| 141 | + STAssertFalse([jav canShowCallout], @"annotation view should not show callout"); |
| 142 | + |
| 143 | + STAssertEquals((NSObject *)[_mapDecorator mapView:_mapView viewForAnnotation:_pois[2]], (NSObject *)nil, @"Annotation class should be nil"); |
| 144 | + |
| 145 | +} |
| 146 | + |
| 147 | + |
| 148 | +- (void)testMapViewDecoratorConfigBlockForAllClasses |
| 149 | +{ |
| 150 | + [_mapDecorator setTranslationBlockForAllClasses:^NSString * (id<MKAnnotation> annotation) { |
| 151 | + if (![annotation respondsToSelector:@selector(title)]) |
| 152 | + return @"YAAnnotationView"; |
| 153 | + else |
| 154 | + return @"YAJesusAnnotationView"; |
| 155 | + }]; |
| 156 | + |
| 157 | + [_mapDecorator setConfigBlockForAllClasses: ^(MKAnnotationView *annotationView, id<MKAnnotation> annotation) { |
| 158 | + if([annotationView class] == [YAAnnotationView class]) { |
| 159 | + [annotationView setCanShowCallout:YES]; |
| 160 | + } else { |
| 161 | + [annotationView setCanShowCallout:NO]; |
| 162 | + } |
| 163 | + }]; |
| 164 | + |
| 165 | + STAssertEquals([[_mapDecorator mapView:_mapView viewForAnnotation:_pois[2]] class], [YAAnnotationView class], |
| 166 | + @"Annotation class should be YAAnotationView"); |
| 167 | + YAAnnotationView *av = (YAAnnotationView *)[_mapDecorator mapView:_mapView viewForAnnotation:_pois[2]]; |
| 168 | + STAssertTrue([av canShowCallout], @"annotation view should show callout"); |
| 169 | + |
| 170 | + STAssertEquals([[_mapDecorator mapView:_mapView viewForAnnotation:_annotation] class], [YAJesusAnnotationView class], |
| 171 | + @"Annotation class should be YAAnotationView"); |
| 172 | + YAJesusAnnotationView *jav = (YAJesusAnnotationView *)[_mapDecorator mapView:_mapView viewForAnnotation:_annotation]; |
| 173 | + STAssertFalse([jav canShowCallout], @"annotation view should not show callout"); |
| 174 | + |
| 175 | +} |
| 176 | + |
| 177 | + |
| 178 | +@end |
0 commit comments