Skip to content

Commit 76d0186

Browse files
DRosadoYewmichaelkirk
authored andcommitted
[Invite Flow] Use email address to collate contacts when no given or family name is provided
* Add tests to ContactsPicker collate contacts functionality
1 parent 89730f2 commit 76d0186

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

Signal.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@
179179
76EB064818170B33006006FC /* Zid.m in Sources */ = {isa = PBXBuildFile; fileRef = 76EB04FD18170B33006006FC /* Zid.m */; };
180180
76EB065618170B34006006FC /* InCallViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 76EB050C18170B33006006FC /* InCallViewController.m */; };
181181
76EB068618170B34006006FC /* ContactTableViewCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 76EB052F18170B33006006FC /* ContactTableViewCell.m */; };
182+
954AEE6A1DF33E01002E5410 /* ContactsPickerTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 954AEE681DF33D32002E5410 /* ContactsPickerTest.swift */; };
182183
A10FDF79184FB4BB007FF963 /* MediaPlayer.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 76C87F18181EFCE600C4ACAB /* MediaPlayer.framework */; };
183184
A11CD70D17FA230600A2D1B1 /* QuartzCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A11CD70C17FA230600A2D1B1 /* QuartzCore.framework */; };
184185
A123C14916F902EE000AE905 /* Security.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A163E8AA16F3F6A90094D68B /* Security.framework */; };
@@ -844,6 +845,7 @@
844845
76EB052E18170B33006006FC /* ContactTableViewCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ContactTableViewCell.h; sourceTree = "<group>"; };
845846
76EB052F18170B33006006FC /* ContactTableViewCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ContactTableViewCell.m; sourceTree = "<group>"; };
846847
80CD5E19DD23200E7926EEA7 /* libPods-Signal.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-Signal.a"; sourceTree = BUILT_PRODUCTS_DIR; };
848+
954AEE681DF33D32002E5410 /* ContactsPickerTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ContactsPickerTest.swift; sourceTree = "<group>"; };
847849
A11CD70C17FA230600A2D1B1 /* QuartzCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = QuartzCore.framework; path = System/Library/Frameworks/QuartzCore.framework; sourceTree = SDKROOT; };
848850
A163E8AA16F3F6A90094D68B /* Security.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Security.framework; path = System/Library/Frameworks/Security.framework; sourceTree = SDKROOT; };
849851
A1C32D4D17A0652C000A904E /* AddressBook.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AddressBook.framework; path = System/Library/Frameworks/AddressBook.framework; sourceTree = SDKROOT; };
@@ -2025,6 +2027,7 @@
20252027
children = (
20262028
B660F6761C29867F00687D6E /* OWSContactsManagerTest.m */,
20272029
45843D211D223BA10013E85A /* OWSContactsSearcherTest.m */,
2030+
954AEE681DF33D32002E5410 /* ContactsPickerTest.swift */,
20282031
);
20292032
path = contact;
20302033
sourceTree = "<group>";
@@ -3180,6 +3183,7 @@
31803183
B660F7751C29988E00687D6E /* UIColor+OWS.m in Sources */,
31813184
B660F7761C29988E00687D6E /* UIFont+OWS.m in Sources */,
31823185
B660F7771C29988E00687D6E /* UIImage+normalizeImage.m in Sources */,
3186+
954AEE6A1DF33E01002E5410 /* ContactsPickerTest.swift in Sources */,
31833187
B660F7781C29988E00687D6E /* ArrayUtil.m in Sources */,
31843188
B660F7791C29988E00687D6E /* CyclicalBuffer.m in Sources */,
31853189
B660F77A1C29988E00687D6E /* PriorityQueue.m in Sources */,

Signal/src/view controllers/ContactsPicker.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,10 @@ fileprivate extension CNContact {
367367
*/
368368
@objc var nameForCollating: String {
369369
get {
370+
if self.familyName.isEmpty && self.givenName.isEmpty {
371+
return self.emailAddresses.first?.value as? String ?? ""
372+
}
373+
370374
let compositeName: String
371375
if ContactSortOrder == .familyName {
372376
compositeName = "\(self.familyName) \(self.givenName)"
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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

Comments
 (0)