Commit 669d4dc2 authored by Brian White's avatar Brian White Committed by Commit Bot

Added test for deletion of metics files when not being uploaded.

There are three cases:
- metrics consent is not given
- consent is given but sampled-out
- consent is given and sampled-in

Bug: 760317
Change-Id: Ie262dfeaba161069b19b5d91659054b136571315
Reviewed-on: https://chromium-review.googlesource.com/996034
Commit-Queue: Brian White <bcwhite@chromium.org>
Reviewed-by: default avatarAlexei Svitkine <asvitkine@chromium.org>
Cr-Commit-Position: refs/heads/master@{#549232}
parent a0e4387e
......@@ -10,12 +10,19 @@
#include <string>
#include "base/command_line.h"
#include "base/files/file_enumerator.h"
#include "base/files/file_path.h"
#include "base/path_service.h"
#include "base/process/memory.h"
#include "base/test/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/scoped_task_environment.h"
#include "base/threading/thread_restrictions.h"
#include "build/build_config.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/metrics/chrome_metrics_service_accessor.h"
#include "chrome/browser/metrics/chrome_metrics_service_client.h"
#include "chrome/browser/metrics/chrome_metrics_services_manager_client.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/chrome_paths.h"
......@@ -23,7 +30,9 @@
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/metrics/metrics_pref_names.h"
#include "components/metrics/metrics_reporting_default_state.h"
#include "components/metrics/metrics_switches.h"
#include "components/prefs/json_pref_store.h"
#include "components/prefs/pref_service.h"
#include "content/public/test/browser_test_utils.h"
#include "net/base/filename_util.h"
......@@ -228,3 +237,160 @@ IN_PROC_BROWSER_TEST_F(MetricsServiceBrowserTest, OOMRenderers) {
histogram_tester.ExpectUniqueSample("Tabs.SadTab.OomCreated", 1, 1);
}
#endif // OS_WIN && !ADDRESS_SANITIZER
// Base class for testing if browser-metrics files get removed or not.
// The code under tests is run before any actual test methods so the test
// conditions must be created during SetUp in order to affect said code.
class MetricsServiceBrowserFilesTest : public InProcessBrowserTest {
using super = InProcessBrowserTest;
public:
MetricsServiceBrowserFilesTest() {}
bool SetUpUserDataDirectory() override {
if (!super::SetUpUserDataDirectory())
return false;
base::ScopedAllowBlockingForTesting allow_blocking;
base::FilePath user_dir;
CHECK(base::PathService::Get(chrome::DIR_USER_DATA, &user_dir));
// Create a local-state file with what we want the browser to use. This
// has to be done here because there is no hook between when the browser
// is initialized and the metrics-client acts on the pref values. The
// "Local State" directory is hard-coded because the FILE_LOCAL_STATE
// path is not yet defined at this point.
{
base::test::ScopedTaskEnvironment task_env;
auto state = base::MakeRefCounted<JsonPrefStore>(
user_dir.Append(FILE_PATH_LITERAL("Local State")));
state->SetValue(
metrics::prefs::kMetricsDefaultOptIn,
std::make_unique<base::Value>(metrics::EnableMetricsDefault::OPT_OUT),
0);
}
// Create the upload dir. Note that ASSERT macros won't fail in SetUp,
// hence the use of CHECK.
upload_dir_ =
user_dir.AppendASCII(ChromeMetricsServiceClient::kBrowserMetricsName);
CHECK(!base::PathExists(upload_dir_));
CHECK(base::CreateDirectory(upload_dir_));
// Create a file inside the upload dir that can be watched to see if an
// attempt was made to delete everything.
base::File upload_file(
upload_dir().AppendASCII("foo.bar"),
base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE);
CHECK_EQ(6, upload_file.WriteAtCurrentPos("foobar", 6));
return true;
}
void SetUp() override {
ChromeMetricsServiceAccessor::SetMetricsAndCrashReportingForTesting(
&metrics_consent_);
super::SetUp();
}
// Check for the existence of any non-pma files that were created as part
// of the test. PMA files may be created as part of the browser setup and
// cannot be deleted while open on all operating systems.
bool HasNonPMAFiles() {
base::ScopedAllowBlockingForTesting allow_blocking;
if (!base::PathExists(upload_dir_))
return false;
base::FileEnumerator file_iter(upload_dir_, true,
base::FileEnumerator::FILES);
while (!file_iter.Next().empty()) {
if (file_iter.GetInfo().GetName().Extension() !=
FILE_PATH_LITERAL(".pma")) {
return true;
}
}
return false;
}
base::FilePath& upload_dir() { return upload_dir_; }
void set_metrics_consent(bool enabled) { metrics_consent_ = enabled; }
private:
bool metrics_consent_ = true;
base::FilePath upload_dir_;
DISALLOW_COPY_AND_ASSIGN(MetricsServiceBrowserFilesTest);
};
// Specific class for testing when metrics upload is fully enabled.
class MetricsServiceBrowserDoUploadTest
: public MetricsServiceBrowserFilesTest {
public:
MetricsServiceBrowserDoUploadTest() {}
void SetUp() override {
set_metrics_consent(true);
feature_list_.InitAndEnableFeature(
metrics::internal::kMetricsReportingFeature);
MetricsServiceBrowserFilesTest::SetUp();
}
private:
base::test::ScopedFeatureList feature_list_;
DISALLOW_COPY_AND_ASSIGN(MetricsServiceBrowserDoUploadTest);
};
IN_PROC_BROWSER_TEST_F(MetricsServiceBrowserDoUploadTest, FilesRemain) {
// SetUp() has provided consent and made metrics "sampled-in" (enabled).
EXPECT_TRUE(HasNonPMAFiles());
}
// Specific class for testing when metrics upload is explicitly disabled.
class MetricsServiceBrowserNoUploadTest
: public MetricsServiceBrowserFilesTest {
public:
MetricsServiceBrowserNoUploadTest() {}
void SetUp() override {
set_metrics_consent(false);
feature_list_.InitAndEnableFeature(
metrics::internal::kMetricsReportingFeature);
MetricsServiceBrowserFilesTest::SetUp();
}
private:
base::test::ScopedFeatureList feature_list_;
DISALLOW_COPY_AND_ASSIGN(MetricsServiceBrowserNoUploadTest);
};
IN_PROC_BROWSER_TEST_F(MetricsServiceBrowserNoUploadTest, FilesRemoved) {
// SetUp() has removed consent and made metrics "sampled-in" (enabled).
EXPECT_FALSE(HasNonPMAFiles());
}
// Specific class for testing when metrics upload is disabled by sampling.
class MetricsServiceBrowserSampledOutTest
: public MetricsServiceBrowserFilesTest {
public:
MetricsServiceBrowserSampledOutTest() {}
void SetUp() override {
set_metrics_consent(true);
feature_list_.InitAndDisableFeature(
metrics::internal::kMetricsReportingFeature);
MetricsServiceBrowserFilesTest::SetUp();
}
private:
base::test::ScopedFeatureList feature_list_;
DISALLOW_COPY_AND_ASSIGN(MetricsServiceBrowserSampledOutTest);
};
IN_PROC_BROWSER_TEST_F(MetricsServiceBrowserSampledOutTest, FilesRemoved) {
// SetUp() has provided consent and made metrics "sampled-out" (disabled).
EXPECT_FALSE(HasNonPMAFiles());
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment