OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/metrics_service.h" | 5 #include "chrome/browser/metrics/metrics_service.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/command_line.h" |
10 #include "base/threading/platform_thread.h" | 10 #include "base/threading/platform_thread.h" |
11 #include "chrome/browser/metrics/metrics_state_manager.h" | 11 #include "chrome/browser/metrics/metrics_state_manager.h" |
| 12 #include "chrome/common/chrome_switches.h" |
12 #include "chrome/common/pref_names.h" | 13 #include "chrome/common/pref_names.h" |
13 #include "chrome/test/base/scoped_testing_local_state.h" | 14 #include "chrome/test/base/scoped_testing_local_state.h" |
14 #include "chrome/test/base/testing_browser_process.h" | 15 #include "chrome/test/base/testing_browser_process.h" |
15 #include "components/metrics/metrics_service_observer.h" | 16 #include "components/metrics/metrics_service_observer.h" |
16 #include "components/variations/metrics_util.h" | 17 #include "components/variations/metrics_util.h" |
17 #include "content/public/common/process_type.h" | 18 #include "content/public/common/process_type.h" |
18 #include "content/public/common/webplugininfo.h" | 19 #include "content/public/common/webplugininfo.h" |
19 #include "content/public/test/test_browser_thread_bundle.h" | 20 #include "content/public/test/test_browser_thread_bundle.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
21 #include "ui/gfx/size.h" | 22 #include "ui/gfx/size.h" |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
82 return 1; | 83 return 1; |
83 } | 84 } |
84 | 85 |
85 DISALLOW_COPY_AND_ASSIGN(TestMetricsLog); | 86 DISALLOW_COPY_AND_ASSIGN(TestMetricsLog); |
86 }; | 87 }; |
87 | 88 |
88 class MetricsServiceTest : public testing::Test { | 89 class MetricsServiceTest : public testing::Test { |
89 public: | 90 public: |
90 MetricsServiceTest() | 91 MetricsServiceTest() |
91 : testing_local_state_(TestingBrowserProcess::GetGlobal()), | 92 : testing_local_state_(TestingBrowserProcess::GetGlobal()), |
92 is_metrics_reporting_enabled_(false), | 93 metrics_state_manager_(metrics::MetricsStateManager::Create( |
93 metrics_state_manager_( | 94 GetLocalState())) { |
94 metrics::MetricsStateManager::Create( | |
95 GetLocalState(), | |
96 base::Bind(&MetricsServiceTest::is_metrics_reporting_enabled, | |
97 base::Unretained(this)))) { | |
98 } | 95 } |
99 | 96 |
100 virtual ~MetricsServiceTest() { | 97 virtual ~MetricsServiceTest() { |
101 MetricsService::SetExecutionPhase(MetricsService::UNINITIALIZED_PHASE); | 98 MetricsService::SetExecutionPhase(MetricsService::UNINITIALIZED_PHASE); |
102 } | 99 } |
103 | 100 |
104 metrics::MetricsStateManager* GetMetricsStateManager() { | 101 metrics::MetricsStateManager* GetMetricsStateManager() { |
105 return metrics_state_manager_.get(); | 102 return metrics_state_manager_.get(); |
106 } | 103 } |
107 | 104 |
108 PrefService* GetLocalState() { | 105 PrefService* GetLocalState() { |
109 return testing_local_state_.Get(); | 106 return testing_local_state_.Get(); |
110 } | 107 } |
111 | 108 |
112 // Sets metrics reporting as enabled for testing. | 109 // Sets metrics reporting as enabled for testing. |
113 void EnableMetricsReporting() { | 110 void EnableMetricsReporting() { |
114 is_metrics_reporting_enabled_ = true; | 111 // TODO(asvitkine): Refactor the code to not need this flag and delete it. |
| 112 CommandLine::ForCurrentProcess()->AppendSwitch( |
| 113 switches::kEnableMetricsReportingForTesting); |
115 } | 114 } |
116 | 115 |
117 // Waits until base::TimeTicks::Now() no longer equals |value|. This should | 116 // Waits until base::TimeTicks::Now() no longer equals |value|. This should |
118 // take between 1-15ms per the documented resolution of base::TimeTicks. | 117 // take between 1-15ms per the documented resolution of base::TimeTicks. |
119 void WaitUntilTimeChanges(const base::TimeTicks& value) { | 118 void WaitUntilTimeChanges(const base::TimeTicks& value) { |
120 while (base::TimeTicks::Now() == value) { | 119 while (base::TimeTicks::Now() == value) { |
121 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(1)); | 120 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(1)); |
122 } | 121 } |
123 } | 122 } |
124 | 123 |
125 // Returns true if there is a synthetic trial in the given vector that matches | 124 // Returns true if there is a synthetic trial in the given vector that matches |
126 // the given trial name and trial group; returns false otherwise. | 125 // the given trial name and trial group; returns false otherwise. |
127 bool HasSyntheticTrial( | 126 bool HasSyntheticTrial( |
128 const std::vector<variations::ActiveGroupId>& synthetic_trials, | 127 const std::vector<variations::ActiveGroupId>& synthetic_trials, |
129 const std::string& trial_name, | 128 const std::string& trial_name, |
130 const std::string& trial_group) { | 129 const std::string& trial_group) { |
131 uint32 trial_name_hash = metrics::HashName(trial_name); | 130 uint32 trial_name_hash = metrics::HashName(trial_name); |
132 uint32 trial_group_hash = metrics::HashName(trial_group); | 131 uint32 trial_group_hash = metrics::HashName(trial_group); |
133 for (std::vector<variations::ActiveGroupId>::const_iterator it = | 132 for (std::vector<variations::ActiveGroupId>::const_iterator it = |
134 synthetic_trials.begin(); | 133 synthetic_trials.begin(); |
135 it != synthetic_trials.end(); ++it) { | 134 it != synthetic_trials.end(); ++it) { |
136 if ((*it).name == trial_name_hash && (*it).group == trial_group_hash) | 135 if ((*it).name == trial_name_hash && (*it).group == trial_group_hash) |
137 return true; | 136 return true; |
138 } | 137 } |
139 return false; | 138 return false; |
140 } | 139 } |
141 | 140 |
142 private: | 141 private: |
143 bool is_metrics_reporting_enabled() const { | |
144 return is_metrics_reporting_enabled_; | |
145 } | |
146 | |
147 content::TestBrowserThreadBundle thread_bundle_; | 142 content::TestBrowserThreadBundle thread_bundle_; |
148 ScopedTestingLocalState testing_local_state_; | 143 ScopedTestingLocalState testing_local_state_; |
149 bool is_metrics_reporting_enabled_; | |
150 scoped_ptr<metrics::MetricsStateManager> metrics_state_manager_; | 144 scoped_ptr<metrics::MetricsStateManager> metrics_state_manager_; |
151 | 145 |
152 DISALLOW_COPY_AND_ASSIGN(MetricsServiceTest); | 146 DISALLOW_COPY_AND_ASSIGN(MetricsServiceTest); |
153 }; | 147 }; |
154 | 148 |
155 class TestMetricsServiceObserver : public MetricsServiceObserver { | 149 class TestMetricsServiceObserver : public MetricsServiceObserver { |
156 public: | 150 public: |
157 TestMetricsServiceObserver(): observed_(0) {} | 151 TestMetricsServiceObserver(): observed_(0) {} |
158 virtual ~TestMetricsServiceObserver() {} | 152 virtual ~TestMetricsServiceObserver() {} |
159 | 153 |
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
361 | 355 |
362 service.RemoveObserver(&observer1); | 356 service.RemoveObserver(&observer1); |
363 | 357 |
364 service.OpenNewLog(); | 358 service.OpenNewLog(); |
365 EXPECT_EQ(2, observer1.observed()); | 359 EXPECT_EQ(2, observer1.observed()); |
366 EXPECT_EQ(2, observer2.observed()); | 360 EXPECT_EQ(2, observer2.observed()); |
367 service.log_manager_.FinishCurrentLog(); | 361 service.log_manager_.FinishCurrentLog(); |
368 | 362 |
369 service.RemoveObserver(&observer2); | 363 service.RemoveObserver(&observer2); |
370 } | 364 } |
OLD | NEW |