| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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 "gtest/gtest.h" | |
| 6 #include "gmock/gmock.h" | |
| 7 #include "chrome_frame/chrome_frame_automation.h" | |
| 8 #include "chrome_frame/chrome_frame_npapi.h" | |
| 9 | |
| 10 TEST(ChromeFrameNPAPI, DoesNotCrashOnConstruction) { | |
| 11 ChromeFrameNPAPI* api = new ChromeFrameNPAPI(); | |
| 12 delete api; | |
| 13 } | |
| 14 | |
| 15 // All mocks in the anonymous namespace. | |
| 16 namespace { | |
| 17 | |
| 18 using ::testing::_; | |
| 19 using ::testing::Eq; | |
| 20 using ::testing::Field; | |
| 21 using ::testing::Invoke; | |
| 22 using ::testing::Return; | |
| 23 using ::testing::StrEq; | |
| 24 | |
| 25 const char* kMimeType = "application/chromeframe"; | |
| 26 // The default profile name is by default derived from the currently | |
| 27 // running executable's name. | |
| 28 const wchar_t* kDefaultProfileName = L"chrome_frame_unittests"; | |
| 29 | |
| 30 | |
| 31 class MockNPAPI: public ChromeFrameNPAPI { | |
| 32 public: | |
| 33 MockNPAPI() : mock_automation_client_(NULL) {} | |
| 34 MOCK_METHOD0(GetLocation, std::string()); | |
| 35 MOCK_METHOD0(GetBrowserIncognitoMode, bool()); | |
| 36 | |
| 37 MOCK_METHOD1(JavascriptToNPObject, virtual NPObject*(const std::string&)); | |
| 38 | |
| 39 // Make public for test purposes | |
| 40 void OnAutomationServerReady() { | |
| 41 ChromeFrameNPAPI::OnAutomationServerReady(); | |
| 42 } | |
| 43 | |
| 44 ChromeFrameAutomationClient* CreateAutomationClient() { | |
| 45 return mock_automation_client_; | |
| 46 } | |
| 47 | |
| 48 ChromeFrameAutomationClient* mock_automation_client_; | |
| 49 }; | |
| 50 | |
| 51 class MockAutomationClient: public ChromeFrameAutomationClient { | |
| 52 public: | |
| 53 MOCK_METHOD2(Initialize, bool(ChromeFrameDelegate*, | |
| 54 ChromeFrameLaunchParams*)); | |
| 55 }; | |
| 56 | |
| 57 namespace { | |
| 58 | |
| 59 MATCHER_P4(LaunchParamEq, version_check, extra, incognito, widget, | |
| 60 "Basic check for ChromeFrameLaunchParams") { | |
| 61 return arg->version_check() == version_check && | |
| 62 arg->extra_arguments().compare(extra) == 0 && | |
| 63 arg->incognito() == incognito, | |
| 64 arg->widget_mode() == widget; | |
| 65 } | |
| 66 | |
| 67 | |
| 68 static const NPIdentifier kOnPrivateMessageId = | |
| 69 reinterpret_cast<NPIdentifier>(0x100); | |
| 70 static const NPIdentifier kPostPrivateMessageId = | |
| 71 reinterpret_cast<NPIdentifier>(0x100); | |
| 72 } | |
| 73 | |
| 74 class MockNetscapeFuncs { | |
| 75 public: | |
| 76 MockNetscapeFuncs() { | |
| 77 CHECK(NULL == current_); | |
| 78 current_ = this; | |
| 79 } | |
| 80 | |
| 81 ~MockNetscapeFuncs() { | |
| 82 CHECK(this == current_); | |
| 83 current_ = NULL; | |
| 84 } | |
| 85 | |
| 86 MOCK_METHOD3(GetValue, NPError(NPP, NPNVariable, void *)); | |
| 87 MOCK_METHOD3(GetStringIdentifiers, void(const NPUTF8 **, | |
| 88 int32_t, | |
| 89 NPIdentifier *)); // NOLINT | |
| 90 MOCK_METHOD1(RetainObject, NPObject*(NPObject*)); // NOLINT | |
| 91 MOCK_METHOD1(ReleaseObject, void(NPObject*)); // NOLINT | |
| 92 | |
| 93 static const NPNetscapeFuncs* netscape_funcs() { | |
| 94 return &netscape_funcs_; | |
| 95 } | |
| 96 | |
| 97 private: | |
| 98 static NPError MockGetValue(NPP instance, | |
| 99 NPNVariable variable, | |
| 100 void *ret_value) { | |
| 101 DCHECK(current_); | |
| 102 return current_->GetValue(instance, variable, ret_value); | |
| 103 } | |
| 104 | |
| 105 static void MockGetStringIdentifiers(const NPUTF8 **names, | |
| 106 int32_t name_count, | |
| 107 NPIdentifier *identifiers) { | |
| 108 DCHECK(current_); | |
| 109 return current_->GetStringIdentifiers(names, name_count, identifiers); | |
| 110 } | |
| 111 | |
| 112 static NPObject* MockRetainObject(NPObject* obj) { | |
| 113 DCHECK(current_); | |
| 114 return current_->RetainObject(obj); | |
| 115 } | |
| 116 | |
| 117 static void MockReleaseObject(NPObject* obj) { | |
| 118 DCHECK(current_); | |
| 119 current_->ReleaseObject(obj); | |
| 120 } | |
| 121 | |
| 122 static MockNetscapeFuncs* current_; | |
| 123 static NPNetscapeFuncs netscape_funcs_; | |
| 124 }; | |
| 125 | |
| 126 // Test fixture to allow testing the privileged NPAPI APIs | |
| 127 class TestNPAPIPrivilegedApi: public ::testing::Test { | |
| 128 public: | |
| 129 virtual void SetUp() { | |
| 130 memset(&instance, 0, sizeof(instance)); | |
| 131 npapi::InitializeBrowserFunctions( | |
| 132 const_cast<NPNetscapeFuncs*>(mock_funcs.netscape_funcs())); | |
| 133 | |
| 134 // Gets owned & destroyed by mock_api (in the | |
| 135 // ChromeFramePlugin<T>::Uninitialize() function). | |
| 136 mock_automation = new MockAutomationClient; | |
| 137 | |
| 138 mock_api.mock_automation_client_ = mock_automation; | |
| 139 } | |
| 140 | |
| 141 virtual void TearDown() { | |
| 142 // Make sure to uninitialize the mock NPAPI before we uninitialize the | |
| 143 // browser function, otherwise we will get a DCHECK in the NPAPI dtor | |
| 144 // when it tries to perform the uninitialize there. | |
| 145 mock_api.Uninitialize(); | |
| 146 npapi::UninitializeBrowserFunctions(); | |
| 147 } | |
| 148 | |
| 149 void SetupPrivilegeTest(bool is_incognito, | |
| 150 bool expect_privilege_check, | |
| 151 bool is_privileged, | |
| 152 const std::wstring& profile_name, | |
| 153 const std::wstring& language, | |
| 154 const std::wstring& extra_args) { | |
| 155 EXPECT_CALL(mock_api, GetLocation()) | |
| 156 .WillOnce(Return(std::string("http://www.google.com"))); | |
| 157 EXPECT_CALL(mock_api, GetBrowserIncognitoMode()) | |
| 158 .WillOnce(Return(is_incognito)); | |
| 159 | |
| 160 scoped_refptr<ChromeFrameLaunchParams> launch_params( | |
| 161 new ChromeFrameLaunchParams(GURL(), GURL(), FilePath(), profile_name, | |
| 162 language, extra_args, is_incognito, true, false)); | |
| 163 | |
| 164 EXPECT_CALL(*mock_automation, | |
| 165 Initialize(_, LaunchParamEq(true, extra_args, is_incognito, true))) | |
| 166 .WillOnce(Return(true)); | |
| 167 } | |
| 168 | |
| 169 public: | |
| 170 MockNetscapeFuncs mock_funcs; | |
| 171 MockNPAPI mock_api; | |
| 172 MockAutomationClient* mock_automation; | |
| 173 NPP_t instance; | |
| 174 }; | |
| 175 | |
| 176 } // namespace | |
| OLD | NEW |