|
| 1 | +// |
| 2 | +// ContactsPickerTest.swift |
| 3 | +// Signal |
| 4 | +// |
| 5 | +// Created by Daniel Rosado on 03/12/16. |
| 6 | +// Copyright © 2016 Open Whisper Systems. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import XCTest |
| 10 | +@testable import Signal |
| 11 | + |
| 12 | +final class ContactsPickerTest: XCTestCase { |
| 13 | + private var prevLang: Any? |
| 14 | + |
| 15 | + override func setUp() { |
| 16 | + super.setUp() |
| 17 | + |
| 18 | + prevLang = getLang() |
| 19 | + } |
| 20 | + |
| 21 | + override func tearDown() { |
| 22 | + super.tearDown() |
| 23 | + |
| 24 | + if let prevLang = prevLang { |
| 25 | + setLang(value: prevLang) |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + @available(iOS 9.0, *) |
| 30 | + func testContactSectionMatchesEmailFirstLetterWhenOnlyEmailContact() { |
| 31 | + setLangEN() |
| 32 | + |
| 33 | + let emailOnlyContactB = CNMutableContact() |
| 34 | + emailOnlyContactB .emailAddresses .append(CNLabeledValue(label : nil, value : "[email protected]")) |
| 35 | + |
| 36 | + let emailOnlyContactD = CNMutableContact() |
| 37 | + emailOnlyContactD .emailAddresses .append(CNLabeledValue(label : nil, value : "[email protected]")) |
| 38 | + |
| 39 | + let contactsPicker = ContactsPicker(delegate: nil) |
| 40 | + let collatedContacts = contactsPicker.collatedContacts([emailOnlyContactB, emailOnlyContactD]) |
| 41 | + |
| 42 | + let sectionTitles = contactsPicker.collation.sectionTitles |
| 43 | + if let bIndex = sectionTitles.index(of: "B") { |
| 44 | + let bSectionContacts = collatedContacts[bIndex] |
| 45 | + XCTAssertEqual(bSectionContacts.first, emailOnlyContactB) |
| 46 | + } |
| 47 | + |
| 48 | + if let dIndex = sectionTitles.index(of: "D") { |
| 49 | + let dSectionContacts = collatedContacts[dIndex] |
| 50 | + XCTAssertEqual(dSectionContacts.first, emailOnlyContactD) |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + @available(iOS 9.0, *) |
| 55 | + func testContactSectionMatchesNameFirstLetterWhenNameExistsInContact() { |
| 56 | + setLangEN() |
| 57 | + |
| 58 | + let nameAndEmailContact = CNMutableContact() |
| 59 | + nameAndEmailContact.givenName = "Alice" |
| 60 | + nameAndEmailContact .emailAddresses .append(CNLabeledValue(label : nil, value : "[email protected]")) |
| 61 | + |
| 62 | + let contactsPicker = ContactsPicker(delegate: nil) |
| 63 | + let collatedContacts = contactsPicker.collatedContacts([nameAndEmailContact]) |
| 64 | + |
| 65 | + let sectionTitles = contactsPicker.collation.sectionTitles |
| 66 | + if let aIndex = sectionTitles.index(of: "A") { |
| 67 | + let aSectionContacts = collatedContacts[aIndex] |
| 68 | + XCTAssertEqual(aSectionContacts.first, nameAndEmailContact) |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + |
| 73 | + private func setLangEN() { |
| 74 | + setLang(value: "en") |
| 75 | + } |
| 76 | + |
| 77 | + private func setLang(value: Any) { |
| 78 | + UserDefaults.standard.set(value, forKey: "AppleLanguages") |
| 79 | + UserDefaults.standard.synchronize() |
| 80 | + } |
| 81 | + |
| 82 | + private func setLang(value: String) { |
| 83 | + setLang(value: [value]) |
| 84 | + } |
| 85 | + |
| 86 | + private func getLang() -> Any? { |
| 87 | + return UserDefaults.standard.value(forKey: "AppleLanguages") |
| 88 | + } |
| 89 | +} |
0 commit comments