1 // Copyright (c) 2012 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 "content/public/browser/web_ui_message_handler.h"
7 #include "base/strings/string16.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "base/values.h"
10 #include "testing/gtest/include/gtest/gtest.h"
14 TEST(WebUIMessageHandlerTest
, ExtractIntegerValue
) {
16 int value
, zero_value
= 0, neg_value
= -1234, pos_value
= 1234;
17 base::string16
zero_string(base::UTF8ToUTF16("0"));
18 base::string16
neg_string(base::UTF8ToUTF16("-1234"));
19 base::string16
pos_string(base::UTF8ToUTF16("1234"));
21 list
.Append(new base::FundamentalValue(zero_value
));
22 EXPECT_TRUE(WebUIMessageHandler::ExtractIntegerValue(&list
, &value
));
23 EXPECT_EQ(value
, zero_value
);
26 list
.Append(new base::FundamentalValue(neg_value
));
27 EXPECT_TRUE(WebUIMessageHandler::ExtractIntegerValue(&list
, &value
));
28 EXPECT_EQ(value
, neg_value
);
31 list
.Append(new base::FundamentalValue(pos_value
));
32 EXPECT_TRUE(WebUIMessageHandler::ExtractIntegerValue(&list
, &value
));
33 EXPECT_EQ(value
, pos_value
);
36 list
.Append(new base::StringValue(zero_string
));
37 EXPECT_TRUE(WebUIMessageHandler::ExtractIntegerValue(&list
, &value
));
38 EXPECT_EQ(value
, zero_value
);
41 list
.Append(new base::StringValue(neg_string
));
42 EXPECT_TRUE(WebUIMessageHandler::ExtractIntegerValue(&list
, &value
));
43 EXPECT_EQ(value
, neg_value
);
46 list
.Append(new base::StringValue(pos_string
));
47 EXPECT_TRUE(WebUIMessageHandler::ExtractIntegerValue(&list
, &value
));
48 EXPECT_EQ(value
, pos_value
);
51 TEST(WebUIMessageHandlerTest
, ExtractDoubleValue
) {
53 double value
, zero_value
= 0.0, neg_value
= -1234.5, pos_value
= 1234.5;
54 base::string16
zero_string(base::UTF8ToUTF16("0"));
55 base::string16
neg_string(base::UTF8ToUTF16("-1234.5"));
56 base::string16
pos_string(base::UTF8ToUTF16("1234.5"));
58 list
.Append(new base::FundamentalValue(zero_value
));
59 EXPECT_TRUE(WebUIMessageHandler::ExtractDoubleValue(&list
, &value
));
60 EXPECT_DOUBLE_EQ(value
, zero_value
);
63 list
.Append(new base::FundamentalValue(neg_value
));
64 EXPECT_TRUE(WebUIMessageHandler::ExtractDoubleValue(&list
, &value
));
65 EXPECT_DOUBLE_EQ(value
, neg_value
);
68 list
.Append(new base::FundamentalValue(pos_value
));
69 EXPECT_TRUE(WebUIMessageHandler::ExtractDoubleValue(&list
, &value
));
70 EXPECT_DOUBLE_EQ(value
, pos_value
);
73 list
.Append(new base::StringValue(zero_string
));
74 EXPECT_TRUE(WebUIMessageHandler::ExtractDoubleValue(&list
, &value
));
75 EXPECT_DOUBLE_EQ(value
, zero_value
);
78 list
.Append(new base::StringValue(neg_string
));
79 EXPECT_TRUE(WebUIMessageHandler::ExtractDoubleValue(&list
, &value
));
80 EXPECT_DOUBLE_EQ(value
, neg_value
);
83 list
.Append(new base::StringValue(pos_string
));
84 EXPECT_TRUE(WebUIMessageHandler::ExtractDoubleValue(&list
, &value
));
85 EXPECT_DOUBLE_EQ(value
, pos_value
);
88 TEST(WebUIMessageHandlerTest
, ExtractStringValue
) {
90 base::string16
in_string(base::UTF8ToUTF16(
91 "The facts, though interesting, are irrelevant."));
92 list
.Append(new base::StringValue(in_string
));
93 base::string16 out_string
= WebUIMessageHandler::ExtractStringValue(&list
);
94 EXPECT_EQ(in_string
, out_string
);
97 } // namespace content