Skip to content

Commit eea3e91

Browse files
committed
Implmenet EarlGrey 2.0: google#1
1 parent c6b5049 commit eea3e91

18 files changed

+469
-1
lines changed

EarlGrey/Action/GREYActionBlock.m

+15
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818

1919
#import "Assertion/GREYAssertionDefines.h"
2020
#import "Common/GREYDefines.h"
21+
#import "Common/GREYTestHelper.h"
2122
#import "Matcher/GREYMatcher.h"
2223

2324
@implementation GREYActionBlock {
2425
GREYPerformBlock _performBlock;
26+
id<GREYMatcher> _constraints;
2527
}
2628

2729
+ (instancetype)actionWithName:(NSString *)name performBlock:(GREYPerformBlock)block {
@@ -41,10 +43,23 @@ - (instancetype)initWithName:(NSString *)name
4143
self = [super initWithName:name constraints:constraints];
4244
if (self) {
4345
_performBlock = block;
46+
_constraints = constraints;
4447
}
4548
return self;
4649
}
4750

51+
- (id)initWithCoder:(NSCoder *)coder {
52+
return [self initWithName:[coder decodeObjectForKey:@"name"]
53+
constraints:[coder decodeObjectForKey:@"constraints"]
54+
performBlock:[GREYTestHelper decodeBlock:[coder decodeObjectForKey:@"performBlock"]]];
55+
}
56+
57+
- (void)encodeWithCoder:(NSCoder *)coder {
58+
[coder encodeObject:[self name] forKey:@"name"];
59+
[coder encodeObject:_constraints forKey:@"constraints"];
60+
[coder encodeObject:[GREYTestHelper encodeBlock:_performBlock] forKey:@"performBlock"];
61+
}
62+
4863
#pragma mark - GREYAction
4964

