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