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