1 // Copyright (c) 2006-2008 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 "chrome/browser/rlz/rlz.h" 6 7 #include "base/path_service.h" 8 #include "base/win/registry.h" 9 #include "testing/gtest/include/gtest/gtest.h" 10 11 using base::win::RegKey; 12 13 namespace { 14 15 // Gets rid of registry leftovers from testing. Returns false if there 16 // is nothing to clean. 17 bool CleanValue(const wchar_t* key_name, const wchar_t* value) { 18 RegKey key; 19 if (key.Open(HKEY_CURRENT_USER, key_name, KEY_READ | KEY_WRITE) != 20 ERROR_SUCCESS) 21 return false; 22 EXPECT_EQ(ERROR_SUCCESS, key.DeleteValue(value)); 23 return true; 24 } 25 26 // The chrome events RLZ key lives here. 27 const wchar_t kKeyName[] = L"Software\\Google\\Common\\Rlz\\Events\\C"; 28 29 } // namespace 30 31 TEST(RlzLibTest, RecordProductEvent) { 32 DWORD recorded_value = 0; 33 EXPECT_TRUE(RLZTracker::RecordProductEvent(rlz_lib::CHROME, 34 rlz_lib::CHROME_OMNIBOX, rlz_lib::FIRST_SEARCH)); 35 const wchar_t kEvent1[] = L"C1F"; 36 RegKey key1; 37 EXPECT_EQ(ERROR_SUCCESS, key1.Open(HKEY_CURRENT_USER, kKeyName, KEY_READ)); 38 EXPECT_EQ(ERROR_SUCCESS, key1.ReadValueDW(kEvent1, &recorded_value)); 39 EXPECT_EQ(1, recorded_value); 40 EXPECT_TRUE(CleanValue(kKeyName, kEvent1)); 41 42 EXPECT_TRUE(RLZTracker::RecordProductEvent(rlz_lib::CHROME, 43 rlz_lib::CHROME_HOME_PAGE, rlz_lib::SET_TO_GOOGLE)); 44 const wchar_t kEvent2[] = L"C2S"; 45 RegKey key2; 46 EXPECT_EQ(ERROR_SUCCESS, key2.Open(HKEY_CURRENT_USER, kKeyName, KEY_READ)); 47 DWORD value = 0; 48 EXPECT_EQ(ERROR_SUCCESS, key2.ReadValueDW(kEvent2, &recorded_value)); 49 EXPECT_EQ(1, recorded_value); 50 EXPECT_TRUE(CleanValue(kKeyName, kEvent2)); 51 } 52 53 TEST(RlzLibTest, CleanProductEvents) { 54 DWORD recorded_value = 0; 55 EXPECT_TRUE(RLZTracker::RecordProductEvent(rlz_lib::CHROME, 56 rlz_lib::CHROME_OMNIBOX, rlz_lib::FIRST_SEARCH)); 57 const wchar_t kEvent1[] = L"C1F"; 58 RegKey key1; 59 EXPECT_EQ(ERROR_SUCCESS, key1.Open(HKEY_CURRENT_USER, kKeyName, KEY_READ)); 60 EXPECT_EQ(ERROR_SUCCESS, key1.ReadValueDW(kEvent1, &recorded_value)); 61 EXPECT_EQ(1, recorded_value); 62 63 EXPECT_TRUE(RLZTracker::ClearAllProductEvents(rlz_lib::CHROME)); 64 EXPECT_FALSE(CleanValue(kKeyName, kEvent1)); 65 } 66