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 "base/gtest_prod_util.h"
6 #include "base/values.h"
7 #include "base/utf_string_conversions.h"
8 #include "content/common/intents_messages.h"
9 #include "content/public/renderer/v8_value_converter.h"
10 #include "content/public/test/render_view_test.h"
11 #include "content/renderer/render_view_impl.h"
12 #include "content/renderer/web_intents_host.h"
13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDeliveredIntentClient.h"
14 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebSerializedScriptValue.h"
15 #include "webkit/glue/web_intent_data.h"
19 class WebIntentsHostTest
: public RenderViewTest
{
21 RenderViewImpl
* view() { return static_cast<RenderViewImpl
*>(view_
); }
22 WebIntentsHost
* web_intents_host() { return view()->intents_host_
; }
24 void SetIntentData(const webkit_glue::WebIntentData
& data
) {
25 web_intents_host()->OnSetIntent(data
);
28 base::DictionaryValue
* ParseValueFromReply(const IPC::Message
* reply_msg
) {
29 IntentsHostMsg_WebIntentReply::Param reply_params
;
30 IntentsHostMsg_WebIntentReply::Read(reply_msg
, &reply_params
);
31 WebKit::WebSerializedScriptValue serialized_data
=
32 WebKit::WebSerializedScriptValue::fromString(reply_params
.a
.data
);
34 v8::HandleScope scope
;
35 v8::Local
<v8::Context
> ctx
= GetMainFrame()->mainWorldScriptContext();
36 v8::Context::Scope
cscope(ctx
);
37 v8::Handle
<v8::Value
> v8_val
= serialized_data
.deserialize();
38 scoped_ptr
<V8ValueConverter
> converter(V8ValueConverter::create());
39 base::Value
* reply
= converter
->FromV8Value(v8_val
, ctx
);
40 EXPECT_TRUE(reply
->IsType(base::Value::TYPE_DICTIONARY
));
41 base::DictionaryValue
* dict
= NULL
;
42 reply
->GetAsDictionary(&dict
);
47 TEST_F(WebIntentsHostTest
, TestUnserialized
) {
48 webkit_glue::WebIntentData
data(ASCIIToUTF16("action"), ASCIIToUTF16("type"),
49 ASCIIToUTF16("unserialized data"));
55 "d.action = window.webkitIntent.action;\n"
56 "d.type = window.webkitIntent.type;\n"
57 "d.data = window.webkitIntent.data;\n"
58 "window.webkitIntent.postResult(d);\n"
60 "<body>body</body></html>");
62 const IPC::Message
* reply_msg
=
63 render_thread_
->sink().GetUniqueMessageMatching(
64 IntentsHostMsg_WebIntentReply::ID
);
65 ASSERT_TRUE(reply_msg
);
66 scoped_ptr
<base::DictionaryValue
> dict(ParseValueFromReply(reply_msg
));
68 std::string reply_action
;
69 dict
->GetStringASCII(std::string("action"), &reply_action
);
70 EXPECT_EQ("action", reply_action
);
71 std::string reply_type
;
72 dict
->GetStringASCII(std::string("type"), &reply_type
);
73 EXPECT_EQ("type", reply_type
);
74 std::string reply_data
;
75 dict
->GetStringASCII(std::string("data"), &reply_data
);
76 EXPECT_EQ("unserialized data", reply_data
);
79 TEST_F(WebIntentsHostTest
, TestVector
) {
80 webkit_glue::WebIntentData
data(ASCIIToUTF16("action"), ASCIIToUTF16("type"));
81 DictionaryValue
* d1
= new DictionaryValue
;
82 d1
->SetString(std::string("key1"), std::string("val1"));
83 data
.mime_data
.Append(d1
);
84 DictionaryValue
* d2
= new DictionaryValue
;
85 d2
->SetString(std::string("key2"), std::string("val2"));
86 data
.mime_data
.Append(d2
);
92 "d.action = window.webkitIntent.action;\n"
93 "d.type = window.webkitIntent.type;\n"
94 "d.data = window.webkitIntent.data;\n"
95 "window.webkitIntent.postResult(d);\n"
97 "<body>body</body></html>");
99 const IPC::Message
* reply_msg
=
100 render_thread_
->sink().GetUniqueMessageMatching(
101 IntentsHostMsg_WebIntentReply::ID
);
102 ASSERT_TRUE(reply_msg
);
103 scoped_ptr
<base::DictionaryValue
> dict(ParseValueFromReply(reply_msg
));
105 base::ListValue
* payload
;
106 ASSERT_TRUE(dict
->GetList("data", &payload
));
107 EXPECT_EQ(2U, payload
->GetSize());
109 base::DictionaryValue
* v1
= NULL
;
110 ASSERT_TRUE(payload
->GetDictionary(0, &v1
));
113 ASSERT_TRUE(v1
->GetStringASCII(std::string("key1"), &val1
));
114 EXPECT_EQ("val1", val1
);
116 base::DictionaryValue
* v2
= NULL
;
117 ASSERT_TRUE(payload
->GetDictionary(1, &v2
));
119 v2
->GetStringASCII(std::string("key2"), &val2
);
120 EXPECT_EQ("val2", val2
);
123 } // namespace content