Skip to content

Commit b918803

Browse files
committed
Disables HaTS.
Prevents HaTS service from showing any surveys. Fixes brave/brave-browser#19685
1 parent f528612 commit b918803

File tree

4 files changed

+175
-0
lines changed

4 files changed

+175
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/* Copyright (c) 2021 The Brave Authors. All rights reserved.
2+
* This Source Code Form is subject to the terms of the Mozilla Public
3+
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
4+
* You can obtain one at http://mozilla.org/MPL/2.0/. */
5+
6+
#include "chrome/browser/ui/hats/hats_service.h"
7+
8+
#define HatsService HatsService_ChromiumImpl
9+
#include "src/chrome/browser/ui/hats/hats_service.cc"
10+
#undef HatsService
11+
12+
HatsService::~HatsService() = default;
13+
14+
bool HatsService::CanShowSurvey(const std::string& trigger) const {
15+
return false;
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/* Copyright (c) 2021 The Brave Authors. All rights reserved.
2+
* This Source Code Form is subject to the terms of the Mozilla Public
3+
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
4+
* You can obtain one at http://mozilla.org/MPL/2.0/. */
5+
6+
#ifndef BRAVE_CHROMIUM_SRC_CHROME_BROWSER_UI_HATS_HATS_SERVICE_H_
7+
#define BRAVE_CHROMIUM_SRC_CHROME_BROWSER_UI_HATS_HATS_SERVICE_H_
8+
9+
#define HatsService HatsService_ChromiumImpl
10+
#define CanShowSurvey virtual CanShowSurvey
11+
#include "src/chrome/browser/ui/hats/hats_service.h"
12+
#undef CanShowSurvey
13+
#undef HatsService
14+
15+
class HatsService : public HatsService_ChromiumImpl {
16+
public:
17+
using HatsService_ChromiumImpl::HatsService_ChromiumImpl;
18+
~HatsService() override;
19+
20+
bool CanShowSurvey(const std::string& trigger) const override;
21+
};
22+
23+
#endif // BRAVE_CHROMIUM_SRC_CHROME_BROWSER_UI_HATS_HATS_SERVICE_H_
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/* Copyright (c) 2021 The Brave Authors. All rights reserved.
2+
* This Source Code Form is subject to the terms of the Mozilla Public
3+
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
4+
* You can obtain one at http://mozilla.org/MPL/2.0/. */
5+
6+
#include <vector>
7+
8+
#include "base/test/scoped_feature_list.h"
9+
#include "base/time/time.h"
10+
#include "chrome/browser/browser_process.h"
11+
#include "chrome/browser/metrics/chrome_metrics_service_accessor.h"
12+
#include "chrome/browser/profiles/profile_impl.h"
13+
#include "chrome/browser/ui/browser.h"
14+
#include "chrome/browser/ui/hats/hats_service.h"
15+
#include "chrome/browser/ui/hats/hats_service_factory.h"
16+
#include "chrome/common/chrome_features.h"
17+
#include "chrome/test/base/in_process_browser_test.h"
18+
#include "chrome/test/base/ui_test_utils.h"
19+
#include "components/metrics_services_manager/metrics_services_manager.h"
20+
#include "content/public/test/browser_test.h"
21+
#include "net/dns/mock_host_resolver.h"
22+
#include "third_party/abseil-cpp/absl/types/optional.h"
23+
24+
// Simplified version of the upstream tests modified to reflect the change in
25+
// the HatsService chromium_src override.
26+
27+
namespace {
28+
29+
base::test::ScopedFeatureList::FeatureAndParams settings_probability_one{
30+
features::kHappinessTrackingSurveysForDesktopSettings,
31+
{{"probability", "1.000"},
32+
{"survey", kHatsSurveyTriggerSettings},
33+
{"en_site_id", "test_site_id"}}};
34+
35+
class ScopedSetMetricsConsent {
36+
public:
37+
// Enables or disables metrics consent based off of |consent|.
38+
explicit ScopedSetMetricsConsent(bool consent) : consent_(consent) {
39+
ChromeMetricsServiceAccessor::SetMetricsAndCrashReportingForTesting(
40+
&consent_);
41+
}
42+
43+
ScopedSetMetricsConsent(const ScopedSetMetricsConsent&) = delete;
44+
ScopedSetMetricsConsent& operator=(const ScopedSetMetricsConsent&) = delete;
45+
46+
~ScopedSetMetricsConsent() {
47+
ChromeMetricsServiceAccessor::SetMetricsAndCrashReportingForTesting(
48+
nullptr);
49+
}
50+
51+
private:
52+
const bool consent_;
53+
};
54+
55+
class HatsServiceBrowserTestBase : public InProcessBrowserTest {
56+
protected:
57+
explicit HatsServiceBrowserTestBase(
58+
std::vector<base::test::ScopedFeatureList::FeatureAndParams>
59+
enabled_features)
60+
: enabled_features_(enabled_features) {
61+
scoped_feature_list_.InitWithFeaturesAndParameters(enabled_features_, {});
62+
}
63+
64+
HatsServiceBrowserTestBase() = default;
65+
66+
HatsServiceBrowserTestBase(const HatsServiceBrowserTestBase&) = delete;
67+
HatsServiceBrowserTestBase& operator=(const HatsServiceBrowserTestBase&) =
68+
delete;
69+
70+
~HatsServiceBrowserTestBase() override = default;
71+
72+
HatsService* GetHatsService() {
73+
HatsService* service =
74+
HatsServiceFactory::GetForProfile(browser()->profile(), true);
75+
return service;
76+
}
77+
78+
void SetMetricsConsent(bool consent) {
79+
scoped_metrics_consent_.emplace(consent);
80+
}
81+
82+
bool HatsNextDialogCreated() {
83+
return GetHatsService()->hats_next_dialog_exists_for_testing();
84+
}
85+
86+
private:
87+
absl::optional<ScopedSetMetricsConsent> scoped_metrics_consent_;
88+
89+
base::test::ScopedFeatureList scoped_feature_list_;
90+
91+
std::vector<base::test::ScopedFeatureList::FeatureAndParams>
92+
enabled_features_;
93+
};
94+
95+
class HatsServiceProbabilityOne : public HatsServiceBrowserTestBase {
96+
public:
97+
HatsServiceProbabilityOne(const HatsServiceProbabilityOne&) = delete;
98+
HatsServiceProbabilityOne& operator=(const HatsServiceProbabilityOne&) =
99+
delete;
100+
101+
protected:
102+
HatsServiceProbabilityOne()
103+
: HatsServiceBrowserTestBase({settings_probability_one}) {}
104+
105+
~HatsServiceProbabilityOne() override = default;
106+
107+
private:
108+
void SetUpOnMainThread() override {
109+
host_resolver()->AddRule("*", "127.0.0.1");
110+
// Set the profile creation time to be old enough to ensure triggering.
111+
browser()->profile()->SetCreationTimeForTesting(base::Time::Now() -
112+
base::Days(45));
113+
}
114+
115+
void TearDownOnMainThread() override {
116+
GetHatsService()->SetSurveyMetadataForTesting({});
117+
}
118+
};
119+
120+
} // namespace
121+
122+
IN_PROC_BROWSER_TEST_F(HatsServiceBrowserTestBase, BubbleNotShownOnDefault) {
123+
GetHatsService()->LaunchSurvey(kHatsSurveyTriggerSettings);
124+
EXPECT_FALSE(HatsNextDialogCreated());
125+
}
126+
127+
IN_PROC_BROWSER_TEST_F(HatsServiceProbabilityOne,
128+
BubbleNotShownOnShowingConditionsMet) {
129+
SetMetricsConsent(true);
130+
ASSERT_TRUE(
131+
g_browser_process->GetMetricsServicesManager()->IsMetricsConsentGiven());
132+
GetHatsService()->LaunchSurvey(kHatsSurveyTriggerSettings);
133+
EXPECT_FALSE(HatsNextDialogCreated());
134+
}

test/BUILD.gn

+2
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,7 @@ if (!is_android) {
677677
"//brave/browser/ui/webui/brave_welcome_ui_browsertest.cc",
678678
"//brave/browser/ui/webui/new_tab_page/brave_new_tab_ui_browsertest.cc",
679679
"//brave/chromium_src/chrome/browser/safe_browsing/download_protection/check_client_download_request_base_browsertest.cc",
680+
"//brave/chromium_src/chrome/browser/ui/hats/hats_service_browsertest.cc",
680681
"//brave/chromium_src/chrome/browser/ui/views/location_bar/location_bar_view_browsertest.cc",
681682
"//brave/chromium_src/chrome/browser/ui/views/tabs/tab_hover_card_bubble_view_browsertest.cc",
682683
"//brave/chromium_src/components/content_settings/core/browser/brave_content_settings_registry_browsertest.cc",
@@ -768,6 +769,7 @@ if (!is_android) {
768769
"//brave/vendor/bat-native-ledger",
769770
"//brave/vendor/bat-native-ledger:publishers_proto",
770771
"//chrome/browser",
772+
"//components/metrics_services_manager",
771773
"//components/optimization_guide/core",
772774
"//components/reading_list/features:flags",
773775
"//components/security_interstitials/content:security_interstitial_page",

0 commit comments

Comments
 (0)