pEpForiOSUITests/NewAccountSetupUITest.swift
author Dirk Zimmermann <dz@pep.security>
Thu, 21 Mar 2019 12:44:24 +0100
branchIOS-1480
changeset 8033 bf94050222a1
parent 7161 0464791a0e21
child 8491 79e316b1974c
permissions -rw-r--r--
IOS-1480 Close branch.
     1 //
     2 //  NewAccountSetupUITest.swift
     3 //  pEpForiOS
     4 //
     5 //  Created by Dirk Zimmermann on 20/05/16.
     6 //  Copyright © 2016 p≡p Security S.A. All rights reserved.
     7 //
     8 
     9 import XCTest
    10 
    11 class NewAccountSetupUITest: XCTestCase {
    12     // MARK: - Setup
    13 
    14     override func setUp() {
    15         super.setUp()
    16 
    17         // In UI tests it is usually best to stop immediately when a failure occurs.
    18         continueAfterFailure = false
    19     }
    20 
    21     // MARK: - Tests
    22 
    23     func testInitialAccountSetup1() {
    24         app().launch()
    25 
    26         dismissInitialSystemAlerts()
    27 
    28         let account = SecretUITestData.workingAccount1
    29         newAccountSetup(account: account)
    30         waitForever()
    31     }
    32 
    33     func testInitialAccountSetup2() {
    34         app().launch()
    35 
    36         dismissInitialSystemAlerts()
    37 
    38         let account = SecretUITestData.workingAccount2
    39         newAccountSetup(account: account)
    40         waitForever()
    41     }
    42 
    43     func testAdditionalAccount() {
    44         app().launch()
    45         addAccount()
    46         let account = SecretUITestData.workingAccount3
    47         newAccountSetup(account: account)
    48         waitForever()
    49     }
    50 
    51     func testAdditionalManualAccount() {
    52         app().launch()
    53         addAdditionalManual(account: SecretUITestData.manualAccount)
    54     }
    55 
    56     func testAutoAccountPlusManual() {
    57         app().launch()
    58 
    59         dismissInitialSystemAlerts()
    60 
    61         let account1 = SecretUITestData.workingAccount1
    62         newAccountSetup(account: account1)
    63 
    64         addAdditionalManual(account: SecretUITestData.manualAccount)
    65     }
    66 
    67     func testTwoInitialAccounts() {
    68         app().launch()
    69 
    70         dismissInitialSystemAlerts()
    71 
    72         let account1 = SecretUITestData.workingAccount1
    73         newAccountSetup(account: account1)
    74 
    75         addAccount()
    76 
    77         let account2 = SecretUITestData.workingAccount2
    78         newAccountSetup(account: account2)
    79         waitForever()
    80     }
    81 
    82     func testNewAccountSetupManual() {
    83         let theApp = app()
    84 
    85         theApp.launch()
    86 
    87         dismissInitialSystemAlerts()
    88 
    89         let account = SecretUITestData.manualAccount
    90         newAccountSetup(account: account)
    91 
    92         switchToManualConfig()
    93 
    94         manualNewAccountSetup(account)
    95 
    96         waitForever()
    97     }
    98 
    99     func testTriggerGmailOauth2() {
   100         app().launch()
   101 
   102         dismissInitialSystemAlerts()
   103 
   104         let account = SecretUITestData.gmailOAuth2Account
   105         newAccountSetup(account: account, enterPassword: false)
   106         waitForever()
   107     }
   108 
   109     func testTriggerYahooOauth2() {
   110         app().launch()
   111 
   112         dismissInitialSystemAlerts()
   113 
   114         let account = SecretUITestData.yahooOAuth2Account
   115         newAccountSetup(account: account, enterPassword: false)
   116         waitForever()
   117     }
   118 
   119     // MARK: - Helpers
   120 
   121     func app() -> XCUIApplication {
   122         let app = XCUIApplication()
   123         app.launchEnvironment = ["ASAN_OPTIONS": "detect_odr_violation=0"]
   124         return app
   125     }
   126 
   127     /*
   128      Use if you want to wait forever. May be useful for debugging.
   129      */
   130     func waitForever() {
   131         let _ = expectation(description: "Never happens")
   132         waitForExpectations(timeout: 3000, handler: nil)
   133     }
   134 
   135     func typeTextIfEmpty(textField: XCUIElement, text: String) {
   136         if (textField.value as? String ?? "") == "" {
   137             textField.typeText(text)
   138         }
   139     }
   140 
   141     func manualNewAccountSetup(_ account: UIAccount) {
   142         let theApp = app()
   143         let tablesQuery = theApp.tables
   144 
   145         var tf = tablesQuery.cells.textFields["username"]
   146         typeTextIfEmpty(textField: tf, text: account.nameOfTheUser)
   147 
   148         tf = tablesQuery.cells.textFields["email"]
   149         tf.tap()
   150         typeTextIfEmpty(textField: tf, text: account.email)
   151 
   152         tf = tablesQuery.cells.secureTextFields["password"]
   153         tf.tap()
   154         typeTextIfEmpty(textField: tf, text: account.password)
   155 
   156         theApp.navigationBars.buttons["Next"].tap()
   157 
   158         tf = tablesQuery.textFields["imapServer"]
   159         tf.typeText(account.imapServerName)
   160         tf = tablesQuery.textFields["imapPort"]
   161         tf.tap()
   162         tf.clearAndEnter(text: String(account.imapPort))
   163 
   164         tablesQuery.buttons["imapTransportSecurity"].tap()
   165         let sheet = theApp.sheets["Transport protocol"]
   166         sheet.buttons[account.imapTransportSecurityString].tap()
   167 
   168         theApp.navigationBars.buttons["Next"].tap()
   169 
   170         tf = tablesQuery.textFields["smtpServer"]
   171         tf.typeText(account.smtpServerName)
   172         tf = tablesQuery.textFields["smtpPort"]
   173         tf.tap()
   174         tf.clearAndEnter(text: String(account.smtpPort))
   175 
   176         tablesQuery.buttons["smtpTransportSecurity"].tap()
   177         sheet.buttons[account.smtpTransportSecurityString].tap()
   178 
   179         let nextButton = theApp.navigationBars.buttons["Next"]
   180         nextButton.tap()
   181     }
   182 
   183     func newAccountSetup(account: UIAccount, enterPassword: Bool = true) {
   184         signIn(account: account, enterPassword: enterPassword)
   185     }
   186 
   187     /**
   188      Dismisses the initial system alerts (access to contacts, allow notifications).
   189      */
   190     func dismissInitialSystemAlerts() {
   191         dismissSystemAlert(buttonTitle: "Allow")
   192         dismissSystemAlert(buttonTitle: "OK")
   193     }
   194 
   195     func dismissSystemAlert(buttonTitle: String) {
   196         let springboard = XCUIApplication(bundleIdentifier: "com.apple.springboard")
   197         let button = springboard.buttons[buttonTitle]
   198         if button.exists {
   199             button.tap()
   200         } else {
   201             let exists = NSPredicate(format: "enabled == true")
   202             expectation(for: exists, evaluatedWith: button, handler: nil)
   203             waitForExpectations(timeout: 2, handler: nil)
   204             button.tap()
   205         }
   206     }
   207 
   208     func addAdditionalManual(account: UIAccount) {
   209         addAccount()
   210 
   211         signIn(account: account, enterPassword: true)
   212         switchToManualConfig()
   213         manualNewAccountSetup(account)
   214 
   215         waitForever()
   216     }
   217 
   218     func signIn(account: UIAccount, enterPassword: Bool = true) {
   219         let theApp = app()
   220         let textFieldsQuery = theApp.textFields
   221 
   222         var tf = textFieldsQuery["username"]
   223         tf.tap()
   224         tf.typeText(account.nameOfTheUser)
   225 
   226         tf = textFieldsQuery["email"]
   227         tf.tap()
   228         tf.typeText(account.email)
   229 
   230         if enterPassword {
   231             tf = theApp.secureTextFields["password"]
   232             tf.tap()
   233             tf.typeText(account.password)
   234         }
   235 
   236         theApp.buttons["Sign In"].tap()
   237     }
   238 
   239     func switchToManualConfig() {
   240         let theApp = app()
   241 
   242         let alertOkButton = theApp.buttons["OK"]
   243         let exists = NSPredicate(format: "enabled == true")
   244         expectation(for: exists, evaluatedWith: alertOkButton, handler: nil)
   245         waitForExpectations(timeout: 5, handler: nil)
   246 
   247         alertOkButton.tap()
   248         theApp.buttons["Manual configuration"].tap()
   249     }
   250 
   251     func addAccount() {
   252         let theApp = app()
   253         theApp.navigationBars["All"].buttons["Folders"].tap()
   254         theApp.tables.buttons["Add Account"].tap()
   255     }
   256 }