Skip to content

Commit

Permalink
add Test for 'ls calc'
Browse files Browse the repository at this point in the history
  • Loading branch information
efu39 committed Mar 26, 2020
1 parent eb9b170 commit f61c5f8
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 20 deletions.
1 change: 1 addition & 0 deletions osxscraper/OSXScraper/AccAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
@interface AccAPI : NSObject

+ (void) initialize;
+ (BOOL) isUnitTesting;
+ (NSArray *) getAllProcessesIDs;
+ (Entity *) getEntityForApp:(pid_t) pid;
+ (void) addValidApp: (NSString*) appName;
Expand Down
18 changes: 13 additions & 5 deletions osxscraper/OSXScraper/AccAPI.m
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,14 @@ + (void) initialize {
valid_apps = [settings objectForKey:@"support_apps"];
}

+ (BOOL) isUnitTesting {
NSDictionary* environment = [[NSProcessInfo processInfo] environment];
return (environment[@"XCTestConfigurationFilePath"] != nil);
}

+ (NSArray *) getAllProcessesIDs {
CFArrayRef appList = CGWindowListCopyWindowInfo((kCGWindowListOptionOnScreenOnly), kCGNullWindowID);
int num_process = (int) CFArrayGetCount(appList);
NSLog(@"Process Count = %i", num_process);

NSMutableArray * processes = [[NSMutableArray alloc] initWithCapacity:num_process];
for (NSMutableDictionary* win in (__bridge NSArray*) appList){
Expand Down Expand Up @@ -90,7 +94,10 @@ + (Entity *) getEntityForApp:(pid_t) pid {
}

+ (void) addValidApp: (NSString*) appName {
[valid_apps addObject:appName];
if(![valid_apps containsObject:appName]){
[valid_apps addObject:appName];
NSLog(@"%s valid apps: %@", __PRETTY_FUNCTION__, valid_apps);
}
}

// ls command
Expand All @@ -99,19 +106,20 @@ + (Sinter *) getListOfApplications {
sinter.header.service_code = [serviceCodes objectForKey:STRLsRes];
sinter.header.sub_code = [serviceCodes objectForKey:STRLsRes];

int nProcessAbleToSee = 0;

NSArray * processes = [self getAllProcessesIDs];

int nProcessAbleToSee = 0;
for ( NSNumber * process_id in processes){
Entity * e = [self getEntityForApp: (pid_t) [process_id integerValue]];
if(e != nil){
nProcessAbleToSee ++;
if ([AccAPI isUnitTesting]) NSLog(@"%s ableToSee: %@", __PRETTY_FUNCTION__, e.name);
if([valid_apps containsObject:e.name]) {
[sinter.entities addObject:e];
}
}
}
NSLog(@"nProcessAbleToSee = %i", nProcessAbleToSee);
if ([AccAPI isUnitTesting]) NSLog(@"%s nProcessAbleToSee = %i", __PRETTY_FUNCTION__, nProcessAbleToSee);
return sinter;
}

Expand Down
1 change: 0 additions & 1 deletion osxscraper/OSXScraper/Scraper.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
@property (nonatomic, strong ) NSMutableDictionary* appCache;
@property (nonatomic, strong ) NSMutableDictionary* appObservers;

+ (BOOL) isUnitTesting;
- (id) initWithId:(int) identifier andClientHandler:(ClientHandler *) clientHandler;
- (Sinter *) execute: (Sinter *) cmdSinter;

Expand Down
11 changes: 3 additions & 8 deletions osxscraper/OSXScraper/Scraper.m
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,6 @@ + (void) initialize {
serviceCodes = [Config getServiceCodes];
}

+ (BOOL) isUnitTesting {
NSDictionary* environment = [[NSProcessInfo processInfo] environment];
return (environment[@"XCTestConfigurationFilePath"] != nil);
}

Scraper * refToSelf;

- (id) initWithId:(int) identifier andClientHandler:(ClientHandler *) clientHandler {
Expand All @@ -56,7 +51,7 @@ - (id) initWithId:(int) identifier andClientHandler:(ClientHandler *) clientHand
[self setIdentifier:identifier];
[self setClientHandler:clientHandler];
_isPasscodeVerified = false;
if ([[self class] isUnitTesting]){
if ([AccAPI isUnitTesting]){
_isPasscodeVerified = true;
}

Expand Down Expand Up @@ -170,7 +165,7 @@ - (Sinter *) execute: (Sinter *) cmdSinter {

}

if ([[self class] isUnitTesting]){
if ([AccAPI isUnitTesting]){
//if unit-testing, just return.
return sinterToSend;
}
Expand Down Expand Up @@ -313,7 +308,7 @@ - (BOOL) registerObserverFor:(pid_t) pid forElementRef:(AXUIElementRef) appRef {

- (bool) checkAccessibilityAPI {
NSDictionary *options;
if ([[self class] isUnitTesting]){
if ([AccAPI isUnitTesting]){
// do not user-prompt when autotesting
options = @{(__bridge id) kAXTrustedCheckOptionPrompt: @NO };
}
Expand Down
33 changes: 27 additions & 6 deletions osxscraper/OSXScraperTests/OSXScraperTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -59,40 +59,60 @@ - (void)setUp
[CalcTask setLaunchPath:@"/Applications/Calculator.app/Contents/MacOS/Calculator"];
[CalcTask setArguments:[NSArray arrayWithObjects:@"/bin/bash", nil]];
[CalcTask launchAndReturnError:&err];
NSLog(@"err: %@", err.localizedDescription);
}

- (void)tearDown
{
// Put teardown code here. This method is called after the invocation of each test method in the class.
[super tearDown];
[CalcTask terminate];

}

- (void) testLS
- (void) test001LS
{
//test 'list of applications', should see itself (OSXScraper)
Sinter * sinterInput = [[Sinter alloc] initWithServiceCode:[serviceCodes objectForKey:STRLsReq]];
Sinter * sinterOutput = [scraper execute:sinterInput];
XCTAssertNotNil(sinterOutput);
XCTAssertTrue([sinterOutput.header.service_code isEqualToNumber: [serviceCodes objectForKey:STRLsRes]]);
XCTAssertTrue(sinterOutput.entities.count >= 1);

BOOL bFoundSelf = NO;
NSLog(@"%s entities count: %ld", __PRETTY_FUNCTION__, [sinterOutput.entities count]);
for (Entity* e in sinterOutput.entities){
NSLog(@"%s %@, %@", __PRETTY_FUNCTION__, e.name, e.process_id);
if ([e.name isEqualToString:OSXScraperName]){
bFoundSelf = YES;
NSLog(@"%s %@ in STRLsRes", __PRETTY_FUNCTION__, e.name);
break;
}
}
XCTAssertTrue(bFoundSelf);
}

- (void) testLongLS
- (void) test001LSCalc
{
//first get the process_id of itself, then test long LS (scrape it and output a response sinter
//test 'list of applications', should see itself (OSXScraper)
Sinter * sinterInput = [[Sinter alloc] initWithServiceCode:[serviceCodes objectForKey:STRLsReq]];
[scraper execute:sinterInput];
Sinter * sinterOutput = [scraper execute:sinterInput];
XCTAssertNotNil(sinterOutput);
XCTAssertTrue([sinterOutput.header.service_code isEqualToNumber: [serviceCodes objectForKey:STRLsRes]]);
XCTAssertTrue(sinterOutput.entities.count >= 1);

BOOL bFound = NO;
NSLog(@"%s entities count: %ld", __PRETTY_FUNCTION__, [sinterOutput.entities count]);
for (Entity* e in sinterOutput.entities){
NSLog(@"%s %@, %@", __PRETTY_FUNCTION__, e.name, e.process_id);
if ([e.name isEqualToString:@"Calculator"]){
bFound = YES;
}
}
XCTAssertTrue(bFound);
}

- (void) test002LongLS
{
// first get the process_id of itself, then test long LS (scrape it and output a response sinter)
Sinter * sinterOutput = [AccAPI getListOfApplications];
NSString* process_id;
for (Entity* e in sinterOutput.entities){
Expand All @@ -102,6 +122,7 @@ - (void) testLongLS
break;
}
}
// create a sinterInput2 with process_id to test
Sinter * sinterInput2 = [[Sinter alloc] init];
sinterInput2.header = [[Header alloc] initWithServiceCode:[serviceCodes objectForKey:STRLsLongReq]
subCode:[serviceCodes objectForKey:STRLsLongReq]
Expand Down

0 comments on commit f61c5f8

Please sign in to comment.