1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/test/bind_test_util.h"
#include "components/ukm/ukm_test_helper.h"
#include "weblayer/browser/android/metrics/weblayer_metrics_service_client.h"
#include "weblayer/browser/profile_impl.h"
#include "weblayer/public/navigation_controller.h"
#include "weblayer/public/tab.h"
#include "weblayer/shell/android/browsertests_apk/metrics_test_helper.h"
#include "weblayer/shell/browser/shell.h"
#include "weblayer/test/weblayer_browser_test.h"
namespace weblayer {
namespace {
ProfileImpl* GetProfileByName(const std::string& name) {
for (auto* profile : ProfileImpl::GetAllProfiles()) {
if (profile->name() == name)
return profile;
}
return nullptr;
}
} // namespace
class UkmBrowserTest : public WebLayerBrowserTest {
public:
void SetUp() override {
InstallTestGmsBridge(user_consent_);
WebLayerBrowserTest::SetUp();
}
void TearDown() override {
RemoveTestGmsBridge();
WebLayerBrowserTest::TearDown();
}
ukm::UkmService* GetUkmService() {
return WebLayerMetricsServiceClient::GetInstance()->GetUkmService();
}
void disable_user_consent() { user_consent_ = false; }
private:
bool user_consent_ = true;
};
// Even if there's user consent for UMA, need to explicitly enable UKM.
IN_PROC_BROWSER_TEST_F(UkmBrowserTest, UserConsentButNotEnabled) {
ukm::UkmTestHelper ukm_test_helper(GetUkmService());
EXPECT_FALSE(ukm_test_helper.IsRecordingEnabled());
}
// UKMs are only enabled when there's user consent and it's explicitly enabled.
IN_PROC_BROWSER_TEST_F(UkmBrowserTest, Enabled) {
ukm::UkmTestHelper ukm_test_helper(GetUkmService());
GetProfile()->SetBooleanSetting(SettingType::UKM_ENABLED, true);
EXPECT_TRUE(ukm_test_helper.IsRecordingEnabled());
}
// If UKMs are disabled it's reflected accordingly.
IN_PROC_BROWSER_TEST_F(UkmBrowserTest, EnabledThenDisable) {
ukm::UkmTestHelper ukm_test_helper(GetUkmService());
GetProfile()->SetBooleanSetting(SettingType::UKM_ENABLED, true);
EXPECT_TRUE(ukm_test_helper.IsRecordingEnabled());
GetProfile()->SetBooleanSetting(SettingType::UKM_ENABLED, false);
EXPECT_FALSE(ukm_test_helper.IsRecordingEnabled());
}
// Make sure that UKM is disabled while an incognito profile is alive.
IN_PROC_BROWSER_TEST_F(UkmBrowserTest, RegularPlusIncognitoCheck) {
ukm::UkmTestHelper ukm_test_helper(GetUkmService());
GetProfile()->SetBooleanSetting(SettingType::UKM_ENABLED, true);
EXPECT_TRUE(ukm_test_helper.IsRecordingEnabled());
uint64_t original_client_id = ukm_test_helper.GetClientId();
EXPECT_NE(0U, original_client_id);
// Incognito profiles have an empty name.
CreateProfile(std::string());
EXPECT_FALSE(ukm_test_helper.IsRecordingEnabled());
// Creating another regular profile mustn't enable UKM.
CreateProfile("foo");
GetProfileByName("foo")->SetBooleanSetting(SettingType::UKM_ENABLED, true);
EXPECT_FALSE(ukm_test_helper.IsRecordingEnabled());
// Note WebLayer can only have one incognito profile so we can't test creating
// and destroying another one here.
DestroyProfile("foo");
EXPECT_FALSE(ukm_test_helper.IsRecordingEnabled());
DestroyProfile(std::string());
EXPECT_TRUE(ukm_test_helper.IsRecordingEnabled());
// Client ID should not have been reset.
EXPECT_EQ(original_client_id, ukm_test_helper.GetClientId());
}
// Make sure creating a real profile after Incognito doesn't enable UKM.
IN_PROC_BROWSER_TEST_F(UkmBrowserTest, IncognitoPlusRegularCheck) {
ukm::UkmTestHelper ukm_test_helper(GetUkmService());
GetProfile()->SetBooleanSetting(SettingType::UKM_ENABLED, true);
// Incognito profiles have an empty name.
CreateProfile(std::string());
EXPECT_FALSE(ukm_test_helper.IsRecordingEnabled());
CreateProfile("foo");
GetProfileByName("foo")->SetBooleanSetting(SettingType::UKM_ENABLED, true);
EXPECT_FALSE(ukm_test_helper.IsRecordingEnabled());
DestroyProfile(std::string());
EXPECT_TRUE(ukm_test_helper.IsRecordingEnabled());
}
class UkmDisabledBrowserTest : public UkmBrowserTest {
public:
UkmDisabledBrowserTest() { disable_user_consent(); }
};
// If there's no user consent UKMs are disabled.
IN_PROC_BROWSER_TEST_F(UkmDisabledBrowserTest, Disabled) {
ukm::UkmTestHelper ukm_test_helper(GetUkmService());
EXPECT_FALSE(ukm_test_helper.IsRecordingEnabled());
// Ensure enabling UKMs still doesn't enable it.
GetProfile()->SetBooleanSetting(SettingType::UKM_ENABLED, true);
EXPECT_FALSE(ukm_test_helper.IsRecordingEnabled());
}
class UkmIncognitoBrowserTest : public UkmBrowserTest {
public:
UkmIncognitoBrowserTest() { SetShellStartsInIncognitoMode(); }
};
// Starting with one incognito window should disable UKM.
IN_PROC_BROWSER_TEST_F(UkmIncognitoBrowserTest, Disabled) {
ukm::UkmTestHelper ukm_test_helper(GetUkmService());
// Enabling UKMs doesn't enable it because of the incognito window.
GetProfile()->SetBooleanSetting(SettingType::UKM_ENABLED, true);
EXPECT_FALSE(ukm_test_helper.IsRecordingEnabled());
}
} // namespace weblayer
|