Prechádzať zdrojové kódy

Tests: VLCMediaThumbnailer

Mike JS. Choi 7 rokov pred
rodič
commit
21114d62d8

+ 6 - 0
MobileVLCKit.xcodeproj/project.pbxproj

@@ -205,6 +205,8 @@
 		CA1E137521087D8E0066F32F /* chinese-traditional.srt in Resources */ = {isa = PBXBuildFile; fileRef = CA1E135E21087D8E0066F32F /* chinese-traditional.srt */; };
 		CA1E137621087D8E0066F32F /* english.srt in Resources */ = {isa = PBXBuildFile; fileRef = CA1E135F21087D8E0066F32F /* english.srt */; };
 		CA1E137721087D8E0066F32F /* english.srt in Resources */ = {isa = PBXBuildFile; fileRef = CA1E135F21087D8E0066F32F /* english.srt */; };
+		CA5D5788210B16960099D6D0 /* VLCMediaThumbnailerTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = CA5D5787210B16960099D6D0 /* VLCMediaThumbnailerTest.swift */; };
+		CA5D5789210B16960099D6D0 /* VLCMediaThumbnailerTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = CA5D5787210B16960099D6D0 /* VLCMediaThumbnailerTest.swift */; };
 		CA7BC89120F4419E00C86820 /* XCTestHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = CA7BC88720F4418000C86820 /* XCTestHelper.swift */; };
 		CA7BC89520F441B500C86820 /* XCTestHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = CA7BC88720F4418000C86820 /* XCTestHelper.swift */; };
 		CAA03D0C20C8DCDB001764DA /* libTVVLCKit.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 7D1329591BA304D900BE647E /* libTVVLCKit.a */; };
