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 <map> 6 7 #include "chrome/browser/browser_process.h" 8 #include "chrome/browser/extensions/extension_apitest.h" 9 #include "chrome/browser/extensions/extension_function.h" 10 #include "chrome/browser/extensions/extension_function_dispatcher.h" 11 #include "chrome/browser/rlz/rlz_extension_api.h" 12 #include "chrome/common/extensions/extension.h" 13 #include "extensions/common/switches.h" 14 #include "net/dns/mock_host_resolver.h" 15 #include "rlz/lib/rlz_lib.h" 16 17 #if (OS_WIN) 18 #include "base/win/registry.h" 19 #endif 20 21 class MockRlzSendFinancialPingFunction : public RlzSendFinancialPingFunction { 22 public: 23 static int expected_count() { 24 return expected_count_; 25 } 26 27 protected: 28 virtual ~MockRlzSendFinancialPingFunction() {} 29 30 // ExtensionFunction 31 virtual bool RunImpl() OVERRIDE; 32 33 private: 34 static int expected_count_; 35 }; 36 37 int MockRlzSendFinancialPingFunction::expected_count_ = 0; 38 39 bool MockRlzSendFinancialPingFunction::RunImpl() { 40 EXPECT_TRUE(RlzSendFinancialPingFunction::RunImpl()); 41 ++expected_count_; 42 return true; 43 } 44 45 ExtensionFunction* MockRlzSendFinancialPingFunctionFactory() { 46 return new MockRlzSendFinancialPingFunction(); 47 } 48 49 // Mac is flaky - http://crbug.com/137834. ChromeOS is not supported yet. 50 #if defined(OS_MACOSX) || defined(OS_CHROMEOS) 51 #define MAYBE_Rlz DISABLED_Rlz 52 #else 53 #define MAYBE_Rlz Rlz 54 #endif 55 56 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, MAYBE_Rlz) { 57 // The default test resolver doesn't allow lookups to *.google.com. That 58 // makes sense, but it does make RLZ's SendFinancialPing() fail -- so allow 59 // connections to google.com in this test. 60 scoped_refptr<net::RuleBasedHostResolverProc> resolver = 61 new net::RuleBasedHostResolverProc(host_resolver()); 62 resolver->AllowDirectLookup("*.google.com"); 63 net::ScopedDefaultHostResolverProc scoper(resolver); 64 65 CommandLine::ForCurrentProcess()->AppendSwitch( 66 extensions::switches::kEnableExperimentalExtensionApis); 67 68 // Before running the tests, clear the state of the RLZ products used. 69 rlz_lib::AccessPoint access_points[] = { 70 rlz_lib::GD_WEB_SERVER, 71 rlz_lib::GD_OUTLOOK, 72 rlz_lib::NO_ACCESS_POINT, 73 }; 74 rlz_lib::ClearProductState(rlz_lib::PINYIN_IME, access_points); 75 rlz_lib::ClearProductState(rlz_lib::DESKTOP, access_points); 76 77 #if defined(OS_WIN) 78 // Check that the state has really been cleared. 79 base::win::RegKey key(HKEY_CURRENT_USER, 80 L"Software\\Google\\Common\\Rlz\\Events\\N", 81 KEY_READ); 82 ASSERT_FALSE(key.Valid()); 83 84 key.Open(HKEY_CURRENT_USER, L"Software\\Google\\Common\\Rlz\\Events\\D", 85 KEY_READ); 86 ASSERT_FALSE(key.Valid()); 87 #endif 88 89 // Mock out experimental.rlz.sendFinancialPing(). 90 ASSERT_TRUE(ExtensionFunctionDispatcher::OverrideFunction( 91 "experimental.rlz.sendFinancialPing", 92 MockRlzSendFinancialPingFunctionFactory)); 93 94 // Set the access point that the test code is expecting. 95 ASSERT_TRUE(rlz_lib::SetAccessPointRlz(rlz_lib::GD_DESKBAND, "rlz_apitest")); 96 97 // Now run all the tests. 98 ASSERT_TRUE(RunExtensionTest("rlz")) << message_; 99 100 ASSERT_EQ(3, MockRlzSendFinancialPingFunction::expected_count()); 101 ExtensionFunctionDispatcher::ResetFunctions(); 102 103 #if defined(OS_WIN) 104 // Now make sure we recorded what was expected. If the code in test.js 105 // changes, need to make appropriate changes here. 106 key.Open(HKEY_CURRENT_USER, L"Software\\Google\\Common\\Rlz\\Events\\N", 107 KEY_READ); 108 ASSERT_TRUE(key.Valid()); 109 110 DWORD value; 111 ASSERT_EQ(ERROR_SUCCESS, key.ReadValueDW(L"D3I", &value)); 112 ASSERT_EQ(1, value); 113 ASSERT_EQ(ERROR_SUCCESS, key.ReadValueDW(L"D3S", &value)); 114 ASSERT_EQ(1, value); 115 ASSERT_EQ(ERROR_SUCCESS, key.ReadValueDW(L"D3F", &value)); 116 ASSERT_EQ(1, value); 117 118 ASSERT_EQ(ERROR_SUCCESS, key.ReadValueDW(L"D4I", &value)); 119 ASSERT_EQ(1, value); 120 121 key.Open(HKEY_CURRENT_USER, L"Software\\Google\\Common\\Rlz\\Events\\D", 122 KEY_READ); 123 ASSERT_FALSE(key.Valid()); 124 #endif 125 126 // Cleanup. 127 rlz_lib::ClearProductState(rlz_lib::PINYIN_IME, access_points); 128 } 129