| Index: chrome/browser/metrics/metrics_service_unittest.cc
|
| diff --git a/chrome/browser/metrics/metrics_service_unittest.cc b/chrome/browser/metrics/metrics_service_unittest.cc
|
| index f0960bf27df60c046ebcb09d91bf5675513999c6..aebb05ad949d7302e066ca74806a8c07a2dd9eb8 100644
|
| --- a/chrome/browser/metrics/metrics_service_unittest.cc
|
| +++ b/chrome/browser/metrics/metrics_service_unittest.cc
|
| @@ -7,7 +7,9 @@
|
| #include <string>
|
|
|
| #include "base/bind.h"
|
| +#include "base/prefs/testing_pref_service.h"
|
| #include "base/threading/platform_thread.h"
|
| +#include "chrome/browser/google/google_util.h"
|
| #include "chrome/browser/metrics/metrics_state_manager.h"
|
| #include "chrome/common/pref_names.h"
|
| #include "chrome/test/base/scoped_testing_local_state.h"
|
| @@ -26,8 +28,9 @@ using metrics::MetricsLogManager;
|
| class TestMetricsService : public MetricsService {
|
| public:
|
| TestMetricsService(metrics::MetricsStateManager* state_manager,
|
| - metrics::MetricsServiceClient* client)
|
| - : MetricsService(state_manager, client) {}
|
| + metrics::MetricsServiceClient* client,
|
| + PrefService* local_state)
|
| + : MetricsService(state_manager, client, local_state) {}
|
| virtual ~TestMetricsService() {}
|
|
|
| using MetricsService::log_manager;
|
| @@ -40,9 +43,13 @@ class TestMetricsLog : public MetricsLog {
|
| public:
|
| TestMetricsLog(const std::string& client_id,
|
| int session_id,
|
| - metrics::MetricsServiceClient* client)
|
| - : MetricsLog(client_id, session_id, MetricsLog::ONGOING_LOG, client) {
|
| - }
|
| + metrics::MetricsServiceClient* client,
|
| + PrefService* local_state)
|
| + : MetricsLog(client_id,
|
| + session_id,
|
| + MetricsLog::ONGOING_LOG,
|
| + client,
|
| + local_state) {}
|
|
|
| virtual ~TestMetricsLog() {}
|
|
|
| @@ -52,27 +59,24 @@ class TestMetricsLog : public MetricsLog {
|
|
|
| class MetricsServiceTest : public testing::Test {
|
| public:
|
| - MetricsServiceTest()
|
| - : testing_local_state_(TestingBrowserProcess::GetGlobal()),
|
| - is_metrics_reporting_enabled_(false),
|
| - metrics_state_manager_(
|
| - metrics::MetricsStateManager::Create(
|
| - GetLocalState(),
|
| - base::Bind(&MetricsServiceTest::is_metrics_reporting_enabled,
|
| - base::Unretained(this)))) {
|
| + MetricsServiceTest() : is_metrics_reporting_enabled_(false) {
|
| + MetricsService::RegisterPrefs(testing_local_state_.registry());
|
| + metrics_state_manager_ = metrics::MetricsStateManager::Create(
|
| + GetLocalState(),
|
| + base::Bind(&MetricsServiceTest::is_metrics_reporting_enabled,
|
| + base::Unretained(this)));
|
| }
|
|
|
| virtual ~MetricsServiceTest() {
|
| - MetricsService::SetExecutionPhase(MetricsService::UNINITIALIZED_PHASE);
|
| + MetricsService::SetExecutionPhase(MetricsService::UNINITIALIZED_PHASE,
|
| + GetLocalState());
|
| }
|
|
|
| metrics::MetricsStateManager* GetMetricsStateManager() {
|
| return metrics_state_manager_.get();
|
| }
|
|
|
| - PrefService* GetLocalState() {
|
| - return testing_local_state_.Get();
|
| - }
|
| + PrefService* GetLocalState() { return &testing_local_state_; }
|
|
|
| // Sets metrics reporting as enabled for testing.
|
| void EnableMetricsReporting() {
|
| @@ -110,8 +114,8 @@ class MetricsServiceTest : public testing::Test {
|
| }
|
|
|
| content::TestBrowserThreadBundle thread_bundle_;
|
| - ScopedTestingLocalState testing_local_state_;
|
| bool is_metrics_reporting_enabled_;
|
| + TestingPrefServiceSimple testing_local_state_;
|
| scoped_ptr<metrics::MetricsStateManager> metrics_state_manager_;
|
|
|
| DISALLOW_COPY_AND_ASSIGN(MetricsServiceTest);
|
| @@ -140,7 +144,8 @@ TEST_F(MetricsServiceTest, InitialStabilityLogAfterCleanShutDown) {
|
| GetLocalState()->SetBoolean(prefs::kStabilityExitedCleanly, true);
|
|
|
| metrics::TestMetricsServiceClient client;
|
| - TestMetricsService service(GetMetricsStateManager(), &client);
|
| + TestMetricsService service(
|
| + GetMetricsStateManager(), &client, GetLocalState());
|
| service.InitializeMetricsRecordingState();
|
| // No initial stability log should be generated.
|
| EXPECT_FALSE(service.log_manager()->has_unsent_logs());
|
| @@ -148,28 +153,35 @@ TEST_F(MetricsServiceTest, InitialStabilityLogAfterCleanShutDown) {
|
| }
|
|
|
| TEST_F(MetricsServiceTest, InitialStabilityLogAfterCrash) {
|
| + // TODO(asvitkine): Eliminate using |testing_local_state| in favor of using
|
| + // |GetLocalState()| once MetricsService no longer internally creates metrics
|
| + // providers that rely on g_browser_process->local_state() being correctly
|
| + // set up. crbug.com/375776.
|
| + ScopedTestingLocalState testing_local_state(
|
| + TestingBrowserProcess::GetGlobal());
|
| + TestingPrefServiceSimple* local_state = testing_local_state.Get();
|
| EnableMetricsReporting();
|
| - GetLocalState()->ClearPref(prefs::kStabilityExitedCleanly);
|
| + local_state->ClearPref(prefs::kStabilityExitedCleanly);
|
|
|
| // Set up prefs to simulate restarting after a crash.
|
|
|
| // Save an existing system profile to prefs, to correspond to what would be
|
| // saved from a previous session.
|
| metrics::TestMetricsServiceClient client;
|
| - TestMetricsLog log("client", 1, &client);
|
| + TestMetricsLog log("client", 1, &client, local_state);
|
| log.RecordEnvironment(std::vector<metrics::MetricsProvider*>(),
|
| std::vector<variations::ActiveGroupId>());
|
|
|
| // Record stability build time and version from previous session, so that
|
| // stability metrics (including exited cleanly flag) won't be cleared.
|
| - GetLocalState()->SetInt64(prefs::kStabilityStatsBuildTime,
|
| - MetricsLog::GetBuildTime());
|
| - GetLocalState()->SetString(prefs::kStabilityStatsVersion,
|
| - client.GetVersionString());
|
| + local_state->SetInt64(prefs::kStabilityStatsBuildTime,
|
| + MetricsLog::GetBuildTime());
|
| + local_state->SetString(prefs::kStabilityStatsVersion,
|
| + client.GetVersionString());
|
|
|
| - GetLocalState()->SetBoolean(prefs::kStabilityExitedCleanly, false);
|
| + local_state->SetBoolean(prefs::kStabilityExitedCleanly, false);
|
|
|
| - TestMetricsService service(GetMetricsStateManager(), &client);
|
| + TestMetricsService service(GetMetricsStateManager(), &client, local_state);
|
| service.InitializeMetricsRecordingState();
|
|
|
| // The initial stability log should be generated and persisted in unsent logs.
|
| @@ -198,7 +210,7 @@ TEST_F(MetricsServiceTest, InitialStabilityLogAfterCrash) {
|
|
|
| TEST_F(MetricsServiceTest, RegisterSyntheticTrial) {
|
| metrics::TestMetricsServiceClient client;
|
| - MetricsService service(GetMetricsStateManager(), &client);
|
| + MetricsService service(GetMetricsStateManager(), &client, GetLocalState());
|
|
|
| // Add two synthetic trials and confirm that they show up in the list.
|
| SyntheticTrialGroup trial1(metrics::HashName("TestTrial1"),
|
| @@ -211,9 +223,12 @@ TEST_F(MetricsServiceTest, RegisterSyntheticTrial) {
|
| // Ensure that time has advanced by at least a tick before proceeding.
|
| WaitUntilTimeChanges(base::TimeTicks::Now());
|
|
|
| - service.log_manager_.BeginLoggingWithLog(
|
| - scoped_ptr<metrics::MetricsLogBase>(new MetricsLog(
|
| - "clientID", 1, MetricsLog::INITIAL_STABILITY_LOG, &client)));
|
| + service.log_manager_.BeginLoggingWithLog(scoped_ptr<metrics::MetricsLogBase>(
|
| + new MetricsLog("clientID",
|
| + 1,
|
| + MetricsLog::INITIAL_STABILITY_LOG,
|
| + &client,
|
| + GetLocalState())));
|
| // Save the time when the log was started (it's okay for this to be greater
|
| // than the time recorded by the above call since it's used to ensure the
|
| // value changes).
|
| @@ -249,8 +264,9 @@ TEST_F(MetricsServiceTest, RegisterSyntheticTrial) {
|
|
|
| // Start a new log and ensure all three trials appear in it.
|
| service.log_manager_.FinishCurrentLog();
|
| - service.log_manager_.BeginLoggingWithLog(scoped_ptr<metrics::MetricsLogBase>(
|
| - new MetricsLog("clientID", 1, MetricsLog::ONGOING_LOG, &client)));
|
| + service.log_manager_.BeginLoggingWithLog(
|
| + scoped_ptr<metrics::MetricsLogBase>(new MetricsLog(
|
| + "clientID", 1, MetricsLog::ONGOING_LOG, &client, GetLocalState())));
|
| service.GetCurrentSyntheticFieldTrials(&synthetic_trials);
|
| EXPECT_EQ(3U, synthetic_trials.size());
|
| EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial1", "Group2"));
|
| @@ -261,7 +277,7 @@ TEST_F(MetricsServiceTest, RegisterSyntheticTrial) {
|
|
|
| TEST_F(MetricsServiceTest, MetricsServiceObserver) {
|
| metrics::TestMetricsServiceClient client;
|
| - MetricsService service(GetMetricsStateManager(), &client);
|
| + MetricsService service(GetMetricsStateManager(), &client, GetLocalState());
|
| TestMetricsServiceObserver observer1;
|
| TestMetricsServiceObserver observer2;
|
|
|
|
|