From 20181cfe06cdc24a335c8029ca99a204073aae12 Mon Sep 17 00:00:00 2001 From: Darnell Andries Date: Tue, 13 Dec 2022 16:20:21 -0800 Subject: [PATCH] Replace stats updater helper unit test with browser test --- .../brave_stats_updater_helper_browsertest.cc | 114 ++++++++++++++++++ .../brave_stats_updater_helper_unittest.cc | 90 -------------- test/BUILD.gn | 2 +- 3 files changed, 115 insertions(+), 91 deletions(-) create mode 100644 browser/brave_ads/brave_stats_updater_helper_browsertest.cc delete mode 100644 browser/brave_ads/brave_stats_updater_helper_unittest.cc diff --git a/browser/brave_ads/brave_stats_updater_helper_browsertest.cc b/browser/brave_ads/brave_stats_updater_helper_browsertest.cc new file mode 100644 index 000000000000..b7a998a4ed06 --- /dev/null +++ b/browser/brave_ads/brave_stats_updater_helper_browsertest.cc @@ -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 + +#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(); + } + + 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 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 diff --git a/browser/brave_ads/brave_stats_updater_helper_unittest.cc b/browser/brave_ads/brave_stats_updater_helper_unittest.cc deleted file mode 100644 index a697d6849e59..000000000000 --- a/browser/brave_ads/brave_stats_updater_helper_unittest.cc +++ /dev/null @@ -1,90 +0,0 @@ -/* 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 - -#include "brave/components/brave_ads/browser/ads_service.h" -#include "brave/components/brave_ads/common/pref_names.h" -#include "chrome/test/base/testing_browser_process.h" -#include "chrome/test/base/testing_profile.h" -#include "chrome/test/base/testing_profile_manager.h" -#include "components/prefs/testing_pref_service.h" -#include "components/sync_preferences/testing_pref_service_syncable.h" -#include "content/public/test/browser_task_environment.h" -#include "testing/gtest/include/gtest/gtest.h" - -namespace brave_ads { - -class BraveStatsUpdaterHelperTest : public testing::Test { - public: - BraveStatsUpdaterHelperTest() - : profile_manager_(TestingBrowserProcess::GetGlobal()) {} - - protected: - void SetUp() override { - ASSERT_TRUE(profile_manager_.SetUp()); - - profile_one_ = profile_manager_.CreateTestingProfile("TestProfile1"); - AdsService::RegisterProfilePrefs( - profile_one_->GetTestingPrefService()->registry()); - profile_one_->GetPrefs()->SetBoolean(ads::prefs::kEnabled, true); - - profile_two_ = profile_manager_.CreateTestingProfile("TestProfile2"); - AdsService::RegisterProfilePrefs( - profile_two_->GetTestingPrefService()->registry()); - - brave_stats_updater_helper_ = std::make_unique(); - } - - void TearDown() override { brave_stats_updater_helper_.release(); } - - TestingPrefServiceSimple* GetLocalState() { - return profile_manager_.local_state()->Get(); - } - - content::BrowserTaskEnvironment task_environment_; - std::unique_ptr brave_stats_updater_helper_; - TestingProfileManager profile_manager_; - TestingProfile* profile_one_; - TestingProfile* profile_two_; -}; - -#if !BUILDFLAG(IS_ANDROID) -TEST_F(BraveStatsUpdaterHelperTest, ProfileSwitch) { - profile_manager_.UpdateLastUser(profile_one_); - EXPECT_EQ(GetLocalState()->GetBoolean(ads::prefs::kEnabledForLastProfile), - true); - - profile_manager_.UpdateLastUser(profile_two_); - EXPECT_EQ(GetLocalState()->GetBoolean(ads::prefs::kEnabledForLastProfile), - false); - - profile_manager_.UpdateLastUser(profile_one_); - EXPECT_EQ(GetLocalState()->GetBoolean(ads::prefs::kEnabledForLastProfile), - true); -} - -TEST_F(BraveStatsUpdaterHelperTest, EnabledUpdate) { - profile_manager_.UpdateLastUser(profile_one_); - EXPECT_EQ(GetLocalState()->GetBoolean(ads::prefs::kEnabledForLastProfile), - true); - - profile_two_->GetPrefs()->SetBoolean(ads::prefs::kEnabled, true); - EXPECT_EQ(GetLocalState()->GetBoolean(ads::prefs::kEnabledForLastProfile), - true); - - profile_one_->GetPrefs()->SetBoolean(ads::prefs::kEnabled, false); - EXPECT_EQ(GetLocalState()->GetBoolean(ads::prefs::kEnabledForLastProfile), - false); - - profile_manager_.UpdateLastUser(profile_two_); - EXPECT_EQ(GetLocalState()->GetBoolean(ads::prefs::kEnabledForLastProfile), - true); -} -#endif - -} // namespace brave_ads diff --git a/test/BUILD.gn b/test/BUILD.gn index 2eb9f72e369f..11d4d415a7aa 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -93,7 +93,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", @@ -678,6 +677,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",