Skip to content

Commit

Permalink
Added Functions for logging into File in SDK and TestBed App. Added U…
Browse files Browse the repository at this point in the history
…I Test Cases.
  • Loading branch information
ndixit-branch committed Mar 29, 2021
1 parent 57b574f commit e681c6f
Show file tree
Hide file tree
Showing 39 changed files with 2,646 additions and 101 deletions.
8 changes: 8 additions & 0 deletions Branch-SDK/BNCLog.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ extern NSString *_Nonnull BNCLogStringFromLogLevel(BNCLogLevel level);
*/
extern BNCLogLevel BNCLogLevelFromString(NSString*_Null_unspecified string);

///@name Pre-defined log message handlers --
typedef void (*BNCLogOutputFunctionPtr)(NSDate*_Nonnull timestamp, BNCLogLevel level, NSString*_Nullable message);

///@param functionPtr A pointer to the logging function. Setting the parameter to NULL will flush
/// and close the currently set log function and future log messages will be
/// ignored until a non-NULL logging function is set.
extern void BNCLogSetOutputFunction(BNCLogOutputFunctionPtr _Nullable functionPtr);

#pragma mark - BNCLogWriteMessage

/// The main logging function used in the variadic logging defines.
Expand Down
7 changes: 7 additions & 0 deletions Branch-SDK/BNCLog.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

#define _countof(array) (sizeof(array)/sizeof(array[0]))

static BNCLogOutputFunctionPtr bnc_LoggingFunction = nil; // Default to just NSLog output.

// A fallback attempt at logging if an error occurs in BNCLog.
// BNCLog can't log itself, but if an error occurs it uses this simple define:
extern void BNCLogInternalError(NSString *message);
Expand Down Expand Up @@ -62,6 +64,9 @@ BNCLogLevel BNCLogLevelFromString(NSString*string) {
return BNCLogLevelNone;
}

void BNCLogSetOutputFunction(BNCLogOutputFunctionPtr _Nullable logFunction) {
bnc_LoggingFunction = logFunction;
}
#pragma mark - BNCLogInternal

void BNCLogWriteMessage(
Expand Down Expand Up @@ -98,5 +103,7 @@ void BNCLogWriteMessage(

if (logLevel >= bnc_LogDisplayLevel) {
NSLog(@"%@", s); // Upgrade this to unified logging when we can.
if (bnc_LoggingFunction)
bnc_LoggingFunction([NSDate date], logLevel, message);
}
}
2 changes: 1 addition & 1 deletion Branch-SDK/BNCServerInterface.m
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ - (NSURLRequest *)preparePostRequest:(NSDictionary *)params
NSData *postData = [BNCEncodingUtils encodeDictionaryToJsonData:preparedParams];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];

