1 // Copyright (c) 2010 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 <map> 6 7 #include "base/win/registry.h" 8 #include "chrome/browser/extensions/extension_function.h" 9 #include "chrome/browser/extensions/extension_function_dispatcher.h" 10 #include "chrome/browser/extensions/extension_apitest.h" 11 #include "chrome/browser/extensions/extension_rlz_module.h" 12 #include "chrome/common/chrome_switches.h" 13 #include "chrome/common/extensions/extension.h" 14 #include "rlz/win/lib/rlz_lib.h" 15 16 class MockRlzSendFinancialPingFunction : public RlzSendFinancialPingFunction { 17 virtual bool RunImpl(); 18 19 static int expected_count_; 20 21 public: 22 static int expected_count() { 23 return expected_count_; 24 } 25 }; 26 27 int MockRlzSendFinancialPingFunction::expected_count_ = 0; 28 29 bool MockRlzSendFinancialPingFunction::RunImpl() { 30 EXPECT_TRUE(RlzSendFinancialPingFunction::RunImpl()); 31 ++expected_count_; 32 return true; 33 } 34 35 ExtensionFunction* MockRlzSendFinancialPingFunctionFactory() { 36 return new MockRlzSendFinancialPingFunction(); 37 } 38 39 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, Rlz) { 40 CommandLine::ForCurrentProcess()->AppendSwitch( 41 switches::kEnableExperimentalExtensionApis); 42 43 // Before running the tests, clear the state of the RLZ products used. 44 rlz_lib::AccessPoint access_points[] = { 45 rlz_lib::GD_WEB_SERVER, 46 rlz_lib::GD_OUTLOOK, 47 rlz_lib::NO_ACCESS_POINT, 48 }; 49 rlz_lib::ClearProductState(rlz_lib::PINYIN_IME, access_points); 50 rlz_lib::ClearProductState(rlz_lib::DESKTOP, access_points); 51 52 // Check that the state has really been cleared. 53 base::win::RegKey key(HKEY_CURRENT_USER, 54 L"Software\\Google\\Common\\Rlz\\Events\\N", 55 KEY_READ); 56 ASSERT_FALSE(key.Valid()); 57 58 key.Open(HKEY_CURRENT_USER, L"Software\\Google\\Common\\Rlz\\Events\\D", 59 KEY_READ); 60 ASSERT_FALSE(key.Valid()); 61 62 // Mock out experimental.rlz.sendFinancialPing(). 63 ASSERT_TRUE(ExtensionFunctionDispatcher::OverrideFunction( 64 "experimental.rlz.sendFinancialPing", 65 MockRlzSendFinancialPingFunctionFactory)); 66 67 // Set the access point that the test code is expecting. 68 ASSERT_TRUE(rlz_lib::SetAccessPointRlz(rlz_lib::GD_DESKBAND, "rlz_apitest")); 69 70 // Now run all the tests. 71 ASSERT_TRUE(RunExtensionTest("rlz")) << message_; 72 73 ASSERT_EQ(3, MockRlzSendFinancialPingFunction::expected_count()); 74 ExtensionFunctionDispatcher::ResetFunctions(); 75 76 // Now make sure we recorded what was expected. If the code in test.js 77 // changes, need to make appropriate changes here. 78 key.Open(HKEY_CURRENT_USER, L"Software\\Google\\Common\\Rlz\\Events\\N", 79 KEY_READ); 80 ASSERT_TRUE(key.Valid()); 81 82 DWORD value; 83 ASSERT_EQ(ERROR_SUCCESS, key.ReadValueDW(L"D3I", &value)); 84 ASSERT_EQ(1, value); 85 ASSERT_EQ(ERROR_SUCCESS, key.ReadValueDW(L"D3S", &value)); 86 ASSERT_EQ(1, value); 87 ASSERT_EQ(ERROR_SUCCESS, key.ReadValueDW(L"D3F", &value)); 88 ASSERT_EQ(1, value); 89 90 ASSERT_EQ(ERROR_SUCCESS, key.ReadValueDW(L"D4I", &value)); 91 ASSERT_EQ(1, value); 92 93 key.Open(HKEY_CURRENT_USER, L"Software\\Google\\Common\\Rlz\\Events\\D", 94 KEY_READ); 95 ASSERT_FALSE(key.Valid()); 96 97 // Cleanup. 98 rlz_lib::ClearProductState(rlz_lib::PINYIN_IME, access_points); 99 } 100