-
Notifications
You must be signed in to change notification settings - Fork 888
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace stats updater helper unit test with browser test
- Loading branch information
Showing
3 changed files
with
115 additions
and
91 deletions.
There are no files selected for viewing
114 changes: 114 additions & 0 deletions
114
browser/brave_ads/brave_stats_updater_helper_browsertest.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters