1 // Copyright 2014 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.
7 #include "base/basictypes.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "content/common/android/gin_java_bridge_value.h"
10 #include "testing/gtest/include/gtest/gtest.h"
14 class GinJavaBridgeValueTest
: public testing::Test
{
17 TEST_F(GinJavaBridgeValueTest
, BasicValues
) {
19 int32 native_object_id
;
21 scoped_ptr
<base::BinaryValue
> undefined(
22 GinJavaBridgeValue::CreateUndefinedValue());
23 ASSERT_TRUE(undefined
.get());
24 EXPECT_TRUE(GinJavaBridgeValue::ContainsGinJavaBridgeValue(undefined
.get()));
25 scoped_ptr
<const GinJavaBridgeValue
> undefined_value(
26 GinJavaBridgeValue::FromValue(undefined
.get()));
27 ASSERT_TRUE(undefined_value
.get());
28 EXPECT_TRUE(undefined_value
->IsType(GinJavaBridgeValue::TYPE_UNDEFINED
));
30 EXPECT_FALSE(undefined_value
->GetAsNonFinite(&native_float
));
31 EXPECT_FALSE(undefined_value
->GetAsObjectID(&native_object_id
));
33 scoped_ptr
<base::BinaryValue
> float_infinity(
34 GinJavaBridgeValue::CreateNonFiniteValue(
35 std::numeric_limits
<float>::infinity()));
36 ASSERT_TRUE(float_infinity
.get());
38 GinJavaBridgeValue::ContainsGinJavaBridgeValue(float_infinity
.get()));
39 scoped_ptr
<const GinJavaBridgeValue
> float_infinity_value(
40 GinJavaBridgeValue::FromValue(float_infinity
.get()));
41 ASSERT_TRUE(float_infinity_value
.get());
42 EXPECT_TRUE(float_infinity_value
->IsType(GinJavaBridgeValue::TYPE_NONFINITE
));
43 EXPECT_TRUE(float_infinity_value
->GetAsNonFinite(&native_float
));
44 EXPECT_TRUE(std::isinf(native_float
));
46 EXPECT_FALSE(undefined_value
->GetAsObjectID(&native_object_id
));
48 scoped_ptr
<base::BinaryValue
> double_infinity(
49 GinJavaBridgeValue::CreateNonFiniteValue(
50 std::numeric_limits
<double>::infinity()));
51 ASSERT_TRUE(double_infinity
.get());
53 GinJavaBridgeValue::ContainsGinJavaBridgeValue(double_infinity
.get()));
54 scoped_ptr
<const GinJavaBridgeValue
> double_infinity_value(
55 GinJavaBridgeValue::FromValue(double_infinity
.get()));
56 ASSERT_TRUE(double_infinity_value
.get());
58 double_infinity_value
->IsType(GinJavaBridgeValue::TYPE_NONFINITE
));
59 EXPECT_TRUE(double_infinity_value
->GetAsNonFinite(&native_float
));
60 EXPECT_TRUE(std::isinf(native_float
));
62 EXPECT_FALSE(undefined_value
->GetAsObjectID(&native_object_id
));
64 scoped_ptr
<base::BinaryValue
> object_id(
65 GinJavaBridgeValue::CreateObjectIDValue(42));
66 ASSERT_TRUE(object_id
.get());
67 EXPECT_TRUE(GinJavaBridgeValue::ContainsGinJavaBridgeValue(object_id
.get()));
68 scoped_ptr
<const GinJavaBridgeValue
> object_id_value(
69 GinJavaBridgeValue::FromValue(object_id
.get()));
70 ASSERT_TRUE(object_id_value
.get());
71 EXPECT_TRUE(object_id_value
->IsType(GinJavaBridgeValue::TYPE_OBJECT_ID
));
72 EXPECT_TRUE(object_id_value
->GetAsObjectID(&native_object_id
));
73 EXPECT_EQ(42, native_object_id
);
75 EXPECT_FALSE(undefined_value
->GetAsNonFinite(&native_float
));
78 TEST_F(GinJavaBridgeValueTest
, BrokenValues
) {
79 scoped_ptr
<base::Value
> non_binary(new base::FundamentalValue(42));
81 GinJavaBridgeValue::ContainsGinJavaBridgeValue(non_binary
.get()));
83 const char dummy_data
[] = "\000\001\002\003\004\005\006\007\010\011\012\013";
84 scoped_ptr
<base::BinaryValue
> broken_binary(
85 base::BinaryValue::CreateWithCopiedBuffer(dummy_data
,
88 GinJavaBridgeValue::ContainsGinJavaBridgeValue(broken_binary
.get()));