Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(506)

Side by Side Diff: chrome/browser/metrics/antivirus_metrics_provider_win_unittest.cc

Issue 2484613002: Migrate more tests to ScopedFeatureList. (Closed)
Patch Set: rebase Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/metrics/antivirus_metrics_provider_win.h" 5 #include "chrome/browser/metrics/antivirus_metrics_provider_win.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/feature_list.h"
11 #include "base/memory/ref_counted.h" 10 #include "base/memory/ref_counted.h"
12 #include "base/memory/weak_ptr.h" 11 #include "base/memory/weak_ptr.h"
13 #include "base/run_loop.h" 12 #include "base/run_loop.h"
14 #include "base/strings/sys_string_conversions.h" 13 #include "base/strings/sys_string_conversions.h"
15 #include "base/test/histogram_tester.h" 14 #include "base/test/histogram_tester.h"
15 #include "base/test/scoped_feature_list.h"
16 #include "base/threading/thread_checker.h" 16 #include "base/threading/thread_checker.h"
17 #include "base/threading/thread_restrictions.h" 17 #include "base/threading/thread_restrictions.h"
18 #include "base/version.h" 18 #include "base/version.h"
19 #include "base/win/windows_version.h" 19 #include "base/win/windows_version.h"
20 #include "components/variations/metrics_util.h" 20 #include "components/variations/metrics_util.h"
21 #include "content/public/browser/browser_thread.h" 21 #include "content/public/browser/browser_thread.h"
22 #include "content/public/test/test_browser_thread_bundle.h" 22 #include "content/public/test/test_browser_thread_bundle.h"
23 #include "content/public/test/test_utils.h" 23 #include "content/public/test/test_utils.h"
24 #include "testing/gtest/include/gtest/gtest.h" 24 #include "testing/gtest/include/gtest/gtest.h"
25 25
26 namespace { 26 namespace {
27 27
28 // Helper function to toggle whether the ReportFullAVProductDetails feature is
29 // enabled or not.
30 void SetFullNamesFeatureEnabled(bool enabled) {
31 base::FeatureList::ClearInstanceForTesting();
32 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList);
33 if (enabled) {
34 feature_list->InitializeFromCommandLine(
35 AntiVirusMetricsProvider::kReportNamesFeature.name, std::string());
36 } else {
37 feature_list->InitializeFromCommandLine(
38 std::string(), AntiVirusMetricsProvider::kReportNamesFeature.name);
39 }
40 base::FeatureList::SetInstance(std::move(feature_list));
41 }
42
43 void VerifySystemProfileData(const metrics::SystemProfileProto& system_profile, 28 void VerifySystemProfileData(const metrics::SystemProfileProto& system_profile,
44 bool expect_unhashed_value) { 29 bool expect_unhashed_value) {
45 const char kWindowsDefender[] = "Windows Defender"; 30 const char kWindowsDefender[] = "Windows Defender";
46 31
47 if (base::win::GetVersion() >= base::win::VERSION_WIN8) { 32 if (base::win::GetVersion() >= base::win::VERSION_WIN8) {
48 bool defender_found = false; 33 bool defender_found = false;
49 for (const auto& av : system_profile.antivirus_product()) { 34 for (const auto& av : system_profile.antivirus_product()) {
50 if (av.product_name_hash() == metrics::HashName(kWindowsDefender)) { 35 if (av.product_name_hash() == metrics::HashName(kWindowsDefender)) {
51 defender_found = true; 36 defender_found = true;
52 if (expect_unhashed_value) { 37 if (expect_unhashed_value) {
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
89 74
90 VerifySystemProfileData(system_profile, expect_unhashed_value_); 75 VerifySystemProfileData(system_profile, expect_unhashed_value_);
91 // This looks weird, but it's to make sure that reading the data out of the 76 // This looks weird, but it's to make sure that reading the data out of the
92 // AntiVirusMetricsProvider does not invalidate it, as the class should be 77 // AntiVirusMetricsProvider does not invalidate it, as the class should be
93 // resilient to this. 78 // resilient to this.
94 system_profile.Clear(); 79 system_profile.Clear();
95 provider_->ProvideSystemProfileMetrics(&system_profile); 80 provider_->ProvideSystemProfileMetrics(&system_profile);
96 VerifySystemProfileData(system_profile, expect_unhashed_value_); 81 VerifySystemProfileData(system_profile, expect_unhashed_value_);
97 } 82 }
98 83
84 // Helper function to toggle whether the ReportFullAVProductDetails feature is
85 // enabled or not.
86 void SetFullNamesFeatureEnabled(bool enabled) {
87 if (enabled) {
88 scoped_feature_list_.InitAndEnableFeature(
89 AntiVirusMetricsProvider::kReportNamesFeature);
90 } else {
91 scoped_feature_list_.InitAndDisableFeature(
92 AntiVirusMetricsProvider::kReportNamesFeature);
93 }
94 }
95
99 bool got_results_; 96 bool got_results_;
100 bool expect_unhashed_value_; 97 bool expect_unhashed_value_;
101 std::unique_ptr<AntiVirusMetricsProvider> provider_; 98 std::unique_ptr<AntiVirusMetricsProvider> provider_;
102 content::TestBrowserThreadBundle thread_bundle_; 99 content::TestBrowserThreadBundle thread_bundle_;
100 base::test::ScopedFeatureList scoped_feature_list_;
103 base::RunLoop run_loop_; 101 base::RunLoop run_loop_;
104 base::ThreadChecker thread_checker_; 102 base::ThreadChecker thread_checker_;
105 base::WeakPtrFactory<AntiVirusMetricsProviderTest> weak_ptr_factory_; 103 base::WeakPtrFactory<AntiVirusMetricsProviderTest> weak_ptr_factory_;
106 104
107 private: 105 private:
108 DISALLOW_COPY_AND_ASSIGN(AntiVirusMetricsProviderTest); 106 DISALLOW_COPY_AND_ASSIGN(AntiVirusMetricsProviderTest);
109 }; 107 };
110 108
111 TEST_P(AntiVirusMetricsProviderTest, GetMetricsFullName) { 109 TEST_P(AntiVirusMetricsProviderTest, GetMetricsFullName) {
112 ASSERT_TRUE(thread_checker_.CalledOnValidThread()); 110 ASSERT_TRUE(thread_checker_.CalledOnValidThread());
(...skipping 12 matching lines...) Expand all
125 AntiVirusMetricsProvider::ResultCode expected_result = 123 AntiVirusMetricsProvider::ResultCode expected_result =
126 AntiVirusMetricsProvider::RESULT_SUCCESS; 124 AntiVirusMetricsProvider::RESULT_SUCCESS;
127 if (base::win::OSInfo::GetInstance()->version_type() == 125 if (base::win::OSInfo::GetInstance()->version_type() ==
128 base::win::SUITE_SERVER) 126 base::win::SUITE_SERVER)
129 expected_result = AntiVirusMetricsProvider::RESULT_WSC_NOT_AVAILABLE; 127 expected_result = AntiVirusMetricsProvider::RESULT_WSC_NOT_AVAILABLE;
130 histograms.ExpectUniqueSample("UMA.AntiVirusMetricsProvider.Result", 128 histograms.ExpectUniqueSample("UMA.AntiVirusMetricsProvider.Result",
131 expected_result, 1); 129 expected_result, 1);
132 } 130 }
133 131
134 INSTANTIATE_TEST_CASE_P(, AntiVirusMetricsProviderTest, ::testing::Bool()); 132 INSTANTIATE_TEST_CASE_P(, AntiVirusMetricsProviderTest, ::testing::Bool());
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698