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.
5 #include "base/basictypes.h"
6 #include "base/float_util.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "content/common/android/gin_java_bridge_value.h"
9 #include "testing/gtest/include/gtest/gtest.h"
13 class GinJavaBridgeValueTest
: public testing::Test
{
16 TEST_F(GinJavaBridgeValueTest
, BasicValues
) {
18 int32 native_object_id
;
20 scoped_ptr
<base::BinaryValue
> undefined(
21 GinJavaBridgeValue::CreateUndefinedValue());
22 ASSERT_TRUE(undefined
.get());
23 EXPECT_TRUE(GinJavaBridgeValue::ContainsGinJavaBridgeValue(undefined
.get()));
24 scoped_ptr
<const GinJavaBridgeValue
> undefined_value(
25 GinJavaBridgeValue::FromValue(undefined
.get()));
26 ASSERT_TRUE(undefined_value
.get());
27 EXPECT_TRUE(undefined_value
->IsType(GinJavaBridgeValue::TYPE_UNDEFINED
));
29 EXPECT_FALSE(undefined_value
->GetAsNonFinite(&native_float
));
30 EXPECT_FALSE(undefined_value
->GetAsObjectID(&native_object_id
));
32 scoped_ptr
<base::BinaryValue
> float_infinity(
33 GinJavaBridgeValue::CreateNonFiniteValue(
34 std::numeric_limits
<float>::infinity()));
35 ASSERT_TRUE(float_infinity
.get());
37 GinJavaBridgeValue::ContainsGinJavaBridgeValue(float_infinity
.get()));
38 scoped_ptr
<const GinJavaBridgeValue
> float_infinity_value(
39 GinJavaBridgeValue::FromValue(float_infinity
.get()));
40 ASSERT_TRUE(float_infinity_value
.get());
41 EXPECT_TRUE(float_infinity_value
->IsType(GinJavaBridgeValue::TYPE_NONFINITE
));
42 EXPECT_TRUE(float_infinity_value
->GetAsNonFinite(&native_float
));
43 EXPECT_FALSE(base::IsFinite(native_float
));
44 EXPECT_FALSE(base::IsNaN(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_FALSE(base::IsFinite(native_float
));
61 EXPECT_FALSE(base::IsNaN(native_float
));
63 EXPECT_FALSE(undefined_value
->GetAsObjectID(&native_object_id
));
65 scoped_ptr
<base::BinaryValue
> object_id(
66 GinJavaBridgeValue::CreateObjectIDValue(42));
67 ASSERT_TRUE(object_id
.get());
68 EXPECT_TRUE(GinJavaBridgeValue::ContainsGinJavaBridgeValue(object_id
.get()));
69 scoped_ptr
<const GinJavaBridgeValue
> object_id_value(
70 GinJavaBridgeValue::FromValue(object_id
.get()));
71 ASSERT_TRUE(object_id_value
.get());
72 EXPECT_TRUE(object_id_value
->IsType(GinJavaBridgeValue::TYPE_OBJECT_ID
));
73 EXPECT_TRUE(object_id_value
->GetAsObjectID(&native_object_id
));
74 EXPECT_EQ(42, native_object_id
);
76 EXPECT_FALSE(undefined_value
->GetAsNonFinite(&native_float
));
79 TEST_F(GinJavaBridgeValueTest
, BrokenValues
) {
80 scoped_ptr
<base::Value
> non_binary(new base::FundamentalValue(42));
82 GinJavaBridgeValue::ContainsGinJavaBridgeValue(non_binary
.get()));
84 const char dummy_data
[] = "\000\001\002\003\004\005\006\007\010\011\012\013";
85 scoped_ptr
<base::BinaryValue
> broken_binary(
86 base::BinaryValue::CreateWithCopiedBuffer(dummy_data
,
89 GinJavaBridgeValue::ContainsGinJavaBridgeValue(broken_binary
.get()));