summaryrefslogtreecommitdiffstats
path: root/ppapi/tests/test_post_message.cc
blob: df7d1815f0cf78d2d492135b02dc40c2da18fd19 (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Copyright (c) 2011 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 "ppapi/tests/test_post_message.h"

#include "ppapi/c/dev/ppb_testing_dev.h"
#include "ppapi/c/pp_var.h"
#include "ppapi/cpp/dev/scriptable_object_deprecated.h"
#include "ppapi/cpp/instance.h"
#include "ppapi/cpp/var.h"
#include "ppapi/tests/testing_instance.h"

REGISTER_TEST_CASE(PostMessage);

namespace {

const PPB_Testing_Dev* GetTestingInterface() {
  static const PPB_Testing_Dev* g_testing_interface =
      reinterpret_cast<PPB_Testing_Dev const*>(
          pp::Module::Get()->GetBrowserInterface(PPB_TESTING_DEV_INTERFACE));
  return g_testing_interface;
}

const char kTestString[] = "Hello world!";
const bool kTestBool = true;
const int32_t kTestInt = 42;
const double kTestDouble = 42.0;
const int32_t kThreadsToRun = 10;

}  // namespace

bool TestPostMessage::Init() {
  testing_interface_ = reinterpret_cast<const PPB_Testing_Dev*>(
      pp::Module::Get()->GetBrowserInterface(PPB_TESTING_DEV_INTERFACE));
  if (!testing_interface_) {
    // Give a more helpful error message for the testing interface being gone
    // since that needs special enabling in Chrome.
    instance_->AppendError("This test needs the testing interface, which is "
        "not currently available. In Chrome, use --enable-pepper-testing when "
        "launching.");
  }
  return (testing_interface_ != NULL);
}

void TestPostMessage::RunTest() {
  RUN_TEST(SendingData);
  RUN_TEST(MessageEvent);
  RUN_TEST(NoHandler);
}

void TestPostMessage::HandleMessage(const pp::Var& message_data) {
  message_data_.push_back(message_data);
  testing_interface_->QuitMessageLoop(instance_->pp_instance());
}

bool TestPostMessage::MakeOnMessageEcho(const std::string& expression) {
  std::string js_code(
      "document.getElementById('plugin').onmessage = function(message_event) {"
      "    document.getElementById('plugin').postMessage(");
  js_code += expression;
  js_code += ");}";
  pp::Var exception;
  // TODO(dmichael):  Move ExecuteScript to the testing interface.
  instance_->ExecuteScript(js_code, &exception);
  return(exception.is_undefined());
}

std::string TestPostMessage::TestSendingData() {
  // Set up the JavaScript onmessage handler to echo the data part of the
  // message event back to us.
  ASSERT_TRUE(MakeOnMessageEcho("message_event.data"));

  // Test sending a message to JavaScript for each supported type.  The JS sends
  // the data back to us, and we check that they match.
  message_data_.clear();
  instance_->PostMessage(pp::Var(kTestString));
  // PostMessage is asynchronous, so we should not receive a response yet.
  ASSERT_EQ(message_data_.size(), 0);

  testing_interface_->RunMessageLoop(instance_->pp_instance());
  ASSERT_EQ(message_data_.size(), 1);
  ASSERT_TRUE(message_data_.back().is_string());
  ASSERT_EQ(message_data_.back().AsString(), kTestString);

  message_data_.clear();
  instance_->PostMessage(pp::Var(kTestBool));
  ASSERT_EQ(message_data_.size(), 0);
  testing_interface_->RunMessageLoop(instance_->pp_instance());
  ASSERT_EQ(message_data_.size(), 1);
  ASSERT_TRUE(message_data_.back().is_bool());
  ASSERT_EQ(message_data_.back().AsBool(), kTestBool);

  message_data_.clear();
  instance_->PostMessage(pp::Var(kTestInt));
  ASSERT_EQ(message_data_.size(), 0);
  testing_interface_->RunMessageLoop(instance_->pp_instance());
  ASSERT_EQ(message_data_.size(), 1);
  ASSERT_TRUE(message_data_.back().is_number());
  ASSERT_DOUBLE_EQ(message_data_.back().AsDouble(),
                   static_cast<double>(kTestInt));

  message_data_.clear();
  instance_->PostMessage(pp::Var(kTestDouble));
  ASSERT_EQ(message_data_.size(), 0);
  testing_interface_->RunMessageLoop(instance_->pp_instance());
  ASSERT_EQ(message_data_.size(), 1);
  ASSERT_TRUE(message_data_.back().is_number());
  ASSERT_DOUBLE_EQ(message_data_.back().AsDouble(), kTestDouble);

  message_data_.clear();
  instance_->PostMessage(pp::Var());
  ASSERT_EQ(message_data_.size(), 0);
  testing_interface_->RunMessageLoop(instance_->pp_instance());
  ASSERT_EQ(message_data_.size(), 1);
  ASSERT_TRUE(message_data_.back().is_undefined());

  message_data_.clear();
  instance_->PostMessage(pp::Var(pp::Var::Null()));
  ASSERT_EQ(message_data_.size(), 0);
  testing_interface_->RunMessageLoop(instance_->pp_instance());
  ASSERT_EQ(message_data_.size(), 1);
  ASSERT_TRUE(message_data_.back().is_null());

  PASS();
}

std::string TestPostMessage::TestMessageEvent() {
  // Set up the JavaScript onmessage handler to pass us some values from the
  // MessageEvent and make sure they match our expectations.

  // Have onmessage pass back the type of message_event and make sure it's
  // "object".
  ASSERT_TRUE(MakeOnMessageEcho("typeof(message_event)"));
  message_data_.clear();
  instance_->PostMessage(pp::Var(kTestInt));
  ASSERT_EQ(message_data_.size(), 0);
  testing_interface_->RunMessageLoop(instance_->pp_instance());
  ASSERT_EQ(message_data_.size(), 1);
  ASSERT_TRUE(message_data_.back().is_string());
  ASSERT_EQ(message_data_.back().AsString(), "object");

  // Make sure all the non-data properties have the expected values.
  bool success = MakeOnMessageEcho("((message_event.origin == '')"
                                   " && (message_event.lastEventId == '')"
                                   " && (message_event.source == null)"
                                   " && (message_event.ports == null)"
                                   " && (message_event.bubbles == false)"
                                   " && (message_event.cancelable == false)"
                                   ")");
  ASSERT_TRUE(success);
  message_data_.clear();
  instance_->PostMessage(pp::Var(kTestInt));
  ASSERT_EQ(message_data_.size(), 0);
  testing_interface_->RunMessageLoop(instance_->pp_instance());
  ASSERT_EQ(message_data_.size(), 1);
  ASSERT_TRUE(message_data_.back().is_bool());
  ASSERT_TRUE(message_data_.back().AsBool());

  PASS();
}

std::string TestPostMessage::TestNoHandler() {
  // Delete the onmessage handler (if it exists)
  std::string js_code(
      "if (document.getElementById('plugin').onmessage) {"
      "  delete document.getElementById('plugin').onmessage;"
      "}");
  pp::Var exception;
  instance_->ExecuteScript(js_code, &exception);
  ASSERT_TRUE(exception.is_undefined());

  // Now send a message and make sure we don't get anything back (and that we
  // don't crash).
  message_data_.clear();
  instance_->PostMessage(pp::Var());
  testing_interface_->RunMessageLoop(instance_->pp_instance());
  ASSERT_EQ(message_data_.size(), 0);

  PASS();
}