@ -169,19 +169,20 @@ class OWSLinkPreviewTest: SSKBaseTestSwift {
}
func testPreviewUrlForMessageBodyText ( ) {
XCTAssertNil( OWSLinkPreview . previewUrl ( forMessageBodyText : " " ) )
XCTAssertNil( OWSLinkPreview . previewUrl ( forMessageBodyText : " alice bob jim " ) )
XCTAssertNil( OWSLinkPreview . previewUrl ( forMessageBodyText : " alice bob jim http:// " ) )
XCTAssertNil( OWSLinkPreview . previewUrl ( forMessageBodyText : " alice bob jim http://a.com " ) )
Assert( bodyText : " " , extractsLink : nil )
Assert( bodyText : " alice bob jim " , extractsLink : nil )
Assert( bodyText : " alice bob jim http:// " , extractsLink : nil )
Assert( bodyText : " alice bob jim http://a.com " , extractsLink : nil )
XCTAssertEqual ( OWSLinkPreview . previewUrl ( forMessageBodyText : " https://www.youtube.com/watch?v=tP-Ipsat90c " ) ,
" https://www.youtube.com/watch?v=tP-Ipsat90c " )
XCTAssertEqual ( OWSLinkPreview . previewUrl ( forMessageBodyText : " alice bob https://www.youtube.com/watch?v=tP-Ipsat90c jim " ) ,
" https://www.youtube.com/watch?v=tP-Ipsat90c " )
Assert ( bodyText : " https://www.youtube.com/watch?v=tP-Ipsat90c " ,
extractsLink : " https://www.youtube.com/watch?v=tP-Ipsat90c " )
Assert ( bodyText : " alice bob https://www.youtube.com/watch?v=tP-Ipsat90c jim " ,
extractsLink : " https://www.youtube.com/watch?v=tP-Ipsat90c " )
// I f t h e r e a r e m o r e t h a n o n e , t a k e t h e f i r s t .
XCTAssertEqual( OWSLinkPreview . previewUrl ( forMessageB odyText: " alice bob https://www.youtube.com/watch?v=tP-Ipsat90c jim https://www.youtube.com/watch?v=other-url carol " ) ,
" https://www.youtube.com/watch?v=tP-Ipsat90c " )
Assert( b odyText: " alice bob https://www.youtube.com/watch?v=tP-Ipsat90c jim https://www.youtube.com/watch?v=other-url carol " ,
extractsLink : " https://www.youtube.com/watch?v=tP-Ipsat90c " )
}
func testUtils ( ) {
@ -454,4 +455,87 @@ class OWSLinkPreviewTest: SSKBaseTestSwift {
options : [ ] ,
range : NSRange ( location : 0 , length : text . utf16 . count ) ) )
}
func testCursorPositions ( ) {
// s a n i t y c h e c k
Assert ( bodyText : " https://www.youtube.com/watch?v=testCursorPositionsa " ,
extractsLink : " https://www.youtube.com/watch?v=testCursorPositionsa " ,
selectedRange : nil )
// D o n ' t e x t r a c t l i n k i f c u r s o r i s t o u c h i n g t e x t
let text2 = " https://www.youtube.com/watch?v=testCursorPositionsb "
XCTAssertEqual ( text2 . count , 52 )
Assert ( bodyText : text2 ,
extractsLink : nil ,
selectedRange : NSRange ( location : 51 , length : 0 ) )
Assert ( bodyText : text2 ,
extractsLink : nil ,
selectedRange : NSRange ( location : 51 , length : 10 ) )
Assert ( bodyText : text2 ,
extractsLink : nil ,
selectedRange : NSRange ( location : 0 , length : 0 ) )
// U n l e s s t h e c u r s o r i s a t t h e e n d o f t h e t e x t
Assert ( bodyText : text2 ,
extractsLink : " https://www.youtube.com/watch?v=testCursorPositionsb " ,
selectedRange : NSRange ( location : 52 , length : 0 ) )
// O n c e e x t r a c t e d , k e e p t h e e x i s t i n g l i n k p r e v i e w , e v e n i f t h e c u r s o r m o v e s b a c k .
Assert ( bodyText : text2 ,
extractsLink : " https://www.youtube.com/watch?v=testCursorPositionsb " ,
selectedRange : NSRange ( location : 51 , length : 0 ) )
let text3 = " foo https://www.youtube.com/watch?v=testCursorPositionsc bar "
XCTAssertEqual ( text3 . count , 60 )
// f r o n t e d g e
Assert ( bodyText : text3 ,
extractsLink : nil ,
selectedRange : NSRange ( location : 4 , length : 0 ) )
// m i d d l e
Assert ( bodyText : text3 ,
extractsLink : nil ,
selectedRange : NSRange ( location : 4 , length : 0 ) )
// r e a r e d g e
Assert ( bodyText : text3 ,
extractsLink : nil ,
selectedRange : NSRange ( location : 56 , length : 0 ) )
// e x t r a c t l i n k i f s e l e c t i n g a f t e r l i n k
Assert ( bodyText : text3 ,
extractsLink : " https://www.youtube.com/watch?v=testCursorPositionsc " ,
selectedRange : NSRange ( location : 57 , length : 0 ) )
let text4 = " bar https://www.youtube.com/watch?v=testCursorPositionsd foo "
XCTAssertEqual ( text4 . count , 60 )
// f r o n t e d g e
Assert ( bodyText : text4 ,
extractsLink : nil ,
selectedRange : NSRange ( location : 4 , length : 0 ) )
// m i d d l e
Assert ( bodyText : text4 ,
extractsLink : nil ,
selectedRange : NSRange ( location : 20 , length : 0 ) )
// r e a r e d g e
Assert ( bodyText : text4 ,
extractsLink : nil ,
selectedRange : NSRange ( location : 56 , length : 0 ) )
// e x t r a c t l i n k i f s e l e c t i n g b e f o r e l i n k
Assert ( bodyText : text4 ,
extractsLink : " https://www.youtube.com/watch?v=testCursorPositionsd " ,
selectedRange : NSRange ( location : 3 , length : 0 ) )
}
private func Assert ( bodyText : String , extractsLink link : String ? , selectedRange : NSRange ? = nil , file : StaticString = #file , line : UInt = #line ) {
let actual = OWSLinkPreview . previewUrl ( forMessageBodyText : bodyText , selectedRange : selectedRange )
XCTAssertEqual ( actual , link , file : file , line : line )
}
}