diff options
author | brettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-11-01 16:16:50 +0000 |
---|---|---|
committer | brettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-11-01 16:16:50 +0000 |
commit | 1758e88fd909ea0ffd49621e8066ffad5627ffdf (patch) | |
tree | c304a5eed047cae5665f5af1739d84655fb5815d /ppapi/tests/test_var.cc | |
parent | e7d8b51953b7d3b2b8a0aba46132305b32f3efce (diff) | |
download | chromium_src-1758e88fd909ea0ffd49621e8066ffad5627ffdf.zip chromium_src-1758e88fd909ea0ffd49621e8066ffad5627ffdf.tar.gz chromium_src-1758e88fd909ea0ffd49621e8066ffad5627ffdf.tar.bz2 |
Move PPAPI into the Chrome repo. The old repo was
http://ppapi.googlecode.com/
TEST=none
BUG=none
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@64613 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ppapi/tests/test_var.cc')
-rw-r--r-- | ppapi/tests/test_var.cc | 178 |
1 files changed, 178 insertions, 0 deletions
diff --git a/ppapi/tests/test_var.cc b/ppapi/tests/test_var.cc new file mode 100644 index 0000000..efd0dd1 --- /dev/null +++ b/ppapi/tests/test_var.cc @@ -0,0 +1,178 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "ppapi/tests/test_var.h" + +#include <limits> + +#include "ppapi/c/pp_var.h" +#include "ppapi/c/dev/ppb_testing_dev.h" +#include "ppapi/c/ppb_var.h" +#include "ppapi/cpp/module.h" +#include "ppapi/cpp/var.h" +#include "ppapi/tests/testing_instance.h" + +REGISTER_TEST_CASE(Var); + +namespace { +pp::Var adoptVar(PP_Var var) { + return pp::Var(pp::Var::PassRef(), var); +} +} // namespace + +bool TestVar::Init() { + var_interface_ = reinterpret_cast<PPB_Var const*>( + pp::Module::Get()->GetBrowserInterface(PPB_VAR_INTERFACE)); + testing_interface_ = reinterpret_cast<PPB_Testing_Dev const*>( + pp::Module::Get()->GetBrowserInterface(PPB_TESTING_DEV_INTERFACE)); + if (!testing_interface_) { + // Give a more helpful error message for the testing interface being gone + // since that needs special enabling in Chrome. + instance_->AppendError("This test needs the testing interface, which is " + "not currently available. In Chrome, use --enable-pepper-testing when " + "launching."); + } + return var_interface_ && testing_interface_; +} + +void TestVar::RunTest() { + RUN_TEST(ConvertType); + RUN_TEST(DefineProperty); +} + +std::string TestVar::TestConvertType() { + pp::Var result; + PP_Var exception = PP_MakeUndefined(); + double NaN = std::numeric_limits<double>::quiet_NaN(); + + // Int to string + result = adoptVar(var_interface_->ConvertType(instance_->pp_instance(), + pp::Var(100).pp_var(), + PP_VARTYPE_STRING, + &exception)); + ASSERT_EQ(pp::Var("100"), result); + ASSERT_TRUE(adoptVar(exception).is_undefined()); + + // Int to double + result = adoptVar(var_interface_->ConvertType(instance_->pp_instance(), + pp::Var(100).pp_var(), + PP_VARTYPE_DOUBLE, + &exception)); + ASSERT_EQ(pp::Var(100.0), result); + ASSERT_TRUE(adoptVar(exception).is_undefined()); + + // Double to int + result = adoptVar(var_interface_->ConvertType(instance_->pp_instance(), + pp::Var(100.0).pp_var(), + PP_VARTYPE_INT32, + &exception)); + ASSERT_EQ(pp::Var(100), result); + ASSERT_TRUE(adoptVar(exception).is_undefined()); + + // Double(NaN) to int + result = adoptVar(var_interface_->ConvertType(instance_->pp_instance(), + pp::Var(NaN).pp_var(), + PP_VARTYPE_INT32, + &exception)); + ASSERT_EQ(pp::Var(0), result); + ASSERT_TRUE(adoptVar(exception).is_undefined()); + + // Double to string + result = adoptVar(var_interface_->ConvertType(instance_->pp_instance(), + pp::Var(100.0).pp_var(), + PP_VARTYPE_STRING, + &exception)); + ASSERT_EQ(pp::Var("100"), result); + ASSERT_TRUE(adoptVar(exception).is_undefined()); + + // Double(NaN) to string + result = adoptVar(var_interface_->ConvertType(instance_->pp_instance(), + pp::Var(NaN).pp_var(), + PP_VARTYPE_STRING, + &exception)); + ASSERT_EQ(pp::Var("NaN"), result); + ASSERT_TRUE(adoptVar(exception).is_undefined()); + + // String to int, valid string + result = adoptVar(var_interface_->ConvertType(instance_->pp_instance(), + pp::Var("100").pp_var(), + PP_VARTYPE_INT32, + &exception)); + ASSERT_EQ(pp::Var(100), result); + ASSERT_TRUE(adoptVar(exception).is_undefined()); + + // String to int, invalid string + result = adoptVar(var_interface_->ConvertType(instance_->pp_instance(), + pp::Var("jockey").pp_var(), + PP_VARTYPE_INT32, + &exception)); + ASSERT_EQ(pp::Var(0), result); + ASSERT_TRUE(adoptVar(exception).is_undefined()); + + PASS(); +} + +std::string TestVar::TestDefineProperty() { + pp::Var exception; + pp::Var property; + pp::Var value; + PP_Var unmanaged_exception = PP_MakeUndefined(); + + // Create an empty object. + pp::Var test_obj = instance_->ExecuteScript("({})", &exception); + ASSERT_TRUE(exception.is_undefined()); + ASSERT_TRUE(test_obj.is_object()); + + // Define a simple property. + property = "x"; + value = 1001; + var_interface_->DefineProperty(test_obj.pp_var(), + PP_MakeSimpleProperty(property.pp_var(), + value.pp_var()), + &unmanaged_exception); + ASSERT_TRUE(adoptVar(unmanaged_exception).is_undefined()); + + ASSERT_EQ(value, test_obj.GetProperty(property, &exception)); + ASSERT_TRUE(exception.is_undefined()); + + // Define a property with a getter that always returns 123 and setter that + // sets another property to the given value. + property = "y"; + pp::Var getter = instance_->ExecuteScript( + "(function(){return 'okey';})", &exception); + ASSERT_TRUE(getter.is_object()); + ASSERT_TRUE(exception.is_undefined()); + pp::Var setter = instance_->ExecuteScript( + "(function(x){this['another']=x;})", &exception); + ASSERT_TRUE(setter.is_object()); + ASSERT_TRUE(exception.is_undefined()); + + struct PP_ObjectProperty property_attributes = { + property.pp_var(), + PP_MakeUndefined(), + getter.pp_var(), + setter.pp_var(), + PP_OBJECTPROPERTY_MODIFIER_NONE + }; + var_interface_->DefineProperty(test_obj.pp_var(), property_attributes, + &unmanaged_exception); + ASSERT_TRUE(adoptVar(unmanaged_exception).is_undefined()); + + value = test_obj.GetProperty(property, &exception); + ASSERT_EQ(pp::Var("okey"), value); + ASSERT_TRUE(exception.is_undefined()); + + value = test_obj.GetProperty("another", &exception); + ASSERT_TRUE(value.is_undefined()); + ASSERT_TRUE(exception.is_undefined()); + + test_obj.SetProperty("another", "dokey", &exception); + ASSERT_TRUE(exception.is_undefined()); + + ASSERT_EQ(pp::Var("dokey"), test_obj.GetProperty("another", &exception)); + ASSERT_TRUE(exception.is_undefined()); + + PASS(); +} + |