pEpForiOSTests/UI/Compose/Cells/BodyCell/BodyCellViewModelTest.swift
author Dirk Zimmermann <dz@pep.security>
Wed, 23 Sep 2020 16:50:50 +0200
branchIOS-2259
changeset 15205 f2d94a86ef56
parent 13013 b2243c1de656
child 15206 4044ef5b3584
permissions -rw-r--r--
IOS-2259 Remove failing test
     1 //
     2 //  BodyCellViewModelTest.swift
     3 //  pEpForiOSTests
     4 //
     5 //  Created by Andreas Buff on 08.11.18.
     6 //  Copyright © 2018 p≡p Security S.A. All rights reserved.
     7 //
     8 
     9 import XCTest
    10 
    11 @testable import pEpForiOS
    12 import MessageModel
    13 import pEpIOSToolbox
    14 
    15 class BodyCellViewModelTest: XCTestCase {
    16     var vm: BodyCellViewModel!
    17     private var testDelegate: TestDelegate?
    18     private var testResultDelegate: TestResultDelegate?
    19 
    20     // MARK: - inititalText
    21 
    22     func testInititalText_plain() {
    23         let intitialPlain = "intitial text"
    24         setupAssertionDelegates(initialPlaintext: intitialPlain,
    25                                 initialAttributedText: nil,
    26                                 initialInlinedAttachments: nil)
    27         let (text, _) = vm.inititalText()
    28         guard let testee = text else {
    29             XCTFail()
    30             return
    31         }
    32         XCTAssertEqual(testee, intitialPlain)
    33     }
    34 
    35     func testInititalText_html() {
    36         let intitialHtml = NSAttributedString(string: "intitial text")
    37         setupAssertionDelegates(initialPlaintext: nil,
    38                                 initialAttributedText: intitialHtml,
    39                                 initialInlinedAttachments: nil)
    40         let (_, html) = vm.inititalText()
    41         guard let testee = html else {
    42             XCTFail()
    43             return
    44         }
    45         XCTAssertEqual(testee, intitialHtml)
    46     }
    47 
    48     func testInititalText_both() {
    49         let intitialPlain = "intitial text"
    50         let initialHtml = NSAttributedString(string: "intitial text")
    51         setupAssertionDelegates(initialPlaintext: intitialPlain,
    52                                 initialAttributedText: initialHtml,
    53                                 initialInlinedAttachments: nil)
    54         let (text, html) = vm.inititalText()
    55         guard let testee = text else {
    56             XCTFail()
    57             return
    58         }
    59         guard let testeeHtml = html else {
    60             XCTFail()
    61             return
    62         }
    63         XCTAssertEqual(testee, intitialPlain)
    64         XCTAssertEqual(testeeHtml, initialHtml)
    65     }
    66 
    67     func testInititalText_noHtml() {
    68         let intitialPlain = "intitial text"
    69         setupAssertionDelegates(initialPlaintext: intitialPlain,
    70                                 initialAttributedText: nil,
    71                                 initialInlinedAttachments: nil)
    72         let (_, html) = vm.inititalText()
    73         XCTAssertNil(html)
    74     }
    75 
    76     func testInititalText_signatureSet() {
    77         let intitialHtml = NSAttributedString(string: "intitial text")
    78         setupAssertionDelegates(initialPlaintext: nil,
    79                                 initialAttributedText: intitialHtml,
    80                                 initialInlinedAttachments: nil)
    81         let (text, _) = vm.inititalText()
    82         XCTAssertEqual(text, .pepSignature)
    83     }
    84 
    85     func testInititalText_emptyInit_signatureSet() {
    86         setupAssertionDelegates(initialPlaintext: nil,
    87                                 initialAttributedText: nil,
    88                                 initialInlinedAttachments: nil)
    89         let (text, _) = vm.inititalText()
    90         XCTAssertEqual(text, .pepSignature)
    91     }
    92 
    93     // MARK: - Initial Inlined Attachents
    94 
    95     func testInitialAttachments() {
    96         let inlinedAttachments = createTestAttachments(numAttachments: 1)
    97         setupAssertionDelegates(initialPlaintext: nil,
    98                                 initialAttributedText: nil,
    99                                 initialInlinedAttachments: inlinedAttachments,
   100                                 expectInsertCalled: nil,
   101                                 inserted: nil,
   102                                 expUserWantsToAddMediaCalled: nil,
   103                                 expUserWantsToAddDocumentCalled: nil,
   104                                 expInlinedAttachmentsCalled: expInlinedAttachmentChanged(mustBeCalled: false),
   105                                 inlined: nil,
   106                                 expBodyChangedCalled: nil,
   107                                 exectedPlain: nil,
   108                                 exectedHtml: nil)
   109         waitForExpectations(timeout: UnitTestUtils.waitTime)
   110     }
   111 
   112     // MARK: - handleTextChange
   113 
   114     func testHandleTextChange() {
   115         let newPlainText = "testPlainText"
   116         let newAttributedContent = "testAttributedText"
   117         let newAttributedText = NSAttributedString(string: newAttributedContent)
   118 
   119         let attributedTextWins = newAttributedContent
   120         let expectedPlainText = attributedTextWins
   121         let expectedHtml = htmlVersion(of: newAttributedContent)
   122 
   123         setupAssertionDelegates(initialPlaintext: nil,
   124                                 initialAttributedText: nil,
   125                                 initialInlinedAttachments: nil,
   126                                 expectInsertCalled: expInsertTextCalled(mustBeCalled: false),
   127                                 inserted: nil,
   128                                 expUserWantsToAddMediaCalled: expUserWantsToAddMediaCalled(mustBeCalled: false),
   129                                 expUserWantsToAddDocumentCalled: expUserWantsToAddDocumentCalled(mustBeCalled: false),
   130                                 expInlinedAttachmentsCalled: expInlinedAttachmentChanged(mustBeCalled: false),
   131                                 inlined: nil,
   132                                 expBodyChangedCalled: expBodyChangedCalled(mustBeCalled: true),
   133                                 exectedPlain: expectedPlainText,
   134                                 exectedHtml: expectedHtml)
   135         vm.handleTextChange(newText: newPlainText, newAttributedText: newAttributedText)
   136         waitForExpectations(timeout: UnitTestUtils.waitTime)
   137     }
   138 
   139     func testHandleTextChange_initialTextSet() {
   140         let initText = "initText"
   141         let initAttributedText = NSAttributedString(string: "initAttributedText")
   142 
   143         let newPlainText = "testPlainText"
   144         let newAttributedContent = "testAttributedText"
   145         let newAttributedText = NSAttributedString(string: newAttributedContent)
   146 
   147         let attributedTextWins = newAttributedContent
   148 
   149         let expectedPlainText = attributedTextWins
   150         let expectedHtml = htmlVersion(of: newAttributedContent)
   151 
   152         setupAssertionDelegates(initialPlaintext: initText,
   153                                 initialAttributedText: initAttributedText,
   154                                 initialInlinedAttachments: nil,
   155                                 expectInsertCalled: expInsertTextCalled(mustBeCalled: false),
   156                                 inserted: nil,
   157                                 expUserWantsToAddMediaCalled: expUserWantsToAddMediaCalled(mustBeCalled: false),
   158                                 expUserWantsToAddDocumentCalled: expUserWantsToAddDocumentCalled(mustBeCalled: false),
   159                                 expInlinedAttachmentsCalled: expInlinedAttachmentChanged(mustBeCalled: false),
   160                                 inlined: nil,
   161                                 expBodyChangedCalled: expBodyChangedCalled(mustBeCalled: true),
   162                                 exectedPlain: expectedPlainText,
   163                                 exectedHtml: expectedHtml)
   164         vm.handleTextChange(newText: newPlainText, newAttributedText: newAttributedText)
   165         waitForExpectations(timeout: UnitTestUtils.waitTime)
   166     }
   167 
   168     func testHandleTextChange_notCalled() {
   169         setupAssertionDelegates(initialPlaintext: nil,
   170                                 initialAttributedText: nil,
   171                                 initialInlinedAttachments: nil,
   172                                 expectInsertCalled: expInsertTextCalled(mustBeCalled: false),
   173                                 inserted: nil,
   174                                 expUserWantsToAddMediaCalled: expUserWantsToAddMediaCalled(mustBeCalled: false),
   175                                 expUserWantsToAddDocumentCalled: expUserWantsToAddDocumentCalled(mustBeCalled: false),
   176                                 expInlinedAttachmentsCalled: expInlinedAttachmentChanged(mustBeCalled: false),
   177                                 inlined: nil,
   178                                 expBodyChangedCalled: expBodyChangedCalled(mustBeCalled: false),
   179                                 exectedPlain: nil,
   180                                 exectedHtml: nil)
   181         // We do not call handleTextChange, so no callback should be called.
   182         waitForExpectations(timeout: UnitTestUtils.waitTime)
   183     }
   184 
   185     // MARK: - inline(attachment:
   186 
   187     func testInlineAttachment_called() {
   188         let attachment = createTestAttachment(addImage: true)
   189         setupAssertionDelegates(initialPlaintext: nil,
   190                                 initialAttributedText: nil,
   191                                 initialInlinedAttachments: nil,
   192                                 expectInsertCalled: expInsertTextCalled(mustBeCalled: true),
   193                                 inserted: nil,
   194                                 expUserWantsToAddMediaCalled: expUserWantsToAddMediaCalled(mustBeCalled: false),
   195                                 expUserWantsToAddDocumentCalled: expUserWantsToAddDocumentCalled(mustBeCalled: false),
   196                                 expInlinedAttachmentsCalled: expInlinedAttachmentChanged(mustBeCalled: true),
   197                                 inlined: [attachment],
   198                                 expBodyChangedCalled: expBodyChangedCalled(mustBeCalled: false),
   199                                 exectedPlain: nil,
   200                                 exectedHtml: nil)
   201         let nonZeroValue: CGFloat = 300.0
   202         vm.maxTextattachmentWidth = nonZeroValue
   203         vm.inline(attachment: attachment)
   204         waitForExpectations(timeout: UnitTestUtils.waitTime)
   205     }
   206 
   207     func testInlineAttachment_notCalled() {
   208         setupAssertionDelegates(initialPlaintext: nil,
   209                                 initialAttributedText: nil,
   210                                 initialInlinedAttachments: nil,
   211                                 expectInsertCalled: expInsertTextCalled(mustBeCalled: false),
   212                                 inserted: nil,
   213                                 expUserWantsToAddMediaCalled: expUserWantsToAddMediaCalled(mustBeCalled: false),
   214                                 expUserWantsToAddDocumentCalled: expUserWantsToAddDocumentCalled(mustBeCalled: false),
   215                                 expInlinedAttachmentsCalled: expInlinedAttachmentChanged(mustBeCalled: false),
   216                                 inlined: nil,
   217                                 expBodyChangedCalled: expBodyChangedCalled(mustBeCalled: false),
   218                                 exectedPlain: nil,
   219                                 exectedHtml: nil)
   220         let nonZeroValue: CGFloat = 300.0
   221         vm.maxTextattachmentWidth = nonZeroValue
   222         waitForExpectations(timeout: UnitTestUtils.waitTime)
   223     }
   224 
   225     // MARK: - shouldReplaceText(in range:of text:with replaceText:)
   226 
   227     func testShouldReplaceText_noAttachment_replacetextEmpty() {
   228         let testText = NSAttributedString(string: "Test text")
   229         let testRange = NSRange(location: 0, length: "Test".count)
   230         let testReplaceText = ""
   231 
   232         setupAssertionDelegates(initialPlaintext: nil,
   233                                 initialAttributedText: nil,
   234                                 initialInlinedAttachments: nil,
   235                                 expectInsertCalled: expInsertTextCalled(mustBeCalled: false),
   236                                 inserted: nil,
   237                                 expUserWantsToAddMediaCalled: expUserWantsToAddMediaCalled(mustBeCalled: false),
   238                                 expUserWantsToAddDocumentCalled: expUserWantsToAddDocumentCalled(mustBeCalled: false),
   239                                 expInlinedAttachmentsCalled: expInlinedAttachmentChanged(mustBeCalled: false),
   240                                 inlined: nil,
   241                                 expBodyChangedCalled: expBodyChangedCalled(mustBeCalled: false),
   242                                 exectedPlain: nil,
   243                                 exectedHtml: nil)
   244         let shouldReplace = vm.shouldReplaceText(in: testRange, of: testText, with: testReplaceText)
   245         XCTAssertTrue(shouldReplace, "Should alway be true")
   246         waitForExpectations(timeout: UnitTestUtils.waitTime)
   247     }
   248 
   249     func testShouldReplaceText_noAttachment_replacetextNonEmpty() {
   250         let testText = NSAttributedString(string: "Test text")
   251         let testRange = NSRange(location: 0, length: "Test".count)
   252         let testReplaceText = "Replace"
   253 
   254         setupAssertionDelegates(initialPlaintext: nil,
   255                                 initialAttributedText: nil,
   256                                 initialInlinedAttachments: nil,
   257                                 expectInsertCalled: expInsertTextCalled(mustBeCalled: false),
   258                                 inserted: nil,
   259                                 expUserWantsToAddMediaCalled: expUserWantsToAddMediaCalled(mustBeCalled: false),
   260                                 expUserWantsToAddDocumentCalled: expUserWantsToAddDocumentCalled(mustBeCalled: false),
   261                                 expInlinedAttachmentsCalled: expInlinedAttachmentChanged(mustBeCalled: false),
   262                                 inlined: nil,
   263                                 expBodyChangedCalled: expBodyChangedCalled(mustBeCalled: false),
   264                                 exectedPlain: nil,
   265                                 exectedHtml: nil)
   266         let shouldReplace = vm.shouldReplaceText(in: testRange, of: testText, with: testReplaceText)
   267         XCTAssertTrue(shouldReplace, "Should alway be true")
   268         waitForExpectations(timeout: UnitTestUtils.waitTime)
   269     }
   270 
   271     func testShouldReplaceText_noAttachment_replacetextNewLine() {
   272         let testText = NSAttributedString(string: "Test text")
   273         let testRange = NSRange(location: 0, length: "Test".count)
   274         let testReplaceText = "\n"
   275 
   276         setupAssertionDelegates(initialPlaintext: nil,
   277                                 initialAttributedText: nil,
   278                                 initialInlinedAttachments: nil,
   279                                 expectInsertCalled: expInsertTextCalled(mustBeCalled: false),
   280                                 inserted: nil,
   281                                 expUserWantsToAddMediaCalled: expUserWantsToAddMediaCalled(mustBeCalled: false),
   282                                 expUserWantsToAddDocumentCalled: expUserWantsToAddDocumentCalled(mustBeCalled: false),
   283                                 expInlinedAttachmentsCalled: expInlinedAttachmentChanged(mustBeCalled: false),
   284                                 inlined: nil,
   285                                 expBodyChangedCalled: expBodyChangedCalled(mustBeCalled: false),
   286                                 exectedPlain: nil,
   287                                 exectedHtml: nil)
   288         let shouldReplace = vm.shouldReplaceText(in: testRange, of: testText, with: testReplaceText)
   289         XCTAssertTrue(shouldReplace, "Should alway be true")
   290         waitForExpectations(timeout: UnitTestUtils.waitTime)
   291     }
   292 
   293     func testShouldReplaceText_attachment_removeNone() {
   294         let textBuilder = NSAttributedString(string: "Test text")
   295         let replaceText = ""
   296 
   297         let insertRange = NSRange(location: "Test".count, length: 1)
   298         let testAttachment = createTestAttachment(addImage: true)
   299         let testText = insertTextattachment(for: testAttachment, in: insertRange, of: textBuilder)
   300         let testRange = NSRange(location: 0, length: "Test".count)
   301         XCTAssertTrue(testText.textAttachments(range: testRange).count == 0)
   302 
   303         setupAssertionDelegates(initialPlaintext: nil,
   304                                 initialAttributedText: nil,
   305                                 initialInlinedAttachments: [testAttachment],
   306                                 expectInsertCalled: expInsertTextCalled(mustBeCalled: false),
   307                                 inserted: nil,
   308                                 expUserWantsToAddMediaCalled: expUserWantsToAddMediaCalled(mustBeCalled: false),
   309                                 expUserWantsToAddDocumentCalled: expUserWantsToAddDocumentCalled(mustBeCalled: false),
   310                                 expInlinedAttachmentsCalled: expInlinedAttachmentChanged(mustBeCalled: false),
   311                                 inlined: nil,
   312                                 expBodyChangedCalled: expBodyChangedCalled(mustBeCalled: false),
   313                                 exectedPlain: nil,
   314                                 exectedHtml: nil)
   315         let shouldReplace = vm.shouldReplaceText(in: testRange, of: testText, with: replaceText)
   316         XCTAssertTrue(shouldReplace, "Should alway be true")
   317         waitForExpectations(timeout: UnitTestUtils.waitTime)
   318     }
   319 
   320     func testShouldReplaceText_attachment_removeAll() {
   321         var textBuilder = NSAttributedString(string: "Test text")
   322         let replaceText = ""
   323         let replaceRange = NSRange(location: 0, length: "Test".count)
   324 
   325         let testAttachment1 = createTestAttachment(addImage: true)
   326         textBuilder = insertTextattachment(for: testAttachment1, in: replaceRange, of: textBuilder)
   327         let testAttachment2 = createTestAttachment(addImage: true)
   328         let testText = insertTextattachment(for: testAttachment2, in: replaceRange, of: textBuilder)
   329         let attachmentRange = NSRange(location: 0, length: testText.length)
   330         XCTAssertTrue(testText.textAttachments(range: attachmentRange).count == 1)
   331 
   332         let expectNoAttachmentLeft = [Attachment]()
   333 
   334         setupAssertionDelegates(initialPlaintext: nil,
   335                                 initialAttributedText: nil,
   336                                 initialInlinedAttachments: nil,
   337                                 expectInsertCalled: expInsertTextCalled(mustBeCalled: false),
   338                                 inserted: nil,
   339                                 expUserWantsToAddMediaCalled: expUserWantsToAddMediaCalled(mustBeCalled: false),
   340                                 expUserWantsToAddDocumentCalled: expUserWantsToAddDocumentCalled(mustBeCalled: false),
   341                                 expInlinedAttachmentsCalled: expInlinedAttachmentChanged(mustBeCalled: true),
   342                                 inlined: expectNoAttachmentLeft,
   343                                 expBodyChangedCalled: expBodyChangedCalled(mustBeCalled: false),
   344                                 exectedPlain: nil,
   345                                 exectedHtml: nil)
   346         let shouldReplace = vm.shouldReplaceText(in: attachmentRange, of: testText, with: replaceText)
   347         XCTAssertTrue(shouldReplace, "Should alway be true")
   348         waitForExpectations(timeout: UnitTestUtils.waitTime)
   349     }
   350 
   351     func testShouldReplaceText_attachment_removeOne() {
   352         var textBuilder = NSAttributedString(string: "Test text")
   353         let replaceText = ""
   354         let replaceRange = NSRange(location: 0, length: 0)
   355 
   356         let testAttachment1 = createTestAttachment(fileName: "1", addImage: true)
   357         textBuilder = insertTextattachment(for: testAttachment1, in: replaceRange, of: textBuilder)
   358         let testAttachment2 = createTestAttachment(fileName: "2", addImage: true)
   359         let testText = insertTextattachment(for: testAttachment2, in: replaceRange, of: textBuilder)
   360         let attachmentToRemoveRange = NSRange(location: 0, length: 1)
   361 
   362         let expectedAttachmentsLeft = [testAttachment1]
   363         XCTAssertTrue(testText.textAttachments(range: attachmentToRemoveRange).count == 1)
   364 
   365         setupAssertionDelegates(initialPlaintext: nil,
   366                                 initialAttributedText: nil,
   367                                 initialInlinedAttachments: [testAttachment1, testAttachment2],
   368                                 expectInsertCalled: nil,
   369                                 inserted: nil,
   370                                 expUserWantsToAddMediaCalled: expUserWantsToAddMediaCalled(mustBeCalled: false),
   371                                 expUserWantsToAddDocumentCalled: expUserWantsToAddDocumentCalled(mustBeCalled: false),
   372                                 expInlinedAttachmentsCalled: expInlinedAttachmentChanged(mustBeCalled: true),
   373                                 inlined: expectedAttachmentsLeft,
   374                                 expBodyChangedCalled: expBodyChangedCalled(mustBeCalled: false),
   375                                 exectedPlain: nil,
   376                                 exectedHtml: nil)
   377         let shouldReplace = vm.shouldReplaceText(in: attachmentToRemoveRange,
   378                                                  of: testText,
   379                                                  with: replaceText)
   380         XCTAssertTrue(shouldReplace, "Should alway be true")
   381         waitForExpectations(timeout: UnitTestUtils.waitTime)
   382     }
   383 
   384     func testShouldReplaceText_attachment_multipleRemove() {
   385         let attachmentsToRemoveCount = 10
   386         shouldReplaceText_attachment(remove: attachmentsToRemoveCount)
   387     }
   388     func shouldReplaceText_attachment (remove: Int) {
   389         var textBuilder = NSAttributedString(string: "Test text")
   390         let range = NSRange(location: 0, length: 0)
   391         var initialAttachments = [Attachment]()
   392 
   393         for i in 0..<remove {
   394             let testAttachment = createTestAttachment(fileName: String(i), addImage: true)
   395             textBuilder = insertTextattachment(for: testAttachment, in: range, of: textBuilder)
   396             initialAttachments.append(testAttachment)
   397         }
   398 
   399         let attachmentToRemoveRange = NSRange(location: 0, length: textBuilder.length)
   400         let expectedAttachmentsLeft = [Attachment]()
   401 
   402         setupAssertionDelegates(initialPlaintext: nil,
   403                                 initialAttributedText: nil,
   404                                 initialInlinedAttachments: initialAttachments,
   405                                 expectInsertCalled: nil,
   406                                 inserted: nil,
   407                                 expUserWantsToAddMediaCalled: expUserWantsToAddMediaCalled(mustBeCalled: false),
   408                                 expUserWantsToAddDocumentCalled: expUserWantsToAddDocumentCalled(mustBeCalled: false),
   409                                 expInlinedAttachmentsCalled: expInlinedAttachmentChanged(mustBeCalled: true),
   410                                 inlined: expectedAttachmentsLeft,
   411                                 expBodyChangedCalled: expBodyChangedCalled(mustBeCalled: false),
   412                                 exectedPlain: nil,
   413                                 exectedHtml: nil)
   414         let shouldReplace = vm.shouldReplaceText(in: attachmentToRemoveRange,
   415                                                  of: textBuilder,
   416                                                  with: "")
   417         XCTAssertTrue(shouldReplace, "Should alway be true")
   418         waitForExpectations(timeout: UnitTestUtils.waitTime)
   419     }
   420 
   421     // MARK: handleUserClickedSelectMedia
   422 
   423     func testHandleUserClickedSelectMedia() {
   424         setupAssertionDelegates(initialPlaintext: nil,
   425                                 initialAttributedText: nil,
   426                                 initialInlinedAttachments: nil,
   427                                 expectInsertCalled: expInsertTextCalled(mustBeCalled: false),
   428                                 inserted: nil,
   429                                 expUserWantsToAddMediaCalled: expUserWantsToAddMediaCalled(mustBeCalled: true),
   430                                 expUserWantsToAddDocumentCalled: expUserWantsToAddDocumentCalled(mustBeCalled: false),
   431                                 expInlinedAttachmentsCalled: expInlinedAttachmentChanged(mustBeCalled: false),
   432                                 inlined: nil,
   433                                 expBodyChangedCalled: expBodyChangedCalled(mustBeCalled: false),
   434                                 exectedPlain: nil,
   435                                 exectedHtml: nil)
   436        vm.handleUserClickedSelectMedia()
   437         waitForExpectations(timeout: UnitTestUtils.waitTime)
   438     }
   439 
   440     // MARK: handleUserClickedSelectDocument
   441 
   442     func testHandleUserClickedSelectDocument() {
   443         setupAssertionDelegates(initialPlaintext: nil,
   444                                 initialAttributedText: nil,
   445                                 initialInlinedAttachments: nil,
   446                                 expectInsertCalled: expInsertTextCalled(mustBeCalled: false),
   447                                 inserted: nil,
   448                                 expUserWantsToAddMediaCalled: expUserWantsToAddMediaCalled(mustBeCalled: false),
   449                                 expUserWantsToAddDocumentCalled: expUserWantsToAddDocumentCalled(mustBeCalled: true),
   450                                 expInlinedAttachmentsCalled: expInlinedAttachmentChanged(mustBeCalled: false),
   451                                 inlined: nil,
   452                                 expBodyChangedCalled: expBodyChangedCalled(mustBeCalled: false),
   453                                 exectedPlain: nil,
   454                                 exectedHtml: nil)
   455         vm.handleUserClickedSelectDocument()
   456         waitForExpectations(timeout: UnitTestUtils.waitTime)
   457     }
   458 
   459     // MARK: handleCursorPositionChange
   460 
   461     func testHandleCursorPositionChange_cursorBehindInlinedAttachment() {
   462         setupAssertionDelegates(initialPlaintext: nil,
   463                                 initialAttributedText: nil,
   464                                 initialInlinedAttachments: nil,
   465                                 expectInsertCalled: expInsertTextCalled(mustBeCalled: false),
   466                                 inserted: nil,
   467                                 expUserWantsToAddMediaCalled: nil,
   468                                 expUserWantsToAddDocumentCalled: nil,
   469                                 expInlinedAttachmentsCalled: nil,
   470                                 inlined: nil,
   471                                 expBodyChangedCalled: expBodyChangedCalled(mustBeCalled: false),
   472                                 exectedPlain: nil,
   473                                 exectedHtml: nil)
   474         let testPosition = 1
   475         vm.handleCursorPositionChange(newPosition: testPosition)
   476         vm.handleUserClickedSelectMedia()
   477         let inlinedImage = 1
   478         let expectedPosition = testPosition + inlinedImage
   479         XCTAssertEqual(vm.cursorPosition, expectedPosition,
   480                        "cursor should be placed behind inlined image")
   481         waitForExpectations(timeout: UnitTestUtils.waitTime)
   482     }
   483 
   484     func testHandleCursorPositionChange_noChangeAddingAttachment() {
   485         setupAssertionDelegates(initialPlaintext: nil,
   486                                 initialAttributedText: nil,
   487                                 initialInlinedAttachments: nil,
   488                                 expectInsertCalled: expInsertTextCalled(mustBeCalled: false),
   489                                 inserted: nil,
   490                                 expUserWantsToAddMediaCalled: nil,
   491                                 expUserWantsToAddDocumentCalled: nil,
   492                                 expInlinedAttachmentsCalled: nil,
   493                                 inlined: nil,
   494                                 expBodyChangedCalled: expBodyChangedCalled(mustBeCalled: false),
   495                                 exectedPlain: nil,
   496                                 exectedHtml: nil)
   497         let testPosition = 1
   498         vm.handleCursorPositionChange(newPosition: testPosition)
   499         vm.handleUserClickedSelectDocument()
   500         let expectedPosition = testPosition
   501         XCTAssertEqual(vm.cursorPosition, expectedPosition,
   502                        "cursor position does not change attaching an non-ilined image")
   503         waitForExpectations(timeout: UnitTestUtils.waitTime)
   504     }
   505 
   506     // MARK: - Helpers
   507 
   508     private func setupAssertionDelegates(
   509         initialPlaintext: String? = nil, initialAttributedText: NSAttributedString? = nil, initialInlinedAttachments: [Attachment]? = nil,
   510         expectInsertCalled: XCTestExpectation? = nil, inserted: NSAttributedString? = nil,
   511 
   512         expUserWantsToAddMediaCalled: XCTestExpectation? = nil,
   513         expUserWantsToAddDocumentCalled: XCTestExpectation? = nil,
   514         expInlinedAttachmentsCalled: XCTestExpectation? = nil, inlined: [Attachment]? = nil,
   515         expBodyChangedCalled: XCTestExpectation? = nil, exectedPlain: String? = nil, exectedHtml: String? = nil) {
   516         // Delegate
   517         testDelegate = TestDelegate(expectInsertCalled: expectInsertCalled, inserted: inserted)
   518 
   519         // Result Delegate
   520         let newTestResultDelegate =
   521             TestResultDelegate(
   522                 expUserWantsToAddMediaCalled: expUserWantsToAddMediaCalled, expUserWantsToAddDocumentCalled: expUserWantsToAddDocumentCalled,
   523                 expInlinedAttachmentsCalled: expInlinedAttachmentsCalled, inlined: inlined,
   524                 expBodyChangedCalled: expBodyChangedCalled, exectedPlain: exectedPlain, exectedHtml: exectedHtml)
   525         testResultDelegate = newTestResultDelegate
   526         vm = BodyCellViewModel(resultDelegate: newTestResultDelegate,
   527                                initialPlaintext: initialPlaintext,
   528                                initialAttributedText: initialAttributedText,
   529                                inlinedAttachments: initialInlinedAttachments)
   530         vm.delegate = testDelegate
   531         let aNonNullValue: CGFloat = 300.0
   532         vm.maxTextattachmentWidth = aNonNullValue
   533     }
   534 
   535     private func createTestAttachment(data: Data? = nil,
   536                                 mimeType: String = "test/mimeType",
   537                                 fileName: String? = nil,
   538                                 size: Int? = nil,
   539                                 url: URL? = nil,
   540                                 addImage: Bool = false,
   541                                 assetUrl: URL? = nil,
   542                                 contentDisposition: Attachment.ContentDispositionType = .inline) -> Attachment {
   543         guard let att = createTestAttachments(numAttachments: 1,
   544                                         data: data,
   545                                         mimeType: mimeType,
   546                                         fileName: fileName,
   547                                         size: size,
   548                                         url: url,
   549                                         addImage: addImage,
   550                                         assetUrl: assetUrl,
   551                                         contentDisposition: contentDisposition).first else {
   552             XCTFail()
   553             return Attachment(data: Data(), mimeType: "", contentDisposition: .other)
   554         }
   555         return att
   556     }
   557 
   558     private func createTestAttachments(numAttachments: Int = 1,
   559                                  data: Data? = nil,
   560                                  mimeType: String = "test/mimeType",
   561                                  fileName: String? = nil,
   562                                  size: Int? = nil,
   563                                  url: URL? = nil,
   564                                  addImage: Bool = false,
   565                                  assetUrl: URL? = nil,
   566                                  contentDisposition: Attachment.ContentDispositionType = .inline)
   567         -> [Attachment] {
   568             var attachments = [Attachment]()
   569             let imageFileName = "PorpoiseGalaxy_HubbleFraile_960.jpg" //IOS-1399: move to Utils
   570             guard
   571                 let imageData = MiscUtil.loadData(bundleClass: BodyCellViewModelTest.self,
   572                                                   fileName: imageFileName),
   573                 let image = UIImage(data: imageData) else {
   574                     XCTFail("No img")
   575                     return []
   576             }
   577 
   578             for i in 0..<numAttachments {
   579                 attachments.append(Attachment(data: data,
   580                                               mimeType: mimeType,
   581                                               fileName: fileName == nil ? "\(i)" : fileName,
   582                                               image: addImage ? image : nil,
   583                                               assetUrl: assetUrl,
   584                                               contentDisposition: contentDisposition))
   585             }
   586             return attachments
   587     }
   588 
   589     private func htmlVersion(of string: String) -> String {
   590         return string.isEmpty ? string : "<p>" + string + "</p>" + "\n"
   591     }
   592 
   593     private func expInsertTextCalled(mustBeCalled: Bool) -> XCTestExpectation {
   594         return expectation(inverted: !mustBeCalled)
   595     }
   596 
   597     private func expUserWantsToAddMediaCalled(mustBeCalled: Bool) -> XCTestExpectation {
   598         return expectation(inverted: !mustBeCalled)
   599     }
   600 
   601     private func expUserWantsToAddDocumentCalled(mustBeCalled: Bool) -> XCTestExpectation {
   602         return expectation(inverted: !mustBeCalled)
   603     }
   604 
   605     private func expInlinedAttachmentChanged(mustBeCalled: Bool) -> XCTestExpectation {
   606         return expectation(inverted: !mustBeCalled)
   607     }
   608 
   609     private func expBodyChangedCalled(mustBeCalled: Bool) -> XCTestExpectation {
   610         return expectation(inverted: !mustBeCalled)
   611     }
   612 
   613     private func insertTextattachment(for attachment: Attachment,
   614                                       in range: NSRange,
   615                                       of string: NSAttributedString) -> NSAttributedString {
   616         let attachmentString = textAttachmentString(for: attachment)
   617         return insert(text: attachmentString, in: range, of: string)
   618     }
   619 
   620     private func textAttachmentString(for attachment: Attachment) -> NSAttributedString {
   621         guard let image = attachment.image else {
   622             XCTFail("No image")
   623             return NSAttributedString()
   624         }
   625         attachment.contentDisposition = .inline
   626         let textAttachment = TextAttachment()
   627         textAttachment.image = image
   628         textAttachment.attachment = attachment
   629         let imageString = NSAttributedString(attachment: textAttachment)
   630         return imageString
   631     }
   632 
   633     private func insert(text insertText: NSAttributedString,
   634                         in range: NSRange,
   635                         of text: NSAttributedString) -> NSAttributedString {
   636         let attrText = NSMutableAttributedString(attributedString: text)
   637         attrText.replaceCharacters(in: range, with: insertText)
   638         return attrText
   639     }
   640 
   641     // MARK: BodyCellViewModelDelegate
   642 
   643     private class TestDelegate: BodyCellViewModelDelegate {
   644         //insert
   645         let expectInsertCalled: XCTestExpectation?
   646         let inserted: NSAttributedString?
   647 
   648         init(expectInsertCalled: XCTestExpectation?, inserted: NSAttributedString?) {
   649             self.expectInsertCalled = expectInsertCalled
   650             self.inserted = inserted
   651         }
   652 
   653         // BodyCellViewModelDelegate
   654 
   655         func insert(text: NSAttributedString) {
   656             guard let exp = expectInsertCalled else {
   657                 // We ignore called or not
   658                 return
   659             }
   660             exp.fulfill()
   661             if let expected = inserted {
   662                 XCTAssertEqual(text, expected)
   663             }
   664         }
   665     }
   666 
   667     // MARK: BodyCellViewModelResultDelegate
   668 
   669     private class TestResultDelegate: BodyCellViewModelResultDelegate {
   670         // context menu
   671         let expUserWantsToAddMediaCalled: XCTestExpectation?
   672         let expUserWantsToAddDocumentCalled: XCTestExpectation?
   673         // inlined image
   674         let expInlinedAttachmentsCalled: XCTestExpectation?
   675         let inlined: [Attachment]?
   676         // content change
   677         let expBodyChangedCalled: XCTestExpectation?
   678         let exectedPlain: String?
   679         let exectedHtml: String?
   680 
   681         init(expUserWantsToAddMediaCalled: XCTestExpectation?,
   682              expUserWantsToAddDocumentCalled: XCTestExpectation?,
   683              expInlinedAttachmentsCalled: XCTestExpectation?, inlined: [Attachment]?,
   684              expBodyChangedCalled: XCTestExpectation?, exectedPlain: String?, exectedHtml: String?) {
   685             self.expUserWantsToAddMediaCalled = expUserWantsToAddMediaCalled
   686             self.expUserWantsToAddDocumentCalled = expUserWantsToAddDocumentCalled
   687             self.expInlinedAttachmentsCalled = expInlinedAttachmentsCalled
   688             self.inlined = inlined
   689             self.expBodyChangedCalled = expBodyChangedCalled
   690             self.exectedPlain = exectedPlain
   691             self.exectedHtml = exectedHtml
   692         }
   693 
   694         func bodyCellViewModelUserWantsToAddMedia(_ vm: BodyCellViewModel) {
   695             guard let exp = expUserWantsToAddMediaCalled else {
   696                 // We ignore called or not
   697                 return
   698             }
   699             exp.fulfill()
   700         }
   701 
   702         func bodyCellViewModelUserWantsToAddDocument(_ vm: BodyCellViewModel) {
   703             guard let exp = expUserWantsToAddDocumentCalled else {
   704                 // We ignore called or not
   705                 return
   706             }
   707             exp.fulfill()
   708         }
   709 
   710         func bodyCellViewModel(_ vm: BodyCellViewModel,
   711                                inlinedAttachmentsChanged inlinedAttachments: [Attachment]) {
   712             guard let exp = expInlinedAttachmentsCalled else {
   713                 // We ignore called or not
   714                 return
   715             }
   716             exp.fulfill()
   717             if let expected = inlined {
   718                 XCTAssertEqual(inlinedAttachments.count, expected.count)
   719                 for testee in inlinedAttachments {
   720                     XCTAssertTrue(expected.contains(testee))
   721                 }
   722             }
   723         }
   724 
   725         func bodyCellViewModel(_ vm: BodyCellViewModel, bodyAttributedString: NSAttributedString) { //!!!: ADAM: commented due to the changes
   726 //            guard let exp = expBodyChangedCalled else {
   727 //                // We ignore called or not
   728 //                return
   729 //            }
   730 //            exp.fulfill()
   731 //            let expBody = bodyAttributedString.toHtml()
   732 //            let expPlainText = expBody.plainText
   733 //            if let expected1 = exectedPlain {
   734 //                XCTAssertEqual(expPlainText, expected1)
   735 //            }
   736         }
   737     }
   738 }