Skip to content

Commit

Permalink
Prevent sending duplicate home tab pixel
Browse files Browse the repository at this point in the history
  • Loading branch information
dus7 committed Nov 15, 2024
1 parent 81c9a09 commit fdfacfa
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 5 deletions.
4 changes: 4 additions & 0 deletions DuckDuckGo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@
6FEC0B852C999352006B4F6E /* FavoriteItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6FEC0B842C999352006B4F6E /* FavoriteItem.swift */; };
6FEC0B882C999961006B4F6E /* FavoritesListInteractingAdapter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6FEC0B872C999961006B4F6E /* FavoritesListInteractingAdapter.swift */; };
6FF915822B88E07A0042AC87 /* AdAttributionFetcherTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6FF915802B88E0750042AC87 /* AdAttributionFetcherTests.swift */; };
6FF9AD452CE766F700C5A406 /* NewTabPageControllerPixelTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6FF9AD442CE766F700C5A406 /* NewTabPageControllerPixelTests.swift */; };
7B1604E82CB685B400A44EC6 /* Logger+TipKit.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B1604E72CB685B400A44EC6 /* Logger+TipKit.swift */; };
7B1604EC2CB68BDA00A44EC6 /* TipKitController+ConvenienceInitializers.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B1604EB2CB68BDA00A44EC6 /* TipKitController+ConvenienceInitializers.swift */; };
7B1604EE2CB68D2600A44EC6 /* TipKitDebugOptionsUIActionHandling.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B1604ED2CB68D2600A44EC6 /* TipKitDebugOptionsUIActionHandling.swift */; };
Expand Down Expand Up @@ -1673,6 +1674,7 @@
6FEC0B842C999352006B4F6E /* FavoriteItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FavoriteItem.swift; sourceTree = "<group>"; };
6FEC0B872C999961006B4F6E /* FavoritesListInteractingAdapter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FavoritesListInteractingAdapter.swift; sourceTree = "<group>"; };
6FF915802B88E0750042AC87 /* AdAttributionFetcherTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AdAttributionFetcherTests.swift; sourceTree = "<group>"; };
6FF9AD442CE766F700C5A406 /* NewTabPageControllerPixelTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NewTabPageControllerPixelTests.swift; sourceTree = "<group>"; };
7B1604E72CB685B400A44EC6 /* Logger+TipKit.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Logger+TipKit.swift"; sourceTree = "<group>"; };
7B1604EB2CB68BDA00A44EC6 /* TipKitController+ConvenienceInitializers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "TipKitController+ConvenienceInitializers.swift"; sourceTree = "<group>"; };
7B1604ED2CB68D2600A44EC6 /* TipKitDebugOptionsUIActionHandling.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TipKitDebugOptionsUIActionHandling.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -3851,6 +3853,7 @@
6F03CAFF2C32ED22004179A8 /* NewTabPage */ = {
isa = PBXGroup;
children = (
6FF9AD442CE766F700C5A406 /* NewTabPageControllerPixelTests.swift */,
6F03CB002C32ED42004179A8 /* NewTabPageMessagesModelTests.swift */,
6F40D15C2C34436200BF22F0 /* HomePageDisplayDailyPixelBucketTests.swift */,
6F934F852C58DB00008364E4 /* NewTabPageSettingsPersistentStorageTests.swift */,
Expand Down Expand Up @@ -8148,6 +8151,7 @@
987130C8294AAB9F00AB05E0 /* BookmarksTestHelpers.swift in Sources */,
9F4CC51D2C48D240006A96EB /* CoreDataDatabaseTestUtilities.swift in Sources */,
C185ED672BD43DA100BAE9DC /* ImportPasswordsStatusHandlerTests.swift in Sources */,
6FF9AD452CE766F700C5A406 /* NewTabPageControllerPixelTests.swift in Sources */,
F198D7981E3A45D90088DA8A /* WKWebViewConfigurationExtensionTests.swift in Sources */,
6F3529FF2CDCEDFF00A59170 /* OmniBarLoadingStateBearerTests.swift in Sources */,
564DE45E2C45218500D23241 /* OnboardingNavigationDelegateTests.swift in Sources */,
Expand Down
18 changes: 14 additions & 4 deletions DuckDuckGo/NewTabPageViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ final class NewTabPageViewController: UIHostingController<AnyView>, NewTabPage {
private weak var daxDialogViewController: DaxDialogViewController?
private var daxDialogHeightConstraint: NSLayoutConstraint?

private let pixelFiring: PixelFiring.Type

var isDaxDialogVisible: Bool {
daxDialogViewController?.view.isHidden == false
}
Expand All @@ -54,12 +56,14 @@ final class NewTabPageViewController: UIHostingController<AnyView>, NewTabPage {
variantManager: VariantManager,
newTabDialogFactory: any NewTabDaxDialogProvider,
newTabDialogTypeProvider: NewTabDialogSpecProvider,
faviconLoader: FavoritesFaviconLoading) {
faviconLoader: FavoritesFaviconLoading,
pixelFiring: PixelFiring.Type = Pixel.self) {

self.associatedTab = tab
self.variantManager = variantManager
self.newTabDialogFactory = newTabDialogFactory
self.newTabDialogTypeProvider = newTabDialogTypeProvider
self.pixelFiring = pixelFiring

newTabPageViewModel = NewTabPageViewModel()
shortcutsSettingsModel = NewTabPageShortcutsSettingsModel()
Expand Down Expand Up @@ -96,14 +100,20 @@ final class NewTabPageViewController: UIHostingController<AnyView>, NewTabPage {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)

view.backgroundColor = UIColor(designSystemColor: .background)

// If there's no tab switcher then this will be true, if there is a tabswitcher then only allow the
// stuff below to happen if it's being dismissed
guard presentedViewController?.isBeingDismissed ?? true else {
return
}

associatedTab.viewed = true

presentNextDaxDialog()

Pixel.fire(pixel: .homeScreenShown)
pixelFiring.fire(.homeScreenShown, withAdditionalParameters: [:])
sendDailyDisplayPixel()

view.backgroundColor = UIColor(designSystemColor: .background)
}

private func setUpDaxDialog() {
Expand Down
93 changes: 93 additions & 0 deletions DuckDuckGoTests/NewTabPageControllerPixelTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
//

Check failure on line 1 in DuckDuckGoTests/NewTabPageControllerPixelTests.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Header comments should be consistent with project patterns (file_header)
// NewTabPagePixel.swift
// DuckDuckGo
//
// Copyright © 2024 DuckDuckGo. All rights reserved.
//
// 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.
//

import XCTest
import Core
@testable import DuckDuckGo
import SwiftUICore

final class NewTabPageControllerPixelTests: XCTestCase {

override func setUp() {
super.setUp()

PixelFiringMock.tearDown()
}

override func tearDown() {
super.tearDown()

PixelFiringMock.tearDown()
}

func testHomeScreenPixelIsFiredOnAppear() {
let sut = createSUT()
sut.loadViewIfNeeded()

sut.viewDidAppear(false)

let count = PixelFiringMock.allPixelsFired.count { $0.pixelName == Pixel.Event.homeScreenShown.name }

XCTAssertEqual(count, 1)
}

func testHomeScreenPixelIsNotFired_WhenPresentingOtherController() {
let expectation = XCTestExpectation(description: "View loaded")
let sut = createSUT()

let window = UIWindow(frame: UIScreen.main.bounds)
let presentedVC = UIViewController()
window.rootViewController = sut
window.makeKeyAndVisible()
window.rootViewController?.present(presentedVC, animated: false, completion: nil)

DispatchQueue.main.async {
XCTAssertTrue(sut.isViewLoaded)
XCTAssertTrue(presentedVC.isViewLoaded)
XCTAssertNotNil(sut.presentedViewController)
expectation.fulfill()
}

wait(for: [expectation], timeout: 1)

sut.viewDidAppear(false)

let count = PixelFiringMock.allPixelsFired.count { $0.pixelName == Pixel.Event.homeScreenShown.name }

XCTAssertEqual(count, 0)
}

private func createSUT() -> NewTabPageViewController {
NewTabPageViewController(tab: Tab(),
isNewTabPageCustomizationEnabled: false,
interactionModel: MockFavoritesListInteracting(),
homePageMessagesConfiguration: HomePageMessagesConfigurationMock(homeMessages: []),
variantManager: MockVariantManager(),
newTabDialogFactory: MockDaxDialogFactory(),
newTabDialogTypeProvider: MockNewTabDialogSpecProvider(),
faviconLoader: EmptyFaviconLoading(),
pixelFiring: PixelFiringMock.self)
}
}

private class MockDaxDialogFactory: NewTabDaxDialogProvider {
func createDaxDialog(for homeDialog: DaxDialogs.HomeScreenSpec, onDismiss: @escaping () -> Void) -> EmptyView {
EmptyView()
}
}
2 changes: 1 addition & 1 deletion DuckDuckGoTests/NewTabPageMessagesModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ final class NewTabPageMessagesModelTests: XCTestCase {
}
}

private class HomePageMessagesConfigurationMock: HomePageMessagesConfiguration {
class HomePageMessagesConfigurationMock: HomePageMessagesConfiguration {
var homeMessages: [HomeMessage]

init(homeMessages: [HomeMessage]) {
Expand Down

0 comments on commit fdfacfa

Please sign in to comment.