merge refactor IOS-1494B
authorAlejandro Gelos <agp@pep.security>
Wed, 22 May 2019 15:39:47 +0200
branchIOS-1494B
changeset 87318756c7e28898
parent 8730 f89052c258f8
parent 8727 fc81e2e79970
child 8740 16259452abc2
merge refactor
pEpForiOS/UI/EmailDisplayList/EmailListViewController.swift
pEpForiOS/UI/EmailDisplayList/EmailListViewModel.swift
     1.1 --- a/pEpForiOS.xcodeproj/xcshareddata/xcschemes/pEp.xcscheme	Wed May 22 15:33:21 2019 +0200
     1.2 +++ b/pEpForiOS.xcodeproj/xcshareddata/xcschemes/pEp.xcscheme	Wed May 22 15:39:47 2019 +0200
     1.3 @@ -269,7 +269,7 @@
     1.4        stopOnEveryUBSanitizerIssue = "YES"
     1.5        stopOnEveryMainThreadCheckerIssue = "YES"
     1.6        debugServiceExtension = "internal"
     1.7 -      allowLocationSimulation = "YES">
     1.8 +      allowLocationSimulation = "NO">
     1.9        <BuildableProductRunnable
    1.10           runnableDebuggingMode = "0">
    1.11           <BuildableReference
    1.12 @@ -299,16 +299,6 @@
    1.13           </EnvironmentVariable>
    1.14        </EnvironmentVariables>
    1.15        <AdditionalOptions>
    1.16 -         <AdditionalOption
    1.17 -            key = "NSZombieEnabled"
    1.18 -            value = "YES"
    1.19 -            isEnabled = "YES">
    1.20 -         </AdditionalOption>
    1.21 -         <AdditionalOption
    1.22 -            key = "MallocScribble"
    1.23 -            value = ""
    1.24 -            isEnabled = "YES">
    1.25 -         </AdditionalOption>
    1.26        </AdditionalOptions>
    1.27     </LaunchAction>
    1.28     <ProfileAction
     2.1 --- a/pEpForiOS/UI/Compose/Cells/RecipientCell/RecipientTextView/RecipientTextViewModel.swift	Wed May 22 15:33:21 2019 +0200
     2.2 +++ b/pEpForiOS/UI/Compose/Cells/RecipientCell/RecipientTextView/RecipientTextViewModel.swift	Wed May 22 15:39:47 2019 +0200
     2.3 @@ -137,6 +137,7 @@
     2.4                  identity = existing
     2.5              } else {
     2.6                  identity = Identity(address: address)
     2.7 +                identity.save()
     2.8              }
     2.9              var (newText, attachment) = text.imageInserted(withAddressOf: identity,
    2.10                                                             in: range,
     3.1 --- a/pEpForiOS/UI/Compose/ViewModel/ComposeViewModelState.swift	Wed May 22 15:33:21 2019 +0200
     3.2 +++ b/pEpForiOS/UI/Compose/ViewModel/ComposeViewModelState.swift	Wed May 22 15:39:47 2019 +0200
     3.3 @@ -226,10 +226,11 @@
     3.4              DispatchQueue.main.async {
     3.5                  me.rating = newRating
     3.6                  //!!!:
     3.7 -                print("COMPOSE: did newRating")
     3.8 +//                print("COMPOSE: did newRating")
     3.9              }
    3.10          }
    3.11  
    3.12 +        //!!!: cleanup
    3.13          //            guard let me = self else {
    3.14          //                // That is a valid case. Compose view is gone before this block started to run.
    3.15          //                return
     4.1 --- a/pEpForiOS/UI/EmailDisplayList/EmailListViewController.swift	Wed May 22 15:33:21 2019 +0200
     4.2 +++ b/pEpForiOS/UI/EmailDisplayList/EmailListViewController.swift	Wed May 22 15:39:47 2019 +0200
     4.3 @@ -61,7 +61,6 @@
     4.4          }
     4.5  
     4.6          setUpTextFilter()
     4.7 -        // Mark this folder as having been looked at by the user
     4.8  
     4.9          guard let vm = model else {
    4.10              Log.shared.errorAndCrash("No VM")
    4.11 @@ -99,6 +98,7 @@
    4.12  
    4.13          if vm.showLoginView {
    4.14              showLoginScreen()
    4.15 +            return
    4.16          }
    4.17  
    4.18          ///if we are in setup and the folder is unifiedInbox
    4.19 @@ -249,13 +249,6 @@
    4.20  
    4.21          showEditToolbar()
    4.22          tableView.setEditing(true, animated: true)
    4.23 -
    4.24 -        //!!!: English is the ONLY used language in our code base
    4.25 -        //modificar toolbar
    4.26 -        //hacer aparecer check de marcado
    4.27 -        //hacer la accion solicitada
    4.28 -        //recuperar toolbar
    4.29 -
    4.30      }
    4.31  
    4.32      private func showEditToolbar() {
    4.33 @@ -698,6 +691,15 @@
    4.34  // MARK: - EmailListViewModelDelegate
    4.35  
    4.36  extension EmailListViewController: EmailListViewModelDelegate {
    4.37 +    func checkIfSplitNeedsUpdate(indexpath: [IndexPath]) {
    4.38 +        guard let isIphone = splitViewController?.isCollapsed, let last = lastSelectedIndexPath else {
    4.39 +            return
    4.40 +        }
    4.41 +        if !isIphone && indexpath.contains(last) {
    4.42 +            showEmail(forCellAt: last)
    4.43 +        }
    4.44 +    }
    4.45 +
    4.46      func reloadData(viewModel: EmailListViewModel) {
    4.47          tableView.reloadData()
    4.48      }
     5.1 --- a/pEpForiOS/UI/EmailDisplayList/EmailListViewModel+MessageQueryResultsDelegate.swift	Wed May 22 15:33:21 2019 +0200
     5.2 +++ b/pEpForiOS/UI/EmailDisplayList/EmailListViewModel+MessageQueryResultsDelegate.swift	Wed May 22 15:39:47 2019 +0200
     5.3 @@ -21,6 +21,7 @@
     5.4          if updatesEnabled {
     5.5              emailListViewModelDelegate?.emailListViewModel(viewModel: self, didUpdateDataAt: [indexPath])
     5.6          }
     5.7 +        emailListViewModelDelegate?.checkIfSplitNeedsUpdate(indexpath: [indexPath])
     5.8      }
     5.9  
    5.10      func didDelete(indexPath: IndexPath) {
     6.1 --- a/pEpForiOS/UI/EmailDisplayList/EmailListViewModel.swift	Wed May 22 15:33:21 2019 +0200
     6.2 +++ b/pEpForiOS/UI/EmailDisplayList/EmailListViewModel.swift	Wed May 22 15:39:47 2019 +0200
     6.3 @@ -19,6 +19,7 @@
     6.4      func emailListViewModel(viewModel: EmailListViewModel, didRemoveDataAt indexPaths: [IndexPath])
     6.5      func emailListViewModel(viewModel: EmailListViewModel,
     6.6                              didMoveData atIndexPath: IndexPath, toIndexPath: IndexPath)
     6.7 +    func checkIfSplitNeedsUpdate(indexpath: [IndexPath])
     6.8      /*
     6.9       //!!!: Some issues here:
    6.10       - bad naming.
     7.1 --- a/pEpForiOSTests/Models/EmailListViewModelTest.swift	Wed May 22 15:33:21 2019 +0200
     7.2 +++ b/pEpForiOSTests/Models/EmailListViewModelTest.swift	Wed May 22 15:39:47 2019 +0200
     7.3 @@ -306,7 +306,6 @@
     7.4          setupViewModel()
     7.5          emailListVM.startMonitoring()
     7.6          XCTAssertEqual(emailListVM.rowCount, 10)
     7.7 -        //setUpMessageFolderDelegate()
     7.8          setUpViewModelExpectations(expectationDidInsertDataAt: true)
     7.9          let msg = TestUtil.createMessage(inFolder: folder, from: folder.account.user)
    7.10          msg.save()
    7.11 @@ -363,7 +362,6 @@
    7.12          setupViewModel()
    7.13          emailListVM.startMonitoring()
    7.14          XCTAssertEqual(emailListVM.rowCount, 11)
    7.15 -        //setUpMessageFolderDelegate()
    7.16          setUpViewModelExpectations(expectationDidDeleteDataAt: true)
    7.17          msg.delete()
    7.18          waitForExpectations(timeout: TestUtil.waitTime)
     8.1 --- a/pEpForiOSTests/Models/Message+FakeMessageTest.swift	Wed May 22 15:33:21 2019 +0200
     8.2 +++ b/pEpForiOSTests/Models/Message+FakeMessageTest.swift	Wed May 22 15:39:47 2019 +0200
     8.3 @@ -12,7 +12,9 @@
     8.4  //@testable import pEpForiOS
     8.5  @testable import MessageModel
     8.6  import PEPObjCAdapterFramework
     8.7 +import CoreData
     8.8  
     8.9 +//!!!: must bemoved to MM
    8.10  class Message_FakeMessageTest: CoreDataDrivenTestBase {
    8.11      let testUuid = UUID().uuidString + #file
    8.12  
    8.13 @@ -113,25 +115,6 @@
    8.14          assureFakeMessageExistence(in: folder)
    8.15      }
    8.16  
    8.17 -    // MARK: - findAndDeleteFakeMessage
    8.18 -
    8.19 -    func testFindAndDeleteFakeMessage() {
    8.20 -        let folderType = FolderType.inbox
    8.21 -        guard let folder = Folder.by(account: account, folderType: folderType) else {
    8.22 -            XCTFail()
    8.23 -            return
    8.24 -        }
    8.25 -        let msg = Message(uuid: testUuid, parentFolder: folder)
    8.26 -        msg.from = account.user
    8.27 -        msg.saveFakeMessage(in: folder)
    8.28 -        guard let fakeMsg = assureFakeMessageExistence(mustExist: true, in: folder) else {
    8.29 -            XCTFail()
    8.30 -            return
    8.31 -        }
    8.32 -        Message.findAndDeleteFakeMessage(withUuid: fakeMsg.uuid, in: folder)
    8.33 -        assureFakeMessageExistence(mustExist: false, in: folder)
    8.34 -    }
    8.35 -
    8.36      // MARK: - Helper
    8.37  
    8.38      @discardableResult private func assureFakeMessageExistence(mustExist: Bool = true, in folder: Folder) -> Message? {
    8.39 @@ -150,7 +133,7 @@
    8.40                                                              withUid uid: Int,
    8.41                                                              in folder: Folder) -> Message? {
    8.42          var result: Message? = nil
    8.43 -        let moc = Record.Context.main
    8.44 +        let moc: NSManagedObjectContext = Stack.shared.mainContext
    8.45          moc.performAndWait {
    8.46              guard let cdFolder =  folder.cdFolder() else {
    8.47                  XCTFail()
    8.48 @@ -163,7 +146,7 @@
    8.49                                   cdFolder)
    8.50              guard
    8.51                  let allCdMesgs = CdMessage.all(predicate: p) as? [CdMessage],
    8.52 -                let msg = allCdMesgs.first?.message()
    8.53 +                let cdMsg = allCdMesgs.first
    8.54                  else {
    8.55                      if mustExist {
    8.56                          XCTFail()
    8.57 @@ -171,7 +154,7 @@
    8.58                      return
    8.59              }
    8.60              XCTAssertEqual(allCdMesgs.count, 1)
    8.61 -            result = msg
    8.62 +            result = MessageModelObjectUtils.getMessage(fromCdMessage: cdMsg)
    8.63          }
    8.64          return result
    8.65      }
    8.66 @@ -199,7 +182,7 @@
    8.67      }
    8.68  
    8.69      private func deleteAllMessages() {
    8.70 -        let moc = Record.Context.main
    8.71 +        let moc: NSManagedObjectContext = Stack.shared.mainContext
    8.72          moc.performAndWait {
    8.73              guard let allCdMesgs = CdMessage.all() as? [CdMessage] else {
    8.74                  return
    8.75 @@ -216,7 +199,7 @@
    8.76      }
    8.77  
    8.78      private func deleteAllMessages(in folder: Folder) {
    8.79 -        let moc = Record.Context.main
    8.80 +        let moc: NSManagedObjectContext = Stack.shared.mainContext
    8.81          moc.performAndWait {
    8.82              guard let cdFolder = folder.cdFolder() else {
    8.83                  XCTFail()
    8.84 @@ -235,7 +218,7 @@
    8.85      }
    8.86  
    8.87      private func simulateSeenByEngine(forAllMessagesIn folder: Folder) {
    8.88 -        let moc = Record.Context.main
    8.89 +        let moc: NSManagedObjectContext = Stack.shared.mainContext
    8.90          moc.performAndWait {
    8.91              guard let cdFolder = folder.cdFolder() else {
    8.92                  XCTFail()
     9.1 --- a/pEpForiOSTests/ReplicationServiceTests.swift	Wed May 22 15:33:21 2019 +0200
     9.2 +++ b/pEpForiOSTests/ReplicationServiceTests.swift	Wed May 22 15:39:47 2019 +0200
     9.3 @@ -13,30 +13,158 @@
     9.4  @testable import pEpForiOS
     9.5  
     9.6  class ReplicationServiceTests: XCTestCase {
     9.7 -    var persistenceSetup: PersistentSetup!
     9.8 -    var moc: NSManagedObjectContext!
     9.9 +    //!!!:  needs complete rewrite.
    9.10 +    // Has randomly failing tests
    9.11 +    // Depends on MessageModelConfig.messageFolderDelegate, which is gone for good
    9.12  
    9.13 -    override func setUp() {
    9.14 -        super.setUp()
    9.15 -        persistenceSetup = PersistentSetup()
    9.16 -        moc = Stack.shared.mainContext
    9.17 -    }
    9.18  
    9.19 -    override func tearDown() {
    9.20 -        persistenceSetup = nil
    9.21 -        super.tearDown()
    9.22 -    }
    9.23 -
    9.24 -    func testSyncOutgoing() {
    9.25 -        testSyncOutgoing(useCorrectSmtpAccount: true)
    9.26 -    }
    9.27 -
    9.28 -    func testSyncOutgoingWithWrongAccount() {
    9.29 -        testSyncOutgoing(useCorrectSmtpAccount: false)
    9.30 -    }
    9.31 -
    9.32 -    //!!!: random fail
    9.33 -//    func testSyncOneTime() {
    9.34 +//    var persistenceSetup: PersistentSetup!
    9.35 +//    var moc: NSManagedObjectContext!
    9.36 +//
    9.37 +//    override func setUp() {
    9.38 +//        super.setUp()
    9.39 +//        persistenceSetup = PersistentSetup()
    9.40 +//        moc = Stack.shared.mainContext
    9.41 +//    }
    9.42 +//
    9.43 +//    override func tearDown() {
    9.44 +//        persistenceSetup = nil
    9.45 +//        super.tearDown()
    9.46 +//    }
    9.47 +//
    9.48 +//    func testSyncOutgoing() {
    9.49 +////        testSyncOutgoing(useCorrectSmtpAccount: true)
    9.50 +//    }
    9.51 +//
    9.52 +//    func testSyncOutgoingWithWrongAccount() {
    9.53 +////        testSyncOutgoing(useCorrectSmtpAccount: false)
    9.54 +//    }
    9.55 +//
    9.56 +//    //!!!: random fail
    9.57 +////    func testSyncOneTime() {
    9.58 +////        XCTAssertNil(CdAccount.all())
    9.59 +////        XCTAssertNil(CdFolder.all())
    9.60 +////        XCTAssertNil(CdMessage.all())
    9.61 +////
    9.62 +////        let modelDelegate = MessageModelObserver()
    9.63 +//////        MessageModelConfig.messageFolderDelegate = modelDelegate // gone for good
    9.64 +////
    9.65 +////        let replicationService = ReplicationService(parentName: #function)
    9.66 +////
    9.67 +////        let del = ReplicationServiceObserver(
    9.68 +////            expAccountsSynced: expectation(description: "expSingleAccountSynced"))
    9.69 +////        replicationService.unitTestDelegate = del
    9.70 +////        replicationService.delegate = del
    9.71 +////
    9.72 +////        _ = SecretTestData().createWorkingCdAccount()
    9.73 +////        Record.saveAndWait()
    9.74 +////
    9.75 +////        replicationService.start()
    9.76 +////
    9.77 +////        waitForExpectations(timeout: TestUtil.waitTime, handler: { error in
    9.78 +////            XCTAssertNil(error)
    9.79 +////        })
    9.80 +////
    9.81 +////        XCTAssertNotNil(del.accountInfo)
    9.82 +////        XCTAssertNotNil(CdFolder.all())
    9.83 +////
    9.84 +////        guard let cdFolder = CdFolder.first(
    9.85 +////            attributes: ["folderTypeRawValue": FolderType.inbox.rawValue]) else {
    9.86 +////                XCTFail()
    9.87 +////                return
    9.88 +////        }
    9.89 +////        XCTAssertGreaterThanOrEqual(cdFolder.messages?.count ?? 0, 0)
    9.90 +////        let allCdMessages = cdFolder.messages?.sortedArray(
    9.91 +////            using: [NSSortDescriptor(key: "uid", ascending: true)]) as? [CdMessage] ?? []
    9.92 +////        XCTAssertGreaterThanOrEqual(allCdMessages.count, 0)
    9.93 +////
    9.94 +////        for cdMsg in allCdMessages {
    9.95 +////            guard let parentF = cdMsg.parent else {
    9.96 +////                XCTFail()
    9.97 +////                continue
    9.98 +////            }
    9.99 +////            XCTAssertEqual(parentF.folderType, FolderType.inbox)
   9.100 +////        }
   9.101 +////
   9.102 +////        let unifiedInbox = UnifiedInbox()
   9.103 +////
   9.104 +////        let unifiedMessageCount = unifiedInbox.messageCount()
   9.105 +////        XCTAssertGreaterThanOrEqual(unifiedMessageCount, 0)
   9.106 +////        for i in 0..<unifiedMessageCount {
   9.107 +////            guard let msg = unifiedInbox.messageAt(index: i) else {
   9.108 +////                XCTFail()
   9.109 +////                continue
   9.110 +////            }
   9.111 +////
   9.112 +////            XCTAssertTrue(msg.isValidMessage())
   9.113 +////
   9.114 +////            let pEpRating = Int16(msg.pEpRatingInt ?? -1)
   9.115 +////            XCTAssertNotEqual(pEpRating, PEPUtil.pEpRatingNone)
   9.116 +////            if !modelDelegate.contains(messageID: msg.messageID) {
   9.117 +////                XCTFail()
   9.118 +////            }
   9.119 +////        }
   9.120 +////
   9.121 +////        let inbox = Folder.from(cdFolder: cdFolder)
   9.122 +////        XCTAssertEqual(modelDelegate.messages.count, unifiedMessageCount)
   9.123 +////
   9.124 +////        for msg in modelDelegate.messages {
   9.125 +////            let msgIsFlaggedDeleted = msg.imapFlags?.deleted ?? false
   9.126 +////            XCTAssertTrue(!msgIsFlaggedDeleted)
   9.127 +////            XCTAssertTrue(inbox.contains(message: msg))
   9.128 +////            if !unifiedInbox.contains(message: msg) {
   9.129 +////                XCTFail()
   9.130 +////            }
   9.131 +////        }
   9.132 +////        XCTAssertFalse(modelDelegate.hasChangedMessages)
   9.133 +////
   9.134 +////        TestUtil.cancelReplicationServiceAndWait(replicationService: replicationService, testCase: self)
   9.135 +////    }
   9.136 +//
   9.137 +//    func testCancelSyncImmediately() {
   9.138 +//        XCTAssertNil(CdAccount.all())
   9.139 +//        XCTAssertNil(CdFolder.all())
   9.140 +//        XCTAssertNil(CdMessage.all())
   9.141 +//
   9.142 +//        let replicationService = ReplicationService(parentName: #function)
   9.143 +//
   9.144 +//        _ = SecretTestData().createWorkingCdAccount(context: moc)
   9.145 +//        Record.saveAndWait()
   9.146 +//
   9.147 +//        for _ in 0...10 {
   9.148 +//            replicationService.start()
   9.149 +//            TestUtil.cancelReplicationServiceAndWait(replicationService: replicationService, testCase: self)
   9.150 +//        }
   9.151 +//
   9.152 +//        XCTAssertNil(CdFolder.all())
   9.153 +//        XCTAssertNil(CdMessage.all())
   9.154 +//    }
   9.155 +//
   9.156 +//    class MySelfObserver: KickOffMySelfProtocol {
   9.157 +//        let expMySelfed: XCTestExpectation?
   9.158 +//        let queue = LimitedOperationQueue()
   9.159 +//        let backgrounder: MockBackgrounder
   9.160 +//
   9.161 +//        init(expMySelfed: XCTestExpectation?,
   9.162 +//             expBackgroundTaskFinishedAtLeastOnce: XCTestExpectation?) {
   9.163 +//            self.expMySelfed = expMySelfed
   9.164 +//            backgrounder = MockBackgrounder(
   9.165 +//                expBackgroundTaskFinishedAtLeastOnce: expBackgroundTaskFinishedAtLeastOnce)
   9.166 +//        }
   9.167 +//
   9.168 +//        func startMySelf() {
   9.169 +//            let op = MySelfOperation(parentName: #function, backgrounder: backgrounder)
   9.170 +//            op.completionBlock = {
   9.171 +//                op.completionBlock = nil
   9.172 +//                self.expMySelfed?.fulfill()
   9.173 +//            }
   9.174 +//            queue.addOperation(op)
   9.175 +//        }
   9.176 +//    }
   9.177 +//
   9.178 +//    //MARK: HELPER
   9.179 +//
   9.180 +//    func testSyncOutgoing(useCorrectSmtpAccount: Bool) {
   9.181  //        XCTAssertNil(CdAccount.all())
   9.182  //        XCTAssertNil(CdFolder.all())
   9.183  //        XCTAssertNil(CdMessage.all())
   9.184 @@ -44,241 +172,118 @@
   9.185  //        let modelDelegate = MessageModelObserver()
   9.186  //        MessageModelConfig.messageFolderDelegate = modelDelegate
   9.187  //
   9.188 -//        let replicationService = ReplicationService(parentName: #function)
   9.189 -//
   9.190 -//        let del = ReplicationServiceObserver(
   9.191 -//            expAccountsSynced: expectation(description: "expSingleAccountSynced"))
   9.192 -//        replicationService.unitTestDelegate = del
   9.193 -//        replicationService.delegate = del
   9.194 -//
   9.195 -//        _ = SecretTestData().createWorkingCdAccount()
   9.196 +//        let cdAccount =
   9.197 +//            useCorrectSmtpAccount ?
   9.198 +//            SecretTestData().createWorkingCdAccount(context: moc) :
   9.199 +//            SecretTestData().createSmtpTimeOutCdAccount(context: moc)
   9.200  //        Record.saveAndWait()
   9.201  //
   9.202 -//        replicationService.start()
   9.203 +//        TestUtil.syncAndWait(testCase: self)
   9.204  //
   9.205 -//        waitForExpectations(timeout: TestUtil.waitTime, handler: { error in
   9.206 -//            XCTAssertNil(error)
   9.207 -//        })
   9.208 +//        let from = CdIdentity(context: moc)
   9.209 +//        from.userName = cdAccount.identity?.userName ?? "Unit 004"
   9.210 +//        from.address = cdAccount.identity?.address ?? "unittest.ios.4@peptest.ch"
   9.211  //
   9.212 -//        XCTAssertNotNil(del.accountInfo)
   9.213 -//        XCTAssertNotNil(CdFolder.all())
   9.214 +//        let to = CdIdentity(context: moc)
   9.215 +//        to.userName = "Unit 001"
   9.216 +//        to.address = "unittest.ios.1@peptest.ch"
   9.217  //
   9.218 -//        guard let cdFolder = CdFolder.first(
   9.219 -//            attributes: ["folderTypeRawValue": FolderType.inbox.rawValue]) else {
   9.220 -//                XCTFail()
   9.221 -//                return
   9.222 +//        guard let sentFolder = CdFolder.by(folderType: .sent, account: cdAccount) else {
   9.223 +//            XCTFail()
   9.224 +//            return
   9.225  //        }
   9.226 -//        XCTAssertGreaterThanOrEqual(cdFolder.messages?.count ?? 0, 0)
   9.227 -//        let allCdMessages = cdFolder.messages?.sortedArray(
   9.228 -//            using: [NSSortDescriptor(key: "uid", ascending: true)]) as? [CdMessage] ?? []
   9.229 -//        XCTAssertGreaterThanOrEqual(allCdMessages.count, 0)
   9.230 +//        XCTAssertEqual((sentFolder.messages ?? NSSet()).count, 0)
   9.231  //
   9.232 -//        for cdMsg in allCdMessages {
   9.233 -//            guard let parentF = cdMsg.parent else {
   9.234 -//                XCTFail()
   9.235 -//                continue
   9.236 -//            }
   9.237 -//            XCTAssertEqual(parentF.folderType, FolderType.inbox)
   9.238 +//        let numMails = 1
   9.239 +//        let outgoingMails = try! TestUtil.createOutgoingMails(cdAccount: cdAccount,
   9.240 +//                                                              testCase: self,
   9.241 +//                                                              numberOfMails: numMails,
   9.242 +//                                                              context: moc)
   9.243 +//        let outgoingMessageIDs: [String] = outgoingMails
   9.244 +//            .map() { $0.messageID ?? "" }
   9.245 +//            .filter() { $0 != "" }
   9.246 +//
   9.247 +//        // Verify outgoing mails
   9.248 +//        for m in outgoingMails {
   9.249 +//            XCTAssertEqual(m.parent?.folderType, FolderType.outbox)
   9.250 +//            XCTAssertEqual(m.uid, Int32(0))
   9.251  //        }
   9.252  //
   9.253 -//        let unifiedInbox = UnifiedInbox()
   9.254 +//        TestUtil.syncAndWait(testCase: self)
   9.255  //
   9.256 -//        let unifiedMessageCount = unifiedInbox.messageCount()
   9.257 -//        XCTAssertGreaterThanOrEqual(unifiedMessageCount, 0)
   9.258 -//        for i in 0..<unifiedMessageCount {
   9.259 -//            guard let msg = unifiedInbox.messageAt(index: i) else {
   9.260 -//                XCTFail()
   9.261 -//                continue
   9.262 -//            }
   9.263 -//
   9.264 -//            XCTAssertTrue(msg.isValidMessage())
   9.265 -//
   9.266 -//            let pEpRating = Int16(msg.pEpRatingInt ?? -1)
   9.267 -//            XCTAssertNotEqual(pEpRating, PEPUtil.pEpRatingNone)
   9.268 -//            if !modelDelegate.contains(messageID: msg.messageID) {
   9.269 -//                XCTFail()
   9.270 +//        // Check that the sent mails have been deleted
   9.271 +//        Stack.refreshRegisteredObjects(mergeChanges: true, in: moc)
   9.272 +//        if useCorrectSmtpAccount {
   9.273 +//            for m in outgoingMails {
   9.274 +//                XCTAssertTrue(m.isDeleted)
   9.275  //            }
   9.276  //        }
   9.277  //
   9.278 -//        let inbox = Folder.from(cdFolder: cdFolder)
   9.279 -//        XCTAssertEqual(modelDelegate.messages.count, unifiedMessageCount)
   9.280 +//        // sync
   9.281 +//        TestUtil.syncAndWait(testCase: self)
   9.282  //
   9.283 -//        for msg in modelDelegate.messages {
   9.284 -//            let msgIsFlaggedDeleted = msg.imapFlags?.deleted ?? false
   9.285 -//            XCTAssertTrue(!msgIsFlaggedDeleted)
   9.286 -//            XCTAssertTrue(inbox.contains(message: msg))
   9.287 -//            if !unifiedInbox.contains(message: msg) {
   9.288 -//                XCTFail()
   9.289 +//        if useCorrectSmtpAccount {
   9.290 +//            // those messages do not exist if we are using an incorrect account
   9.291 +//            TestUtil.checkForExistanceAndUniqueness(uuids: outgoingMessageIDs)
   9.292 +//        }
   9.293 +//    }
   9.294 +//
   9.295 +//    class MessageModelObserver: MessageFolderDelegate {
   9.296 +//        var messages: [Message] {
   9.297 +//            var messages = [Message]()
   9.298 +//            for ms in messagesByID.values {
   9.299 +//                for m in ms {
   9.300 +//                    messages.append(m)
   9.301 +//                }
   9.302 +//            }
   9.303 +//            return messages.sorted { m1, m2 in
   9.304 +//                if let d1 = m1.sent, let d2 = m2.sent {
   9.305 +//                    return areInIncreasingOrder(d1: d1, d2: d2)
   9.306 +//                }
   9.307 +//                return false
   9.308  //            }
   9.309  //        }
   9.310 -//        XCTAssertFalse(modelDelegate.hasChangedMessages)
   9.311 +//        var messagesByID = [MessageID: [Message]]()
   9.312 +//        var changedMessagesByID = [MessageID: Message]()
   9.313  //
   9.314 -//        TestUtil.cancelReplicationServiceAndWait(replicationService: replicationService, testCase: self)
   9.315 +//        var hasChangedMessages: Bool {
   9.316 +//            return !changedMessagesByID.isEmpty
   9.317 +//        }
   9.318 +//
   9.319 +//        func contains(messageID: MessageID) -> Bool {
   9.320 +//            return messagesByID[messageID] != nil
   9.321 +//        }
   9.322 +//
   9.323 +//        func areInIncreasingOrder(d1: Date, d2: Date) -> Bool {
   9.324 +//            switch d1.compare(d2 as Date) {
   9.325 +//            case .orderedAscending: return true
   9.326 +//            default: return false
   9.327 +//            }
   9.328 +//        }
   9.329 +//
   9.330 +//        func add(message: Message) {
   9.331 +//            if let existing = messagesByID[message.uuid] {
   9.332 +//                var news = existing
   9.333 +//                news.append(message)
   9.334 +//                messagesByID[message.uuid] = news
   9.335 +//            } else {
   9.336 +//                messagesByID[message.uuid] = [message]
   9.337 +//            }
   9.338 +//        }
   9.339 +//
   9.340 +//        func didUpdate(message: Message) {
   9.341 +//            // messages has been changed during the test
   9.342 +//            XCTAssertNotNil(messagesByID[message.messageID])
   9.343 +//            add(message: message)
   9.344 +//            changedMessagesByID[message.messageID] = message
   9.345 +//        }
   9.346 +//
   9.347 +//        func didDelete(message: Message) {
   9.348 +//            // this message has been deleted from the start, ignore
   9.349 +//        }
   9.350 +//
   9.351 +//        func didCreate(message: Message) {
   9.352 +//            add(message: message)
   9.353 +//        }
   9.354  //    }
   9.355 -
   9.356 -    func testCancelSyncImmediately() {
   9.357 -        XCTAssertNil(CdAccount.all())
   9.358 -        XCTAssertNil(CdFolder.all())
   9.359 -        XCTAssertNil(CdMessage.all())
   9.360 -
   9.361 -        let replicationService = ReplicationService(parentName: #function)
   9.362 -
   9.363 -        _ = SecretTestData().createWorkingCdAccount(context: moc)
   9.364 -        Record.saveAndWait()
   9.365 -
   9.366 -        for _ in 0...10 {
   9.367 -            replicationService.start()
   9.368 -            TestUtil.cancelReplicationServiceAndWait(replicationService: replicationService, testCase: self)
   9.369 -        }
   9.370 -
   9.371 -        XCTAssertNil(CdFolder.all())
   9.372 -        XCTAssertNil(CdMessage.all())
   9.373 -    }
   9.374 -
   9.375 -    class MySelfObserver: KickOffMySelfProtocol {
   9.376 -        let expMySelfed: XCTestExpectation?
   9.377 -        let queue = LimitedOperationQueue()
   9.378 -        let backgrounder: MockBackgrounder
   9.379 -
   9.380 -        init(expMySelfed: XCTestExpectation?,
   9.381 -             expBackgroundTaskFinishedAtLeastOnce: XCTestExpectation?) {
   9.382 -            self.expMySelfed = expMySelfed
   9.383 -            backgrounder = MockBackgrounder(
   9.384 -                expBackgroundTaskFinishedAtLeastOnce: expBackgroundTaskFinishedAtLeastOnce)
   9.385 -        }
   9.386 -
   9.387 -        func startMySelf() {
   9.388 -            let op = MySelfOperation(parentName: #function, backgrounder: backgrounder)
   9.389 -            op.completionBlock = {
   9.390 -                op.completionBlock = nil
   9.391 -                self.expMySelfed?.fulfill()
   9.392 -            }
   9.393 -            queue.addOperation(op)
   9.394 -        }
   9.395 -    }
   9.396 -
   9.397 -    //MARK: HELPER
   9.398 -
   9.399 -    func testSyncOutgoing(useCorrectSmtpAccount: Bool) {
   9.400 -        XCTAssertNil(CdAccount.all())
   9.401 -        XCTAssertNil(CdFolder.all())
   9.402 -        XCTAssertNil(CdMessage.all())
   9.403 -
   9.404 -        let modelDelegate = MessageModelObserver()
   9.405 -        MessageModelConfig.messageFolderDelegate = modelDelegate
   9.406 -
   9.407 -        let cdAccount =
   9.408 -            useCorrectSmtpAccount ?
   9.409 -            SecretTestData().createWorkingCdAccount(context: moc) :
   9.410 -            SecretTestData().createSmtpTimeOutCdAccount(context: moc)
   9.411 -        Record.saveAndWait()
   9.412 -
   9.413 -        TestUtil.syncAndWait(testCase: self)
   9.414 -
   9.415 -        let from = CdIdentity(context: moc)
   9.416 -        from.userName = cdAccount.identity?.userName ?? "Unit 004"
   9.417 -        from.address = cdAccount.identity?.address ?? "unittest.ios.4@peptest.ch"
   9.418 -
   9.419 -        let to = CdIdentity(context: moc)
   9.420 -        to.userName = "Unit 001"
   9.421 -        to.address = "unittest.ios.1@peptest.ch"
   9.422 -
   9.423 -        guard let sentFolder = CdFolder.by(folderType: .sent, account: cdAccount) else {
   9.424 -            XCTFail()
   9.425 -            return
   9.426 -        }
   9.427 -        XCTAssertEqual((sentFolder.messages ?? NSSet()).count, 0)
   9.428 -
   9.429 -        let numMails = 1
   9.430 -        let outgoingMails = try! TestUtil.createOutgoingMails(cdAccount: cdAccount,
   9.431 -                                                              testCase: self,
   9.432 -                                                              numberOfMails: numMails,
   9.433 -                                                              context: moc)
   9.434 -        let outgoingMessageIDs: [String] = outgoingMails
   9.435 -            .map() { $0.messageID ?? "" }
   9.436 -            .filter() { $0 != "" }
   9.437 -
   9.438 -        // Verify outgoing mails
   9.439 -        for m in outgoingMails {
   9.440 -            XCTAssertEqual(m.parent?.folderType, FolderType.outbox)
   9.441 -            XCTAssertEqual(m.uid, Int32(0))
   9.442 -        }
   9.443 -
   9.444 -        TestUtil.syncAndWait(testCase: self)
   9.445 -
   9.446 -        // Check that the sent mails have been deleted
   9.447 -        Stack.refreshRegisteredObjects(mergeChanges: true, in: moc)
   9.448 -        if useCorrectSmtpAccount {
   9.449 -            for m in outgoingMails {
   9.450 -                XCTAssertTrue(m.isDeleted)
   9.451 -            }
   9.452 -        }
   9.453 -
   9.454 -        // sync
   9.455 -        TestUtil.syncAndWait(testCase: self)
   9.456 -
   9.457 -        if useCorrectSmtpAccount {
   9.458 -            // those messages do not exist if we are using an incorrect account
   9.459 -            TestUtil.checkForExistanceAndUniqueness(uuids: outgoingMessageIDs)
   9.460 -        }
   9.461 -    }
   9.462 -
   9.463 -    class MessageModelObserver: MessageFolderDelegate {
   9.464 -        var messages: [Message] {
   9.465 -            var messages = [Message]()
   9.466 -            for ms in messagesByID.values {
   9.467 -                for m in ms {
   9.468 -                    messages.append(m)
   9.469 -                }
   9.470 -            }
   9.471 -            return messages.sorted { m1, m2 in
   9.472 -                if let d1 = m1.sent, let d2 = m2.sent {
   9.473 -                    return areInIncreasingOrder(d1: d1, d2: d2)
   9.474 -                }
   9.475 -                return false
   9.476 -            }
   9.477 -        }
   9.478 -        var messagesByID = [MessageID: [Message]]()
   9.479 -        var changedMessagesByID = [MessageID: Message]()
   9.480 -
   9.481 -        var hasChangedMessages: Bool {
   9.482 -            return !changedMessagesByID.isEmpty
   9.483 -        }
   9.484 -
   9.485 -        func contains(messageID: MessageID) -> Bool {
   9.486 -            return messagesByID[messageID] != nil
   9.487 -        }
   9.488 -
   9.489 -        func areInIncreasingOrder(d1: Date, d2: Date) -> Bool {
   9.490 -            switch d1.compare(d2 as Date) {
   9.491 -            case .orderedAscending: return true
   9.492 -            default: return false
   9.493 -            }
   9.494 -        }
   9.495 -
   9.496 -        func add(message: Message) {
   9.497 -            if let existing = messagesByID[message.uuid] {
   9.498 -                var news = existing
   9.499 -                news.append(message)
   9.500 -                messagesByID[message.uuid] = news
   9.501 -            } else {
   9.502 -                messagesByID[message.uuid] = [message]
   9.503 -            }
   9.504 -        }
   9.505 -
   9.506 -        func didUpdate(message: Message) {
   9.507 -            // messages has been changed during the test
   9.508 -            XCTAssertNotNil(messagesByID[message.messageID])
   9.509 -            add(message: message)
   9.510 -            changedMessagesByID[message.messageID] = message
   9.511 -        }
   9.512 -
   9.513 -        func didDelete(message: Message) {
   9.514 -            // this message has been deleted from the start, ignore
   9.515 -        }
   9.516 -
   9.517 -        func didCreate(message: Message) {
   9.518 -            add(message: message)
   9.519 -        }
   9.520 -    }
   9.521  }