diff options
author | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2020-10-12 14:27:29 +0200 |
---|---|---|
committer | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2020-10-13 09:35:20 +0000 |
commit | c30a6232df03e1efbd9f3b226777b07e087a1122 (patch) | |
tree | e992f45784689f373bcc38d1b79a239ebe17ee23 /chromium/weblayer/browser/ukm_browsertest.cc | |
parent | 7b5b123ac58f58ffde0f4f6e488bcd09aa4decd3 (diff) | |
download | qtwebengine-chromium-85-based.tar.gz |
BASELINE: Update Chromium to 85.0.4183.14085-based
Change-Id: Iaa42f4680837c57725b1344f108c0196741f6057
Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
Diffstat (limited to 'chromium/weblayer/browser/ukm_browsertest.cc')
-rw-r--r-- | chromium/weblayer/browser/ukm_browsertest.cc | 158 |
1 files changed, 158 insertions, 0 deletions
diff --git a/chromium/weblayer/browser/ukm_browsertest.cc b/chromium/weblayer/browser/ukm_browsertest.cc new file mode 100644 index 00000000000..52b734a4559 --- /dev/null +++ b/chromium/weblayer/browser/ukm_browsertest.cc @@ -0,0 +1,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 |