BNCLogDebug([NSString stringWithFormat:@"URL: %@.", url]);
BNCLogDebug([NSString stringWithFormat:@"URL: %@.\n", url]);
BNCLogDebug([NSString stringWithFormat:@"Body: %@\nJSON: %@.",
preparedParams,
[[NSString alloc] initWithData:postData encoding:NSUTF8StringEncoding]]
Expand Down
106 changes: 106 additions & 0 deletions Branch-TestBed/Branch-TestBed-UITests/UITestCase0OpenNInstall.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
//
// UITestOpenNInstall.m
// Branch-TestBed-UITests
//
// Created by Nidhi on 3/10/21.
// Copyright © 2021 Branch, Inc. All rights reserved.
//

#import "UITestCaseTestBed.h"
#import <Foundation/Foundation.h>

@interface UITestCase0OpenNInstall : UITestCaseTestBed

@end

@implementation UITestCase0OpenNInstall

- (void)setUp {
[super setUp];
}

- (void)tearDown {
}
/*
* Test Case :
* 1.1 First initSession call results in API call to /v1/install or /v1/open
* 1.2 Subsequent initSession calls result in API call to /v1/open
*/
- (void)test0Install {

// Launch App and Disable Tracking
XCUIApplication *app = [[XCUIApplication alloc] init];
[app launch];
[self disableTracking:FALSE];

// Press Button "Load Logs for Last Command" and copy logs
XCUIElementQuery *tablesQuery = app.tables;
[tablesQuery.staticTexts[@"Load Logs for Last Command"] tap];
XCUIElement *deeplinkdataTextView = app.textViews[@"DeepLinkData"];
[deeplinkdataTextView tap];
NSString *prevLogResults = app.textViews[@"DeepLinkData"].value;

// Parse logs
NSString *url;
NSDictionary *json;
NSString *status;
NSDictionary *dataJson;
[self parseURL:&url andJSON:&json andStatus:&status andDataJSON:&dataJson FromLogs:prevLogResults];

// Check url contains /v1/install
XCTAssertNotNil(url);
XCTAssertNotNil(json);
XCTAssertTrue([url containsString:@"/v1/install"]);

// Get and Assert device_fingerprint_id & identity_id
NSNumber *device_fingerprint_id = [dataJson objectForKey:@"device_fingerprint_id"];
NSNumber *identity_id = [dataJson objectForKey:@"identity_id"];
XCTAssertNotNil(identity_id);
XCTAssertNotNil(device_fingerprint_id);

//Re-launch App
[app.navigationBars[@"Branch-TestBed"].buttons[@"Branch-TestBed"] tap];
[app terminate];
[app launch];

// Load logs and assert -
// - URL contains /v1/open
// - device_fingerprint_id is the same as returned above
// - identity_id is the same as returned above
[tablesQuery/*@START_MENU_TOKEN@*/.staticTexts[@"Load Logs for Last Command"]/*[[".cells",".buttons[@\"Load Logs for Last Command\"].staticTexts[@\"Load Logs for Last Command\"]",".staticTexts[@\"Load Logs for Last Command\"]"],[[[-1,2],[-1,1],[-1,0,1]],[[-1,2],[-1,1]]],[0]]@END_MENU_TOKEN@*/ tap];
deeplinkdataTextView = app.textViews[@"DeepLinkData"];
[deeplinkdataTextView tap];
prevLogResults = app.textViews[@"DeepLinkData"].value;
[self parseURL:&url andJSON:&json andStatus:&status andDataJSON:&dataJson FromLogs:prevLogResults];

XCTAssertTrue([url containsString:@"/v1/open"]);

XCTAssertNotNil([json valueForKey:@"identity_id"]);
NSNumber *newID = [NSNumber numberWithInteger: [[json objectForKey:@"identity_id"] integerValue]] ;
XCTAssertTrue([newID isEqualToNumber:identity_id]);

XCTAssertNotNil([json valueForKey:@"device_fingerprint_id"]);
NSNumber *newFPID = [NSNumber numberWithInteger: [[json objectForKey:@"device_fingerprint_id"] integerValue]] ;
XCTAssertTrue([newFPID isEqualToNumber:device_fingerprint_id]);
XCTAssertTrue([status containsString:@"200"]);
}

/*
When a link is clicked with the app installed, it opens the app
- the call to /v1/open should include information about the link clicked
- the response from /v1/open should include deep link data inside the `data` object
*/
- (void) testOpenAppFromWebPage
{
[self clickLinkInWebPage:@"TestWebPage.html"];

XCUIApplication *app = [[XCUIApplication alloc] init];
if ([ app waitForExistenceWithTimeout:15] != NO) {
NSString *deepLinkData = app.textViews[@"DeepLinkData"].value;
XCTAssertTrue([deepLinkData containsString:self.deeplinkDataToCheck]);
} else {
XCTFail("Application not launched");
}
}

@end
145 changes: 145 additions & 0 deletions Branch-TestBed/Branch-TestBed-UITests/UITestCaseMisc.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
//
// UITestCaseMisc.m
// Branch-TestBed-UITests
//
// Created by Nidhi on 2/26/21.
// Copyright © 2021 Branch, Inc. All rights reserved.
//

#import "UITestCaseTestBed.h"
#import "Branch.h"
#import "BranchEvent.h"

@interface UITestCaseMisc : UITestCaseTestBed

@end

@implementation UITestCaseMisc

- (void)setUp {
[super setUp];
}

- (void)tearDown {
}

- (void)testShareLink {

// Tap on 'Creat Branch Link' and then 'Share Link'
XCUIApplication *app = [[XCUIApplication alloc] init];
[app launch];
[self disableTracking:FALSE];
XCUIElementQuery *tablesQuery = app.tables;
[tablesQuery/*@START_MENU_TOKEN@*/.staticTexts[@"Create Branch Link"]/*[[".cells",".buttons[@\"Create Branch Link\"].staticTexts[@\"Create Branch Link\"]",".staticTexts[@\"Create Branch Link\"]"],[[[-1,2],[-1,1],[-1,0,1]],[[-1,2],[-1,1]]],[0]]@END_MENU_TOKEN@*/ tap];
[tablesQuery.staticTexts[@"Share Link"] tap];
sleep(3);

// Copy Link
[[[[app/*@START_MENU_TOKEN@*/.collectionViews/*[[".otherElements[@\"ActivityListView\"].collectionViews",".collectionViews"],[[[-1,1],[-1,0]]],[0]]@END_MENU_TOKEN@*/.buttons[@"Copy"].otherElements containingType:XCUIElementTypeImage identifier:@"copy"] childrenMatchingType:XCUIElementTypeOther].element childrenMatchingType:XCUIElementTypeOther].element tap];
sleep(3);

// Assert Link is copied in Pasteboard
NSString *pasteboardString = [UIPasteboard generalPasteboard].string;
if ([pasteboardString containsString:@"Shared through 'Pasteboard'"] != YES) {
XCTFail("Link not copied");
}
}

- (void)testCreateAndOpenURL {

XCUIApplication *app = [[XCUIApplication alloc] init];
[app launch];
[self disableTracking:FALSE];

//Tap "Create Branch Link" and assert text Field "Branch Link" shows link
XCUIElementQuery *tablesQuery = app.tables;
[tablesQuery.staticTexts[@"Create Branch Link"] tap];
XCUIElement *branchLinkTextField = tablesQuery.textFields[@"Branch Link"];
NSString *shortURL = branchLinkTextField.value;
XCTAssertNotNil(shortURL);

// Tap "Open Branch Link" and assert deep link data
[tablesQuery.buttons[@"Open Branch Link"] tap];
sleep(1);
NSString *deepLinkData = app.textViews[@"DeepLinkData"].value;
self.deeplinkDataToCheck = @"This text was embedded as data in a Branch link with the following characteristics:\n\ncanonicalUrl: https://dev.branch.io/getting-started/deep-link-routing/guide/ios/\n title: Content Title\n contentDescription: My Content Description\n imageUrl: https://pbs.twimg.com/profile_images/658759610220703744/IO1HUADP.png\n";
NSLog(@"%@" , self.deeplinkDataToCheck);
XCTAssertTrue([deepLinkData containsString:self.deeplinkDataToCheck]);

}

- (void)testReferringParams
{
// Launch App, disable tracking and logout
XCUIApplication *app = [[XCUIApplication alloc] init];
[app launch];
[self disableTracking:FALSE];
XCUIElementQuery *tablesQuery = app.tables;
[tablesQuery/*@START_MENU_TOKEN@*/.staticTexts[@"SimulateLogout"]/*[[".cells",".buttons[@\"SimulateLogout\"].staticTexts[@\"SimulateLogout\"]",".staticTexts[@\"SimulateLogout\"]"],[[[-1,2],[-1,1],[-1,0,1]],[[-1,2],[-1,1]]],[0]]@END_MENU_TOKEN@*/ tap];
[app.alerts[@"Logout succeeded"].scrollViews.otherElements.buttons[@"OK"] tap];

// Tap 'View FirstReferringParams' and copy params
XCUIElement *branchTestbedButton = app.navigationBars[@"Branch-TestBed"].buttons[@"Branch-TestBed"];
XCUIElement *viewFirstreferringparamsStaticText = tablesQuery.staticTexts[@"View FirstReferringParams"];
[viewFirstreferringparamsStaticText tap];
NSString *viewFirstreferringString = app.textViews[@"DeepLinkData"].value;
XCTAssertTrue([viewFirstreferringString containsString:@"{\n}"]);
[branchTestbedButton tap];

// Tap 'View LatestReferringParams' and copy params
XCUIElement *viewLatestreferringparamsStaticText = tablesQuery.staticTexts[@"View LatestReferringParams"];
[viewLatestreferringparamsStaticText tap];
NSString *viewLatesttreferringString = app.textViews[@"DeepLinkData"].value;
XCTAssertTrue([viewLatesttreferringString containsString:@"{\n}"]);
[branchTestbedButton tap];

// Tap "Set User ID" and then Tap 'View FirstReferringParams' & 'View LatestReferringParams' and copy params again
[tablesQuery.buttons[@"Set User ID"] tap];
[branchTestbedButton tap];
sleep(1);
[viewFirstreferringparamsStaticText tap];
viewFirstreferringString = app.textViews[@"DeepLinkData"].value;
[branchTestbedButton tap];
[viewLatestreferringparamsStaticText tap];
viewLatesttreferringString = app.textViews[@"DeepLinkData"].value;
[branchTestbedButton tap];

// Re-launch app.
[app terminate];
[app launch];

// Tap on 'View FirstReferringParams' & 'View LatestReferringParams' again and check viewFirstreferringparamsStaticText is still same and viewLatestreferringparamsStaticText shows latest paramms.
[viewFirstreferringparamsStaticText tap];
NSString *viewFirstreferringStringNew = app.textViews[@"DeepLinkData"].value;
[branchTestbedButton tap];
XCTAssertTrue([viewFirstreferringString isEqualToString:viewFirstreferringStringNew]);
[viewLatestreferringparamsStaticText tap];
viewLatesttreferringString = app.textViews[@"DeepLinkData"].value;
[branchTestbedButton tap];

NSString *viewLatesttreferringStringNew =@"{\n \"+clicked_branch_link\" = 0;\n \"+is_first_session\" = 0;\n}";

XCTAssertTrue([viewLatesttreferringString isEqualToString:viewLatesttreferringStringNew]);

}

- (void)testFBParams
{
XCUIApplication *app = [[XCUIApplication alloc] init];
[app launch];
[self disableTracking:FALSE];

XCUIElementQuery *tablesQuery = [[XCUIApplication alloc] init].tables;
[tablesQuery/*@START_MENU_TOKEN@*/.buttons[@"Set FB Params"]/*[[".cells.buttons[@\"Set FB Params\"]",".buttons[@\"Set FB Params\"]"],[[[-1,1],[-1,0]]],[0]]@END_MENU_TOKEN@*/ tap];
setFBParams = TRUE;
checkFBParams = TRUE;
[self sendEvent:BranchStandardEventAddToCart];
XCUIElement *branchTestbedButton = app.navigationBars[@"Branch-TestBed"].buttons[@"Branch-TestBed"];
[branchTestbedButton tap];
[tablesQuery/*@START_MENU_TOKEN@*/.buttons[@"Clear FB Params"]/*[[".cells.buttons[@\"Clear FB Params\"]",".buttons[@\"Clear FB Params\"]"],[[[-1,1],[-1,0]]],[0]]@END_MENU_TOKEN@*/ tap];
setFBParams = FALSE;
[self sendEvent:BranchStandardEventAddToCart];
checkFBParams = FALSE;
}

@end
39 changes: 39 additions & 0 deletions Branch-TestBed/Branch-TestBed-UITests/UITestCaseSafari.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// UITestCaseSafari.m
// Branch-TestBed-UITests
//
// Created by Nidhi on 3/25/21.
// Copyright © 2021 Branch, Inc. All rights reserved.
//

#import "UITestCaseTestBed.h"

@interface UITestCaseSafari : UITestCaseTestBed

@end

@implementation UITestCaseSafari

- (void)setUp {
self.continueAfterFailure = NO;
[super setUp];
//[[[XCUIApplication alloc] init] launch];
}

- (void)tearDown {
}

- (void)testOpenURL {
//[self OpenLinkInWebPage:@"TestWebPage.html"];
}

- (void)testOpenURLInNewWindow {

}

- (void)testClickURL {

}


@end
31 changes: 31 additions & 0 deletions Branch-TestBed/Branch-TestBed-UITests/UITestCaseTestBed.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// UITestCaseTestBed.h
// Branch-TestBed-UITests
//
// Created by Nidhi on 3/7/21.
// Copyright © 2021 Branch, Inc. All rights reserved.
//

#ifndef UITestCaseTestBed_h
#define UITestCaseTestBed_h

#import <XCTest/XCTest.h>

extern BOOL setIdentity;
extern BOOL checkIdentity;
extern BOOL setFBParams;
extern BOOL checkFBParams;

@interface UITestCaseTestBed : XCTestCase

@property NSString *deeplinkDataToCheck;

- (void)sendEvent:(NSString*)eventName;
-(void)parseURL:(NSString **)url andJSON:(NSDictionary **)json FromLogs:(NSString*)logs;
-(void)parseURL:(NSString **)url andJSON:(NSDictionary **)json andStatus:(NSString**)status andDataJSON:(NSDictionary **)dataJson FromLogs:(NSString*)logs;
- (void)clickLinkInWebPage:(NSString*)webPage;
//- (void)OpenLinkInWebPage:(NSString*)webPage;
- (void)disableTracking:(BOOL)disable;
@end

#endif /* UITestCaseTestBed_h */
Loading

0 comments on commit e681c6f

Please sign in to comment.