Authored by 周蓉君

将网络状态监控和定位服务调用封装到YHAssemblyAssistant中。更新相关测试用例。Review by 阿瑟。

... ... @@ -19,7 +19,7 @@
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1</string>
<string>1.0.0.20150427</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>NSLocationAlwaysUsageDescription</key>
... ...
... ... @@ -10,13 +10,18 @@
#import <CoreLocation/CoreLocation.h>
#import "YHNetworkAssistant.h"
#import "YHAnalyItemData.h"
//#import "YHDevice.h"
//#import "YHStatus.h"
//阀值
#define kMaxLocalEventsCount 3000 // 本地持久化event最大条数
#define kMaxLocalRecoderFileSize (1024 * 300) // 本地持久化文件大小(300KB)
extern NSString * const kNetWorkStatusUnknow;
extern NSString * const kNetWorkStatusWIFI;
extern NSString * const kNetWorkStatus2G;
extern NSString * const kNetWorkStatus3G;
extern NSString * const kNetWorkStatus4G;
extern NSString * const JsonKeyDataTypeDevice;
extern NSString * const JsonKeyDataTypeStatus;
extern NSString * const JsonKeyDataTypeEvents;
... ... @@ -26,7 +31,7 @@ extern NSString * const JsonKeyDataTypeErrors;
用于组装数据
*/
@interface YHAssemblyAssistant : NSObject
@interface YHAssemblyAssistant : NSObject <CLLocationManagerDelegate>
+ (instancetype)sharedInstance;
... ... @@ -40,14 +45,8 @@ extern NSString * const JsonKeyDataTypeErrors;
- (void)prepareImmediUploadDic:(NSString *)appId sessionId:(NSString *)sessionId;
- (void)updateImmediUploadDic;
- (void)uploadDiskData;
- (void)uploadImmedilyWithEvent:(YHAnalyItemData *)itemData;
- (BOOL)updateLocation:(CLLocation *)location;
- (BOOL)updateNetWorkStatus:(NSString *)status;
@end
... ...
... ... @@ -6,6 +6,9 @@
// Copyright (c) 2015年 YOHO. All rights reserved.
//
#import <CoreTelephony/CTCarrier.h>
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import "Reachability.h"
#import "YHAssemblyAssistant.h"
//#import "YHErrorFactory.h"
//#import "YHEventFactory.h"
... ... @@ -21,6 +24,12 @@
#define kPathLogSystem [kPathCaches stringByAppendingPathComponent:@"YHLogSystem"]
#define kYASApiMethod @"yas_mobile"
NSString * const kNetWorkStatusUnknow = @"0";
NSString * const kNetWorkStatusWIFI = @"1";
NSString * const kNetWorkStatus2G = @"2";
NSString * const kNetWorkStatus3G = @"3";
NSString * const kNetWorkStatus4G = @"4";
NSString * const JsonKeyDataTypeDevice = @"device";
NSString * const JsonKeyDataTypeStatus = @"status";
NSString * const JsonKeyDataTypeEvents = @"events";
... ... @@ -39,18 +48,58 @@ static dispatch_queue_t persisting_queue() {
@interface YHAssemblyAssistant ()
/**
* 定位服务
*/
@property (strong, nonatomic) CLLocationManager *locationManager;
/**
* 设备信息
*/
@property (strong, nonatomic) YHDevice *device;
/**
* 当前状态信息
*/
@property (strong, nonatomic) YHStatus *currentStatus;
@property (strong, nonatomic) NSMutableDictionary *immediUploadItemDic; // 用于立即发送策略下(device信息是相同的)
@property (assign, nonatomic) NSUInteger allEventsCount; // 本地持久化event的条数
/**
* 用于立即发送策略下(device信息是相同的)
*/
@property (strong, nonatomic) NSMutableDictionary *immediUploadItemDic;
/**
* 本地持久化event的条数
*/
@property (assign, nonatomic) NSUInteger allEventsCount;
/**
* 日志文件存储路径
*/
@property (strong, nonatomic) NSString *eventFilePath;
/**
* 日志文件名称
*/
@property (strong, nonatomic) NSString *eventFileName;
/**
* 文件管理服务
*/
@property (strong, nonatomic) NSFileManager *fileManager;
// 启动定位服务
- (void)startLocationService;
// 开启网络状态监测
- (void)startObserverNetworkReachabilityStatus;
// 将一个事件写入到文件
- (BOOL)writeEventToFile:(YHEvent *)eventData;
// 将一个错误写入到文件
- (BOOL)writeErrorToFile:(YHError *)errorData;
// 当触发预设的条件时,自动上传数据
- (void)autoUploadData;
// 删除本地持久化文件
- (void)removeLocalFile;
// 更新网络状态
- (BOOL)updateNetWorkStatus:(NSString *)status;
// 更新位置信息
- (BOOL)updateLocation:(CLLocation *)location;
// 更新立即上传信息
- (void)updateImmediUploadDic;
@end
... ... @@ -110,6 +159,18 @@ static dispatch_queue_t persisting_queue() {
return _eventFileName;
}
- (CLLocationManager *)locationManager
{
if (!_locationManager) {
_locationManager = [[CLLocationManager alloc]init];
_locationManager.delegate = self;
_locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 精度越高,越费电,根据需要设定
_locationManager.distanceFilter = 1000; // 位置移动1000米,发一次更新消息
}
return _locationManager;
}
#pragma mark - public method
- (BOOL)canPersisting
... ... @@ -141,21 +202,10 @@ static dispatch_queue_t persisting_queue() {
// 设置status
[self.immediUploadItemDic setObject: [[NSMutableArray alloc]initWithObjects:self.currentStatus.jsonDictionary, nil] forKey:JsonKeyDataTypeStatus];
}
- (void)updateImmediUploadDic
{
NSMutableArray *allStatus = [self.immediUploadItemDic objectForKey:JsonKeyDataTypeStatus];
if ([allStatus count]) {
[allStatus replaceObjectAtIndex:0 withObject:self.currentStatus.jsonDictionary];
[self.immediUploadItemDic setObject:allStatus forKey:JsonKeyDataTypeStatus];
} else {
NSException *exception = [NSException exceptionWithName:NSGenericException
reason:@"appId and sessionId do not be initialized. Please use prepareImmediUploadDic:sessionId: first."
userInfo:nil];
[exception raise];
}
[self startObserverNetworkReachabilityStatus];
[self startLocationService];
}
#pragma mark - 持久化数据
... ... @@ -266,6 +316,93 @@ static dispatch_queue_t persisting_queue() {
}
}
#pragma mark - private method
// 添加网络监测回调block
- (void)startObserverNetworkReachabilityStatus
{
/**
CTRadioAccessTechnologyGPRS - 2G
CTRadioAccessTechnologyEdge - 2G (sometimes called 2.5G)
CTRadioAccessTechnologyWCDMA - 3G
CTRadioAccessTechnologyHSDPA - 3G (sometimes called 3.5G)
CTRadioAccessTechnologyHSUPA - 3G
CTRadioAccessTechnologyCDMA1x - 2G
CTRadioAccessTechnologyCDMAEVDORev0 - 3G
CTRadioAccessTechnologyCDMAEVDORevA - 3G
CTRadioAccessTechnologyCDMAEVDORevB - 3G
CTRadioAccessTechnologyeHRPD - 3G (or 3.5 - eHRPD is to allow migration from CDMA EVDO to LTE)
CTRadioAccessTechnologyLTE - 4G (allowed to be called 4G by the ITU as mentioend above)
*/
[[YHNetworkAssistant sharedInstance] addReachabilityChangedHandler:^(AFNetworkReachabilityStatus networkStatus) {
NSString *net = kNetWorkStatusUnknow;
CTTelephonyNetworkInfo *netInfo = [[CTTelephonyNetworkInfo alloc] init];
switch (networkStatus) {
case AFNetworkReachabilityStatusUnknown:
case AFNetworkReachabilityStatusNotReachable:
net = kNetWorkStatusUnknow;
break;
case AFNetworkReachabilityStatusReachableViaWWAN:
{
if (NSFoundationVersionNumber >= NSFoundationVersionNumber_iOS_7_0) {
if ([netInfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyGPRS]|| [netInfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyEdge]) {
net = kNetWorkStatus2G;
} else if([netInfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyWCDMA]|| [netInfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyHSDPA] || [netInfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyHSUPA]|| [netInfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyeHRPD]) {
net = kNetWorkStatus3G;
} else if([netInfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyLTE]) {
net = kNetWorkStatus4G;
} else {
net = kNetWorkStatusUnknow;
}
} else {
net = kNetWorkStatusUnknow;
}
}
break;
case AFNetworkReachabilityStatusReachableViaWiFi:
net = kNetWorkStatusWIFI;
break;
default:
break;
}
if ([self updateNetWorkStatus:net]) {
[self updateImmediUploadDic];
}
}];
}
// 启动定位服务
- (void)startLocationService
{
//判断用户定位服务是否开启
if ([CLLocationManager locationServicesEnabled]) {
[self.locationManager startUpdatingLocation];
} else {
//不能定位用户的位置
//1.提醒用户检查当前的网络状况
//2.提醒用户打开定位开关
}
}
// 更新网络状态
- (BOOL)updateNetWorkStatus:(NSString *)status
{
if (status == nil || [status length] == 0) {
return NO;
}
if ([status isEqualToString:self.currentStatus.net]) {
return NO;
}
self.currentStatus.net = status;
return YES;
}
- (BOOL)updateLocation:(CLLocation *)location
{
if (location == nil || ![location isKindOfClass:[CLLocation class]]) {
... ... @@ -294,22 +431,21 @@ static dispatch_queue_t persisting_queue() {
return YES;
}
- (BOOL)updateNetWorkStatus:(NSString *)status
// 更新立即上传信息
- (void)updateImmediUploadDic
{
if (status == nil || [status length] == 0) {
return NO;
}
if ([status isEqualToString:self.currentStatus.net]) {
return NO;
NSMutableArray *allStatus = [self.immediUploadItemDic objectForKey:JsonKeyDataTypeStatus];
if ([allStatus count]) {
[allStatus replaceObjectAtIndex:0 withObject:self.currentStatus.jsonDictionary];
[self.immediUploadItemDic setObject:allStatus forKey:JsonKeyDataTypeStatus];
} else {
NSException *exception = [NSException exceptionWithName:NSGenericException
reason:@"appId and sessionId do not be initialized. Please use prepareImmediUploadDic:sessionId: first."
userInfo:nil];
[exception raise];
}
self.currentStatus.net = status;
return YES;
}
#pragma mark - private method
// 将一个事件写入到文件
- (BOOL)writeEventToFile:(YHEvent *)event
{
... ... @@ -452,4 +588,51 @@ static dispatch_queue_t persisting_queue() {
self.allEventsCount = 0;
}
#pragma mark - CLLocationManagerDelegate
// 返回定位服务认证状态的委托方法
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
switch (status) {
case kCLAuthorizationStatusNotDetermined: {
if ([manager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
[manager requestAlwaysAuthorization];
}
}
break;
case kCLAuthorizationStatusRestricted:
//定位服务无法使用,请开启手机定位服务
break;
case kCLAuthorizationStatusDenied:
//程序定位服务未开启, 您需要去设置界面启用我们App定位服务
break;
default:
break;
}
}
//协议中的方法,作用是每当位置发生更新时会调用的委托方法
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
//locations数组里边存放的是CLLocation对象,一个CLLocation对象就代表着一个位置
CLLocation *newLocation = [locations firstObject];
if ([self updateLocation:newLocation]) {
[self updateImmediUploadDic];
}
}
//当位置获取或更新失败会调用的方法
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSString *errorMsg = nil;
if ([error code] == kCLErrorDenied) {
errorMsg = @"定位功能访问被拒绝";
}
if ([error code] == kCLErrorLocationUnknown) {
errorMsg = @"获取位置信息失败";
}
NSLog(@"%@", errorMsg);
}
@end
... ...
... ... @@ -7,7 +7,6 @@
//
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
#import "YHError.h"
#import "YHEvent.h"
... ... @@ -23,7 +22,7 @@ typedef enum _YHLogStrategy {
LogStrategyImmedi // 立即发送
} YHLogStrategy;
@interface YH_Analytics : NSObject <CLLocationManagerDelegate>
@interface YH_Analytics : NSObject
/**
* 获取统计对象的实例
*/
... ...
... ... @@ -6,34 +6,20 @@
// Copyright (c) 2015年 YOHO. All rights reserved.
//
#import <CoreTelephony/CTCarrier.h>
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
//#import "YHErrorFactory.h"
//#import "YHEventFactory.h"
//#import "YHDeviceFactory.h"
//#import "YHStatusFactory.h"
#import "YH_Analytics.h"
#import "NSString+YHAnalytics.h"
#import "Reachability.h"
#import "YHCrashReporter.h"
#import "YHAssemblyAssistant.h"
#import "OpenUDID.h"
#define kNetWorkStatusUnknow @"0"
#define kNetWorkStatusWIFI @"1"
#define kNetWorkStatus2G @"2"
#define kNetWorkStatus3G @"3"
#define kNetWorkStatus4G @"4"
@interface YH_Analytics ()
/**
* 定位服务
*/
@property (strong, nonatomic) CLLocationManager *locationManager;
/**
* 应用ID
*/
@property (strong, nonatomic) NSString *appId;
... ... @@ -68,8 +54,6 @@
- (NSString *)timestamp;
- (void)startObserverNetworkReachabilityStatus;
- (void)startLocationService;
- (void)registerCrashReporter;
- (void)registerAppWillEnterForegroundNotification;
- (void)resetSession;
... ... @@ -136,89 +120,8 @@
return _session;
}
- (CLLocationManager *)locationManager
{
if (!_locationManager) {
_locationManager = [[CLLocationManager alloc]init];
_locationManager.delegate = self;
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
_locationManager.distanceFilter = 100;
}
return _locationManager;
}
#pragma mark - Private Method
// 添加网络监测回调block
- (void)startObserverNetworkReachabilityStatus
{
/**
CTRadioAccessTechnologyGPRS - 2G
CTRadioAccessTechnologyEdge - 2G (sometimes called 2.5G)
CTRadioAccessTechnologyWCDMA - 3G
CTRadioAccessTechnologyHSDPA - 3G (sometimes called 3.5G)
CTRadioAccessTechnologyHSUPA - 3G
CTRadioAccessTechnologyCDMA1x - 2G
CTRadioAccessTechnologyCDMAEVDORev0 - 3G
CTRadioAccessTechnologyCDMAEVDORevA - 3G
CTRadioAccessTechnologyCDMAEVDORevB - 3G
CTRadioAccessTechnologyeHRPD - 3G (or 3.5 - eHRPD is to allow migration from CDMA EVDO to LTE)
CTRadioAccessTechnologyLTE - 4G (allowed to be called 4G by the ITU as mentioend above)
*/
[[YHNetworkAssistant sharedInstance] addReachabilityChangedHandler:^(AFNetworkReachabilityStatus networkStatus) {
NSString *net = kNetWorkStatusUnknow;
CTTelephonyNetworkInfo *netInfo = [[CTTelephonyNetworkInfo alloc] init];
switch (networkStatus) {
case AFNetworkReachabilityStatusUnknown:
case AFNetworkReachabilityStatusNotReachable:
net = kNetWorkStatusUnknow;
break;
case AFNetworkReachabilityStatusReachableViaWWAN:
{
if (NSFoundationVersionNumber >= NSFoundationVersionNumber_iOS_7_0) {
if ([netInfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyGPRS]|| [netInfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyEdge]) {
net = kNetWorkStatus2G;
} else if([netInfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyWCDMA]|| [netInfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyHSDPA] || [netInfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyHSUPA]|| [netInfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyeHRPD]) {
net = kNetWorkStatus3G;
} else if([netInfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyLTE]) {
net = kNetWorkStatus4G;
} else {
net = kNetWorkStatusUnknow;
}
} else {
net = kNetWorkStatusUnknow;
}
}
break;
case AFNetworkReachabilityStatusReachableViaWiFi:
net = kNetWorkStatusWIFI;
break;
default:
break;
}
if ([[YHAssemblyAssistant sharedInstance] updateNetWorkStatus:net]) {
[[YHAssemblyAssistant sharedInstance] updateImmediUploadDic];
}
}];
}
// 启动定位服务
- (void)startLocationService
{
if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
[self.locationManager requestAlwaysAuthorization];
}
if ([CLLocationManager locationServicesEnabled]) {
[self.locationManager startUpdatingLocation];
}
}
- (void)registerCrashReporter
{
[YHCrashReporter sharedInstance];
... ... @@ -288,10 +191,6 @@
[[YHAssemblyAssistant sharedInstance] prepareImmediUploadDic:appId sessionId:self.session];
[self startObserverNetworkReachabilityStatus];
[self startLocationService];
[self registerCrashReporter];
[self registerAppWillEnterForegroundNotification];
... ... @@ -321,8 +220,6 @@
{
NSLog(@"logEvent %@, %@", eventId, param);
[self startLocationService];
self.event = [[YHEvent alloc] init];
self.event.op = eventId;
self.event.uid = self.uid ? self.uid : @"";
... ... @@ -384,39 +281,4 @@
// }
//}
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
if (status == kCLAuthorizationStatusAuthorizedAlways || status == kCLAuthorizationStatusAuthorizedWhenInUse){
[manager startUpdatingLocation];
// ...
} else {
if ([manager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
[manager requestAlwaysAuthorization];
}
}
}
//协议中的方法,作用是每当位置发生更新时会调用的委托方法
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
if ([[YHAssemblyAssistant sharedInstance] updateLocation:newLocation]) {
[[YHAssemblyAssistant sharedInstance] updateImmediUploadDic];
}
}
//当位置获取或更新失败会调用的方法
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSString *errorMsg = nil;
if ([error code] == kCLErrorDenied) {
errorMsg = @"定位功能访问被拒绝";
}
if ([error code] == kCLErrorLocationUnknown) {
errorMsg = @"获取位置信息失败";
}
NSLog(@"%@", errorMsg);
}
@end
... ...
... ... @@ -16,7 +16,7 @@
- (void)changeAuthorizationStatus:(CLAuthorizationStatus)status;
- (void)updateLocation:(CLLocation *)newLocation oldLocation:(CLLocation *)oldLocation;
- (void)updateLocations:(NSArray *)locations;
- (void)failedLocation:(NSError *)error;
... ...
... ... @@ -22,10 +22,10 @@
}
}
- (void)updateLocation:(CLLocation *)newLocation oldLocation:(CLLocation *)oldLocation
- (void)updateLocations:(NSArray *)locations
{
if (self.delegate && [self.delegate respondsToSelector:@selector(locationManager:didUpdateToLocation:fromLocation:)]) {
[self.delegate locationManager:self didUpdateToLocation:newLocation fromLocation:oldLocation];
if (self.delegate && [self.delegate respondsToSelector:@selector(locationManager:didUpdateLocations:)]) {
[self.delegate locationManager:self didUpdateLocations:locations];
}
}
... ...
... ... @@ -9,28 +9,36 @@
#import <UIKit/UIKit.h>
#import <XCTest/XCTest.h>
#import "OCMock.h"
#import <CoreLocation/CoreLocation.h>
#import "YHAssemblyAssistant.h"
#import "YHDevice.h"
#import "YHStatus.h"
#import "YHEvent.h"
#import "YHError.h"
#import "FakeLocationManager.h"
@interface YHAssemblyAssistant (XCTestCase)
@property (strong, nonatomic) NSString *eventFilePath;
@property (strong, nonatomic) NSString *eventFileName;
@property (strong, nonatomic) NSFileManager *fileManager; // 属性注入 DI
@property (strong, nonatomic) CLLocationManager *locationManager;
@property (strong, nonatomic, readonly) YHDevice *device;
@property (strong, nonatomic, readonly) YHStatus *currentStatus;
@property (strong, nonatomic, readonly) NSMutableDictionary *immediUploadItemDic;
- (BOOL)updateNetWorkStatus:(NSString *)status;
- (BOOL)updateLocation:(CLLocation *)location;
- (void)updateImmediUploadDic;
@end
@interface YHAssemblyAssistantLogicTests : XCTestCase {
YHAssemblyAssistant *assembly;
id partialMock;
id mock;
}
... ... @@ -51,6 +59,8 @@
mock = OCMClassMock([NSFileManager class]);
OCMStub([mock defaultManager]).andReturn(mock);
assembly.fileManager = mock;
partialMock = OCMPartialMock(assembly);
}
- (void)tearDown {
... ... @@ -183,7 +193,6 @@
- (void)testSaveItemDataWithInputNil {
NSLog(@"%@ start", self.name);
id partialMock = OCMPartialMock(assembly);
[[partialMock reject] canPersisting];
XCTAssertThrows([assembly saveItemData:nil]);
... ... @@ -195,7 +204,6 @@
- (void)testSaveItemDataWithInputInvalid {
NSLog(@"%@ start", self.name);
id partialMock = OCMPartialMock(assembly);
[[partialMock reject] canPersisting];
XCTAssertThrows([assembly saveItemData:@"invalid"]);
... ... @@ -208,7 +216,7 @@
NSLog(@"%@ start", self.name);
YHAnalyItemData *data = [[YHAnalyItemData alloc] init];
id partialMock = OCMPartialMock(assembly);
OCMExpect([partialMock canPersisting]).andReturn(NO);
XCTAssertThrows([assembly saveItemData:data]);
... ... @@ -221,7 +229,7 @@
NSLog(@"%@ start", self.name);
YHDevice *data = [[YHDevice alloc] init];
id partialMock = OCMPartialMock(assembly);
OCMExpect([partialMock canPersisting]).andReturn(YES);
XCTAssertThrows([assembly saveItemData:data]);
... ... @@ -234,7 +242,7 @@
NSLog(@"%@ start", self.name);
YHStatus *data = [[YHStatus alloc] init];
id partialMock = OCMPartialMock(assembly);
OCMExpect([partialMock canPersisting]).andReturn(YES);
XCTAssertThrows([assembly saveItemData:data]);
... ... @@ -247,7 +255,7 @@
NSLog(@"%@ start", self.name);
YHEvent *data = [[YHEvent alloc] init];
id partialMock = OCMPartialMock(assembly);
OCMExpect([partialMock canPersisting]).andReturn(YES);
XCTAssertNoThrow([assembly saveItemData:data]);
... ... @@ -260,7 +268,7 @@
NSLog(@"%@ start", self.name);
YHError *data = [[YHError alloc] init];
id partialMock = OCMPartialMock(assembly);
OCMExpect([partialMock canPersisting]).andReturn(YES);
XCTAssertNoThrow([assembly saveItemData:data]);
... ... @@ -337,4 +345,42 @@
NSLog(@"%@ end", self.name);
}
#pragma mark - Test Location Sevice
- (void)testLocationUpdateSuccess {
NSLog(@"%@ start", self.name);
assembly.locationManager = (CLLocationManager *)[FakeLocationManager new];
assembly.locationManager.delegate = assembly;
CLLocation *newLocation = [[CLLocation alloc] initWithLatitude:20.0f longitude:120.0f];
[[[partialMock expect] andReturnValue:@YES] updateLocation:newLocation];
OCMExpect([partialMock updateImmediUploadDic]);
[assembly prepareImmediUploadDic:@"appId" sessionId:@"seesionId"];
[(FakeLocationManager *)assembly.locationManager updateLocations:@[newLocation]];
OCMVerifyAll(partialMock);
NSLog(@"%@ end", self.name);
}
- (void)testLocationUpdateFailed {
NSLog(@"%@ start", self.name);
assembly.locationManager = (CLLocationManager *)[FakeLocationManager new];
assembly.locationManager.delegate = assembly;
CLLocation *newLocation = [[CLLocation alloc] initWithLatitude:20.0f longitude:120.0f];
[[[partialMock expect] andReturnValue:@NO] updateLocation:newLocation];
[[partialMock reject] updateImmediUploadDic];
[assembly prepareImmediUploadDic:@"appId" sessionId:@"seesionId"];
[(FakeLocationManager *)assembly.locationManager updateLocations:@[newLocation]];
OCMVerifyAll(partialMock);
NSLog(@"%@ end", self.name);
}
@end
... ...
... ... @@ -128,15 +128,6 @@
NSLog(@"%@ end", self.name);
}
- (void)testUpdateImmediUploadDicMethod {
NSLog(@"%@ start", self.name);
[[mock expect] updateImmediUploadDic];
[mock updateImmediUploadDic];
NSLog(@"%@ end", self.name);
}
- (void)testUploadDiskDataMethod {
NSLog(@"%@ start", self.name);
... ...
... ... @@ -9,19 +9,15 @@
#import <UIKit/UIKit.h>
#import <XCTest/XCTest.h>
#import "OCMock.h"
#import <CoreLocation/CoreLocation.h>
#import "YHCrashReporter.h"
#import "YHAssemblyAssistant.h"
#import "YH_Analytics.h"
#import "YHEvent.h"
#import "FakeLocationManager.h"
@interface YH_Analytics (XCTestCase)
@property (assign, nonatomic) YHLogStrategy logStrategy;
@property (assign, nonatomic) NSTimeInterval currentInterval;
@property (strong, nonatomic) CLLocationManager *locationManager;
@end
... ... @@ -29,7 +25,6 @@
@private
id assemblyAssistantMock;
id crashReporterMock;
id networkAssistantMock;
YH_Analytics *analytics;
}
... ... @@ -52,9 +47,6 @@
crashReporterMock = OCMClassMock([YHCrashReporter class]);
OCMStub([crashReporterMock sharedInstance]).andReturn(crashReporterMock);
networkAssistantMock = OCMClassMock([YHNetworkAssistant class]);
OCMStub([networkAssistantMock sharedInstance]).andReturn(networkAssistantMock);
analytics = [[YH_Analytics alloc] init];
}
... ... @@ -62,7 +54,6 @@
// Put teardown code here. This method is called after the invocation of each test method in the class.
assemblyAssistantMock = nil;
crashReporterMock = nil;
networkAssistantMock = nil;
analytics = nil;
[super tearDown];
... ... @@ -83,13 +74,11 @@
- (void)testStartWithAppIdMethod {
NSLog(@"%@ start", self.name);
OCMExpect([networkAssistantMock addReachabilityChangedHandler:[OCMArg any]]);
OCMExpect(ClassMethod([crashReporterMock sharedInstance]));
OCMExpect([assemblyAssistantMock prepareImmediUploadDic:@"appId" sessionId:[OCMArg any]]);
[analytics startWithAppId:@"appId"];
OCMVerifyAll(networkAssistantMock);
OCMVerify(ClassMethod([crashReporterMock sharedInstance]));
OCMVerifyAll(assemblyAssistantMock);
... ... @@ -287,41 +276,4 @@
NSLog(@"%@ end", self.name);
}
#pragma mark - Test Location Sevice
- (void)testLocationUpdateSuccess {
NSLog(@"%@ start", self.name);
analytics.locationManager = [[FakeLocationManager alloc] init];
analytics.locationManager.delegate = analytics;
CLLocation *newLocation = [[CLLocation alloc] initWithLatitude:20.0f longitude:120.0f];
[[[assemblyAssistantMock expect] andReturnValue:@YES] updateLocation:newLocation];
OCMExpect([assemblyAssistantMock updateImmediUploadDic]);
[analytics startWithAppId:@"appId"];
[(FakeLocationManager *)analytics.locationManager updateLocation:newLocation oldLocation:nil];
OCMVerifyAll(assemblyAssistantMock);
NSLog(@"%@ end", self.name);
}
- (void)testLocationUpdateFailed {
NSLog(@"%@ start", self.name);
analytics.locationManager = [[FakeLocationManager alloc] init];
analytics.locationManager.delegate = analytics;
CLLocation *newLocation = [[CLLocation alloc] initWithLatitude:20.0f longitude:120.0f];
[[[assemblyAssistantMock expect] andReturnValue:@NO] updateLocation:newLocation];
[[assemblyAssistantMock reject] updateImmediUploadDic];
[analytics startWithAppId:@"appId"];
[(FakeLocationManager *)analytics.locationManager updateLocation:newLocation oldLocation:nil];
OCMVerifyAll(assemblyAssistantMock);
NSLog(@"%@ end", self.name);
}
@end
... ...