From da32570dc65449da4f12f1f72376f73c65cad0cd Mon Sep 17 00:00:00 2001 From: Michael Kirk Date: Sun, 19 Mar 2017 16:33:48 -0400 Subject: [PATCH] [SSK] remove convoluted dependency Moved query method to PhoneNumberUtil since that's they only caller. Moved corresponding tests to SSK // FREEBIE --- Podfile.lock | 2 +- Signal.xcodeproj/project.pbxproj | 4 - Signal/src/contact/OWSContactsManager.m | 16 -- Signal/test/contact/OWSContactsManagerTest.m | 22 +-- Signal/test/phone/PhoneNumberTest.m | 155 ------------------- 5 files changed, 5 insertions(+), 194 deletions(-) delete mode 100644 Signal/test/phone/PhoneNumberTest.m diff --git a/Podfile.lock b/Podfile.lock index 9660882ce..ce0f82cf7 100644 --- a/Podfile.lock +++ b/Podfile.lock @@ -133,7 +133,7 @@ CHECKOUT OPTIONS: :commit: a3c843cc8a423c5924c663490978f81dba34d04e :git: https://github.com/WhisperSystems/SignalProtocolKit.git SignalServiceKit: - :commit: 778d3dd2bc4541b998a481d4b233dd621642a363 + :commit: ca5bcaf3c2525555cfe140a78b1850632db1d440 :git: https://github.com/WhisperSystems/SignalServiceKit.git SocketRocket: :commit: 877ac7438be3ad0b45ef5ca3969574e4b97112bf diff --git a/Signal.xcodeproj/project.pbxproj b/Signal.xcodeproj/project.pbxproj index c14787e48..b3769f063 100644 --- a/Signal.xcodeproj/project.pbxproj +++ b/Signal.xcodeproj/project.pbxproj @@ -332,7 +332,6 @@ B660F6CB1C29868000687D6E /* NetworkStreamTest.m in Sources */ = {isa = PBXBuildFile; fileRef = B660F6901C29868000687D6E /* NetworkStreamTest.m */; }; B660F6CC1C29868000687D6E /* SecureEndPointTest.m in Sources */ = {isa = PBXBuildFile; fileRef = B660F6911C29868000687D6E /* SecureEndPointTest.m */; }; B660F6CD1C29868000687D6E /* UdpSocketTest.m in Sources */ = {isa = PBXBuildFile; fileRef = B660F6931C29868000687D6E /* UdpSocketTest.m */; }; - B660F6CE1C29868000687D6E /* PhoneNumberTest.m in Sources */ = {isa = PBXBuildFile; fileRef = B660F6951C29868000687D6E /* PhoneNumberTest.m */; }; B660F6CF1C29868000687D6E /* SessionDescriptorTest.m in Sources */ = {isa = PBXBuildFile; fileRef = B660F6971C29868000687D6E /* SessionDescriptorTest.m */; }; B660F6D01C29868000687D6E /* DecayingSampleEstimatorTest.m in Sources */ = {isa = PBXBuildFile; fileRef = B660F6991C29868000687D6E /* DecayingSampleEstimatorTest.m */; }; B660F6D11C29868000687D6E /* EventWindowTest.m in Sources */ = {isa = PBXBuildFile; fileRef = B660F69A1C29868000687D6E /* EventWindowTest.m */; }; @@ -1086,7 +1085,6 @@ B660F6901C29868000687D6E /* NetworkStreamTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NetworkStreamTest.m; sourceTree = ""; }; B660F6911C29868000687D6E /* SecureEndPointTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SecureEndPointTest.m; sourceTree = ""; }; B660F6931C29868000687D6E /* UdpSocketTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = UdpSocketTest.m; sourceTree = ""; }; - B660F6951C29868000687D6E /* PhoneNumberTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PhoneNumberTest.m; sourceTree = ""; }; B660F6971C29868000687D6E /* SessionDescriptorTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SessionDescriptorTest.m; sourceTree = ""; }; B660F6991C29868000687D6E /* DecayingSampleEstimatorTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DecayingSampleEstimatorTest.m; sourceTree = ""; }; B660F69A1C29868000687D6E /* EventWindowTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = EventWindowTest.m; sourceTree = ""; }; @@ -2409,7 +2407,6 @@ B660F6941C29868000687D6E /* phone */ = { isa = PBXGroup; children = ( - B660F6951C29868000687D6E /* PhoneNumberTest.m */, B660F6961C29868000687D6E /* signaling */, ); path = phone; @@ -3630,7 +3627,6 @@ 456F6E201E2411A000FD2210 /* CallService.swift in Sources */, B660F6DF1C29868000687D6E /* QueueTest.m in Sources */, B660F6BB1C29868000687D6E /* OWSContactsManagerTest.m in Sources */, - B660F6CE1C29868000687D6E /* PhoneNumberTest.m in Sources */, B660F6C41C29868000687D6E /* HandshakePacketTest.m in Sources */, B660F6CD1C29868000687D6E /* UdpSocketTest.m in Sources */, B660F6DD1C29868000687D6E /* ObservableTest.m in Sources */, diff --git a/Signal/src/contact/OWSContactsManager.m b/Signal/src/contact/OWSContactsManager.m index 2b2a35a96..8001c7f84 100644 --- a/Signal/src/contact/OWSContactsManager.m +++ b/Signal/src/contact/OWSContactsManager.m @@ -377,22 +377,6 @@ void onAddressBookChanged(ABAddressBookRef notifyAddressBook, CFDictionaryRef in return allContacts; } - -+ (BOOL)name:(NSString * _Nonnull)nameString matchesQuery:(NSString * _Nonnull)queryString { - NSCharacterSet *whitespaceSet = NSCharacterSet.whitespaceCharacterSet; - NSArray *queryStrings = [queryString componentsSeparatedByCharactersInSet:whitespaceSet]; - NSArray *nameStrings = [nameString componentsSeparatedByCharactersInSet:whitespaceSet]; - - return [queryStrings all:^int(NSString *query) { - if (query.length == 0) - return YES; - return [nameStrings any:^int(NSString *nameWord) { - NSStringCompareOptions searchOpts = NSCaseInsensitiveSearch | NSAnchoredSearch; - return [nameWord rangeOfString:query options:searchOpts].location != NSNotFound; - }]; - }]; -} - #pragma mark - Whisper User Management - (NSArray *)getSignalUsersFromContactsArray:(NSArray *)contacts { diff --git a/Signal/test/contact/OWSContactsManagerTest.m b/Signal/test/contact/OWSContactsManagerTest.m index 9efc7ab55..70475b13c 100644 --- a/Signal/test/contact/OWSContactsManagerTest.m +++ b/Signal/test/contact/OWSContactsManagerTest.m @@ -1,3 +1,7 @@ +// +// Copyright (c) 2017 Open Whisper Systems. All rights reserved. +// + #import #import "TestUtil.h" #import "OWSContactsManager.h" @@ -9,22 +13,4 @@ @implementation OWSContactsManagerTest -- (void)testQueryMatching { - test([OWSContactsManager name:@"big dave" matchesQuery:@"big dave"]); - test([OWSContactsManager name:@"big dave" matchesQuery:@"dave big"]); - test([OWSContactsManager name:@"big dave" matchesQuery:@"dave"]); - test([OWSContactsManager name:@"big dave" matchesQuery:@"big"]); - test([OWSContactsManager name:@"big dave" matchesQuery:@"big "]); - test([OWSContactsManager name:@"big dave" matchesQuery:@" big "]); - test([OWSContactsManager name:@"big dave" matchesQuery:@"dav"]); - test([OWSContactsManager name:@"big dave" matchesQuery:@"bi dav"]); - test([OWSContactsManager name:@"big dave" matchesQuery:@"big big big big big big big big big big dave dave dave dave dave"]); - - test(![OWSContactsManager name:@"big dave" matchesQuery:@"ave"]); - test(![OWSContactsManager name:@"big dave" matchesQuery:@"dare"]); - test(![OWSContactsManager name:@"big dave" matchesQuery:@"mike"]); - test(![OWSContactsManager name:@"big dave" matchesQuery:@"mike"]); - test(![OWSContactsManager name:@"dave" matchesQuery:@"big"]); -} - @end diff --git a/Signal/test/phone/PhoneNumberTest.m b/Signal/test/phone/PhoneNumberTest.m deleted file mode 100644 index 3eae009e5..000000000 --- a/Signal/test/phone/PhoneNumberTest.m +++ /dev/null @@ -1,155 +0,0 @@ -// -// Copyright (c) 2017 Open Whisper Systems. All rights reserved. -// - -#import "OWSContactsManager.h" -#import "PhoneNumber.h" -#import "PhoneNumberUtil.h" -#import "TestUtil.h" -#import "TextSecureKitEnv.h" -#import - -@interface PhoneNumberTest : XCTestCase - -@end - -@implementation PhoneNumberTest - --(void) testE164 { - test([[[PhoneNumber tryParsePhoneNumberFromText:@"+1 (902) 555-5555" fromRegion:@"US"] toE164] isEqualToString:@"+19025555555"]); - test([[[PhoneNumber tryParsePhoneNumberFromText:@"1 (902) 555-5555" fromRegion:@"US"] toE164] isEqualToString:@"+19025555555"]); - test([[[PhoneNumber tryParsePhoneNumberFromText:@"1-902-555-5555" fromRegion:@"US"] toE164] isEqualToString:@"+19025555555"]); - test([[[PhoneNumber tryParsePhoneNumberFromText:@"1-902-555-5555" fromRegion:@"US"] toE164] isEqualToString:@"+19025555555"]); -} - --(void) testTranslateCursorPosition { - testThrows([PhoneNumberUtil translateCursorPosition:0 from:nil to:@"" stickingRightward:true]); - testThrows([PhoneNumberUtil translateCursorPosition:0 from:@"" to:nil stickingRightward:true]); - testThrows([PhoneNumberUtil translateCursorPosition:1 from:@"" to:@"" stickingRightward:true]); - - test([PhoneNumberUtil translateCursorPosition:0 from:@"" to:@"" stickingRightward:true] == 0); - - test([PhoneNumberUtil translateCursorPosition:0 from:@"12" to:@"1" stickingRightward:true] == 0); - test([PhoneNumberUtil translateCursorPosition:1 from:@"12" to:@"1" stickingRightward:true] == 1); - test([PhoneNumberUtil translateCursorPosition:2 from:@"12" to:@"1" stickingRightward:true] == 1); - - test([PhoneNumberUtil translateCursorPosition:0 from:@"1" to:@"12" stickingRightward:false] == 0); - test([PhoneNumberUtil translateCursorPosition:0 from:@"1" to:@"12" stickingRightward:true] == 0); - test([PhoneNumberUtil translateCursorPosition:1 from:@"1" to:@"12" stickingRightward:false] == 1); - test([PhoneNumberUtil translateCursorPosition:1 from:@"1" to:@"12" stickingRightward:true] == 2); - - test([PhoneNumberUtil translateCursorPosition:0 from:@"12" to:@"132" stickingRightward:false] == 0); - test([PhoneNumberUtil translateCursorPosition:0 from:@"12" to:@"132" stickingRightward:true] == 0); - test([PhoneNumberUtil translateCursorPosition:1 from:@"12" to:@"132" stickingRightward:false] == 1); - test([PhoneNumberUtil translateCursorPosition:1 from:@"12" to:@"132" stickingRightward:true] == 2); - test([PhoneNumberUtil translateCursorPosition:2 from:@"12" to:@"132" stickingRightward:false] == 3); - test([PhoneNumberUtil translateCursorPosition:2 from:@"12" to:@"132" stickingRightward:true] == 3); - - test([PhoneNumberUtil translateCursorPosition:0 from:@"(55) 123-4567" to:@"(551) 234-567" stickingRightward:true] == 0); - test([PhoneNumberUtil translateCursorPosition:1 from:@"(55) 123-4567" to:@"(551) 234-567" stickingRightward:true] == 1); - test([PhoneNumberUtil translateCursorPosition:2 from:@"(55) 123-4567" to:@"(551) 234-567" stickingRightward:true] == 2); - test([PhoneNumberUtil translateCursorPosition:3 from:@"(55) 123-4567" to:@"(551) 234-567" stickingRightward:true] == 3); - test([PhoneNumberUtil translateCursorPosition:4 from:@"(55) 123-4567" to:@"(551) 234-567" stickingRightward:true] == 3); - test([PhoneNumberUtil translateCursorPosition:5 from:@"(55) 123-4567" to:@"(551) 234-567" stickingRightward:true] == 3); - test([PhoneNumberUtil translateCursorPosition:6 from:@"(55) 123-4567" to:@"(551) 234-567" stickingRightward:true] == 6); - test([PhoneNumberUtil translateCursorPosition:7 from:@"(55) 123-4567" to:@"(551) 234-567" stickingRightward:true] == 7); - test([PhoneNumberUtil translateCursorPosition:8 from:@"(55) 123-4567" to:@"(551) 234-567" stickingRightward:true] == 8); - test([PhoneNumberUtil translateCursorPosition:9 from:@"(55) 123-4567" to:@"(551) 234-567" stickingRightward:true] == 8); - - test([PhoneNumberUtil translateCursorPosition:0 from:@"(55) 123-4567" to:@"(551) 234-567" stickingRightward:false] == 0); - test([PhoneNumberUtil translateCursorPosition:1 from:@"(55) 123-4567" to:@"(551) 234-567" stickingRightward:false] == 1); - test([PhoneNumberUtil translateCursorPosition:2 from:@"(55) 123-4567" to:@"(551) 234-567" stickingRightward:false] == 2); - test([PhoneNumberUtil translateCursorPosition:3 from:@"(55) 123-4567" to:@"(551) 234-567" stickingRightward:false] == 3); - test([PhoneNumberUtil translateCursorPosition:4 from:@"(55) 123-4567" to:@"(551) 234-567" stickingRightward:false] == 3); - test([PhoneNumberUtil translateCursorPosition:5 from:@"(55) 123-4567" to:@"(551) 234-567" stickingRightward:false] == 3); - test([PhoneNumberUtil translateCursorPosition:6 from:@"(55) 123-4567" to:@"(551) 234-567" stickingRightward:false] == 4); - test([PhoneNumberUtil translateCursorPosition:7 from:@"(55) 123-4567" to:@"(551) 234-567" stickingRightward:false] == 7); - test([PhoneNumberUtil translateCursorPosition:8 from:@"(55) 123-4567" to:@"(551) 234-567" stickingRightward:false] == 8); - test([PhoneNumberUtil translateCursorPosition:9 from:@"(55) 123-4567" to:@"(551) 234-567" stickingRightward:false] == 8); - - test([PhoneNumberUtil translateCursorPosition:0 from:@"(5551) 234-567" to:@"(555) 123-4567" stickingRightward:true] == 0); - test([PhoneNumberUtil translateCursorPosition:1 from:@"(5551) 234-567" to:@"(555) 123-4567" stickingRightward:true] == 1); - test([PhoneNumberUtil translateCursorPosition:2 from:@"(5551) 234-567" to:@"(555) 123-4567" stickingRightward:true] == 2); - test([PhoneNumberUtil translateCursorPosition:3 from:@"(5551) 234-567" to:@"(555) 123-4567" stickingRightward:true] == 3); - test([PhoneNumberUtil translateCursorPosition:4 from:@"(5551) 234-567" to:@"(555) 123-4567" stickingRightward:true] == 6); - test([PhoneNumberUtil translateCursorPosition:5 from:@"(5551) 234-567" to:@"(555) 123-4567" stickingRightward:true] == 7); - - test([PhoneNumberUtil translateCursorPosition:0 from:@"(5551) 234-567" to:@"(555) 123-4567" stickingRightward:false] == 0); - test([PhoneNumberUtil translateCursorPosition:1 from:@"(5551) 234-567" to:@"(555) 123-4567" stickingRightward:false] == 1); - test([PhoneNumberUtil translateCursorPosition:2 from:@"(5551) 234-567" to:@"(555) 123-4567" stickingRightward:false] == 2); - test([PhoneNumberUtil translateCursorPosition:3 from:@"(5551) 234-567" to:@"(555) 123-4567" stickingRightward:false] == 3); - test([PhoneNumberUtil translateCursorPosition:4 from:@"(5551) 234-567" to:@"(555) 123-4567" stickingRightward:false] == 4); - test([PhoneNumberUtil translateCursorPosition:5 from:@"(5551) 234-567" to:@"(555) 123-4567" stickingRightward:false] == 7); -} - -- (void)testCallingCodeFromCountryCode -{ - XCTAssertEqualObjects([PhoneNumberUtil callingCodeFromCountryCode:@"US"], @"+1"); - XCTAssertEqualObjects([PhoneNumberUtil callingCodeFromCountryCode:@"GB"], @"+44"); - // Invalid country code. - XCTAssertEqualObjects([PhoneNumberUtil callingCodeFromCountryCode:@"EK"], @"+0"); - XCTAssertEqualObjects([PhoneNumberUtil callingCodeFromCountryCode:@"ZZZ"], @"+0"); - XCTAssertEqualObjects([PhoneNumberUtil callingCodeFromCountryCode:nil], @"+0"); -} - -- (void)testCountryNameFromCountryCode -{ - XCTAssertEqualObjects([PhoneNumberUtil countryNameFromCountryCode:@"US"], @"United States"); - XCTAssertEqualObjects([PhoneNumberUtil countryNameFromCountryCode:@"GB"], @"United Kingdom"); - // Invalid country code. - XCTAssertEqualObjects([PhoneNumberUtil countryNameFromCountryCode:@"EK"], @"EK"); - XCTAssertEqualObjects([PhoneNumberUtil countryNameFromCountryCode:@"ZZZ"], @"ZZZ"); - testThrows([PhoneNumberUtil countryNameFromCountryCode:nil]); -} - -- (void)testCountryCodesForSearchTerm -{ -// PhoneNumberUtil needs a valid OWSContactsManager in the TextSecureKitEnv, -// but we want to avoid constructing the entire app apparatus so we pass nil -// for the other parameters of the environment and disable nonnull warnings. -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wnonnull" - TextSecureKitEnv *sharedEnv = [[TextSecureKitEnv alloc] initWithCallMessageHandler:nil - contactsManager:[OWSContactsManager new] - notificationsManager:nil]; -#pragma clang diagnostic pop - [TextSecureKitEnv setSharedEnv:sharedEnv]; - - // Empty search. - XCTAssertGreaterThan([PhoneNumberUtil countryCodesForSearchTerm:nil].count, (NSUInteger)30); - XCTAssertGreaterThan([PhoneNumberUtil countryCodesForSearchTerm:@""].count, (NSUInteger)30); - XCTAssertGreaterThan([PhoneNumberUtil countryCodesForSearchTerm:@" "].count, (NSUInteger)30); - - // Searches with no results. - XCTAssertEqual([PhoneNumberUtil countryCodesForSearchTerm:@" . "].count, (NSUInteger)0); - XCTAssertEqual([PhoneNumberUtil countryCodesForSearchTerm:@" XXXXX "].count, (NSUInteger)0); - XCTAssertEqual([PhoneNumberUtil countryCodesForSearchTerm:@" ! "].count, (NSUInteger)0); - - // Search by country code. - XCTAssertEqualObjects([PhoneNumberUtil countryCodesForSearchTerm:@"GB"], (@[ @"GB" ])); - XCTAssertEqualObjects([PhoneNumberUtil countryCodesForSearchTerm:@"gb"], (@[ @"GB" ])); - XCTAssertEqualObjects([PhoneNumberUtil countryCodesForSearchTerm:@"GB "], (@[ @"GB" ])); - XCTAssertEqualObjects([PhoneNumberUtil countryCodesForSearchTerm:@" GB"], (@[ @"GB" ])); - XCTAssertTrue([[PhoneNumberUtil countryCodesForSearchTerm:@" G"] containsObject:@"GB"]); - XCTAssertFalse([[PhoneNumberUtil countryCodesForSearchTerm:@" B"] containsObject:@"GB"]); - - // Search by country name. - XCTAssertEqualObjects([PhoneNumberUtil countryCodesForSearchTerm:@"united kingdom"], (@[ @"GB" ])); - XCTAssertEqualObjects([PhoneNumberUtil countryCodesForSearchTerm:@" UNITED KINGDOM "], (@[ @"GB" ])); - XCTAssertEqualObjects([PhoneNumberUtil countryCodesForSearchTerm:@" UNITED KING "], (@[ @"GB" ])); - XCTAssertEqualObjects([PhoneNumberUtil countryCodesForSearchTerm:@" UNI KING "], (@[ @"GB" ])); - XCTAssertEqualObjects([PhoneNumberUtil countryCodesForSearchTerm:@" u k "], (@[ @"GB" ])); - XCTAssertTrue([[PhoneNumberUtil countryCodesForSearchTerm:@" u"] containsObject:@"GB"]); - XCTAssertTrue([[PhoneNumberUtil countryCodesForSearchTerm:@" k"] containsObject:@"GB"]); - XCTAssertFalse([[PhoneNumberUtil countryCodesForSearchTerm:@" m"] containsObject:@"GB"]); - - // Search by calling code. - XCTAssertTrue([[PhoneNumberUtil countryCodesForSearchTerm:@" +44 "] containsObject:@"GB"]); - XCTAssertTrue([[PhoneNumberUtil countryCodesForSearchTerm:@" 44 "] containsObject:@"GB"]); - XCTAssertTrue([[PhoneNumberUtil countryCodesForSearchTerm:@" +4 "] containsObject:@"GB"]); - XCTAssertTrue([[PhoneNumberUtil countryCodesForSearchTerm:@" 4 "] containsObject:@"GB"]); - XCTAssertFalse([[PhoneNumberUtil countryCodesForSearchTerm:@" +123 "] containsObject:@"GB"]); - XCTAssertFalse([[PhoneNumberUtil countryCodesForSearchTerm:@" +444 "] containsObject:@"GB"]); -} - -@end