VLCMediaThumbnailerTest.swift 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*****************************************************************************
  2. * VLCMediaThumbnailerTest.swift
  3. *****************************************************************************
  4. * Copyright (C) 2018 VLC authors and VideoLAN
  5. * $Id$
  6. *
  7. * Authors: Mike JS. Choi <mkchoi212 # icloud.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU Lesser General Public License as published by
  11. * the Free Software Foundation; either version 2.1 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public License
  20. * along with this program; if not, write to the Free Software Foundation,
  21. * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  22. *****************************************************************************/
  23. import XCTest
  24. class MockThumbnailerDelegate: NSObject, VLCMediaThumbnailerDelegate {
  25. var timedOutExpectation: XCTestExpectation?
  26. var finishedExpectation: XCTestExpectation?
  27. func mediaThumbnailerDidTimeOut(_ mediaThumbnailer: VLCMediaThumbnailer!) {
  28. timedOutExpectation?.fulfill()
  29. }
  30. func mediaThumbnailer(_ mediaThumbnailer: VLCMediaThumbnailer!, didFinishThumbnail thumbnail: CGImage!) {
  31. finishedExpectation?.fulfill()
  32. }
  33. }
  34. class VLCMediaThumbnailerTest: XCTestCase {
  35. // MARK: Initializers
  36. func testInitWithMediaAndDelegate() throws {
  37. let tests = Video.standards.map{ $0.media }
  38. for media in tests {
  39. let delegate = MockThumbnailerDelegate()
  40. let thumbnailer = try XCTAssertNotNilAndUnwrap(VLCMediaThumbnailer(media: media, andDelegate: delegate))
  41. XCTAssertNil(thumbnailer.thumbnail)
  42. XCTAssertNotNil(thumbnailer.delegate)
  43. XCTAssertEqual(thumbnailer.media, media)
  44. XCTAssertEqual(thumbnailer.libVLCinstance, VLCLibrary.shared().instance)
  45. }
  46. }
  47. func testInitWithMediaDelegateAndLibrary() throws {
  48. let sharedLibrary = VLCLibrary.shared()
  49. let customLibrary = VLCLibrary(options: [])
  50. let tests: [(library: VLCLibrary?, expected: VLCLibrary?)] = [
  51. (sharedLibrary, sharedLibrary),
  52. (customLibrary, customLibrary),
  53. (nil, sharedLibrary)
  54. ]
  55. for (library, expected) in tests {
  56. let delegate = MockThumbnailerDelegate()
  57. let media = Video.test1.media
  58. let thumbnailer = try XCTAssertNotNilAndUnwrap(VLCMediaThumbnailer(media: media, delegate: delegate, andVLCLibrary: library))
  59. XCTAssertNil(thumbnailer.thumbnail)
  60. XCTAssertNotNil(thumbnailer.delegate)
  61. XCTAssertEqual(thumbnailer.media, media)
  62. XCTAssertEqual(thumbnailer.libVLCinstance, expected?.instance)
  63. }
  64. }
  65. func testFetchThumbnail() throws {
  66. let skipMediaParse: (XCTestExpectation) -> (VLCMedia) = { expectation in
  67. expectation.fulfill()
  68. return Video.test1.media
  69. }
  70. let completeMediaParse: (XCTestExpectation) -> (VLCMedia) = { expectation in
  71. let media = Video.test1.media
  72. media.lengthWait(until: Date.distantFuture)
  73. expectation.fulfill()
  74. return media
  75. }
  76. let tests: [(parseFunc: ((XCTestExpectation) -> (VLCMedia)), expectation: XCTestExpectation)] = [
  77. (skipMediaParse, expectation(description: "Skipped parsing media")),
  78. (completeMediaParse, expectation(description: "Completed parsing media"))
  79. ]
  80. for (parseFunc, parseExpectation) in tests {
  81. let media = parseFunc(parseExpectation)
  82. wait(for: [parseExpectation], timeout: STANDARD_TIME_OUT)
  83. let delegate = MockThumbnailerDelegate()
  84. let fetched = expectation(description: "delegate::didFinishThumbnail called")
  85. delegate.finishedExpectation = fetched
  86. let thumbnailer = try XCTAssertNotNilAndUnwrap(VLCMediaThumbnailer(media: media, andDelegate: delegate))
  87. thumbnailer.fetchThumbnail()
  88. wait(for: [fetched], timeout: STANDARD_TIME_OUT)
  89. XCTAssertNotNil(thumbnailer.thumbnail)
  90. XCTAssertEqual(thumbnailer.thumbnailWidth, 417)
  91. XCTAssertEqual(thumbnailer.thumbnailHeight, 240)
  92. XCTAssertEqual(thumbnailer.snapshotPosition, 0.3)
  93. }
  94. }
  95. // MARK: Delegate callbacks
  96. func testDelegateDidFinishThumbnail() throws {
  97. let tests: [(video: Video, width: CGFloat, height: CGFloat)] = [
  98. (Video.test1, 417, 240),
  99. (Video.test2, 427, 240),
  100. (Video.test3, 427, 240),
  101. (Video.test4, 427, 240)
  102. ]
  103. for (video, width, height) in tests {
  104. let delegate = MockThumbnailerDelegate()
  105. let fetched = expectation(description: "delegate::didFinishThumbnail called")
  106. delegate.finishedExpectation = fetched
  107. let thumbnailer = try XCTAssertNotNilAndUnwrap(VLCMediaThumbnailer(media: video.media, andDelegate: delegate))
  108. thumbnailer.fetchThumbnail()
  109. wait(for: [fetched], timeout: STANDARD_TIME_OUT)
  110. XCTAssertNotNil(thumbnailer.thumbnail)
  111. XCTAssertEqual(thumbnailer.thumbnailWidth, width)
  112. XCTAssertEqual(thumbnailer.thumbnailHeight, height)
  113. XCTAssertEqual(thumbnailer.snapshotPosition, 0.3)
  114. }
  115. }
  116. func testDelegateMediaThumbnailerDidTimeOut() throws {
  117. let media = Video.invalid
  118. let delegate = MockThumbnailerDelegate()
  119. let timedOut = expectation(description: "delegate::mediaThumbnailerDidTimeOut called")
  120. delegate.timedOutExpectation = timedOut
  121. let thumbnailer = try XCTAssertNotNilAndUnwrap(VLCMediaThumbnailer(media: media.media, andDelegate: delegate))
  122. thumbnailer.fetchThumbnail()
  123. let internalWaitDuration = 10.0
  124. wait(for: [timedOut], timeout: internalWaitDuration + STANDARD_TIME_OUT)
  125. XCTAssertNil(thumbnailer.thumbnail)
  126. XCTAssertEqual(thumbnailer.thumbnailWidth, 0)
  127. XCTAssertEqual(thumbnailer.thumbnailHeight, 0)
  128. }
  129. func testCustomThumbnailOptions() throws {
  130. let tests: [(video: Video, height: CGFloat, width: CGFloat, position: Float)] = [
  131. (Video.test1, 320, 184, 0.5),
  132. (Video.test2, 327, 184, 0.6),
  133. (Video.test3, 500, 281, 0.7),
  134. (Video.test4, 711, 400, 0.8)
  135. ]
  136. for (video, width, height, position) in tests {
  137. let delegate = MockThumbnailerDelegate()
  138. let fetched = expectation(description: "delegate::didFinishThumbnail called")
  139. delegate.finishedExpectation = fetched
  140. let thumbnailer = try XCTAssertNotNilAndUnwrap(VLCMediaThumbnailer(media: video.media, andDelegate: delegate))
  141. thumbnailer.thumbnailWidth = width
  142. thumbnailer.thumbnailHeight = height
  143. thumbnailer.snapshotPosition = position
  144. thumbnailer.fetchThumbnail()
  145. wait(for: [fetched], timeout: STANDARD_TIME_OUT)
  146. XCTAssertNotNil(thumbnailer.thumbnail)
  147. XCTAssertEqual(thumbnailer.thumbnailWidth, width)
  148. XCTAssertEqual(thumbnailer.thumbnailHeight, height)
  149. XCTAssertEqual(thumbnailer.snapshotPosition, position)
  150. }
  151. }
  152. }