@@ -467,6 +469,7 @@
 		CA1E135D21087D8E0066F32F /* french.srt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = french.srt; sourceTree = "<group>"; };
 		CA1E135E21087D8E0066F32F /* chinese-traditional.srt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = "chinese-traditional.srt"; sourceTree = "<group>"; };
 		CA1E135F21087D8E0066F32F /* english.srt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = english.srt; sourceTree = "<group>"; };
+		CA5D5787210B16960099D6D0 /* VLCMediaThumbnailerTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = VLCMediaThumbnailerTest.swift; sourceTree = "<group>"; };
 		CA7BC88720F4418000C86820 /* XCTestHelper.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = XCTestHelper.swift; sourceTree = "<group>"; };
 		CA978D1E20B89F4B00F7E768 /* MobileVLCKitTests-Bridging-Header.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "MobileVLCKitTests-Bridging-Header.h"; sourceTree = "<group>"; };
 		CAA03D0720C8DCDA001764DA /* TVVLCKitTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = TVVLCKitTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -910,6 +913,7 @@
 		CAA9F00020D254A600CDBB2C /* Sources */ = {
 			isa = PBXGroup;
 			children = (
+				CA5D5787210B16960099D6D0 /* VLCMediaThumbnailerTest.swift */,
 				CABCBADD20EB0B080040E2F5 /* VLCAudioTest.swift */,
 				CABEDAB721095242005FED09 /* VLCLibraryTest.swift */,
 				CAA9F00120D254A600CDBB2C /* VLCTimeTest.swift */,
@@ -1331,6 +1335,7 @@
 			files = (
 				CABF4D4120D8DBA900FCCE29 /* VLCMediaTest.swift in Sources */,
 				CA7BC89120F4419E00C86820 /* XCTestHelper.swift in Sources */,
+				CA5D5788210B16960099D6D0 /* VLCMediaThumbnailerTest.swift in Sources */,
 				CAA9F00320D254AB00CDBB2C /* VLCTimeTest.swift in Sources */,
 				CABCBAE220EB0B080040E2F5 /* VLCAudioTest.swift in Sources */,
 				CABEDAB821095242005FED09 /* VLCLibraryTest.swift in Sources */,
@@ -1344,6 +1349,7 @@
 			files = (
 				CABF4D4220D8DBA900FCCE29 /* VLCMediaTest.swift in Sources */,
 				CA7BC89520F441B500C86820 /* XCTestHelper.swift in Sources */,
+				CA5D5789210B16960099D6D0 /* VLCMediaThumbnailerTest.swift in Sources */,
 				CAA9F00420D254AC00CDBB2C /* VLCTimeTest.swift in Sources */,
 				CABCBAE320EB0B080040E2F5 /* VLCAudioTest.swift in Sources */,
 				CABEDAB921095242005FED09 /* VLCLibraryTest.swift in Sources */,

+ 193 - 0
Tests/Sources/VLCMediaThumbnailerTest.swift

@@ -0,0 +1,193 @@
+/*****************************************************************************
+ * VLCMediaThumbnailerTest.swift
+ *****************************************************************************
+ * Copyright (C) 2018 VLC authors and VideoLAN
+ * $Id$
+ *
+ * Authors: Mike JS. Choi <mkchoi212 # icloud.com>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+import XCTest
+
+class MockThumbnailerDelegate: NSObject, VLCMediaThumbnailerDelegate {
+    
+    var timedOutExpectation: XCTestExpectation?
+    var finishedExpectation: XCTestExpectation?
+    
+    func mediaThumbnailerDidTimeOut(_ mediaThumbnailer: VLCMediaThumbnailer!) {
+        timedOutExpectation?.fulfill()
+    }
+    
+    func mediaThumbnailer(_ mediaThumbnailer: VLCMediaThumbnailer!, didFinishThumbnail thumbnail: CGImage!) {
+        finishedExpectation?.fulfill()
+    }
+    
+}
+
+class VLCMediaThumbnailerTest: XCTestCase {
+    
+    // MARK: Initializers
+    
+    func testInitWithMediaAndDelegate() throws {
+        let tests = Video.standards.map{ $0.media }
+        
+        for media in tests {
+            let delegate = MockThumbnailerDelegate()
+            let thumbnailer = try XCTAssertNotNilAndUnwrap(VLCMediaThumbnailer(media: media, andDelegate: delegate))
+            
+            XCTAssertNil(thumbnailer.thumbnail)
+            XCTAssertNotNil(thumbnailer.delegate)
+            XCTAssertEqual(thumbnailer.media, media)
+            XCTAssertEqual(thumbnailer.libVLCinstance, VLCLibrary.shared().instance)
+        }
+    }
+    
+    func testInitWithMediaDelegateAndLibrary() throws {
+        let sharedLibrary = VLCLibrary.shared()
+        let customLibrary = VLCLibrary(options: [])
+        
+        let tests: [(library: VLCLibrary?, expected: VLCLibrary?)] = [
+            (sharedLibrary, sharedLibrary),
+            (customLibrary, customLibrary),
+            (nil, sharedLibrary)
+        ]
+        
+        for (library, expected) in tests {
+            let delegate = MockThumbnailerDelegate()
+            let media = Video.test1.media
+            let thumbnailer = try XCTAssertNotNilAndUnwrap(VLCMediaThumbnailer(media: media, delegate: delegate, andVLCLibrary: library))
+            
+            XCTAssertNil(thumbnailer.thumbnail)
+            XCTAssertNotNil(thumbnailer.delegate)
+            XCTAssertEqual(thumbnailer.media, media)
+            XCTAssertEqual(thumbnailer.libVLCinstance, expected?.instance)
+        }
+    }
+    
+    func testFetchThumbnail() throws {
+        
+        let skipMediaParse: (XCTestExpectation) -> (VLCMedia) = { expectation in
+            expectation.fulfill()
+            return Video.test1.media
+        }
+        
+        let completeMediaParse: (XCTestExpectation) -> (VLCMedia) = { expectation in
+            let media = Video.test1.media
+            media.lengthWait(until: Date.distantFuture)
+            expectation.fulfill()
+            return media
+        }
+        
+        let tests: [(parseFunc: ((XCTestExpectation) -> (VLCMedia)), expectation: XCTestExpectation)] = [
+            (skipMediaParse, expectation(description: "Skipped parsing media")),
+            (completeMediaParse, expectation(description: "Completed parsing media"))
+        ]
+        
+        for (parseFunc, parseExpectation) in tests {
+            let media = parseFunc(parseExpectation)
+            wait(for: [parseExpectation], timeout: STANDARD_TIME_OUT)
+            
+            let delegate = MockThumbnailerDelegate()
+            let fetched = expectation(description: "delegate::didFinishThumbnail called")
+            delegate.finishedExpectation = fetched
+            
+            let thumbnailer = try XCTAssertNotNilAndUnwrap(VLCMediaThumbnailer(media: media, andDelegate: delegate))
+            thumbnailer.fetchThumbnail()
+            
+            wait(for: [fetched], timeout: STANDARD_TIME_OUT)
+            
+            XCTAssertNotNil(thumbnailer.thumbnail)
+            XCTAssertEqual(thumbnailer.thumbnailWidth, 417)
+            XCTAssertEqual(thumbnailer.thumbnailHeight, 240)
+            XCTAssertEqual(thumbnailer.snapshotPosition, 0.3)
+        }
+    }
+    
+    // MARK: Delegate callbacks
+    
+    func testDelegateDidFinishThumbnail() throws {
+        
+        let tests: [(video: Video, width: CGFloat, height: CGFloat)] = [
+            (Video.test1, 417, 240),
+            (Video.test2, 427, 240),
+            (Video.test3, 427, 240),
+            (Video.test4, 427, 240)
+        ]
+        
+        for (video, width, height) in tests {
+            let delegate = MockThumbnailerDelegate()
+            let fetched = expectation(description: "delegate::didFinishThumbnail called")
+            delegate.finishedExpectation = fetched
+
+            let thumbnailer = try XCTAssertNotNilAndUnwrap(VLCMediaThumbnailer(media: video.media, andDelegate: delegate))
+            thumbnailer.fetchThumbnail()
+
+            wait(for: [fetched], timeout: STANDARD_TIME_OUT)
+
+            XCTAssertNotNil(thumbnailer.thumbnail)
+            XCTAssertEqual(thumbnailer.thumbnailWidth, width)
+            XCTAssertEqual(thumbnailer.thumbnailHeight, height)
+            XCTAssertEqual(thumbnailer.snapshotPosition, 0.3)
+        }
+    }
+    
+    func testDelegateMediaThumbnailerDidTimeOut() throws {
+        let media = Video.invalid
+        let delegate = MockThumbnailerDelegate()
+        let timedOut = expectation(description: "delegate::mediaThumbnailerDidTimeOut called")
+        delegate.timedOutExpectation = timedOut
+        
+        let thumbnailer = try XCTAssertNotNilAndUnwrap(VLCMediaThumbnailer(media: media.media, andDelegate: delegate))
+        thumbnailer.fetchThumbnail()
+        
+        let internalWaitDuration = 10.0
+        wait(for: [timedOut], timeout: internalWaitDuration + STANDARD_TIME_OUT)
+        
+        XCTAssertNil(thumbnailer.thumbnail)
+        XCTAssertEqual(thumbnailer.thumbnailWidth, 0)
+        XCTAssertEqual(thumbnailer.thumbnailHeight, 0)
+    }
+    
+    func testCustomThumbnailOptions() throws {
+        
+        let tests: [(video: Video, height: CGFloat, width: CGFloat, position: Float)] = [
+            (Video.test1, 320, 184, 0.5),
+            (Video.test2, 327, 184, 0.6),
+            (Video.test3, 500, 281, 0.7),
+            (Video.test4, 711, 400, 0.8)
+        ]
+        
+        for (video, width, height, position) in tests {
+            let delegate = MockThumbnailerDelegate()
+            let fetched = expectation(description: "delegate::didFinishThumbnail called")
+            delegate.finishedExpectation = fetched
+            
+            let thumbnailer = try XCTAssertNotNilAndUnwrap(VLCMediaThumbnailer(media: video.media, andDelegate: delegate))
+            thumbnailer.thumbnailWidth = width
+            thumbnailer.thumbnailHeight = height
+            thumbnailer.snapshotPosition = position
+            
+            thumbnailer.fetchThumbnail()
+            wait(for: [fetched], timeout: STANDARD_TIME_OUT)
+            
+            XCTAssertNotNil(thumbnailer.thumbnail)
+            XCTAssertEqual(thumbnailer.thumbnailWidth, width)
+            XCTAssertEqual(thumbnailer.thumbnailHeight, height)
+            XCTAssertEqual(thumbnailer.snapshotPosition, position)
+        }
+    }
+}

+ 4 - 0
VLCKit.xcodeproj/project.pbxproj

@@ -100,6 +100,7 @@
 		CA1E134B21087CEB0066F32F /* english.srt in Resources */ = {isa = PBXBuildFile; fileRef = CA1E133F21087CEB0066F32F /* english.srt */; };
 		CA23EBC120BDF02700C0D635 /* VLCKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 7DC28D7F20194117002D05C4 /* VLCKit.framework */; };
 		CA23EBCC20BDF07700C0D635 /* VLCTimeTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = CA23EBCB20BDF07700C0D635 /* VLCTimeTest.swift */; };
+		CA6F70B3210AD41400FADCAB /* VLCMediaThumbnailerTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = CA6F70B2210AD41400FADCAB /* VLCMediaThumbnailerTest.swift */; };
 		CA766ED620E9E53E009B3F3F /* VLCAudioTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = CA766ED520E9E53E009B3F3F /* VLCAudioTest.swift */; };
 		CA7BC8B720F4424100C86820 /* XCTestHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = CA7BC8B020F4424100C86820 /* XCTestHelper.swift */; };
 		CABEDABB2109525B005FED09 /* VLCLibraryTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = CABEDABA2109525B005FED09 /* VLCLibraryTest.swift */; };
