ImageURLSenderItemCreatorTest.swift 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. import XCTest
  21. @testable import ThreemaFramework
  22. class ImageURLSenderItemCreatorTest: XCTestCase {
  23. let testBundle: Bundle = Bundle(for: ImageURLSenderItemCreatorTest.self)
  24. override func setUpWithError() throws {
  25. // Put setup code here. This method is called before the invocation of each test method in the class.
  26. // necessary for ValidationLogger
  27. AppGroup.setGroupId("group.ch.threema") //THREEMA_GROUP_IDENTIFIER @"group.ch.threema"
  28. }
  29. override func tearDownWithError() throws {
  30. // Put teardown code here. This method is called after the invocation of each test method in the class.
  31. }
  32. static private let jpg1 = "Bild-1-0"
  33. static private let heic1 = "Bild-2"
  34. static private let heic2 = "Bild-3"
  35. static private let pngRegular = "Bild-4"
  36. static private let pngSticker = "Bild-5-0"
  37. static private let pngAlpha = "Bild-5-1"
  38. static private let gifStatic = "Bild-6"
  39. static private let rawFilename = "Bild-7"
  40. static private let pngScreenshot = "Bild-8"
  41. let testMatrix : [(String, String, String, CGFloat, CFString, NSNumber)] = [
  42. (heic1, "heic", "original", ImageURLSenderItemCreator.kImageSizeOriginal, kUTTypeJPEG, 1),
  43. (heic1, "heic", "xLarge", ImageURLSenderItemCreator.kImageSizeXLarge, kUTTypeJPEG, 1),
  44. (heic1, "heic", "large", ImageURLSenderItemCreator.kImageSizeLarge, kUTTypeJPEG, 1),
  45. (heic1, "heic", "medium", ImageURLSenderItemCreator.kImageSizeMedium, kUTTypeJPEG, 1),
  46. (heic1, "heic", "small", ImageURLSenderItemCreator.kImageSizeSmall, kUTTypeJPEG, 1),
  47. (jpg1, "jpg", "original", ImageURLSenderItemCreator.kImageSizeOriginal, kUTTypeJPEG, 1),
  48. (jpg1, "jpg", "xLarge", ImageURLSenderItemCreator.kImageSizeXLarge, kUTTypeJPEG, 1),
  49. (jpg1, "jpg", "large", ImageURLSenderItemCreator.kImageSizeLarge, kUTTypeJPEG, 1),
  50. (jpg1, "jpg", "medium", ImageURLSenderItemCreator.kImageSizeMedium, kUTTypeJPEG, 1),
  51. (jpg1, "jpg", "small", ImageURLSenderItemCreator.kImageSizeSmall, kUTTypeJPEG, 1),
  52. (gifStatic, "gif", "original", ImageURLSenderItemCreator.kImageSizeOriginal, kUTTypeGIF, 2),
  53. (gifStatic, "gif", "xLarge", ImageURLSenderItemCreator.kImageSizeOriginal, kUTTypeGIF, 2),
  54. (gifStatic, "gif", "large", ImageURLSenderItemCreator.kImageSizeOriginal, kUTTypeGIF, 2),
  55. (gifStatic, "gif", "medium", ImageURLSenderItemCreator.kImageSizeOriginal, kUTTypeGIF, 2),
  56. (gifStatic, "gif", "small", ImageURLSenderItemCreator.kImageSizeOriginal, kUTTypeGIF, 2),
  57. (pngRegular, "png", "original", ImageURLSenderItemCreator.kImageSizeOriginal, kUTTypeJPEG, 1),
  58. (pngRegular, "png", "xLarge", ImageURLSenderItemCreator.kImageSizeXLarge, kUTTypeJPEG, 1),
  59. (pngRegular, "png", "large", ImageURLSenderItemCreator.kImageSizeLarge, kUTTypeJPEG, 1),
  60. (pngRegular, "png", "medium", ImageURLSenderItemCreator.kImageSizeMedium, kUTTypeJPEG, 1),
  61. (pngRegular, "png", "small", ImageURLSenderItemCreator.kImageSizeSmall, kUTTypeJPEG, 1),
  62. (pngSticker, "png", "original", ImageURLSenderItemCreator.kImageSizeOriginal, kUTTypePNG, 2),
  63. (pngSticker, "png", "xLarge", ImageURLSenderItemCreator.kImageSizeXLarge, kUTTypePNG, 2),
  64. (pngSticker, "png", "large", ImageURLSenderItemCreator.kImageSizeLarge, kUTTypePNG, 2),
  65. (pngSticker, "png", "medium", ImageURLSenderItemCreator.kImageSizeMedium, kUTTypePNG, 2),
  66. (pngSticker, "png", "small", ImageURLSenderItemCreator.kImageSizeSmall, kUTTypePNG, 2),
  67. (pngScreenshot, "png", "original", ImageURLSenderItemCreator.kImageSizeOriginal, kUTTypeJPEG, 1),
  68. (pngScreenshot, "png", "xLarge", ImageURLSenderItemCreator.kImageSizeXLarge, kUTTypeJPEG, 1),
  69. (pngScreenshot, "png", "large", ImageURLSenderItemCreator.kImageSizeLarge, kUTTypeJPEG, 1),
  70. (pngScreenshot, "png", "medium", ImageURLSenderItemCreator.kImageSizeMedium, kUTTypeJPEG, 1),
  71. (pngScreenshot, "png", "small", ImageURLSenderItemCreator.kImageSizeSmall, kUTTypeJPEG, 1),
  72. (pngRegular, "png", "original", ImageURLSenderItemCreator.kImageSizeOriginal, kUTTypeJPEG, 1),
  73. (pngRegular, "png", "xLarge", ImageURLSenderItemCreator.kImageSizeXLarge, kUTTypeJPEG, 1),
  74. (pngRegular, "png", "large", ImageURLSenderItemCreator.kImageSizeLarge, kUTTypeJPEG, 1),
  75. (pngRegular, "png", "medium", ImageURLSenderItemCreator.kImageSizeMedium, kUTTypeJPEG, 1),
  76. (pngRegular, "png", "small", ImageURLSenderItemCreator.kImageSizeSmall, kUTTypeJPEG, 1),
  77. (pngRegular, "png", "original", ImageURLSenderItemCreator.kImageSizeOriginal, kUTTypeJPEG, 1),
  78. (pngRegular, "png", "xLarge", ImageURLSenderItemCreator.kImageSizeXLarge, kUTTypeJPEG, 1),
  79. (pngRegular, "png", "large", ImageURLSenderItemCreator.kImageSizeLarge, kUTTypeJPEG, 1),
  80. (pngRegular, "png", "medium", ImageURLSenderItemCreator.kImageSizeMedium, kUTTypeJPEG, 1),
  81. (pngRegular, "png", "small", ImageURLSenderItemCreator.kImageSizeSmall, kUTTypeJPEG, 1),
  82. (rawFilename, "srf", "original", ImageURLSenderItemCreator.kImageSizeOriginal, kUTTypeJPEG, 1),
  83. (rawFilename, "srf", "xLarge", ImageURLSenderItemCreator.kImageSizeXLarge, kUTTypeJPEG, 1),
  84. (rawFilename, "srf", "large", ImageURLSenderItemCreator.kImageSizeLarge, kUTTypeJPEG, 1),
  85. (rawFilename, "srf", "medium", ImageURLSenderItemCreator.kImageSizeMedium, kUTTypeJPEG, 1),
  86. (rawFilename, "srf", "small", ImageURLSenderItemCreator.kImageSizeSmall, kUTTypeJPEG, 1),
  87. (rawFilename, "sr2", "original", ImageURLSenderItemCreator.kImageSizeOriginal, kUTTypeJPEG, 1),
  88. (rawFilename, "sr2", "xLarge", ImageURLSenderItemCreator.kImageSizeXLarge, kUTTypeJPEG, 1),
  89. (rawFilename, "sr2", "large", ImageURLSenderItemCreator.kImageSizeLarge, kUTTypeJPEG, 1),
  90. (rawFilename, "sr2", "medium", ImageURLSenderItemCreator.kImageSizeMedium, kUTTypeJPEG, 1),
  91. (rawFilename, "sr2", "small", ImageURLSenderItemCreator.kImageSizeSmall, kUTTypeJPEG, 1),
  92. (rawFilename, "raf", "original", ImageURLSenderItemCreator.kImageSizeOriginal, kUTTypeJPEG, 1),
  93. (rawFilename, "raf", "xLarge", ImageURLSenderItemCreator.kImageSizeXLarge, kUTTypeJPEG, 1),
  94. (rawFilename, "raf", "large", ImageURLSenderItemCreator.kImageSizeLarge, kUTTypeJPEG, 1),
  95. (rawFilename, "raf", "medium", ImageURLSenderItemCreator.kImageSizeMedium, kUTTypeJPEG, 1),
  96. (rawFilename, "raf", "small", ImageURLSenderItemCreator.kImageSizeSmall, kUTTypeJPEG, 1),
  97. (rawFilename, "pef", "original", ImageURLSenderItemCreator.kImageSizeOriginal, kUTTypeJPEG, 1),
  98. (rawFilename, "pef", "xLarge", ImageURLSenderItemCreator.kImageSizeXLarge, kUTTypeJPEG, 1),
  99. (rawFilename, "pef", "large", ImageURLSenderItemCreator.kImageSizeLarge, kUTTypeJPEG, 1),
  100. (rawFilename, "pef", "medium", ImageURLSenderItemCreator.kImageSizeMedium, kUTTypeJPEG, 1),
  101. (rawFilename, "pef", "small", ImageURLSenderItemCreator.kImageSizeSmall, kUTTypeJPEG, 1),
  102. (rawFilename, "orf", "original", ImageURLSenderItemCreator.kImageSizeOriginal, kUTTypeJPEG, 1),
  103. (rawFilename, "orf", "xLarge", ImageURLSenderItemCreator.kImageSizeXLarge, kUTTypeJPEG, 1),
  104. (rawFilename, "orf", "large", ImageURLSenderItemCreator.kImageSizeLarge, kUTTypeJPEG, 1),
  105. (rawFilename, "orf", "medium", ImageURLSenderItemCreator.kImageSizeMedium, kUTTypeJPEG, 1),
  106. (rawFilename, "orf", "small", ImageURLSenderItemCreator.kImageSizeSmall, kUTTypeJPEG, 1),
  107. (rawFilename, "nef", "original", ImageURLSenderItemCreator.kImageSizeOriginal, kUTTypeJPEG, 1),
  108. (rawFilename, "nef", "xLarge", ImageURLSenderItemCreator.kImageSizeXLarge, kUTTypeJPEG, 1),
  109. (rawFilename, "nef", "large", ImageURLSenderItemCreator.kImageSizeLarge, kUTTypeJPEG, 1),
  110. (rawFilename, "nef", "medium", ImageURLSenderItemCreator.kImageSizeMedium, kUTTypeJPEG, 1),
  111. (rawFilename, "nef", "small", ImageURLSenderItemCreator.kImageSizeSmall, kUTTypeJPEG, 1),
  112. (rawFilename, "mrw", "original", ImageURLSenderItemCreator.kImageSizeOriginal, kUTTypeJPEG, 1),
  113. (rawFilename, "mrw", "xLarge", ImageURLSenderItemCreator.kImageSizeXLarge, kUTTypeJPEG, 1),
  114. (rawFilename, "mrw", "large", ImageURLSenderItemCreator.kImageSizeLarge, kUTTypeJPEG, 1),
  115. (rawFilename, "mrw", "medium", ImageURLSenderItemCreator.kImageSizeMedium, kUTTypeJPEG, 1),
  116. (rawFilename, "mrw", "small", ImageURLSenderItemCreator.kImageSizeSmall, kUTTypeJPEG, 1),
  117. (rawFilename, "erf", "original", ImageURLSenderItemCreator.kImageSizeOriginal, kUTTypeJPEG, 1),
  118. (rawFilename, "erf", "xLarge", ImageURLSenderItemCreator.kImageSizeXLarge, kUTTypeJPEG, 1),
  119. (rawFilename, "erf", "large", ImageURLSenderItemCreator.kImageSizeLarge, kUTTypeJPEG, 1),
  120. (rawFilename, "erf", "medium", ImageURLSenderItemCreator.kImageSizeMedium, kUTTypeJPEG, 1),
  121. (rawFilename, "erf", "small", ImageURLSenderItemCreator.kImageSizeSmall, kUTTypeJPEG, 1),
  122. (rawFilename, "dng", "original", ImageURLSenderItemCreator.kImageSizeOriginal, kUTTypeJPEG, 1),
  123. (rawFilename, "dng", "xLarge", ImageURLSenderItemCreator.kImageSizeXLarge, kUTTypeJPEG, 1),
  124. (rawFilename, "dng", "large", ImageURLSenderItemCreator.kImageSizeLarge, kUTTypeJPEG, 1),
  125. (rawFilename, "dng", "medium", ImageURLSenderItemCreator.kImageSizeMedium, kUTTypeJPEG, 1),
  126. (rawFilename, "dng", "small", ImageURLSenderItemCreator.kImageSizeSmall, kUTTypeJPEG, 1),
  127. (rawFilename, "dcr", "original", ImageURLSenderItemCreator.kImageSizeOriginal, kUTTypeJPEG, 1),
  128. (rawFilename, "dcr", "xLarge", ImageURLSenderItemCreator.kImageSizeXLarge, kUTTypeJPEG, 1),
  129. (rawFilename, "dcr", "large", ImageURLSenderItemCreator.kImageSizeLarge, kUTTypeJPEG, 1),
  130. (rawFilename, "dcr", "medium", ImageURLSenderItemCreator.kImageSizeMedium, kUTTypeJPEG, 1),
  131. (rawFilename, "dcr", "small", ImageURLSenderItemCreator.kImageSizeSmall, kUTTypeJPEG, 1),
  132. (rawFilename, "crw", "original", ImageURLSenderItemCreator.kImageSizeOriginal, kUTTypeJPEG, 1),
  133. (rawFilename, "crw", "xLarge", ImageURLSenderItemCreator.kImageSizeXLarge, kUTTypeJPEG, 1),
  134. (rawFilename, "crw", "large", ImageURLSenderItemCreator.kImageSizeLarge, kUTTypeJPEG, 1),
  135. (rawFilename, "crw", "medium", ImageURLSenderItemCreator.kImageSizeMedium, kUTTypeJPEG, 1),
  136. (rawFilename, "crw", "small", ImageURLSenderItemCreator.kImageSizeSmall, kUTTypeJPEG, 1),
  137. (rawFilename, "cr2", "original", ImageURLSenderItemCreator.kImageSizeOriginal, kUTTypeJPEG, 1),
  138. (rawFilename, "cr2", "xLarge", ImageURLSenderItemCreator.kImageSizeXLarge, kUTTypeJPEG, 1),
  139. (rawFilename, "cr2", "large", ImageURLSenderItemCreator.kImageSizeLarge, kUTTypeJPEG, 1),
  140. (rawFilename, "cr2", "medium", ImageURLSenderItemCreator.kImageSizeMedium, kUTTypeJPEG, 1),
  141. (rawFilename, "cr2", "small", ImageURLSenderItemCreator.kImageSizeSmall, kUTTypeJPEG, 1),
  142. (rawFilename, "arw", "original", ImageURLSenderItemCreator.kImageSizeOriginal, kUTTypeJPEG, 1),
  143. (rawFilename, "arw", "xLarge", ImageURLSenderItemCreator.kImageSizeXLarge, kUTTypeJPEG, 1),
  144. (rawFilename, "arw", "large", ImageURLSenderItemCreator.kImageSizeLarge, kUTTypeJPEG, 1),
  145. (rawFilename, "arw", "medium", ImageURLSenderItemCreator.kImageSizeMedium, kUTTypeJPEG, 1),
  146. (rawFilename, "arw", "small", ImageURLSenderItemCreator.kImageSizeSmall, kUTTypeJPEG, 1),
  147. (rawFilename, "raw", "original", ImageURLSenderItemCreator.kImageSizeOriginal, kUTTypeJPEG, 1),
  148. (rawFilename, "raw", "xLarge", ImageURLSenderItemCreator.kImageSizeXLarge, kUTTypeJPEG, 1),
  149. (rawFilename, "raw", "large", ImageURLSenderItemCreator.kImageSizeLarge, kUTTypeJPEG, 1),
  150. (rawFilename, "raw", "medium", ImageURLSenderItemCreator.kImageSizeMedium, kUTTypeJPEG, 1),
  151. (rawFilename, "raw", "small", ImageURLSenderItemCreator.kImageSizeSmall, kUTTypeJPEG, 1),
  152. ]
  153. func testImageSize() throws {
  154. let matrix : [(String, String, String, CGFloat)] = [
  155. ("Bild-1-1", "jpg", "original", 4592),
  156. (ImageURLSenderItemCreatorTest.jpg1, "jpg", "original", 1837),
  157. (ImageURLSenderItemCreatorTest.jpg1, "jpg", "small", ImageURLSenderItemCreator.kImageSizeSmall),
  158. (ImageURLSenderItemCreatorTest.jpg1, "jpg", "medium", ImageURLSenderItemCreator.kImageSizeMedium),
  159. (ImageURLSenderItemCreatorTest.jpg1, "jpg", "large", ImageURLSenderItemCreator.kImageSizeLarge),
  160. (ImageURLSenderItemCreatorTest.jpg1, "jpg", "xlarge", ImageURLSenderItemCreator.kImageSizeXLarge),
  161. (ImageURLSenderItemCreatorTest.heic1, "heic", "small", ImageURLSenderItemCreator.kImageSizeSmall),
  162. (ImageURLSenderItemCreatorTest.heic1, "heic", "medium", ImageURLSenderItemCreator.kImageSizeMedium),
  163. (ImageURLSenderItemCreatorTest.heic1, "heic", "large", ImageURLSenderItemCreator.kImageSizeLarge),
  164. (ImageURLSenderItemCreatorTest.heic1, "heic", "xlarge", ImageURLSenderItemCreator.kImageSizeXLarge)]
  165. for item in matrix {
  166. let testImageUrl = testBundle.url(forResource: item.0, withExtension: item.1)
  167. let testImageData = (try? Data(contentsOf: testImageUrl!))!
  168. let image = UIImage(data: testImageData)!
  169. let itemCreator = ImageURLSenderItemCreator(with: item.2, forceSize: true)
  170. let resolution = itemCreator.imageMaxSize(image)
  171. XCTAssert(resolution == item.3, "Expected \(item.3) but received \(resolution)")
  172. }
  173. }
  174. func testNumberOfImageSizes() {
  175. let noSizes = 5
  176. XCTAssert(ImageURLSenderItemCreator.getImageSizeNo() == noSizes, "Number of sizes should be \(noSizes). Check if settings still work.")
  177. }
  178. func testPixelSizes() {
  179. let sizes = ImageURLSenderItemCreator.imagePixelSizes()!
  180. XCTAssert(sizes[0] as! CGFloat == ImageURLSenderItemCreator.kImageSizeSmall)
  181. XCTAssert(sizes[1] as! CGFloat == ImageURLSenderItemCreator.kImageSizeMedium)
  182. XCTAssert(sizes[2] as! CGFloat == ImageURLSenderItemCreator.kImageSizeLarge)
  183. XCTAssert(sizes[3] as! CGFloat == ImageURLSenderItemCreator.kImageSizeXLarge)
  184. XCTAssert(sizes[4] as! CGFloat == ImageURLSenderItemCreator.kImageSizeOriginal)
  185. }
  186. func testCreateSenderFromUIImage() {
  187. let matrix : [(filename : String, String, String, CGFloat, expectedMimeType : CFString, NSNumber)] = [
  188. (ImageURLSenderItemCreatorTest.pngRegular, "png", "original", ImageURLSenderItemCreator.kImageSizeOriginal, kUTTypeJPEG, 1),
  189. (ImageURLSenderItemCreatorTest.pngRegular, "png", "xLarge", ImageURLSenderItemCreator.kImageSizeXLarge, kUTTypeJPEG, 1),
  190. (ImageURLSenderItemCreatorTest.pngRegular, "png", "large", ImageURLSenderItemCreator.kImageSizeLarge, kUTTypeJPEG, 1),
  191. (ImageURLSenderItemCreatorTest.pngRegular, "png", "medium", ImageURLSenderItemCreator.kImageSizeMedium, kUTTypeJPEG, 1),
  192. (ImageURLSenderItemCreatorTest.pngRegular, "png", "small", ImageURLSenderItemCreator.kImageSizeSmall, kUTTypeJPEG, 1),
  193. (ImageURLSenderItemCreatorTest.pngSticker, "png", "original", ImageURLSenderItemCreator.kImageSizeOriginal, kUTTypeJPEG, 2),
  194. (ImageURLSenderItemCreatorTest.pngSticker, "png", "xLarge", ImageURLSenderItemCreator.kImageSizeXLarge, kUTTypeJPEG, 2),
  195. (ImageURLSenderItemCreatorTest.pngSticker, "png", "large", ImageURLSenderItemCreator.kImageSizeLarge, kUTTypeJPEG, 2),
  196. (ImageURLSenderItemCreatorTest.pngSticker, "png", "medium", ImageURLSenderItemCreator.kImageSizeMedium, kUTTypeJPEG, 2),
  197. (ImageURLSenderItemCreatorTest.pngSticker, "png", "small", ImageURLSenderItemCreator.kImageSizeSmall, kUTTypeJPEG, 2)
  198. ]
  199. let specialTestMatrix = testMatrix[0..<10] + matrix
  200. for item in specialTestMatrix {
  201. let testImageUrl = testBundle.url(forResource: item.0, withExtension: item.1)
  202. let testImageData = (try? Data(contentsOf: testImageUrl!))!
  203. let image = UIImage(data: testImageData)!
  204. let itemCreator = ImageURLSenderItemCreator(with: item.2, forceSize: true)
  205. let senderItem = itemCreator.senderItem(fromImage: image)
  206. XCTAssert(senderItem!.caption == nil)
  207. XCTAssert(senderItem!.getMimeType() == UTIConverter.mimeType(fromUTI: item.4 as String), "\(item.0) had mime type \(senderItem!.getMimeType()) but expected \(item.4)")
  208. XCTAssertNotNil(senderItem!.getThumbnail())
  209. }
  210. }
  211. func testCreateSenderFromData() {
  212. for item in testMatrix {
  213. let testImageUrl = testBundle.url(forResource: item.0, withExtension: item.1)
  214. let testImageData = (try? Data(contentsOf: testImageUrl!))!
  215. let itemCreator = ImageURLSenderItemCreator(with: item.2, forceSize: true)
  216. guard let senderItem = itemCreator.senderItem(from: testImageData, uti: item.4 as String) else {
  217. XCTFail("Could not create senderItem from valid testdata \(testImageUrl)")
  218. return
  219. }
  220. checkSenderItem(senderItem: senderItem, item: item)
  221. }
  222. }
  223. func testCreateSenderFromURL() {
  224. for item in testMatrix {
  225. let testImageUrl = testBundle.url(forResource: item.0, withExtension: item.1)
  226. let itemCreator = ImageURLSenderItemCreator(with: item.2, forceSize: true)
  227. guard let senderItem = itemCreator.senderItem(from: testImageUrl!) else {
  228. XCTFail("Could not create senderItem from valid testdata \(String(describing: testImageUrl))")
  229. return
  230. }
  231. checkSenderItem(senderItem: senderItem, item: item)
  232. }
  233. }
  234. func checkSenderItem(senderItem : URLSenderItem, item : (String, String, String, CGFloat, CFString, NSNumber)) {
  235. let testImageUrl = testBundle.url(forResource: item.0, withExtension: item.1)
  236. let testImageData = (try? Data(contentsOf: testImageUrl!))!
  237. let image = UIImage(data: testImageData)!
  238. var height = item.3
  239. if height == 0.0 {
  240. height = image.size.height
  241. }
  242. var width = item.3
  243. if width == 0.0 {
  244. width = image.size.width
  245. }
  246. XCTAssert(senderItem.caption == nil)
  247. let goldUTI = UTIConverter.mimeType(fromUTI: item.4 as String)
  248. let actualUTI = senderItem.getMimeType()
  249. XCTAssert(actualUTI == goldUTI, "UTI must be \(String(describing: goldUTI)) but is \(String(describing: actualUTI))")
  250. XCTAssert(senderItem.getHeight() <= height, "\(item.0): \(senderItem.getHeight()) should be less than or equal to \(height)")
  251. XCTAssert(senderItem.getWidth() <= width, "\(item.0): \(senderItem.getWidth()) should be less than or equal to \(width)")
  252. XCTAssertNotNil(senderItem.getThumbnail(), "Thumbnail must not be nil")
  253. XCTAssert(senderItem.renderType! == item.5)
  254. }
  255. func testAllowedUTITypes() {
  256. let matrix : [(CFString, Bool)] = [(kUTTypePNG, true),
  257. (kUTTypeGIF, true),
  258. (kUTTypeJPEG, true),
  259. (kUTTypeData, false),
  260. (kUTTypeBMP, false),
  261. (kUTTypeMP3, false)]
  262. for item in matrix {
  263. XCTAssert(ImageURLSenderItemCreator.isAllowedUTI(uti: item.0 as String) == item.1)
  264. }
  265. }
  266. func testHasAlpha() {
  267. let testHasAlphaMatrix : [(String, String, String, Bool, Bool)] = [
  268. (ImageURLSenderItemCreatorTest.pngRegular, "png", "original", false, false),
  269. (ImageURLSenderItemCreatorTest.pngSticker, "png", "original", true, true),
  270. (ImageURLSenderItemCreatorTest.pngScreenshot, "png", "original", false, false),
  271. (ImageURLSenderItemCreatorTest.pngAlpha, "png", "original", true, false),
  272. ]
  273. for item in testHasAlphaMatrix {
  274. let testImageUrl = testBundle.url(forResource: item.0, withExtension: item.1)
  275. let testImageData = (try? Data(contentsOf: testImageUrl!))!
  276. let image = UIImage(data: testImageData)!
  277. let hasAlpha = ImageURLSenderItemCreator.hasAlpha(image: image.cgImage!)
  278. let hasTransparentPixel = ImageURLSenderItemCreator.hasTransparentPixel(cgImage: image.cgImage!)
  279. XCTAssert(hasAlpha == item.3, "\(item.0) should be \(item.3) but is \(hasAlpha)")
  280. XCTAssert(hasTransparentPixel == item.4, "\(item.0) Should be \(item.4) but is \(hasTransparentPixel)")
  281. }
  282. }
  283. func testGarbageData() {
  284. let data = Data(count: 128)
  285. let creator = ImageURLSenderItemCreator()
  286. let item = creator.senderItem(from: data, uti: kUTTypeGIF as String)
  287. XCTAssert(item == nil)
  288. }
  289. func testGarbageURL() {
  290. let urlMatrix : [String] = [
  291. "https://threema.ch/en",
  292. "file://threema.ch/en",
  293. ]
  294. for urlString in urlMatrix {
  295. guard let url = URL(string: urlString) else {
  296. XCTFail("Invalid test parameter \(urlString)")
  297. return
  298. }
  299. let creator = ImageURLSenderItemCreator()
  300. let item = creator.senderItem(from: url)
  301. XCTAssert(item == nil)
  302. }
  303. }
  304. }