|
| 1 | +#import <XCTest/XCTest.h> |
| 2 | +#import "QueryConfiguration.h" |
| 3 | +#import "QueryFactory.h" |
| 4 | +#import "QuerySpecifier.h" |
| 5 | +#import "Application.h" |
| 6 | +#import "CBXConstants.h" |
| 7 | +#import "XCTest+CBXAdditions.h" |
| 8 | +#import "CBXServerUnitTestUmbrellaHeader.h" |
| 9 | + |
| 10 | +@interface QuerySpecifierTests : XCTestCase |
| 11 | +@end |
| 12 | + |
| 13 | +@implementation QuerySpecifierTests |
| 14 | + |
| 15 | +- (void)setUp { |
| 16 | + [super setUp]; |
| 17 | +} |
| 18 | + |
| 19 | +- (void)tearDown { |
| 20 | + [super tearDown]; |
| 21 | +} |
| 22 | + |
| 23 | +- (void)testQuerySpecifierByDescendantTypeThrowsExceptionForInvalidMainKeyCase { |
| 24 | + id invalidJson = @{@"descendantElement": @{@"parent_type" : @"Keyboard", |
| 25 | + @"descendant_type" : @"Button"}}; |
| 26 | + |
| 27 | + expect(^{ |
| 28 | + [QueryConfiguration withJSON:invalidJson validator:nil]; |
| 29 | + }).to.raise(@"InvalidArgumentException"); |
| 30 | +} |
| 31 | + |
| 32 | +- (void)testQuerySpecifierByDescendantTypeThrowsExceptionForInvalidParentKeyCase { |
| 33 | + // [Set Up] Stage 0: prepare query with invalid config |
| 34 | + id invalidJson = @{@"descendant_element": @{@"parent_typeeeee" : @"Keyboard", |
| 35 | + @"descendant_type" : @"Button"}}; |
| 36 | + QueryConfiguration *queryConfig = [QueryConfiguration withJSON:invalidJson validator:nil]; |
| 37 | + |
| 38 | + Query *query = [QueryFactory queryWithQueryConfiguration:queryConfig]; |
| 39 | + |
| 40 | + // [Preconditions] Stage 1: prepare mock objects for app and query |
| 41 | + id appMock = OCMClassMock([Application class]); |
| 42 | + id uiAppMock = OCMClassMock([XCUIApplication class]); |
| 43 | + |
| 44 | + id queryMock = OCMClassMock([XCUIElementQuery class]); |
| 45 | + |
| 46 | + // [Preconditions] Stage 1: mock methods called in [query execute] |
| 47 | + OCMStub([uiAppMock cbxQueryForDescendantsOfAnyType]).andReturn(queryMock); |
| 48 | + OCMStub([appMock currentApplication]).andReturn(uiAppMock); |
| 49 | + |
| 50 | + // [Check] Stage 2: throw Exception in case parent type key is malformed |
| 51 | + expect(^{ |
| 52 | + [query execute]; |
| 53 | + }).to.raise(@"NSInternalInconsistencyException"); |
| 54 | +} |
| 55 | + |
| 56 | +- (void)testQuerySpecifierByDescendantTypeThrowsExceptionForInvalidDescendantKeyCase { |
| 57 | + // [Set Up] Stage 0: prepare query with invalid config |
| 58 | + id invalidJson = @{@"descendant_element": @{@"parent_type" : @"Keyboard", |
| 59 | + @"descendantType" : @"Button"}}; |
| 60 | + QueryConfiguration *queryConfig = [QueryConfiguration withJSON:invalidJson validator:nil]; |
| 61 | + |
| 62 | + Query *query = [QueryFactory queryWithQueryConfiguration:queryConfig]; |
| 63 | + |
| 64 | + // [Preconditions] Stage 1: prepare mock objects for app and query |
| 65 | + id appMock = OCMClassMock([Application class]); |
| 66 | + id uiAppMock = OCMClassMock([XCUIApplication class]); |
| 67 | + |
| 68 | + id queryMock = OCMClassMock([XCUIElementQuery class]); |
| 69 | + |
| 70 | + // [Preconditions] Stage 1: mock methods called in [query execute] |
| 71 | + OCMStub([uiAppMock cbxQueryForDescendantsOfAnyType]).andReturn(queryMock); |
| 72 | + OCMStub([appMock currentApplication]).andReturn(uiAppMock); |
| 73 | + |
| 74 | + // [Check] Stage 2: throw Exception in case descendant type key is malformed |
| 75 | + expect(^{ |
| 76 | + [query execute]; |
| 77 | + }).to.raise(@"NSInternalInconsistencyException"); |
| 78 | +} |
| 79 | + |
| 80 | +- (void)testQuerySpecifierByDescendantTypeThrowsExceptionForInvalidMissingKeyCase { |
| 81 | + // [Set Up] Stage 0: prepare query with invalid config |
| 82 | + id invalidJson = @{@"descendant_element": @{@"parent_type" : @"Keyboard" }}; |
| 83 | + QueryConfiguration *queryConfig = [QueryConfiguration withJSON:invalidJson validator:nil]; |
| 84 | + |
| 85 | + Query *query = [QueryFactory queryWithQueryConfiguration:queryConfig]; |
| 86 | + |
| 87 | + // [Preconditions] Stage 1: prepare mock objects for app and query |
| 88 | + id appMock = OCMClassMock([Application class]); |
| 89 | + id uiAppMock = OCMClassMock([XCUIApplication class]); |
| 90 | + |
| 91 | + id queryMock = OCMClassMock([XCUIElementQuery class]); |
| 92 | + |
| 93 | + // [Preconditions] Stage 1: mock methods called in [query execute] |
| 94 | + OCMStub([uiAppMock cbxQueryForDescendantsOfAnyType]).andReturn(queryMock); |
| 95 | + OCMStub([appMock currentApplication]).andReturn(uiAppMock); |
| 96 | + |
| 97 | + // [Check] Stage 2: throw Exception in case missed descendant_type key |
| 98 | + expect(^{ |
| 99 | + [query execute]; |
| 100 | + }).to.raise(@"NSInternalInconsistencyException"); |
| 101 | +} |
| 102 | + |
| 103 | +- (void)testQuerySpecifierByDescendantTypeThrowsExceptionForInvalidParentTypeValueCase { |
| 104 | + // [Set Up] Stage 0: prepare query with invalid config |
| 105 | + id invalidJson = @{@"descendant_element": @{@"parent_type" : @"UIKeyboard", |
| 106 | + @"descendant_type" : @"Button"}}; |
| 107 | + QueryConfiguration *queryConfig = [QueryConfiguration withJSON:invalidJson validator:nil]; |
| 108 | + |
| 109 | + Query *query = [QueryFactory queryWithQueryConfiguration:queryConfig]; |
| 110 | + |
| 111 | + // [Preconditions] Stage 1: prepare mock objects for app and query |
| 112 | + id appMock = OCMClassMock([Application class]); |
| 113 | + id uiAppMock = OCMClassMock([XCUIApplication class]); |
| 114 | + |
| 115 | + id queryMock = OCMClassMock([XCUIElementQuery class]); |
| 116 | + |
| 117 | + // [Preconditions] Stage 1: mock methods called in [query execute] |
| 118 | + OCMStub([uiAppMock cbxQueryForDescendantsOfAnyType]).andReturn(queryMock); |
| 119 | + OCMStub([appMock currentApplication]).andReturn(uiAppMock); |
| 120 | + |
| 121 | + // [Check] Stage 2: throw Exception in case UIElement type value is invalid |
| 122 | + expect(^{ |
| 123 | + [query execute]; |
| 124 | + }).to.raise(@"CBXException"); |
| 125 | +} |
| 126 | + |
| 127 | +- (void)testQuerySpecifierByDescendantTypeReturnsArrayOfElementsForValidCase { |
| 128 | + // [Set Up] Stage 0: prepare query with valid config |
| 129 | + id validJson = @{@"descendant_element": @{@"parent_type" : @"Keyboard", |
| 130 | + @"descendant_type" : @"Button"}}; |
| 131 | + QueryConfiguration *queryConfig = [QueryConfiguration withJSON:validJson validator:nil]; |
| 132 | + |
| 133 | + Query *query = [QueryFactory queryWithQueryConfiguration:queryConfig]; |
| 134 | + |
| 135 | + // [Preconditions] Stage 1: prepare mock objects for app and query |
| 136 | + // (we want mock interactions with real app object and |
| 137 | + // check specific implementation of applyInternal for QuerySpecifierByDescendantType) |
| 138 | + id appMock = OCMClassMock([Application class]); |
| 139 | + id uiAppMock = OCMClassMock([XCUIApplication class]); |
| 140 | + id element = OCMClassMock([XCUIElement class]); |
| 141 | + id arrayOfElements = [NSArray arrayWithObject:element]; |
| 142 | + |
| 143 | + id queryMock = OCMClassMock([XCUIElementQuery class]); |
| 144 | + id contextQueryMock = OCMClassMock([XCUIElementQuery class]); |
| 145 | + id resultQueryMock = OCMClassMock([XCUIElementQuery class]); |
| 146 | + |
| 147 | + // [Preconditions] Stage 1: mock methods called in [query execute] |
| 148 | + OCMStub([uiAppMock cbxQueryForDescendantsOfAnyType]).andReturn(queryMock); |
| 149 | + OCMStub([appMock currentApplication]).andReturn(uiAppMock); |
| 150 | + |
| 151 | + // [Expectations] Stage 2: set up expectations for [specifier applyInternal:query] |
| 152 | + // where specifier is QuerySpecifierByDescendantType |
| 153 | + XCUIElementType keyboard = XCUIElementTypeKeyboard; |
| 154 | + XCUIElementType button = XCUIElementTypeButton; |
| 155 | + OCMExpect([queryMock matchingType:keyboard identifier:nil]).andReturn(contextQueryMock); |
| 156 | + OCMExpect([contextQueryMock descendantsMatchingType:button]).andReturn(resultQueryMock); |
| 157 | + // [Expectations] Stage 2: set up expectations for [query execute] |
| 158 | + OCMExpect([resultQueryMock allElementsBoundByIndex]).andReturn(arrayOfElements); |
| 159 | + |
| 160 | + // [Test Step] Stage 3: perform test step |
| 161 | + [query execute]; |
| 162 | + |
| 163 | + // [Check] Stage 4: verify [specifier applyInternal:query] |
| 164 | + OCMVerifyAll(queryMock); |
| 165 | + OCMVerifyAll(contextQueryMock); |
| 166 | + // [Check] Stage 4: verify [query execute] |
| 167 | + OCMVerifyAll(resultQueryMock); |
| 168 | +} |
| 169 | + |
| 170 | +@end |
0 commit comments