mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-02 12:07:55 +00:00
Added MediaProvider tests (#386)
* Media provider tests * Moved classes to separate files * Moved image file * Removed unused image file * Rebase and refactor. Replace removed error type. Refactor XYZMock to MockXYZ. Co-authored-by: Doug <douglase@element.io>
This commit is contained in:
committed by
GitHub
parent
e4d087a0a4
commit
e573bc0dd2
@@ -0,0 +1,246 @@
|
||||
//
|
||||
// Copyright 2022 New Vector Ltd
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
@testable import ElementX
|
||||
import Kingfisher
|
||||
import XCTest
|
||||
|
||||
@MainActor
|
||||
final class MediaProviderTests: XCTestCase {
|
||||
private let mediaProxy = MockMediaProxy()
|
||||
private let fileCache = MockFileCache()
|
||||
private var imageCache: MockImageCache!
|
||||
private var backgroundTaskService = MockBackgroundTaskService()
|
||||
|
||||
var mediaProvider: MediaProvider!
|
||||
|
||||
override func setUp() {
|
||||
imageCache = MockImageCache(name: "Test")
|
||||
mediaProvider = MediaProvider(mediaProxy: mediaProxy,
|
||||
imageCache: imageCache,
|
||||
fileCache: fileCache,
|
||||
backgroundTaskService: backgroundTaskService)
|
||||
}
|
||||
|
||||
func test_whenImageFromSourceWithSourceNil_nilReturned() throws {
|
||||
let image = mediaProvider.imageFromSource(nil, avatarSize: .room(on: .timeline))
|
||||
XCTAssertNil(image)
|
||||
}
|
||||
|
||||
func test_whenImageFromSourceWithSourceNotNilAndImageCacheContainsImage_ImageIsReturned() throws {
|
||||
let avatarSize = AvatarSize.room(on: .timeline)
|
||||
let urlString = "test"
|
||||
let key = "\(urlString){\(avatarSize.scaledValue),\(avatarSize.scaledValue)}"
|
||||
let imageForKey = UIImage()
|
||||
imageCache.retrievedImagesInMemory[key] = imageForKey
|
||||
let image = mediaProvider.imageFromSource(MediaSourceProxy(urlString: urlString), avatarSize: avatarSize)
|
||||
XCTAssertEqual(image, imageForKey)
|
||||
}
|
||||
|
||||
func test_whenImageFromSourceWithSourceNotNilAndImageNotCached_nilReturned() throws {
|
||||
let image = mediaProvider.imageFromSource(MediaSourceProxy(urlString: "test"), avatarSize: .room(on: .timeline))
|
||||
XCTAssertNil(image)
|
||||
}
|
||||
|
||||
func test_whenImageFromURLStringWithURLStringNil_nilReturned() throws {
|
||||
let image = mediaProvider.imageFromURLString(nil, avatarSize: .room(on: .timeline))
|
||||
XCTAssertNil(image)
|
||||
}
|
||||
|
||||
func test_whenImageFromURLStringWithURLStringNotNilAndImageCacheContainsImage_imageIsReturned() throws {
|
||||
let avatarSize = AvatarSize.room(on: .timeline)
|
||||
let urlString = "test"
|
||||
let key = "\(urlString){\(avatarSize.scaledValue),\(avatarSize.scaledValue)}"
|
||||
let imageForKey = UIImage()
|
||||
imageCache.retrievedImagesInMemory[key] = imageForKey
|
||||
let image = mediaProvider.imageFromURLString("test", avatarSize: avatarSize)
|
||||
XCTAssertEqual(image, imageForKey)
|
||||
}
|
||||
|
||||
func test_whenImageFromURLStringWithURLStringNotNilAndImageNotCached_nilReturned() throws {
|
||||
let image = mediaProvider.imageFromURLString("test", avatarSize: .room(on: .timeline))
|
||||
XCTAssertNil(image)
|
||||
}
|
||||
|
||||
func test_whenLoadImageFromSourceAndImageCacheContainsImage_successIsReturned() async throws {
|
||||
let avatarSize = AvatarSize.room(on: .timeline)
|
||||
let urlString = "test"
|
||||
let key = "\(urlString){\(avatarSize.scaledValue),\(avatarSize.scaledValue)}"
|
||||
let imageForKey = UIImage()
|
||||
imageCache.retrievedImagesInMemory[key] = imageForKey
|
||||
let result = await mediaProvider.loadImageFromSource(MediaSourceProxy(urlString: urlString), avatarSize: avatarSize)
|
||||
XCTAssertEqual(Result.success(imageForKey), result)
|
||||
}
|
||||
|
||||
func test_whenLoadImageFromSourceAndImageNotCachedAndRetrieveImageSucceeds_successIsReturned() async throws {
|
||||
let avatarSize = AvatarSize.room(on: .timeline)
|
||||
let urlString = "test"
|
||||
let key = "\(urlString){\(avatarSize.scaledValue),\(avatarSize.scaledValue)}"
|
||||
let imageForKey = UIImage()
|
||||
imageCache.retrievedImages[key] = imageForKey
|
||||
let result = await mediaProvider.loadImageFromSource(MediaSourceProxy(urlString: urlString), avatarSize: avatarSize)
|
||||
XCTAssertEqual(Result.success(imageForKey), result)
|
||||
}
|
||||
|
||||
func test_whenLoadImageFromSourceAndImageNotCachedAndRetrieveImageFails_imageThumbnailIsLoaded() async throws {
|
||||
let avatarSize = AvatarSize.room(on: .timeline)
|
||||
let expectedImage = try loadTestImage()
|
||||
mediaProxy.mediaThumbnailData = expectedImage.pngData()
|
||||
let result = await mediaProvider.loadImageFromSource(MediaSourceProxy(urlString: "test"), avatarSize: avatarSize)
|
||||
switch result {
|
||||
case .success(let image):
|
||||
XCTAssertEqual(image.pngData(), expectedImage.pngData())
|
||||
case .failure:
|
||||
XCTFail("Should be success")
|
||||
}
|
||||
}
|
||||
|
||||
func test_whenLoadImageFromSourceAndImageNotCachedAndRetrieveImageFails_imageIsStored() async throws {
|
||||
let avatarSize = AvatarSize.room(on: .timeline)
|
||||
let urlString = "test"
|
||||
let key = "\(urlString){\(avatarSize.scaledValue),\(avatarSize.scaledValue)}"
|
||||
let expectedImage = try loadTestImage()
|
||||
mediaProxy.mediaThumbnailData = expectedImage.pngData()
|
||||
_ = await mediaProvider.loadImageFromSource(MediaSourceProxy(urlString: urlString), avatarSize: avatarSize)
|
||||
let storedImage = try XCTUnwrap(imageCache.storedImages[key])
|
||||
XCTAssertEqual(expectedImage.pngData(), storedImage.pngData())
|
||||
}
|
||||
|
||||
func test_whenLoadImageFromSourceAndImageNotCachedAndRetrieveImageFailsAndNoAvatarSize_imageContentIsLoaded() async throws {
|
||||
let expectedImage = try loadTestImage()
|
||||
mediaProxy.mediaContentData = expectedImage.pngData()
|
||||
let result = await mediaProvider.loadImageFromSource(MediaSourceProxy(urlString: "test"), avatarSize: nil)
|
||||
switch result {
|
||||
case .success(let image):
|
||||
XCTAssertEqual(image.pngData(), expectedImage.pngData())
|
||||
case .failure:
|
||||
XCTFail("Should be success")
|
||||
}
|
||||
}
|
||||
|
||||
func test_whenLoadImageFromSourceAndImageNotCachedAndRetrieveImageFailsAndLoadImageThumbnailFails_errorIsThrown() async throws {
|
||||
let result = await mediaProvider.loadImageFromSource(MediaSourceProxy(urlString: "test"), avatarSize: AvatarSize.room(on: .timeline))
|
||||
switch result {
|
||||
case .success:
|
||||
XCTFail("Should fail")
|
||||
case .failure(let error):
|
||||
XCTAssertEqual(error, MediaProviderError.failedRetrievingImage)
|
||||
}
|
||||
}
|
||||
|
||||
func test_whenLoadImageFromSourceAndImageNotCachedAndRetrieveImageFailsAndNoAvatarSizeAndLoadImageContentFails_errorIsThrown() async throws {
|
||||
let result = await mediaProvider.loadImageFromSource(MediaSourceProxy(urlString: "test"), avatarSize: nil)
|
||||
switch result {
|
||||
case .success:
|
||||
XCTFail("Should fail")
|
||||
case .failure(let error):
|
||||
XCTAssertEqual(error, MediaProviderError.failedRetrievingImage)
|
||||
}
|
||||
}
|
||||
|
||||
func test_whenLoadImageFromSourceAndImageNotCachedAndRetrieveImageFailsAndImageThumbnailIsLoadedWithCorruptedData_errorIsThrown() async throws {
|
||||
mediaProxy.mediaThumbnailData = Data()
|
||||
let result = await mediaProvider.loadImageFromSource(MediaSourceProxy(urlString: "test"), avatarSize: AvatarSize.room(on: .timeline))
|
||||
switch result {
|
||||
case .success:
|
||||
XCTFail("Should fail")
|
||||
case .failure(let error):
|
||||
XCTAssertEqual(error, MediaProviderError.invalidImageData)
|
||||
}
|
||||
}
|
||||
|
||||
func test_whenFileFromSourceWithSourceNil_nilIsReturned() throws {
|
||||
let url = mediaProvider.fileFromSource(nil, fileExtension: "png")
|
||||
XCTAssertNil(url)
|
||||
}
|
||||
|
||||
func test_whenFileFromSourceWithSource_correctValuesAreReturned() throws {
|
||||
let expectedURL = try XCTUnwrap(URL(string: "some_url"))
|
||||
fileCache.fileURLToReturn = expectedURL
|
||||
let url = mediaProvider.fileFromSource(MediaSourceProxy(urlString: "test/test1"), fileExtension: "png")
|
||||
XCTAssertEqual(fileCache.fileKey, "test1")
|
||||
XCTAssertEqual(fileCache.fileExtension, "png")
|
||||
XCTAssertEqual(url?.absoluteString, expectedURL.absoluteString)
|
||||
}
|
||||
|
||||
func test_whenLoadFileFromSourceAndFileFromSourceExists_urlIsReturned() async throws {
|
||||
let expectedURL = try XCTUnwrap(URL(string: "some_url"))
|
||||
let expectedResult: Result<URL, MediaProviderError> = .success(expectedURL)
|
||||
fileCache.fileURLToReturn = expectedURL
|
||||
let result = await mediaProvider.loadFileFromSource(MediaSourceProxy(urlString: "test/test1"), fileExtension: "png")
|
||||
XCTAssertEqual(result, expectedResult)
|
||||
}
|
||||
|
||||
func test_whenLoadFileFromSourceAndNoFileFromSourceExists_mediaLoadedFromSource() async throws {
|
||||
let expectedURL = try XCTUnwrap(URL(string: "some_url"))
|
||||
let expectedResult: Result<URL, MediaProviderError> = .success(expectedURL)
|
||||
mediaProxy.mediaContentData = try loadTestImage().pngData()
|
||||
fileCache.storeURLToReturn = expectedURL
|
||||
let result = await mediaProvider.loadFileFromSource(MediaSourceProxy(urlString: "test/test1"), fileExtension: "png")
|
||||
XCTAssertEqual(result, expectedResult)
|
||||
XCTAssertEqual(mediaProxy.mediaContentData, fileCache.storedData)
|
||||
XCTAssertEqual("test1", fileCache.storedFileKey)
|
||||
XCTAssertEqual("png", fileCache.storedFileExtension)
|
||||
}
|
||||
|
||||
func test_whenLoadFileFromSourceAndNoFileFromSourceExistsAndLoadContentSourceFails_failureIsReturned() async throws {
|
||||
let expectedResult: Result<URL, MediaProviderError> = .failure(.failedRetrievingImage)
|
||||
mediaProxy.mediaContentData = nil
|
||||
let result = await mediaProvider.loadFileFromSource(MediaSourceProxy(urlString: "test/test1"), fileExtension: "png")
|
||||
XCTAssertEqual(result, expectedResult)
|
||||
}
|
||||
|
||||
func test_whenLoadFileFromSourceAndNoFileFromSourceExistsAndStoreDataFails_failureIsReturned() async throws {
|
||||
let expectedResult: Result<URL, MediaProviderError> = .failure(.failedRetrievingImage)
|
||||
mediaProxy.mediaContentData = try loadTestImage().pngData()
|
||||
let result = await mediaProvider.loadFileFromSource(MediaSourceProxy(urlString: "test/test1"), fileExtension: "png")
|
||||
XCTAssertEqual(result, expectedResult)
|
||||
}
|
||||
|
||||
func test_whenFileFromURLStringAndURLIsNil_nilIsReturned() async throws {
|
||||
mediaProxy.mediaContentData = try loadTestImage().pngData()
|
||||
let url = mediaProvider.fileFromURLString(nil, fileExtension: "png")
|
||||
XCTAssertNil(url)
|
||||
}
|
||||
|
||||
func test_whenFileFromURLString_correctURLIsReturned() throws {
|
||||
let expectedURL = try XCTUnwrap(URL(string: "some_url"))
|
||||
fileCache.fileURLToReturn = expectedURL
|
||||
let url = mediaProvider.fileFromURLString("test/test1", fileExtension: "png")
|
||||
XCTAssertEqual(url?.absoluteString, expectedURL.absoluteString)
|
||||
}
|
||||
|
||||
func test_whenLoadFileFromURLString_correctURLIsReturned() async throws {
|
||||
let expectedURL = try XCTUnwrap(URL(string: "some_url"))
|
||||
let expectedResult: Result<URL, MediaProviderError> = .success(expectedURL)
|
||||
fileCache.fileURLToReturn = expectedURL
|
||||
let result = await mediaProvider.loadFileFromURLString("test/test1", fileExtension: "png")
|
||||
XCTAssertEqual(result, expectedResult)
|
||||
}
|
||||
|
||||
private func loadTestImage() throws -> UIImage {
|
||||
let bundle = Bundle(for: classForCoder)
|
||||
guard let path = bundle.path(forResource: "test_image", ofType: "png"),
|
||||
let image = UIImage(contentsOfFile: path) else {
|
||||
throw MediaProviderTestsError.screenshotNotFound
|
||||
}
|
||||
return image
|
||||
}
|
||||
}
|
||||
|
||||
enum MediaProviderTestsError: Error {
|
||||
case screenshotNotFound
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
//
|
||||
// Copyright 2022 New Vector Ltd
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
@testable import ElementX
|
||||
import Foundation
|
||||
|
||||
class MockBackgroundTaskService: BackgroundTaskServiceProtocol {
|
||||
func startBackgroundTask(withName name: String, isReusable: Bool, expirationHandler: (() -> Void)?) -> ElementX.BackgroundTaskProtocol? {
|
||||
nil
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
//
|
||||
// Copyright 2022 New Vector Ltd
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
@testable import ElementX
|
||||
import Foundation
|
||||
|
||||
enum MockFileCacheError: Error {
|
||||
case someError
|
||||
}
|
||||
|
||||
class MockFileCache: FileCacheProtocol {
|
||||
var fileKey: String?
|
||||
var fileExtension: String?
|
||||
var fileURLToReturn: URL?
|
||||
var storedData: Data?
|
||||
var storedFileExtension: String?
|
||||
var storedFileKey: String?
|
||||
var storeURLToReturn: URL?
|
||||
|
||||
func file(forKey key: String, fileExtension: String) -> URL? {
|
||||
fileKey = key
|
||||
self.fileExtension = fileExtension
|
||||
return fileURLToReturn
|
||||
}
|
||||
|
||||
func store(_ data: Data, with fileExtension: String, forKey key: String) throws -> URL {
|
||||
storedData = data
|
||||
storedFileExtension = fileExtension
|
||||
storedFileKey = key
|
||||
if let storeURLToReturn {
|
||||
return storeURLToReturn
|
||||
} else {
|
||||
throw MockFileCacheError.someError
|
||||
}
|
||||
}
|
||||
|
||||
func remove(forKey key: String, fileExtension: String) throws { }
|
||||
|
||||
func removeAll() throws { }
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
//
|
||||
// Copyright 2022 New Vector Ltd
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
@testable import ElementX
|
||||
import Kingfisher
|
||||
import UIKit
|
||||
|
||||
class MockImageCache: ImageCache {
|
||||
var retrievedImagesInMemory = [String: UIImage]()
|
||||
var retrievedImages = [String: UIImage]()
|
||||
var storedImages = [String: UIImage]()
|
||||
|
||||
override func retrieveImageInMemoryCache(forKey key: String, options: KingfisherParsedOptionsInfo) -> KFCrossPlatformImage? {
|
||||
retrievedImagesInMemory[key]
|
||||
}
|
||||
|
||||
override func retrieveImage(forKey key: String, options: KingfisherOptionsInfo? = nil, callbackQueue: CallbackQueue = .mainCurrentOrAsync, completionHandler: ((Result<ImageCacheResult, KingfisherError>) -> Void)?) {
|
||||
if let image = retrievedImages[key] {
|
||||
completionHandler?(.success(ImageCacheResult.disk(image)))
|
||||
} else {
|
||||
let error = KingfisherError.cacheError(reason: .imageNotExisting(key: key))
|
||||
completionHandler?(.failure(error))
|
||||
}
|
||||
}
|
||||
|
||||
override func store(_ image: KFCrossPlatformImage,
|
||||
original: Data? = nil,
|
||||
forKey key: String,
|
||||
processorIdentifier identifier: String = "",
|
||||
cacheSerializer serializer: CacheSerializer = DefaultCacheSerializer.default,
|
||||
toDisk: Bool = true,
|
||||
callbackQueue: CallbackQueue = .untouch,
|
||||
completionHandler: ((CacheStoreResult) -> Void)? = nil) {
|
||||
storedImages[key] = image
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
//
|
||||
// Copyright 2022 New Vector Ltd
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
@testable import ElementX
|
||||
import Foundation
|
||||
|
||||
enum MockMediaProxyError: Error {
|
||||
case someError
|
||||
}
|
||||
|
||||
class MockMediaProxy: MediaProxyProtocol {
|
||||
var mediaContentData: Data?
|
||||
var mediaThumbnailData: Data?
|
||||
|
||||
func mediaSourceForURLString(_ urlString: String) -> MediaSourceProxy {
|
||||
MediaSourceProxy(urlString: "test")
|
||||
}
|
||||
|
||||
func loadMediaContentForSource(_ source: ElementX.MediaSourceProxy) async throws -> Data {
|
||||
if let mediaContentData {
|
||||
return mediaContentData
|
||||
} else {
|
||||
throw MockMediaProxyError.someError
|
||||
}
|
||||
}
|
||||
|
||||
func loadMediaThumbnailForSource(_ source: ElementX.MediaSourceProxy, width: UInt, height: UInt) async throws -> Data {
|
||||
if let mediaThumbnailData {
|
||||
return mediaThumbnailData
|
||||
} else {
|
||||
throw MockMediaProxyError.someError
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user