| OLD | NEW |
| (Empty) |
| 1 // Copyright 2009 Google Inc. | |
| 2 // | |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
| 4 // you may not use this file except in compliance with the License. | |
| 5 // You may obtain a copy of the License at | |
| 6 // | |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | |
| 8 // | |
| 9 // Unless required by applicable law or agreed to in writing, software | |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 12 // See the License for the specific language governing permissions and | |
| 13 // limitations under the License. | |
| 14 // ======================================================================== | |
| 15 | |
| 16 #include "omaha/plugins/update/npapi/dispatch_host.h" | |
| 17 #include <atlbase.h> | |
| 18 #include <atlcom.h> | |
| 19 #include <string.h> | |
| 20 #include <vector> | |
| 21 #include "omaha/plugins/update/npapi/testing/dispatch_host_test_interface.h" | |
| 22 #include "omaha/plugins/update/npapi/testing/stubs.h" | |
| 23 #include "omaha/testing/unit_test.h" | |
| 24 | |
| 25 namespace omaha { | |
| 26 | |
| 27 class DispatchHostTest : public testing::Test { | |
| 28 protected: | |
| 29 virtual void SetUp() { | |
| 30 CComPtr<IDispatch> dispatch; | |
| 31 ASSERT_SUCCEEDED( | |
| 32 CComCoClass<DispatchHostTestInterface>::CreateInstance(&dispatch)); | |
| 33 dispatch_host_ = NPN_CreateObject(NULL, &DispatchHost::kNPClass_); | |
| 34 static_cast<DispatchHost*>(dispatch_host_)->dispatch_ = dispatch.Detach(); | |
| 35 VOID_TO_NPVARIANT(result_); | |
| 36 } | |
| 37 | |
| 38 virtual void TearDown() { | |
| 39 NPN_ReleaseObject(dispatch_host_); | |
| 40 for (std::vector<NPVariant>::iterator it = args_.begin(); | |
| 41 it != args_.end(); ++it) { | |
| 42 NPN_ReleaseVariantValue(&*it); | |
| 43 } | |
| 44 NPN_ReleaseVariantValue(&result_); | |
| 45 } | |
| 46 | |
| 47 void UseTestInterface2() { | |
| 48 NPN_ReleaseObject(dispatch_host_); | |
| 49 CComPtr<IDispatch> dispatch; | |
| 50 ASSERT_SUCCEEDED( | |
| 51 CComCoClass<DispatchHostTestInterface2>::CreateInstance(&dispatch)); | |
| 52 dispatch_host_ = NPN_CreateObject(NULL, &DispatchHost::kNPClass_); | |
| 53 static_cast<DispatchHost*>(dispatch_host_)->dispatch_ = dispatch.Detach(); | |
| 54 } | |
| 55 | |
| 56 void PushArg(bool value) { | |
| 57 args_.push_back(NPVariant()); | |
| 58 BOOLEAN_TO_NPVARIANT(value, args_.back()); | |
| 59 } | |
| 60 | |
| 61 void PushArg(int32 value) { | |
| 62 args_.push_back(NPVariant()); | |
| 63 INT32_TO_NPVARIANT(value, args_.back()); | |
| 64 } | |
| 65 | |
| 66 void PushArg(double value) { | |
| 67 args_.push_back(NPVariant()); | |
| 68 DOUBLE_TO_NPVARIANT(value, args_.back()); | |
| 69 } | |
| 70 | |
| 71 void PushArg(const char* value) { | |
| 72 args_.push_back(NPVariant()); | |
| 73 // TODO(omaha): _strdup is an implementation detail of the stubs. | |
| 74 STRINGZ_TO_NPVARIANT(_strdup(value), args_.back()); | |
| 75 } | |
| 76 | |
| 77 NPObject* dispatch_host_; | |
| 78 NPIdentifierFactory id_factory_; | |
| 79 | |
| 80 std::vector<NPVariant> args_; | |
| 81 NPVariant result_; | |
| 82 }; | |
| 83 | |
| 84 TEST_F(DispatchHostTest, HasMethod) { | |
| 85 EXPECT_TRUE(NPN_HasMethod(NULL, dispatch_host_, | |
| 86 id_factory_.Create("Random"))); | |
| 87 EXPECT_TRUE(NPN_HasMethod(NULL, dispatch_host_, | |
| 88 id_factory_.Create("AddAsMethod"))); | |
| 89 | |
| 90 // Property getters with input arguments should be treated as methods. | |
| 91 EXPECT_TRUE(NPN_HasMethod(NULL, dispatch_host_, | |
| 92 id_factory_.Create("AddAsProperty"))); | |
| 93 | |
| 94 // Properties and non-existent members are not methods. | |
| 95 EXPECT_FALSE(NPN_HasMethod(NULL, dispatch_host_, | |
| 96 id_factory_.Create("Property"))); | |
| 97 EXPECT_FALSE(NPN_HasMethod(NULL, dispatch_host_, | |
| 98 id_factory_.Create("ReadOnlyProperty"))); | |
| 99 EXPECT_FALSE(NPN_HasMethod(NULL, dispatch_host_, | |
| 100 id_factory_.Create("WriteOnlyProperty"))); | |
| 101 EXPECT_FALSE(NPN_HasMethod(NULL, dispatch_host_, | |
| 102 id_factory_.Create("DoesNotExist"))); | |
| 103 } | |
| 104 | |
| 105 TEST_F(DispatchHostTest, InvokeNoArgs) { | |
| 106 EXPECT_TRUE(NPN_Invoke(NULL, dispatch_host_, id_factory_.Create("Random"), | |
| 107 NULL, 0, &result_)); | |
| 108 EXPECT_TRUE(NPVARIANT_IS_INT32(result_)); | |
| 109 EXPECT_EQ(42, result_.value.intValue); | |
| 110 } | |
| 111 | |
| 112 TEST_F(DispatchHostTest, InvokeWithArgs) { | |
| 113 PushArg(7); | |
| 114 PushArg(27); | |
| 115 EXPECT_TRUE(NPN_Invoke(NULL, dispatch_host_, | |
| 116 id_factory_.Create("AddAsMethod"), &args_.front(), | |
| 117 args_.size(), &result_)); | |
| 118 EXPECT_TRUE(NPVARIANT_IS_INT32(result_)); | |
| 119 EXPECT_EQ(34, result_.value.intValue); | |
| 120 } | |
| 121 | |
| 122 TEST_F(DispatchHostTest, InvokePropertyWithArgs) { | |
| 123 // Property getters that have input args should be handle by Invoke | |
| 124 PushArg(8); | |
| 125 PushArg(15); | |
| 126 EXPECT_TRUE(NPN_Invoke(NULL, dispatch_host_, | |
| 127 id_factory_.Create("AddAsProperty"), &args_.front(), | |
| 128 args_.size(), &result_)); | |
| 129 EXPECT_TRUE(NPVARIANT_IS_INT32(result_)); | |
| 130 EXPECT_EQ(23, result_.value.intValue); | |
| 131 } | |
| 132 | |
| 133 TEST_F(DispatchHostTest, InvokeNonexistentMethod) { | |
| 134 // Non-existent method, should fail. | |
| 135 INT32_TO_NPVARIANT(0x19821982, result_); | |
| 136 EXPECT_FALSE(NPN_Invoke(NULL, dispatch_host_, | |
| 137 id_factory_.Create("NonExistent"), NULL, 0, | |
| 138 &result_)); | |
| 139 EXPECT_TRUE(NPVARIANT_IS_VOID(result_)); | |
| 140 } | |
| 141 | |
| 142 TEST_F(DispatchHostTest, InvokeWithIncompatibleArgs) { | |
| 143 PushArg("Hello World!"); | |
| 144 PushArg(0x19851985); | |
| 145 INT32_TO_NPVARIANT(0x19881988, result_); | |
| 146 EXPECT_FALSE(NPN_Invoke(NULL, dispatch_host_, | |
| 147 id_factory_.Create("AddAsMethod"), &args_.front(), | |
| 148 args_.size(), &result_)); | |
| 149 EXPECT_TRUE(NPVARIANT_IS_VOID(result_)); | |
| 150 } | |
| 151 | |
| 152 TEST_F(DispatchHostTest, InvokeWithIncorrectNumberOfArgs) { | |
| 153 PushArg("Don't panic."); | |
| 154 INT32_TO_NPVARIANT(0x77777777, result_); | |
| 155 EXPECT_FALSE(NPN_Invoke(NULL, dispatch_host_, id_factory_.Create("Random"), | |
| 156 &args_.front(), args_.size(), &result_)); | |
| 157 EXPECT_TRUE(NPVARIANT_IS_VOID(result_)); | |
| 158 } | |
| 159 | |
| 160 TEST_F(DispatchHostTest, InvokeDefault) { | |
| 161 EXPECT_TRUE(NPN_InvokeDefault(NULL, dispatch_host_, NULL, 0, &result_)); | |
| 162 EXPECT_TRUE(NPVARIANT_IS_OBJECT(result_)); | |
| 163 } | |
| 164 | |
| 165 TEST_F(DispatchHostTest, InvokeDefaultPropertyWithArgs) { | |
| 166 UseTestInterface2(); | |
| 167 PushArg(1048576); | |
| 168 EXPECT_TRUE(NPN_InvokeDefault(NULL, dispatch_host_, &args_.front(), | |
| 169 args_.size(), &result_)); | |
| 170 EXPECT_TRUE(NPVARIANT_IS_INT32(result_)); | |
| 171 EXPECT_EQ(1048576 * 2, result_.value.intValue); | |
| 172 } | |
| 173 | |
| 174 // TODO(omaha): implement negative test | |
| 175 | |
| 176 TEST_F(DispatchHostTest, HasProperty) { | |
| 177 EXPECT_TRUE(NPN_HasProperty(NULL, dispatch_host_, | |
| 178 id_factory_.Create("Property"))); | |
| 179 EXPECT_TRUE(NPN_HasProperty(NULL, dispatch_host_, | |
| 180 id_factory_.Create("ReadOnlyProperty"))); | |
| 181 EXPECT_TRUE(NPN_HasProperty(NULL, dispatch_host_, | |
| 182 id_factory_.Create("WriteOnlyProperty"))); | |
| 183 | |
| 184 // Property getters with input arguments should not be treated as properties. | |
| 185 EXPECT_FALSE(NPN_HasProperty(NULL, dispatch_host_, | |
| 186 id_factory_.Create("AddAsProperty"))); | |
| 187 | |
| 188 // Methods and non-existent members are not properties. | |
| 189 EXPECT_FALSE(NPN_HasProperty(NULL, dispatch_host_, | |
| 190 id_factory_.Create("Random"))); | |
| 191 EXPECT_FALSE(NPN_HasProperty(NULL, dispatch_host_, | |
| 192 id_factory_.Create("DoesNotExist"))); | |
| 193 } | |
| 194 | |
| 195 TEST_F(DispatchHostTest, GetProperty) { | |
| 196 EXPECT_TRUE(NPN_GetProperty(NULL, dispatch_host_, | |
| 197 id_factory_.Create("Property"), &result_)); | |
| 198 EXPECT_TRUE(NPVARIANT_IS_INT32(result_)); | |
| 199 EXPECT_EQ(0xdeadbeef, result_.value.intValue); | |
| 200 } | |
| 201 | |
| 202 TEST_F(DispatchHostTest, GetPropertyReadOnly) { | |
| 203 EXPECT_TRUE(NPN_GetProperty(NULL, dispatch_host_, | |
| 204 id_factory_.Create("ReadOnlyProperty"), | |
| 205 &result_)); | |
| 206 EXPECT_TRUE(NPVARIANT_IS_INT32(result_)); | |
| 207 EXPECT_EQ(19700101, result_.value.intValue); | |
| 208 } | |
| 209 | |
| 210 TEST_F(DispatchHostTest, SetProperty) { | |
| 211 PushArg(20002000); | |
| 212 EXPECT_TRUE(NPN_SetProperty(NULL, dispatch_host_, | |
| 213 id_factory_.Create("Property"), &args_.front())); | |
| 214 } | |
| 215 | |
| 216 TEST_F(DispatchHostTest, SetPropertyWriteOnly) { | |
| 217 PushArg(20612061); | |
| 218 EXPECT_TRUE(NPN_SetProperty(NULL, dispatch_host_, | |
| 219 id_factory_.Create("WriteOnlyProperty"), | |
| 220 &args_.front())); | |
| 221 } | |
| 222 | |
| 223 TEST_F(DispatchHostTest, Unsupported) { | |
| 224 EXPECT_FALSE(NPN_RemoveProperty(NULL, dispatch_host_, NULL)); | |
| 225 EXPECT_FALSE(NPN_Enumerate(NULL, dispatch_host_, NULL, NULL)); | |
| 226 EXPECT_FALSE(NPN_Construct(NULL, dispatch_host_, NULL, 0, NULL)); | |
| 227 } | |
| 228 | |
| 229 } // namespace omaha | |
| OLD | NEW |