Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent making Favicons network call in NewTabPageFavoritesModelTests #3607

Merged
merged 3 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion DuckDuckGo/FavoritesViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ protocol NewTabPageFavoriteDataSource {
func removeFavorite(_ favorite: Favorite)
}

protocol FavoritesFaviconCaching {
func populateFavicon(for domain: String, intoCache: FaviconsCacheType, fromCache: FaviconsCacheType?)
}

struct FavoritesSlice {
let items: [FavoriteItem]
let isCollapsible: Bool
Expand All @@ -52,6 +56,7 @@ class FavoritesViewModel: ObservableObject {
private var cancellables = Set<AnyCancellable>()

private let favoriteDataSource: NewTabPageFavoriteDataSource
private let faviconsCache: FavoritesFaviconCaching
private let pixelFiring: PixelFiring.Type
private let dailyPixelFiring: DailyPixelFiring.Type

Expand All @@ -64,13 +69,15 @@ class FavoritesViewModel: ObservableObject {
init(isNewTabPageCustomizationEnabled: Bool = false,
favoriteDataSource: NewTabPageFavoriteDataSource,
faviconLoader: FavoritesFaviconLoading,
faviconsCache: FavoritesFaviconCaching = Favicons.shared,
pixelFiring: PixelFiring.Type = Pixel.self,
dailyPixelFiring: DailyPixelFiring.Type = DailyPixel.self) {
self.favoriteDataSource = favoriteDataSource
self.pixelFiring = pixelFiring
self.dailyPixelFiring = dailyPixelFiring
self.isNewTabPageCustomizationEnabled = isNewTabPageCustomizationEnabled
self.isCollapsed = isNewTabPageCustomizationEnabled
self.faviconsCache = faviconsCache

self.faviconLoader = MissingFaviconWrapper(loader: faviconLoader, onFaviconMissing: { [weak self] in
guard let self else { return }
Expand Down Expand Up @@ -130,7 +137,9 @@ class FavoritesViewModel: ObservableObject {

pixelFiring.fire(.favoriteLaunchedNTP, withAdditionalParameters: [:])
dailyPixelFiring.fireDaily(.favoriteLaunchedNTPDaily)
Favicons.shared.loadFavicon(forDomain: url.host, intoCache: .fireproof, fromCache: .tabs)
if let host = url.host {
faviconsCache.populateFavicon(for: host, intoCache: .fireproof, fromCache: .tabs)
}

onFavoriteURLSelected?(url)
}
Expand Down Expand Up @@ -231,3 +240,9 @@ private extension FavoriteItem {
}
}
}

extension Favicons: FavoritesFaviconCaching {
func populateFavicon(for domain: String, intoCache: FaviconsCacheType, fromCache: FaviconsCacheType?) {
loadFavicon(forDomain: domain, intoCache: intoCache, fromCache: fromCache)
}
}
23 changes: 22 additions & 1 deletion DuckDuckGoTests/NewTabPageFavoritesModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ final class NewTabPageFavoritesModelTests: XCTestCase {
private func createSUT(isNewTabPageCustomizationEnabled: Bool = true) -> FavoritesViewModel {
FavoritesViewModel(isNewTabPageCustomizationEnabled: isNewTabPageCustomizationEnabled,
favoriteDataSource: favoriteDataSource,
faviconLoader: FavoritesFaviconLoader(),
faviconLoader: MockFavoritesFaviconLoading(),
faviconsCache: MockFavoritesFaviconCaching(),
pixelFiring: PixelFiringMock.self,
dailyPixelFiring: PixelFiringMock.self)
}
Expand Down Expand Up @@ -205,3 +206,23 @@ private extension FavoriteItem {
}
}
}

private final class MockFavoritesFaviconLoading: FavoritesFaviconLoading {
func loadFavicon(for favorite: Favorite, size: CGFloat) async -> Favicon? {
nil
}

func fakeFavicon(for favorite: Favorite, size: CGFloat) -> Favicon {
Favicon(image: .init(), isUsingBorder: false, isFake: false)
}

func existingFavicon(for favorite: Favorite, size: CGFloat) -> Favicon? {
nil
}
}

private final class MockFavoritesFaviconCaching: FavoritesFaviconCaching {
func populateFavicon(for domain: String, intoCache: FaviconsCacheType, fromCache: FaviconsCacheType?) {

}
}
Loading