Flutter Engine
The Flutter Engine
AppExtensionTests.m
Go to the documentation of this file.
1// Copyright 2020 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#import <XCTest/XCTest.h>
7
8@interface AppExtensionTests : XCTestCase
9@property(nonatomic, strong) XCUIApplication* hostApplication;
10@end
11
12@implementation AppExtensionTests
13
14- (void)setUp {
15 [super setUp];
16 self.continueAfterFailure = NO;
17 self.hostApplication =
18 [[XCUIApplication alloc] initWithBundleIdentifier:@"dev.flutter.FlutterAppExtensionTestHost"];
19}
20
22 // Launch the Scenarios app first to ensure it's installed then close it.
23 XCUIApplication* app = [[XCUIApplication alloc] init];
24 [app launch];
25 [app terminate];
26
27 [self.hostApplication launch];
28 XCUIElement* button = self.hostApplication.buttons[@"Open Share"];
29 if (![button waitForExistenceWithTimeout:10]) {
30 NSLog(@"%@", self.hostApplication.debugDescription);
31 XCTFail(@"Failed due to not able to find any button with %@ seconds", @(10));
32 }
33 [button tap];
34 BOOL launchedExtensionInFlutter = NO;
35
36 // Custom share extension button (like the one in this test) does not have a
37 // unique identity on older versions of iOS. They are all identified as
38 // `XCElementSnapshotPrivilegedValuePlaceholder`. On iOS 17, they are
39 // identified by name. Loop through all the buttons labeled
40 // `XCElementSnapshotPrivilegedValuePlaceholder` or `Scenarios` to find the
41 // Flutter one.
42 NSPredicate* cellPredicate = [NSPredicate
43 predicateWithFormat:
44 @"label == 'XCElementSnapshotPrivilegedValuePlaceholder' OR label = 'Scenarios'"];
45
46 // Wait for the first cell matching the cellPredicate on the share sheet to appear.
47 XCUIElement* firstCell =
48 [self.hostApplication.collectionViews.cells matchingPredicate:cellPredicate].firstMatch;
49 if (![firstCell waitForExistenceWithTimeout:10]) {
50 NSLog(@"%@", self.hostApplication.debugDescription);
51 XCTFail(@"Failed due to not able to find Scenarios cell within %@ seconds", @(10));
52 }
53
54 NSArray<XCUIElement*>* shareSheetCells =
55 [self.hostApplication.collectionViews.cells matchingPredicate:cellPredicate]
56 .allElementsBoundByIndex;
57 for (XCUIElement* shareSheetCell in shareSheetCells) {
58 [shareSheetCell tap];
59
60 XCUIElement* flutterView = self.hostApplication.otherElements[@"flutter_view"];
61 if ([flutterView waitForExistenceWithTimeout:10]) {
62 launchedExtensionInFlutter = YES;
63 break;
64 }
65
66 // All the built-in share extensions have a Cancel button.
67 // Tap the Cancel button to close the built-in extension.
68 XCUIElement* cancel = self.hostApplication.buttons[@"Cancel"];
69 if ([cancel waitForExistenceWithTimeout:10]) {
70 [cancel tap];
71 }
72 }
73 // App extension successfully launched flutter view.
74 XCTAssertTrue(launchedExtensionInFlutter);
75}
76
77@end
XCUIApplication * hostApplication
int BOOL
Definition: windows_types.h:37