ImageDataProviderTests.swift 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. //
  2. // ImageDataProviderTests.swift
  3. // Kingfisher
  4. //
  5. // Created by onevcat on 2018/11/18.
  6. //
  7. // Copyright (c) 2019 Wei Wang <onevcat@gmail.com>
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. import XCTest
  27. import Kingfisher
  28. class ImageDataProviderTests: XCTestCase {
  29. func testLocalFileImageDataProvider() {
  30. let fm = FileManager.default
  31. let document = try! fm.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
  32. let fileURL = document.appendingPathComponent("test")
  33. try! testImageData.write(to: fileURL)
  34. let provider = LocalFileImageDataProvider(fileURL: fileURL)
  35. XCTAssertEqual(provider.cacheKey, fileURL.absoluteString)
  36. XCTAssertEqual(provider.fileURL, fileURL)
  37. let exp = expectation(description: #function)
  38. provider.data { result in
  39. XCTAssertEqual(result.value, testImageData)
  40. try! fm.removeItem(at: fileURL)
  41. exp.fulfill()
  42. }
  43. waitForExpectations(timeout: 1, handler: nil)
  44. }
  45. func testBase64ImageDataProvider() {
  46. let base64String = testImageData.base64EncodedString()
  47. let provider = Base64ImageDataProvider(base64String: base64String, cacheKey: "123")
  48. XCTAssertEqual(provider.cacheKey, "123")
  49. var syncCalled = false
  50. provider.data { result in
  51. XCTAssertEqual(result.value, testImageData)
  52. syncCalled = true
  53. }
  54. XCTAssertTrue(syncCalled)
  55. }
  56. }