5065
- (BOOL)perform:(id)element error:(__strong NSError **)errorOrNil {

EarlGrey/Action/GREYChangeStepperAction.m

+8
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ - (instancetype)initWithValue:(double)value {
4444
return self;
4545
}
4646

47+
- (id)initWithCoder:(NSCoder *)coder {
48+
return [self initWithValue:[coder decodeDoubleForKey:@"value"]];
49+
}
50+
51+
- (void)encodeWithCoder:(NSCoder *)coder {
52+
[coder encodeDouble:_value forKey:@"value"];
53+
}
54+
4755
#pragma mark - GREYAction
4856

4957
- (BOOL)perform:(UIStepper *)stepper error:(__strong NSError **)errorOrNil {

EarlGrey/Action/GREYPickerAction.m

+10
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ - (instancetype)initWithColumn:(NSInteger)column value:(NSString *)value {
4949
return self;
5050
}
5151

52+
- (id)initWithCoder:(NSCoder *)coder {
53+
return [self initWithColumn:[coder decodeIntegerForKey:@"column"]
54+
value:[coder decodeObjectForKey:@"value"]];
55+
}
56+
57+
- (void)encodeWithCoder:(NSCoder *)coder {
58+
[coder encodeInteger:_column forKey:@"column"];
59+
[coder encodeObject:_value forKey:@"value"];
60+
}
61+
5262
#pragma mark - GREYAction
5363

5464
- (BOOL)perform:(UIPickerView *)pickerView error:(__strong NSError **)errorOrNil {

EarlGrey/Action/GREYScrollAction.m

+13
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#import "Additions/NSString+GREYAdditions.h"
2525
#import "Additions/UIScrollView+GREYAdditions.h"
2626
#import "Assertion/GREYAssertionDefines.h"
27+
#import "Common/GREYTestHelper.h"
2728
#import "Event/GREYSyntheticEvents.h"
2829
#import "Matcher/GREYAllOf.h"
2930
#import "Matcher/GREYAnyOf.h"
@@ -82,6 +83,18 @@ - (instancetype)initWithDirection:(GREYDirection)direction amount:(CGFloat)amoun
8283
return [self initWithDirection:direction amount:amount startPointPercents:GREYCGPointNull];
8384
}
8485

86+
- (id)initWithCoder:(NSCoder *)coder {
87+
return [self initWithDirection:[coder decodeIntegerForKey:@"direction"]
88+
amount:[GREYTestHelper decodeCGFloat:[coder decodeObjectForKey:@"amount"]]
89+
startPointPercents:[GREYTestHelper decodeCGPoint:[coder decodeObjectForKey:@"startPointPercents"]]];
90+
}
91+
92+
- (void)encodeWithCoder:(NSCoder *)coder {
93+
[coder encodeInteger:_direction forKey:@"direction"];
94+
[coder encodeObject:[GREYTestHelper encodeCGFloat:_amount] forKey:@"amount"];
95+
[coder encodeObject:[GREYTestHelper encodeCGPoint:_startPointPercents] forKey:@"startPointPercents"];
96+
}
97+
8598
#pragma mark - GREYAction
8699

87100
- (BOOL)perform:(id)element error:(__strong NSError **)errorOrNil {

EarlGrey/Action/GREYScrollToContentEdgeAction.m

+11
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#import "Additions/NSString+GREYAdditions.h"
2525
#import "Additions/UIScrollView+GREYAdditions.h"
2626
#import "Assertion/GREYAssertionDefines.h"
27+
#import "Common/GREYTestHelper.h"
2728
#import "Common/GREYVisibilityChecker.h"
2829
#import "Event/GREYSyntheticEvents.h"
2930
#import "Matcher/GREYAllOf.h"
@@ -66,6 +67,16 @@ - (instancetype)initWithEdge:(GREYContentEdge)edge {
6667
return [self initWithEdge:edge startPointPercents:GREYCGPointNull];
6768
}
6869

70+
- (id)initWithCoder:(NSCoder *)coder {
71+
return [self initWithEdge:[coder decodeIntegerForKey:@"edge"]
72+
startPointPercents:[GREYTestHelper decodeCGPoint:[coder decodeObjectForKey:@"startPointPercents"]]];
73+
}
74+
75+
- (void)encodeWithCoder:(NSCoder *)coder {
76+
[coder encodeInteger:_edge forKey:@"edge"];
77+
[coder encodeObject:[GREYTestHelper encodeCGPoint:_startPointPercents] forKey:@"startPointPercents"];
78+
}
79+
6980
#pragma mark - GREYAction
7081

7182
- (BOOL)perform:(UIScrollView *)element error:(__strong NSError **)errorOrNil {

EarlGrey/Action/GREYSlideAction.m

+8
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ - (instancetype)initWithSliderValue:(float)value {
4747
return self;
4848
}
4949

50+
- (id)initWithCoder:(NSCoder *)coder {
51+
return [self initWithSliderValue:[coder decodeFloatForKey:@"finalValue"]];
52+
}
53+
54+
- (void)encodeWithCoder:(NSCoder *)coder {
55+
[coder encodeFloat:_finalValue forKey:@"finalValue"];
56+
}
57+
5058
#pragma mark - GREYAction
5159

5260
- (BOOL)perform:(UISlider *)slider error:(__strong NSError **)errorOrNil {

EarlGrey/Action/GREYSwipeAction.m

+13
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#import "Action/GREYPathGestureUtils.h"
2020
#import "Additions/NSString+GREYAdditions.h"
2121
#import "Assertion/GREYAssertionDefines.h"
22+
#import "Common/GREYTestHelper.h"
2223
#import "Event/GREYSyntheticEvents.h"
2324
#import "Matcher/GREYAllOf.h"
2425
#import "Matcher/GREYMatcher.h"
@@ -80,6 +81,18 @@ - (instancetype)initWithDirection:(GREYDirection)direction
8081
percentPoint:startPercents];
8182
}
8283

84+
- (id)initWithCoder:(NSCoder *)coder {
85+
return [self initWithDirection:[coder decodeIntegerForKey:@"direction"]
86+
duration:[coder decodeDoubleForKey:@"duration"]
87+
percentPoint:[GREYTestHelper decodeCGPoint:[coder decodeObjectForKey:@"startPercents"]]];
88+
}
89+
90+
- (void)encodeWithCoder:(NSCoder *)coder {
91+
[coder encodeInteger:_direction forKey:@"direction"];
92+
[coder encodeDouble:_duration forKey:@"duration"];
93+
[coder encodeObject:[GREYTestHelper encodeCGPoint:_startPercents] forKey:@"startPercents"];
94+
}
95+
8396
#pragma mark - GREYAction
8497

8598
- (BOOL)perform:(id)element error:(__strong NSError **)errorOrNil {

EarlGrey/Action/GREYTapAction.m

+15
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#import "Additions/NSError+GREYAdditions.h"
2222
#import "Additions/NSObject+GREYAdditions.h"
2323
#import "Common/GREYDefines.h"
24+
#import "Common/GREYTestHelper.h"
2425
#import "Common/GREYVisibilityChecker.h"
2526
#import "Core/GREYInteraction.h"
2627
#import "Matcher/GREYAllOf.h"
@@ -103,6 +104,20 @@ - (instancetype)initWithType:(GREYTapType)tapType
103104
return self;
104105
}
105106

107+
- (instancetype)initWithCoder:(NSCoder *)coder {
108+
return [self initWithType:[coder decodeIntegerForKey:@"type"]
109+
numberOfTaps:[GREYTestHelper decodeNSUInteger:[coder decodeObjectForKey:@"numberOfTaps"]]
110+
duration:[coder decodeDoubleForKey:@"duration"]
111+
location:[GREYTestHelper decodeCGPoint:[coder decodeObjectForKey:@"tapLocation"]]];
112+
}
113+
114+
- (void)encodeWithCoder:(NSCoder *)coder {
115+
[coder encodeInteger:_type forKey:@"type"];
116+
[coder encodeObject:[GREYTestHelper encodeNSUInteger:_numberOfTaps] forKey:@"numberOfTaps"];
117+
[coder encodeDouble:_duration forKey:@"duration"];
118+
[coder encodeObject:[GREYTestHelper encodeCGPoint:_tapLocation] forKey:@"tapLocation"];
119+
}
120+
106121
#pragma mark - GREYAction protocol
107122

108123
- (BOOL)perform:(id)element error:(__strong NSError **)errorOrNil {

EarlGrey/Assertion/GREYAssertionBlock.m

+11
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#import "Assertion/GREYAssertionDefines.h"
2020
#import "Common/GREYDefines.h"
21+
#import "Common/GREYTestHelper.h"
2122

2223
@implementation GREYAssertionBlock {
2324
NSString *_name;
@@ -42,6 +43,16 @@ - (instancetype)initWithName:(NSString *)name
4243
return self;
4344
}
4445

46+
- (id)initWithCoder:(NSCoder *)coder {
47+
return [self initWithName:[coder decodeObjectForKey:@"name"]
48+
assertionBlockWithError:[GREYTestHelper decodeBlock:[coder decodeObjectForKey:@"checkBlockWithError"]]];
49+
}
50+
51+
- (void)encodeWithCoder:(NSCoder *)coder {
52+
[coder encodeObject:_name forKey:@"name"];
53+
[coder encodeObject:[GREYTestHelper encodeBlock:_checkBlockWithError] forKey:@"checkBlockWithError"];
54+
}
55+
4556
#pragma mark - GREYAssertion
4657

4758
- (NSString *)name {

EarlGrey/Assertion/GREYAssertionDefines.h

+12-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#import <EarlGrey/GREYDefines.h>
2626
#import <EarlGrey/GREYFailureHandler.h>
2727
#import <EarlGrey/GREYFrameworkException.h>
28+
#import <EarlGrey/GREYTestHelper.h>
2829

2930
GREY_EXTERN id<GREYFailureHandler> greyFailureHandler;
3031

@@ -328,7 +329,17 @@ GREY_EXTERN id<GREYFailureHandler> greyFailureHandler;
328329
##__VA_ARGS__)
329330

330331
#define I_CHECK_MAIN_THREAD() \
331-
I_GREYAssertTrue([NSThread isMainThread], @"Must be on the main thread.")
332+
I_GREYAssertTrue([NSThread isMainThread], @"Must be on the main thread.\n")
333+
334+
#define I_CHECK_APPLICATION_PROCESS() \
335+
I_GREYAssertTrue([GREYTestHelper isInApplicationProcess], @"Must be in an application process.\n")
336+
337+
#define I_CHECK_REMOTE_APPLICATION_PROCESS() \
338+
I_GREYAssertTrue([GREYTestHelper isInRemoteApplicationProcess], \
339+
@"Must be in a remote application process.\n")
340+
341+
#define I_CHECK_XCTEST_PROCESS() \
342+
I_GREYAssertTrue([GREYTestHelper isInXCTestProcess], @"Must be in the process running XCTest.\n")
332343

333344
/// @endcond
334345

EarlGrey/Common/GREYDefines.h

+5
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@
3333
#define iOS8_2_OR_ABOVE() ([UIDevice currentDevice].systemVersion.doubleValue >= 8.2)
3434
#define iOS9_OR_ABOVE() ([UIDevice currentDevice].systemVersion.intValue >= 9)
3535

36+
// Used for Objective-C block syntax.
37+
typedef void(^GREYExecBlock)(void);
38+
// Used for Swift block syntax.
39+
typedef void(*GREYExecFunction)(void);
40+
3641
#pragma mark - Math
3742

3843
/**

EarlGrey/Common/GREYExposed.h

+36
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,35 @@
2121

2222
#import <UIKit/UIKit.h>
2323

24+
// Source: https://opensource.apple.com/source/libclosure/libclosure-65/Block_private.h
25+
struct Block_descriptor {
26+
unsigned long int reserved;
27+
unsigned long int size;
28+
void (*copy)(void *dst, void *src);
29+
void (*dispose)(void *);
30+
};
31+
32+
struct Block_layout {
33+
void *isa;
34+
int flags;
35+
int reserved;
36+
void (*invoke)(void);
37+
struct Block_descriptor *descriptor;
38+
/* imported variables */
39+
};
40+
41+
enum {
42+
BLOCK_REFCOUNT_MASK = (0xffff),
43+
BLOCK_NEEDS_FREE = (1 << 24),
44+
BLOCK_HAS_COPY_DISPOSE = (1 << 25),
45+
BLOCK_HAS_CTOR = (1 << 26), /* Helpers have C++ code. */
46+
BLOCK_IS_GC = (1 << 27),
47+
BLOCK_IS_GLOBAL = (1 << 28),
48+
BLOCK_HAS_DESCRIPTOR = (1 << 29)
49+
};
50+
51+
CFNotificationCenterRef CFNotificationCenterGetDistributedCenter(void);
52+
2453
@interface UIWindow (GREYExposed)
2554
- (id)firstResponder;
2655
@end
@@ -359,3 +388,10 @@ IOHIDEventRef IOHIDEventCreateDigitizerFingerEvent(CFAllocatorRef allocator,
359388
*/
360389
- (void)setPredictionPreferenceValue:(NSNumber *)enabled forSpecifier:(id)specifier;
361390
@end
391+
392+
@interface XCTestConfiguration : NSObject
393+
394+
+ (instancetype)activeTestConfiguration;
395+
- (NSString *)targetApplicationBundleID;
396+
397+
@end

EarlGrey/Common/GREYTestHelper.h

+24
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
#import <UIKit/UIKit.h>
1818

19+
#import <EarlGrey/GREYDefines.h>
20+
1921
/**
2022
* Provides interface for test helper methods.
2123
*/
@@ -32,4 +34,26 @@
3234
*/
3335
+ (void)disableFastAnimation;
3436

37+
+ (BOOL)isSystemAlertShown;
38+
+ (BOOL)isInApplicationProcess;
39+
+ (BOOL)isInRemoteApplicationProcess;
40+
+ (BOOL)isInXCTestProcess;
41+
42+
+ (NSString *)absoluteXCTestPluginPath;
43+
+ (NSString *)relativeXCTestPluginPath;
44+
+ (NSString *)absoluteEarlGreyPath;
45+
+ (NSString *)relativeEarlGreyPath;
46+
+ (NSString *)targetApplicationBundleID;
47+
48+
+ (NSDictionary *)encodeBlock:(id)block;
49+
+ (id)decodeBlock:(NSDictionary *)blockDictionary;
50+
+ (NSDictionary *)encodeFunction:(GREYExecFunction)function;
51+
+ (GREYExecFunction)decodeFunction:(NSDictionary *)dictionary;
52+
+ (NSDictionary *)encodeCGPoint:(CGPoint)value;
53+
+ (CGPoint)decodeCGPoint:(NSDictionary *)dictionary;
54+
+ (NSDictionary *)encodeNSUInteger:(NSUInteger)value;
55+
+ (NSUInteger)decodeNSUInteger:(NSDictionary *)dictionary;
56+
+ (NSDictionary *)encodeCGFloat:(CGFloat)value;
57+
+ (CGFloat)decodeCGFloat:(NSDictionary *)dictionary;
58+
3559
@end

0 commit comments

Comments
 (0)