Skip to content

Commit

Permalink
Replace stats updater helper unit test with browser test
Browse files Browse the repository at this point in the history
  • Loading branch information
DJAndries committed Dec 14, 2022
1 parent 2dd782c commit 00d2fae
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 91 deletions.
114 changes: 114 additions & 0 deletions browser/brave_ads/brave_stats_updater_helper_browsertest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/* Copyright (c) 2022 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at https://mozilla.org/MPL/2.0/. */

#include "brave/browser/brave_ads/brave_stats_updater_helper.h"

#include <memory>

#include "brave/components/brave_ads/common/pref_names.h"
#include "chrome/browser/profiles/profile_test_util.h"
#include "chrome/test/base/testing_browser_process.h"
#include "components/prefs/pref_service.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "testing/gtest/include/gtest/gtest.h"

#if BUILDFLAG(IS_ANDROID)
#include "chrome/test/base/android/android_browser_test.h"
#else
#include "chrome/browser/ui/browser.h"
#include "chrome/test/base/in_process_browser_test.h"
#endif

namespace brave_ads {

class BraveStatsUpdaterHelperTest : public PlatformBrowserTest {
public:
BraveStatsUpdaterHelperTest() {}

protected:
void SetUpOnMainThread() override {
PlatformBrowserTest::SetUpOnMainThread();
profile_manager_ = g_browser_process->profile_manager();
local_state_ = g_browser_process->local_state();
brave_stats_updater_helper_ = std::make_unique<BraveStatsUpdaterHelper>();
}

void PostRunTestOnMainThread() override {
brave_stats_updater_helper_.reset();
PlatformBrowserTest::PostRunTestOnMainThread();
}

void CreateMultipleProfiles() {
profile_one_path_ = profile_manager_->GenerateNextProfileDirectoryPath();
profile_one_ = profiles::testing::CreateProfileSync(profile_manager_,
profile_one_path_);
profile_two_path_ = profile_manager_->GenerateNextProfileDirectoryPath();
profile_two_ = profiles::testing::CreateProfileSync(profile_manager_,
profile_two_path_);
}

base::FilePath profile_one_path_;
Profile* profile_one_;

base::FilePath profile_two_path_;
Profile* profile_two_;

ProfileManager* profile_manager_;
PrefService* local_state_;
std::unique_ptr<BraveStatsUpdaterHelper> brave_stats_updater_helper_;
};

IN_PROC_BROWSER_TEST_F(BraveStatsUpdaterHelperTest,
PrimaryProfileEnabledUpdate) {
Profile* primary_profile = profile_manager_->GetPrimaryUserProfile();

EXPECT_EQ(local_state_->GetBoolean(ads::prefs::kEnabledForLastProfile),
false);

primary_profile->GetPrefs()->SetBoolean(ads::prefs::kEnabled, true);
EXPECT_EQ(local_state_->GetBoolean(ads::prefs::kEnabledForLastProfile), true);

primary_profile->GetPrefs()->SetBoolean(ads::prefs::kEnabled, false);
EXPECT_EQ(local_state_->GetBoolean(ads::prefs::kEnabledForLastProfile),
false);
}

#if !BUILDFLAG(IS_ANDROID)
IN_PROC_BROWSER_TEST_F(BraveStatsUpdaterHelperTest, ProfileSwitch) {
CreateMultipleProfiles();
profile_one_->GetPrefs()->SetBoolean(ads::prefs::kEnabled, true);

profiles::testing::SwitchToProfileSync(profile_one_path_);
EXPECT_EQ(local_state_->GetBoolean(ads::prefs::kEnabledForLastProfile), true);

profiles::testing::SwitchToProfileSync(profile_two_path_);
EXPECT_EQ(local_state_->GetBoolean(ads::prefs::kEnabledForLastProfile),
false);

profiles::testing::SwitchToProfileSync(profile_one_path_);
EXPECT_EQ(local_state_->GetBoolean(ads::prefs::kEnabledForLastProfile), true);
}

IN_PROC_BROWSER_TEST_F(BraveStatsUpdaterHelperTest, MultiProfileEnabledUpdate) {
CreateMultipleProfiles();
profile_one_->GetPrefs()->SetBoolean(ads::prefs::kEnabled, true);

profiles::testing::SwitchToProfileSync(profile_one_path_);
EXPECT_EQ(local_state_->GetBoolean(ads::prefs::kEnabledForLastProfile), true);

profile_two_->GetPrefs()->SetBoolean(ads::prefs::kEnabled, true);
EXPECT_EQ(local_state_->GetBoolean(ads::prefs::kEnabledForLastProfile), true);

profile_one_->GetPrefs()->SetBoolean(ads::prefs::kEnabled, false);
EXPECT_EQ(local_state_->GetBoolean(ads::prefs::kEnabledForLastProfile),
false);

profiles::testing::SwitchToProfileSync(profile_two_path_);
EXPECT_EQ(local_state_->GetBoolean(ads::prefs::kEnabledForLastProfile), true);
}
#endif

} // namespace brave_ads
90 changes: 0 additions & 90 deletions browser/brave_ads/brave_stats_updater_helper_unittest.cc

This file was deleted.

2 changes: 1 addition & 1 deletion test/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ test("brave_unit_tests") {
]

sources = [
"//brave/browser/brave_ads/brave_stats_updater_helper_unittest.cc",
"//brave/browser/brave_ads/search_result_ad/search_result_ad_service_unittest.cc",
"//brave/browser/brave_content_browser_client_unittest.cc",
"//brave/browser/brave_resources_util_unittest.cc",
Expand Down Expand Up @@ -682,6 +681,7 @@ test("brave_browser_tests") {
"//brave/app/brave_main_delegate_browsertest.cc",
"//brave/app/brave_main_delegate_runtime_flags_browsertest.cc",
"//brave/browser/brave_ads/ads_service_browsertest.cc",
"//brave/browser/brave_ads/brave_stats_updater_helper_browsertest.cc",
"//brave/browser/brave_ads/notification_helper/notification_helper_impl_mock.cc",
"//brave/browser/brave_ads/notification_helper/notification_helper_impl_mock.h",
"//brave/browser/brave_ads/search_result_ad/search_result_ad_browsertest.cc",
Expand Down

0 comments on commit 00d2fae

Please sign in to comment.