ImageDataProviderTests.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. @testable 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.localFileCacheKey)
  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 testLocalFileImageDataProviderMainQueue() {
  46. let fm = FileManager.default
  47. let document = try! fm.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
  48. let fileURL = document.appendingPathComponent("test")
  49. try! testImageData.write(to: fileURL)
  50. let provider = LocalFileImageDataProvider(fileURL: fileURL, loadingQueue: .mainCurrentOrAsync)
  51. XCTAssertEqual(provider.cacheKey, fileURL.localFileCacheKey)
  52. XCTAssertEqual(provider.fileURL, fileURL)
  53. var called = false
  54. provider.data { result in
  55. XCTAssertEqual(result.value, testImageData)
  56. try! fm.removeItem(at: fileURL)
  57. called = true
  58. }
  59. XCTAssertTrue(called)
  60. }
  61. func testLocalFileCacheKey() {
  62. let url1 = URL(string: "file:///Users/onevcat/Library/Developer/CoreSimulator/Devices/ABC/data/Containers/Bundle/Application/DEF/Kingfisher-Demo.app/images/kingfisher-1.jpg")!
  63. XCTAssertEqual(url1.localFileCacheKey, "\(URL.localFileCacheKeyPrefix)/Kingfisher-Demo.app/images/kingfisher-1.jpg")
  64. let url2 = URL(string: "file:///private/var/containers/Bundle/Application/ABC/Kingfisher-Demo.app/images/kingfisher-1.jpg")!
  65. XCTAssertEqual(url2.localFileCacheKey, "\(URL.localFileCacheKeyPrefix)/Kingfisher-Demo.app/images/kingfisher-1.jpg")
  66. let url3 = URL(string: "file:///private/var/containers/Bundle/Application/ABC/Kingfisher-Demo.app/images/kingfisher-1.jpg?foo=bar")!
  67. XCTAssertEqual(url3.localFileCacheKey, "\(URL.localFileCacheKeyPrefix)/Kingfisher-Demo.app/images/kingfisher-1.jpg?foo=bar")
  68. let url4 = URL(string: "file:///private/var/containers/Bundle/Application/ABC/Kingfisher-Demo.appex/images/kingfisher-1.jpg")!
  69. XCTAssertEqual(url4.localFileCacheKey, "\(URL.localFileCacheKeyPrefix)/Kingfisher-Demo.appex/images/kingfisher-1.jpg")
  70. let url5 = URL(string: "file:///private/var/containers/Bundle/Application/ABC/Kingfisher-Demo.other/images/kingfisher-1.jpg")!
  71. XCTAssertEqual(url5.localFileCacheKey, "\(URL.localFileCacheKeyPrefix)///private/var/containers/Bundle/Application/ABC/Kingfisher-Demo.other/images/kingfisher-1.jpg")
  72. }
  73. func testBase64ImageDataProvider() {
  74. let base64String = testImageData.base64EncodedString()
  75. let provider = Base64ImageDataProvider(base64String: base64String, cacheKey: "123")
  76. XCTAssertEqual(provider.cacheKey, "123")
  77. var syncCalled = false
  78. provider.data { result in
  79. XCTAssertEqual(result.value, testImageData)
  80. syncCalled = true
  81. }
  82. XCTAssertTrue(syncCalled)
  83. }
  84. }