iOS accessing AddressBook Contacts via UnitTest; how to set permissions?
Asked Answered
B

4

17

This question pertains to using the iPhone Simulator with Unit Tests. I have written a series of tests that test storing of our data, merged or not merged with data we can access from the user's contacts, depending on whether or not the contact exists. I would like a way to set the permissions such that I can test (A) when the user gives permission to access the contacts and (B) when the user denies access to the contacts. What I would like is a way, in Unit Tests only, to hard-code the permission value. I don't want to prompt for it, since that would block the unit test from running with the additional hardship that the permission remains set to that value forever.

So I am reaching out to the SO community to see who else might be testing their code's interaction with address book contacts by controlling the permissions in a Unit Test. Does anyone have a recipe which allows me test both sides of the user giving and denying access to the Address Book contacts?

I am using the XCTestCase parent class. I would be open to using something else if that helped solved this problem.

I have read through all the related SO questions and answers in this area. They are focused on helping people with writing the permission-asking routines and accessing the address book contacts in their application. I know how to do that part. I am specifically talking about how to fake the address book permissions in a unit test.

Bluish answered 18/3, 2014 at 18:36 Comment(1)
Would this help in any way? Doesn't look like it would be specific to the KIF framework. groups.google.com/forum/#!topic/kif-framework/xayP4VVBPygAndy
B
0

At first as I do this 1) in app ->

if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusDenied) ...
if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusRestricted) ...

2) for test this issues I use class of OCMock, for predefined values and methods link here - http://ocmock.org

Briggs answered 17/7, 2014 at 14:49 Comment(0)
D
0
    if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusDenied ||
    ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusRestricted){
} else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized){
} else{
    ABAddressBookRequestAccessWithCompletion(ABAddressBookCreateWithOptions(NULL, nil), ^(bool granted, CFErrorRef error) {
        if (!granted){
            return;
        }  
    });
}

i found the preceding code on a handy little website called:

www.raywenderlich.com

Depraved answered 7/8, 2014 at 22:36 Comment(0)
F
0

// Address Book Authorization grant

if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusDenied ||
    ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusRestricted)
{
    NSLog(@"Denied");
}
else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized)
{
    NSLog(@"Authorized");

}
else
{
    ABAddressBookRequestAccessWithCompletion(ABAddressBookCreateWithOptions(NULL, nil), ^(bool granted, CFErrorRef error)
                                             {
                                                 if (!granted) {
                                                     NSLog(@"Just denied");
                                                     return;
                                                 }


                                                 NSLog(@"Just authorized");
                                             });
    NSLog(@"Not determined");
}
Faught answered 13/1, 2015 at 10:3 Comment(0)
S
0

For Unit testing

 Nsstring *authorizationStatus; 

 if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusDenied ||
    ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusRestricted){
    //1
    NSLog(@"Denied");

authorizationStatus = @"Denied";

} else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized){
    NSLog(@"Authorized");
authorizationStatus = @"Authorized";


} else{ //ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined
    //3
    NSLog(@"Not determined");
 authorizationStatus = @"Not determined";

}
XCTAssertTrue(authorizationStatus, @"Authorized");
Stockpile answered 24/3, 2015 at 6:42 Comment(1)

© 2022 - 2024 — McMap. All rights reserved.