-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathFBSession.m
173 lines (153 loc) · 5.45 KB
/
FBSession.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#import "FBSession.h"
#import "FBSession-Private.h"
#import <objc/runtime.h>
#import "FBConfiguration.h"
#import "FBElementCache.h"
#import "FBExceptions.h"
#import "FBMacros.h"
#import "XCUIApplication+AMHelpers.h"
NSString *const FINDER_BUNDLE_ID = @"com.apple.finder";
@interface FBSession ()
@property (nonatomic, nullable) XCUIApplication *testedApplication;
@end
@implementation FBSession
static FBSession *_activeSession = nil;
+ (instancetype)activeSession
{
return _activeSession;
}
+ (void)markSessionActive:(FBSession *)session
{
[_activeSession kill];
_activeSession = session;
}
+ (instancetype)sessionWithIdentifier:(NSString *)identifier
{
if (!identifier) {
return nil;
}
if (![identifier isEqualToString:_activeSession.identifier]) {
return nil;
}
return _activeSession;
}
+ (instancetype)initWithApplication:(XCUIApplication *)application
{
FBSession *session = [FBSession new];
session.identifier = [[NSUUID UUID] UUIDString];
session.testedApplication = application;
session.elementCache = [FBElementCache new];
[FBSession markSessionActive:session];
return session;
}
- (void)kill
{
if (!self.skipAppTermination
&& nil != self.testedApplication
&& self.testedApplication.state > XCUIApplicationStateNotRunning
&& ![self.testedApplication.am_bundleID isEqualToString:FINDER_BUNDLE_ID]) {
[self.testedApplication terminate];
}
self.testedApplication = nil;
[self.elementCache reset];
_activeSession = nil;
}
- (XCUIApplication *)currentApplication
{
if (nil != self.testedApplication) {
if (self.testedApplication.state <= XCUIApplicationStateNotRunning) {
NSString *description = @"The application under test is not running, possibly crashed";
@throw [NSException exceptionWithName:FBApplicationCrashedException reason:description userInfo:nil];
}
return self.testedApplication;
}
return [[XCUIApplication alloc] initWithBundleIdentifier:FINDER_BUNDLE_ID];
}
- (XCUIApplication *)launchApplicationWithBundleId:(nullable NSString *)bundleIdentifier
path:(nullable NSString *)path
arguments:(nullable NSArray<NSString *> *)arguments
environment:(nullable NSDictionary <NSString *, NSString *> *)environment
{
XCUIApplication *app = [self applicationWithBundleId:bundleIdentifier orPath:path];
if (app.state <= XCUIApplicationStateNotRunning) {
app.launchArguments = arguments ?: @[];
app.launchEnvironment = environment ?: @{};
[app launch];
} else {
[app activate];
}
self.testedApplication = app;
return app;
}
- (XCUIApplication *)activateApplicationWithBundleId:(nullable NSString *)bundleIdentifier
path:(nullable NSString *)path
{
BOOL isCurrentApp = [self isCurrentApplicationForBundleId:bundleIdentifier orPath:path];
XCUIApplication *app = isCurrentApp
? self.testedApplication
: [self applicationWithBundleId:bundleIdentifier orPath:path];
[app activate];
if (!isCurrentApp) {
self.testedApplication = app;
}
return app;
}
- (BOOL)terminateApplicationWithBundleId:(nullable NSString *)bundleIdentifier
path:(nullable NSString *)path
{
BOOL isCurrentApp = [self isCurrentApplicationForBundleId:bundleIdentifier orPath:path];
XCUIApplication *app = isCurrentApp
? self.testedApplication
: [self applicationWithBundleId:bundleIdentifier orPath:path];
BOOL result = NO;
if (app.state > XCUIApplicationStateNotRunning) {
[app terminate];
result = YES;
}
if (isCurrentApp) {
self.testedApplication = nil;
}
return result;
}
- (NSUInteger)applicationStateWithBundleId:(nullable NSString *)bundleIdentifier
path:(nullable NSString *)path
{
XCUIApplication *app = [self isCurrentApplicationForBundleId:bundleIdentifier orPath:path]
? self.testedApplication
: [self applicationWithBundleId:bundleIdentifier orPath:path];
return app.state;
}
- (BOOL)isCurrentApplicationForBundleId:(nullable NSString *)bundleId
orPath:(nullable NSString *)path
{
if (nil == self.testedApplication) {
return NO;
}
if (nil != path) {
NSURL *appUrl = [NSURL fileURLWithPath:self.testedApplication.am_path].URLByStandardizingPath;
NSURL *expectedUrl = [NSURL fileURLWithPath:path].URLByStandardizingPath;
return [appUrl.path compare:expectedUrl.path] == NSOrderedSame;
}
return nil != bundleId && [self.testedApplication.am_bundleID isEqualToString:bundleId];
}
- (XCUIApplication *)applicationWithBundleId:(nullable NSString *)bundleId
orPath:(nullable NSString *)path
{
if (nil == bundleId && nil == path) {
@throw [NSException exceptionWithName:FBInvalidArgumentException
reason:@"Either app bundle identifier or app path must be provided"
userInfo:@{}];
}
return nil != path
? [[XCUIApplication alloc] initWithURL:[NSURL fileURLWithPath:(id)path]]
: [[XCUIApplication alloc] initWithBundleIdentifier:(id)bundleId];
}
@end