@@ -220,6 +221,7 @@
 		CA23EBC020BDF02700C0D635 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
 		CA23EBCA20BDF07700C0D635 /* VLCKitTests-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "VLCKitTests-Bridging-Header.h"; sourceTree = "<group>"; };
 		CA23EBCB20BDF07700C0D635 /* VLCTimeTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = VLCTimeTest.swift; sourceTree = "<group>"; };
+		CA6F70B2210AD41400FADCAB /* VLCMediaThumbnailerTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = VLCMediaThumbnailerTest.swift; sourceTree = "<group>"; };
 		CA766ED520E9E53E009B3F3F /* VLCAudioTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VLCAudioTest.swift; sourceTree = "<group>"; };
 		CA7BC8B020F4424100C86820 /* XCTestHelper.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = XCTestHelper.swift; sourceTree = "<group>"; };
 		CABEDABA2109525B005FED09 /* VLCLibraryTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = VLCLibraryTest.swift; sourceTree = "<group>"; };
@@ -531,6 +533,7 @@
 			isa = PBXGroup;
 			children = (
 				CABEDABA2109525B005FED09 /* VLCLibraryTest.swift */,
+				CA6F70B2210AD41400FADCAB /* VLCMediaThumbnailerTest.swift */,
 				CABF4D5320D8E31700FCCE29 /* VLCMediaTest.swift */,
 				CA766ED520E9E53E009B3F3F /* VLCAudioTest.swift */,
 				CA23EBCB20BDF07700C0D635 /* VLCTimeTest.swift */,
@@ -835,6 +838,7 @@
 			files = (
 				CABF4D5420D8E31700FCCE29 /* VLCMediaTest.swift in Sources */,
 				CA23EBCC20BDF07700C0D635 /* VLCTimeTest.swift in Sources */,
+				CA6F70B3210AD41400FADCAB /* VLCMediaThumbnailerTest.swift in Sources */,
 				CA766ED620E9E53E009B3F3F /* VLCAudioTest.swift in Sources */,
 				CABEDABB2109525B005FED09 /* VLCLibraryTest.swift in Sources */,
 				CA7BC8B720F4424100C86820 /* XCTestHelper.swift in Sources */,