FlutterUITests.m 5.28 KB
Newer Older
1 2 3 4 5
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

@import XCTest;
6
@import os.log;
7

8 9
static const CGFloat kStandardTimeOut = 60.0;

10
@interface FlutterUITests : XCTestCase
11
@property (strong) XCUIApplication *app;
12 13 14 15 16
@end

@implementation FlutterUITests

- (void)setUp {
17
    [super setUp];
18 19 20 21
    self.continueAfterFailure = NO;

    XCUIApplication *app = [[XCUIApplication alloc] init];
    [app launch];
22 23
    self.app = app;
}
24

25 26 27
- (void)testFullScreenColdPop {
    XCUIApplication *app = self.app;
    [self waitForAndTapElement:app.buttons[@"Full Screen (Cold)"]];
28
    XCTAssertTrue([app.staticTexts[@"Button tapped 0 times."] waitForExistenceWithTimeout:kStandardTimeOut]);
29 30

    [self waitForAndTapElement:app.otherElements[@"Increment via Flutter"]];
31
    XCTAssertTrue([app.staticTexts[@"Button tapped 1 time."] waitForExistenceWithTimeout:kStandardTimeOut]);
32 33 34

    // Back navigation.
    [app.buttons[@"POP"] tap];
35
    XCTAssertTrue([app.navigationBars[@"Flutter iOS Demos Home"] waitForExistenceWithTimeout:kStandardTimeOut]);
36 37 38
}

- (void)testFullScreenWarm {
39
    XCUIApplication *app = self.app;
40

41
    [self waitForAndTapElement:app.buttons[@"Full Screen (Warm)"]];
42 43 44 45
    BOOL newPageAppeared = [app.staticTexts[@"Button tapped 0 times."] waitForExistenceWithTimeout:kStandardTimeOut];
    if (!newPageAppeared) {
        // Sometimes, the element doesn't respond to the tap, it seems an XCUITest race condition where the tap happened
        // too soon. Trying to tap the element again.
46
        [self waitForAndTapElement:app.buttons[@"Full Screen (Warm)"]];
47 48 49
        newPageAppeared = [app.staticTexts[@"Button tapped 0 times."] waitForExistenceWithTimeout:kStandardTimeOut];
    }
    XCTAssertTrue(newPageAppeared);
50 51

    [self waitForAndTapElement:app.otherElements[@"Increment via Flutter"]];
52
    XCTAssertTrue([app.staticTexts[@"Button tapped 1 time."] waitForExistenceWithTimeout:kStandardTimeOut]);
53 54 55

    // Back navigation.
    [app.buttons[@"POP"] tap];
56
    XCTAssertTrue([app.navigationBars[@"Flutter iOS Demos Home"] waitForExistenceWithTimeout:kStandardTimeOut]);
57 58 59
}

- (void)testFlutterViewWarm {
60
    XCUIApplication *app = self.app;
61

62
    [self waitForAndTapElement:app.buttons[@"Flutter View (Warm)"]];
63 64
    BOOL newPageAppeared = [app.staticTexts[@"Button tapped 0 times."] waitForExistenceWithTimeout:kStandardTimeOut];
    if (!newPageAppeared) {
65 66 67 68 69 70 71
      // Sometimes, the element doesn't respond to the tap, it seems an XCUITest race condition where the tap happened
      // too soon. Trying to tap the element again.
      [self waitForAndTapElement:app.buttons[@"Flutter View (Warm)"]];
      newPageAppeared = [app.staticTexts[@"Button tapped 0 times."] waitForExistenceWithTimeout:kStandardTimeOut];
      if (!newPageAppeared) {
        os_log(OS_LOG_DEFAULT, "%@", app.debugDescription);
      }
72 73
    }
    XCTAssertTrue(newPageAppeared);
74 75

    [self waitForAndTapElement:app.otherElements[@"Increment via Flutter"]];
76
    XCTAssertTrue([app.staticTexts[@"Button tapped 1 time."] waitForExistenceWithTimeout:kStandardTimeOut]);
77 78 79

    // Back navigation.
    [app.buttons[@"POP"] tap];
80
    XCTAssertTrue([app.navigationBars[@"Flutter iOS Demos Home"] waitForExistenceWithTimeout:kStandardTimeOut]);
81 82 83
}

- (void)testHybridViewWarm {
84
    XCUIApplication *app = self.app;
85

86
    [self waitForAndTapElement:app.buttons[@"Hybrid View (Warm)"]];
87

88
    XCTAssertTrue([app.staticTexts[@"Flutter button tapped 0 times."] waitForExistenceWithTimeout:kStandardTimeOut]);
89
    XCTAssertTrue(app.staticTexts[@"Platform button tapped 0 times."].exists);
90

91
    [self waitForAndTapElement:app.otherElements[@"Increment via Flutter"]];
92
    XCTAssertTrue([app.staticTexts[@"Flutter button tapped 1 time."] waitForExistenceWithTimeout:kStandardTimeOut]);
93
    XCTAssertTrue(app.staticTexts[@"Platform button tapped 0 times."].exists);
94 95

    [app.buttons[@"Increment via iOS"] tap];
96
    XCTAssertTrue([app.staticTexts[@"Flutter button tapped 1 time."] waitForExistenceWithTimeout:kStandardTimeOut]);
97
    XCTAssertTrue(app.staticTexts[@"Platform button tapped 1 time."].exists);
98 99 100

    // Back navigation.
    [app.navigationBars[@"Hybrid Flutter/Native"].buttons[@"Flutter iOS Demos Home"] tap];
101
    XCTAssertTrue([app.navigationBars[@"Flutter iOS Demos Home"] waitForExistenceWithTimeout:kStandardTimeOut]);
102 103 104
}

- (void)testDualCold {
105
    XCUIApplication *app = self.app;
106

107
    [self waitForAndTapElement:app.buttons[@"Dual Flutter View (Cold)"]];
108 109

    // There are two marquees.
110 111 112
    XCUIElementQuery *marqueeQuery = [app.staticTexts matchingIdentifier:@"This is Marquee"];
    [self expectationForPredicate:[NSPredicate predicateWithFormat:@"count = 2"] evaluatedWithObject:marqueeQuery handler:nil];
    [self waitForExpectationsWithTimeout:30.0 handler:nil];
113 114 115

    // Back navigation.
    [app.navigationBars[@"Dual Flutter Views"].buttons[@"Flutter iOS Demos Home"] tap];
116
    XCTAssertTrue([app.navigationBars[@"Flutter iOS Demos Home"] waitForExistenceWithTimeout:kStandardTimeOut]);
117 118
}

119 120 121 122 123 124 125
- (void)waitForAndTapElement:(XCUIElement *)element {
    NSPredicate *hittable = [NSPredicate predicateWithFormat:@"exists == YES AND hittable == YES"];
    [self expectationForPredicate:hittable evaluatedWithObject:element handler:nil];
    [self waitForExpectationsWithTimeout:30.0 handler:nil];
    [element tap];
}

126
@end