<commit_before>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import XCTest

class BaseTestCase: XCTestCase {

    override func setUp() {
        super.setUp()
        continueAfterFailure = false
        let app = XCUIApplication()
        restart(app)
    }

    override func tearDown() {
        XCUIApplication().terminate()
        super.tearDown()
    }

    func restart(_ app: XCUIApplication) {
        app.terminate()
        app.launchArguments.append(LaunchArguments.Test)
        app.launchArguments.append(LaunchArguments.ClearProfile)
        app.launch()
        sleep(1)
    }
    
    //If it is a first run, first run window should be gone
    func dismissFirstRunUI() {
        let firstRunUI = XCUIApplication().buttons["Start Browsing"]
        
        if (firstRunUI.exists) {
            firstRunUI.tap()
        }
    }
    
    func waitforExistence(_ element: XCUIElement) {
        let exists = NSPredicate(format: "exists == true")
        
        expectation(for: exists, evaluatedWith: element, handler: nil)
        waitForExpectations(timeout: 20, handler: nil)
    }
    
    func waitforNoExistence(_ element: XCUIElement) {
        let exists = NSPredicate(format: "exists != true")
        
        expectation(for: exists, evaluatedWith: element, handler: nil)
        waitForExpectations(timeout: 20, handler: nil)
    }
    
    func waitForValueContains(_ element: XCUIElement, value: String) {
        let predicateText = "value CONTAINS " + "'" + value + "'"
        let valueCheck = NSPredicate(format: predicateText)
        
        expectation(for: valueCheck, evaluatedWith: element, handler: nil)
        waitForExpectations(timeout: 20, handler: nil)
    }

    func loadWebPage(_ url: String, waitForLoadToFinish: Bool = true) {
        let loaded = NSPredicate(format: "value BEGINSWITH '100'")

        let app = XCUIApplication()

        UIPasteboard.general.string = url
        app.textFields["url"].press(forDuration: 2.0)
        app.sheets.element(boundBy: 0).buttons.element(boundBy: 0).tap()

        if waitForLoadToFinish {
            let finishLoadingTimeout: TimeInterval = 30
            
            let progressIndicator = app.progressIndicators.element(boundBy: 0)
            expectation(for: loaded, evaluatedWith: progressIndicator, handler: nil)
            waitForExpectations(timeout: finishLoadingTimeout, handler: nil)
        }
    }

}

extension BaseTestCase {
    func tabTrayButton(forApp app: XCUIApplication) -> XCUIElement {
        return app.buttons["TopTabsViewController.tabsButton"].exists ? app.buttons["TopTabsViewController.tabsButton"] : app.buttons["URLBarView.tabsButton"]
    }
}

extension XCUIElement {
    func tap(force: Bool) {
        // There appears to be a bug with tapping elements sometimes, despite them being on-screen and tappable, due to hittable being false.
        // See: http://stackoverflow.com/a/33534187/1248491
        if isHittable {
            tap()
        } else if force {
            coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.5)).tap()
        }<commit_msg>wait until the progressIndicator disappears<commit_after>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import XCTest

class BaseTestCase: XCTestCase {

    override func setUp() {
        super.setUp()
        continueAfterFailure = false
        let app = XCUIApplication()
        restart(app)
    }

    override func tearDown() {
        XCUIApplication().terminate()
        super.tearDown()
    }

    func restart(_ app: XCUIApplication) {
        app.terminate()
        app.launchArguments.append(LaunchArguments.Test)
        app.launchArguments.append(LaunchArguments.ClearProfile)
        app.launch()
        sleep(1)
    }
    
    //If it is a first run, first run window should be gone
    func dismissFirstRunUI() {
        let firstRunUI = XCUIApplication().buttons["Start Browsing"]
        
        if (firstRunUI.exists) {
            firstRunUI.tap()
        }
    }
    
    func waitforExistence(_ element: XCUIElement) {
        let exists = NSPredicate(format: "exists == true")
        
        expectation(for: exists, evaluatedWith: element, handler: nil)
        waitForExpectations(timeout: 20, handler: nil)
    }
    
    func waitforNoExistence(_ element: XCUIElement) {
        let exists = NSPredicate(format: "exists != true")
        
        expectation(for: exists, evaluatedWith: element, handler: nil)
        waitForExpectations(timeout: 20, handler: nil)
    }
    
    func waitForValueContains(_ element: XCUIElement, value: String) {
        let predicateText = "value CONTAINS " + "'" + value + "'"
        let valueCheck = NSPredicate(format: predicateText)
        
        expectation(for: valueCheck, evaluatedWith: element, handler: nil)
        waitForExpectations(timeout: 20, handler: nil)
    }

    func loadWebPage(_ url: String, waitForLoadToFinish: Bool = true) {
        let app = XCUIApplication()
        UIPasteboard.general.string = url
        app.textFields["url"].press(forDuration: 2.0)
        app.sheets.element(boundBy: 0).buttons.element(boundBy: 0).tap()

        if waitForLoadToFinish {
            let finishLoadingTimeout: TimeInterval = 30
            let progressIndicator = app.progressIndicators.element(boundBy: 0)
            expectation(for: NSPredicate(format: "exists = false"), evaluatedWith: progressIndicator, handler: nil)
            waitForExpectations(timeout: finishLoadingTimeout, handler: nil)
        }
    }

}

extension BaseTestCase {
    func tabTrayButton(forApp app: XCUIApplication) -> XCUIElement {
        return app.buttons["TopTabsViewController.tabsButton"].exists ? app.buttons["TopTabsViewController.tabsButton"] : app.buttons["URLBarView.tabsButton"]
    }
}

extension XCUIElement {
    func tap(force: Bool) {
        // There appears to be a bug with tapping elements sometimes, despite them being on-screen and tappable, due to hittable being false.
        // See: http://stackoverflow.com/a/33534187/1248491
        if isHittable {
            tap()
        } else if force {
            coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.5)).tap()
        }
{'fraction_non_alphanumeric': 0.09771086188354586, 'fraction_numerical': 0.011829774158856967, 'mean_word_length': 2.62876254180602, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1754173', 'n_tokens_mistral': 1935, 'n_tokens_neox': 1845, 'n_words': 482}