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. 4 5 #include <ctype.h> 6 #include <string> 7 8 #include "base/command_line.h" 9 #include "base/message_loop/message_loop.h" 10 #include "chrome/browser/metrics/metrics_service.h" 11 #include "chrome/common/chrome_switches.h" 12 #include "chrome/common/pref_names.h" 13 #include "chrome/test/base/scoped_testing_local_state.h" 14 #include "chrome/test/base/testing_browser_process.h" 15 #include "content/public/common/process_type.h" 16 #include "content/public/test/test_browser_thread.h" 17 #include "testing/gtest/include/gtest/gtest.h" 18 19 namespace { 20 21 class MetricsServiceTest : public testing::Test { 22 public: 23 MetricsServiceTest() 24 : ui_thread_(content::BrowserThread::UI, &message_loop_), 25 testing_local_state_(TestingBrowserProcess::GetGlobal()) { 26 } 27 28 PrefService* GetLocalState() { 29 return testing_local_state_.Get(); 30 } 31 32 private: 33 base::MessageLoopForUI message_loop_; 34 content::TestBrowserThread ui_thread_; 35 ScopedTestingLocalState testing_local_state_; 36 37 DISALLOW_COPY_AND_ASSIGN(MetricsServiceTest); 38 }; 39 40 } // namespace 41 42 // Ensure the ClientId is formatted as expected. 43 TEST_F(MetricsServiceTest, ClientIdCorrectlyFormatted) { 44 std::string clientid = MetricsService::GenerateClientID(); 45 EXPECT_EQ(36U, clientid.length()); 46 47 for (size_t i = 0; i < clientid.length(); ++i) { 48 char current = clientid[i]; 49 if (i == 8 || i == 13 || i == 18 || i == 23) 50 EXPECT_EQ('-', current); 51 else 52 EXPECT_TRUE(isxdigit(current)); 53 } 54 } 55 56 TEST_F(MetricsServiceTest, IsPluginProcess) { 57 EXPECT_TRUE( 58 MetricsService::IsPluginProcess(content::PROCESS_TYPE_PLUGIN)); 59 EXPECT_TRUE( 60 MetricsService::IsPluginProcess(content::PROCESS_TYPE_PPAPI_PLUGIN)); 61 EXPECT_FALSE( 62 MetricsService::IsPluginProcess(content::PROCESS_TYPE_GPU)); 63 } 64 65 TEST_F(MetricsServiceTest, LowEntropySource0NotReset) { 66 MetricsService service; 67 68 // Get the low entropy source once, to initialize it. 69 service.GetLowEntropySource(); 70 71 // Now, set it to 0 and ensure it doesn't get reset. 72 service.low_entropy_source_ = 0; 73 EXPECT_EQ(0, service.GetLowEntropySource()); 74 // Call it another time, just to make sure. 75 EXPECT_EQ(0, service.GetLowEntropySource()); 76 } 77 78 TEST_F(MetricsServiceTest, PermutedEntropyCacheClearedWhenLowEntropyReset) { 79 const PrefService::Preference* low_entropy_pref = 80 GetLocalState()->FindPreference(prefs::kMetricsLowEntropySource); 81 const char* kCachePrefName = prefs::kMetricsPermutedEntropyCache; 82 int low_entropy_value = -1; 83 84 // First, generate an initial low entropy source value. 85 { 86 EXPECT_TRUE(low_entropy_pref->IsDefaultValue()); 87 88 MetricsService service; 89 service.GetLowEntropySource(); 90 91 EXPECT_FALSE(low_entropy_pref->IsDefaultValue()); 92 EXPECT_TRUE(low_entropy_pref->GetValue()->GetAsInteger(&low_entropy_value)); 93 } 94 95 // Now, set a dummy value in the permuted entropy cache pref and verify that 96 // another call to GetLowEntropySource() doesn't clobber it when 97 // --reset-variation-state wasn't specified. 98 { 99 GetLocalState()->SetString(kCachePrefName, "test"); 100 101 MetricsService service; 102 service.GetLowEntropySource(); 103 104 EXPECT_EQ("test", GetLocalState()->GetString(kCachePrefName)); 105 EXPECT_EQ(low_entropy_value, 106 GetLocalState()->GetInteger(prefs::kMetricsLowEntropySource)); 107 } 108 109 // Verify that the cache does get reset if --reset-variations-state is passed. 110 { 111 CommandLine::ForCurrentProcess()->AppendSwitch( 112 switches::kResetVariationState); 113 114 MetricsService service; 115 service.GetLowEntropySource(); 116 117 EXPECT_TRUE(GetLocalState()->GetString(kCachePrefName).empty()); 118 } 119 } 120