Chromium Code Reviews| Index: chrome/browser/metrics/antivirus_metrics_provider_win_unittest.cc |
| diff --git a/chrome/browser/metrics/antivirus_metrics_provider_win_unittest.cc b/chrome/browser/metrics/antivirus_metrics_provider_win_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..42c5e19eaa5caddb2d2f3bdf13456f41fb1c7717 |
| --- /dev/null |
| +++ b/chrome/browser/metrics/antivirus_metrics_provider_win_unittest.cc |
| @@ -0,0 +1,137 @@ |
| +// Copyright 2016 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 "chrome/browser/metrics/antivirus_metrics_provider_win.h" |
| + |
| +#include <vector> |
| + |
| +#include "base/bind.h" |
| +#include "base/feature_list.h" |
| +#include "base/memory/ref_counted.h" |
| +#include "base/memory/weak_ptr.h" |
| +#include "base/message_loop/message_loop.h" |
| +#include "base/run_loop.h" |
| +#include "base/strings/sys_string_conversions.h" |
| +#include "base/threading/thread_restrictions.h" |
| +#include "base/version.h" |
| +#include "base/win/windows_version.h" |
| +#include "components/variations/metrics_util.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "content/public/test/test_browser_thread_bundle.h" |
| +#include "content/public/test/test_utils.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace { |
| + |
| +void setFeatureEnabled(bool enabled) { |
|
Alexei Svitkine (slow)
2016/06/02 21:46:57
Nit: set -> Set
Will Harris
2016/06/02 23:57:17
Done.
|
| + base::FeatureList::ClearInstanceForTesting(); |
| + std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList); |
| + if (enabled) { |
| + feature_list->InitializeFromCommandLine( |
| + AntiVirusMetricsProvider::kReportFullAvProductDetailsName, |
| + std::string()); |
| + } else { |
| + feature_list->InitializeFromCommandLine( |
| + std::string(), |
| + AntiVirusMetricsProvider::kReportFullAvProductDetailsName); |
| + } |
| + base::FeatureList::SetInstance(std::move(feature_list)); |
| +} |
| + |
| +} // namespace |
| + |
| +class AntiVirusMetricsProviderTest : public testing::Test { |
| + public: |
| + AntiVirusMetricsProviderTest() |
| + : got_results_(false), |
| + expect_unhashed_value_(false), |
| + provider_(new AntiVirusMetricsProvider( |
| + content::BrowserThread::GetMessageLoopProxyForThread( |
| + content::BrowserThread::FILE))), |
| + thread_bundle_(content::TestBrowserThreadBundle::REAL_FILE_THREAD), |
| + weak_ptr_factory_(this) {} |
| + |
| + void GetMetricsCallback() { |
| + ASSERT_TRUE(base::MessageLoop::current()->is_running()); |
| + base::MessageLoop::current()->QuitWhenIdle(); |
| + |
| + got_results_ = true; |
| + |
| + metrics::SystemProfileProto system_profile; |
| + provider_->ProvideSystemProfileMetrics(&system_profile); |
| + |
| + if (base::win::GetVersion() >= base::win::VERSION_WIN8) { |
| + bool defender_found = false; |
| + for (const auto& av : system_profile.antivirus_product()) { |
| + if (av.product_name_hash() == metrics::HashName("Windows Defender")) { |
| + defender_found = true; |
| + if (expect_unhashed_value_) { |
| + EXPECT_TRUE(av.has_product_name()); |
| + EXPECT_EQ(av.product_name(), "Windows Defender"); |
| + } else { |
| + EXPECT_FALSE(av.has_product_name()); |
| + } |
| + break; |
| + } |
| + } |
| + EXPECT_TRUE(defender_found); |
| + } |
| + } |
| + |
| + void internalRunTest(bool enabled) { |
| + expect_unhashed_value_ = enabled; |
| + setFeatureEnabled(enabled); |
| + // Make sure the I/O is happening on the FILE thread by disallowing it on |
| + // the main thread. |
| + bool previous_value = base::ThreadRestrictions::SetIOAllowed(false); |
| + provider_->GetAntiVirusMetrics( |
| + base::Bind(&AntiVirusMetricsProviderTest::GetMetricsCallback, |
| + weak_ptr_factory_.GetWeakPtr())); |
| + content::RunMessageLoop(); |
| + EXPECT_TRUE(got_results_); |
| + base::ThreadRestrictions::SetIOAllowed(previous_value); |
| + } |
| + |
| + bool got_results_; |
| + bool expect_unhashed_value_; |
| + std::unique_ptr<AntiVirusMetricsProvider> provider_; |
| + content::TestBrowserThreadBundle thread_bundle_; |
| + base::WeakPtrFactory<AntiVirusMetricsProviderTest> weak_ptr_factory_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(AntiVirusMetricsProviderTest); |
| +}; |
| + |
| +TEST_F(AntiVirusMetricsProviderTest, GetAvProducts) { |
|
Will Harris
2016/06/02 23:57:18
this test wasn't adding any value any more so I re
|
| + // Windows 8 and above ships with a built-in AV product called |
| + // "Windows Defender" so it is possible to check for this on those platforms. |
| + // |
| + // On previous versions of Windows, the testing has to be manual since the |
| + // API to add a new AV is private. |
| + if (base::win::GetVersion() >= base::win::VERSION_WIN8) { |
| + AntiVirusMetricsProvider::AvProductList products; |
| + bool result = AntiVirusMetricsProvider::FillAntiVirusProducts(&products); |
| + ASSERT_TRUE(result); |
| + |
| + bool defender_found = false; |
| + for (const auto& product : products) { |
| + if (product.product_name == L"Windows Defender") { |
|
Alexei Svitkine (slow)
2016/06/02 21:46:57
Nit: Make this a constant at the top of the file.
Will Harris
2016/06/02 23:57:18
Done, but put it closest to first use.
|
| + base::Version defender_version( |
| + base::SysWideToUTF8(product.product_version)); |
| + EXPECT_TRUE(defender_version.IsValid()); |
| + defender_found = true; |
| + break; |
| + } |
| + } |
| + |
| + EXPECT_TRUE(defender_found); |
| + } |
| +} |
| + |
| +TEST_F(AntiVirusMetricsProviderTest, GetMetricsFullName) { |
| + internalRunTest(true); |
|
Alexei Svitkine (slow)
2016/06/02 21:46:57
I think you're looking for TEST_P() - please use t
Will Harris
2016/06/02 23:57:17
I did not know about TEST_P. That's cool. Thanks!
|
| +} |
| + |
| +TEST_F(AntiVirusMetricsProviderTest, GetMetricsHashOnly) { |
| + internalRunTest(false); |
| +} |