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
3 // found in the LICENSE file.
8 #include "base/command_line.h"
9 #include "base/message_loop.h"
10 #include "chrome/browser/metrics/metrics_service.h"
11 #include "chrome/common/chrome_process_type.h"
12 #include "chrome/common/chrome_switches.h"
13 #include "chrome/common/pref_names.h"
14 #include "chrome/test/base/scoped_testing_local_state.h"
15 #include "chrome/test/base/testing_browser_process.h"
16 #include "content/public/test/test_browser_thread.h"
17 #include "testing/gtest/include/gtest/gtest.h"
21 class MetricsServiceTest
: public testing::Test
{
24 : ui_thread_(content::BrowserThread::UI
, &message_loop_
),
25 testing_local_state_(TestingBrowserProcess::GetGlobal()) {
28 PrefService
* GetLocalState() {
29 return testing_local_state_
.Get();
33 MessageLoopForUI message_loop_
;
34 content::TestBrowserThread ui_thread_
;
35 ScopedTestingLocalState testing_local_state_
;
37 DISALLOW_COPY_AND_ASSIGN(MetricsServiceTest
);
40 // Scoped helper that allows modifying CommandLine::ForCurrentProcess() by
41 // tests, restoring it to its original value when destroyed.
42 class ScopedTestingCommandLine
{
44 ScopedTestingCommandLine()
45 : old_command_line_(*CommandLine::ForCurrentProcess()) {
48 ~ScopedTestingCommandLine() {
49 *CommandLine::ForCurrentProcess() = old_command_line_
;
53 const CommandLine old_command_line_
;
55 DISALLOW_COPY_AND_ASSIGN(ScopedTestingCommandLine
);
60 // Ensure the ClientId is formatted as expected.
61 TEST_F(MetricsServiceTest
, ClientIdCorrectlyFormatted
) {
62 std::string clientid
= MetricsService::GenerateClientID();
63 EXPECT_EQ(36U, clientid
.length());
65 for (size_t i
= 0; i
< clientid
.length(); ++i
) {
66 char current
= clientid
[i
];
67 if (i
== 8 || i
== 13 || i
== 18 || i
== 23)
68 EXPECT_EQ('-', current
);
70 EXPECT_TRUE(isxdigit(current
));
74 TEST_F(MetricsServiceTest
, IsPluginProcess
) {
76 MetricsService::IsPluginProcess(content::PROCESS_TYPE_PLUGIN
));
78 MetricsService::IsPluginProcess(content::PROCESS_TYPE_PPAPI_PLUGIN
));
80 MetricsService::IsPluginProcess(content::PROCESS_TYPE_GPU
));
83 TEST_F(MetricsServiceTest
, LowEntropySource0NotReset
) {
84 MetricsService service
;
86 // Get the low entropy source once, to initialize it.
87 service
.GetLowEntropySource();
89 // Now, set it to 0 and ensure it doesn't get reset.
90 service
.low_entropy_source_
= 0;
91 EXPECT_EQ(0, service
.GetLowEntropySource());
92 // Call it another time, just to make sure.
93 EXPECT_EQ(0, service
.GetLowEntropySource());
96 TEST_F(MetricsServiceTest
, PermutedEntropyCacheClearedWhenLowEntropyReset
) {
97 const PrefService::Preference
* low_entropy_pref
=
98 GetLocalState()->FindPreference(prefs::kMetricsLowEntropySource
);
99 const char* kCachePrefName
= prefs::kMetricsPermutedEntropyCache
;
100 int low_entropy_value
= -1;
102 // First, generate an initial low entropy source value.
104 EXPECT_TRUE(low_entropy_pref
->IsDefaultValue());
106 MetricsService service
;
107 service
.GetLowEntropySource();
109 EXPECT_FALSE(low_entropy_pref
->IsDefaultValue());
110 EXPECT_TRUE(low_entropy_pref
->GetValue()->GetAsInteger(&low_entropy_value
));
113 // Now, set a dummy value in the permuted entropy cache pref and verify that
114 // another call to GetLowEntropySource() doesn't clobber it when
115 // --reset-variation-state wasn't specified.
117 GetLocalState()->SetString(kCachePrefName
, "test");
119 MetricsService service
;
120 service
.GetLowEntropySource();
122 EXPECT_EQ("test", GetLocalState()->GetString(kCachePrefName
));
123 EXPECT_EQ(low_entropy_value
,
124 GetLocalState()->GetInteger(prefs::kMetricsLowEntropySource
));
127 // Verify that the cache does get reset if --reset-variations-state is passed.
129 ScopedTestingCommandLine command_line
;
130 CommandLine::ForCurrentProcess()->AppendSwitch(
131 switches::kResetVariationState
);
133 MetricsService service
;
134 service
.GetLowEntropySource();
136 EXPECT_TRUE(GetLocalState()->GetString(kCachePrefName
).empty());