summaryrefslogtreecommitdiffstats
path: root/content/renderer/web_intents_host_browsertest.cc
blob: abe7adb865a4ee96b94d7b4890d1e8e964b0c2a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright (c) 2012 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 "base/gtest_prod_util.h"
#include "base/values.h"
#include "base/utf_string_conversions.h"
#include "content/common/intents_messages.h"
#include "content/public/renderer/v8_value_converter.h"
#include "content/public/test/render_view_test.h"
#include "content/renderer/render_view_impl.h"
#include "content/renderer/web_intents_host.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebDeliveredIntentClient.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebSerializedScriptValue.h"
#include "webkit/glue/web_intent_data.h"

namespace content {

class WebIntentsHostTest : public RenderViewTest {
 protected:
  RenderViewImpl* view() { return static_cast<RenderViewImpl*>(view_); }
  WebIntentsHost* web_intents_host() { return view()->intents_host_; }

  void SetIntentData(const webkit_glue::WebIntentData& data) {
    web_intents_host()->OnSetIntent(data);
  }

  base::DictionaryValue* ParseValueFromReply(const IPC::Message* reply_msg) {
    IntentsHostMsg_WebIntentReply::Param reply_params;
    IntentsHostMsg_WebIntentReply::Read(reply_msg, &reply_params);
    WebKit::WebSerializedScriptValue serialized_data =
        WebKit::WebSerializedScriptValue::fromString(reply_params.a.data);

    v8::HandleScope scope;
    v8::Local<v8::Context> ctx = GetMainFrame()->mainWorldScriptContext();
    v8::Context::Scope cscope(ctx);
    v8::Handle<v8::Value> v8_val = serialized_data.deserialize();
    scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create());
    base::Value* reply = converter->FromV8Value(v8_val, ctx);
    EXPECT_TRUE(reply->IsType(base::Value::TYPE_DICTIONARY));
    base::DictionaryValue* dict = NULL;
    reply->GetAsDictionary(&dict);
    return dict;
  }
};

TEST_F(WebIntentsHostTest, TestUnserialized) {
  webkit_glue::WebIntentData data(ASCIIToUTF16("action"), ASCIIToUTF16("type"),
                                  ASCIIToUTF16("unserialized data"));
  SetIntentData(data);

  LoadHTML("<html>"
           "<head><script>\n"
           "var d = {};\n"
           "d.action = window.webkitIntent.action;\n"
           "d.type = window.webkitIntent.type;\n"
           "d.data = window.webkitIntent.data;\n"
           "window.webkitIntent.postResult(d);\n"
           "</script></head>"
           "<body>body</body></html>");

  const IPC::Message* reply_msg =
      render_thread_->sink().GetUniqueMessageMatching(
          IntentsHostMsg_WebIntentReply::ID);
  ASSERT_TRUE(reply_msg);
  scoped_ptr<base::DictionaryValue> dict(ParseValueFromReply(reply_msg));

  std::string reply_action;
  dict->GetStringASCII(std::string("action"), &reply_action);
  EXPECT_EQ("action", reply_action);
  std::string reply_type;
  dict->GetStringASCII(std::string("type"), &reply_type);
  EXPECT_EQ("type", reply_type);
  std::string reply_data;
  dict->GetStringASCII(std::string("data"), &reply_data);
  EXPECT_EQ("unserialized data", reply_data);
}

TEST_F(WebIntentsHostTest, TestVector) {
  webkit_glue::WebIntentData data(ASCIIToUTF16("action"), ASCIIToUTF16("type"));
  DictionaryValue* d1 = new DictionaryValue;
  d1->SetString(std::string("key1"), std::string("val1"));
  data.mime_data.Append(d1);
  DictionaryValue* d2 = new DictionaryValue;
  d2->SetString(std::string("key2"), std::string("val2"));
  data.mime_data.Append(d2);
  SetIntentData(data);

  LoadHTML("<html>"
           "<head><script>\n"
           "var d = {};\n"
           "d.action = window.webkitIntent.action;\n"
           "d.type = window.webkitIntent.type;\n"
           "d.data = window.webkitIntent.data;\n"
           "window.webkitIntent.postResult(d);\n"
           "</script></head>"
           "<body>body</body></html>");

  const IPC::Message* reply_msg =
      render_thread_->sink().GetUniqueMessageMatching(
          IntentsHostMsg_WebIntentReply::ID);
  ASSERT_TRUE(reply_msg);
  scoped_ptr<base::DictionaryValue> dict(ParseValueFromReply(reply_msg));

  base::ListValue* payload;
  ASSERT_TRUE(dict->GetList("data", &payload));
  EXPECT_EQ(2U, payload->GetSize());

  base::DictionaryValue* v1 = NULL;
  ASSERT_TRUE(payload->GetDictionary(0, &v1));
  DCHECK(v1);
  std::string val1;
  ASSERT_TRUE(v1->GetStringASCII(std::string("key1"), &val1));
  EXPECT_EQ("val1", val1);

  base::DictionaryValue* v2 = NULL;
  ASSERT_TRUE(payload->GetDictionary(1, &v2));
  std::string val2;
  v2->GetStringASCII(std::string("key2"), &val2);
  EXPECT_EQ("val2", val2);
}

}  // namespace content