summaryrefslogtreecommitdiffstats
path: root/webkit/glue/cpp_bound_class_unittest.cc
blob: 5edb23d7590b3b823e33aca0044345e00e0bcc01 (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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
// Copyright (c) 2006-2008 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.

// Tests for CppBoundClass, in conjunction with CppBindingExample.  Binds
// a CppBindingExample class into JavaScript in a custom test shell and tests
// the binding from the outside by loading JS into the shell.

#include <vector>

#include "base/message_loop.h"
#include "webkit/api/public/WebData.h"
#include "webkit/api/public/WebFrame.h"
#include "webkit/api/public/WebURL.h"
#include "webkit/api/public/WebView.h"
#include "webkit/glue/cpp_binding_example.h"
#include "webkit/glue/webkit_glue.h"
#include "webkit/tools/test_shell/test_shell_test.h"

using WebKit::WebFrame;

namespace {

class CppBindingExampleSubObject : public CppBindingExample {
 public:
  CppBindingExampleSubObject() {
    sub_value_.Set("sub!");
    BindProperty("sub_value", &sub_value_);
  }
 private:
  CppVariant sub_value_;
};


class CppBindingExampleWithOptionalFallback : public CppBindingExample {
 public:
  CppBindingExampleWithOptionalFallback() {
    BindProperty("sub_object", sub_object_.GetAsCppVariant());
  }

  void set_fallback_method_enabled(bool state) {
    BindFallbackMethod(state ?
        &CppBindingExampleWithOptionalFallback::fallbackMethod
        : NULL);
  }

  // The fallback method does nothing, but because of it the JavaScript keeps
  // running when a nonexistent method is called on an object.
  void fallbackMethod(const CppArgumentList& args, CppVariant* result) {
  }

 private:
  CppBindingExampleSubObject sub_object_;
};

class ExampleTestShell : public TestShell {
 public:

  ExampleTestShell(bool use_fallback_method) {
    example_bound_class_.set_fallback_method_enabled(use_fallback_method);
  }

  // When called by WebViewDelegate::WindowObjectCleared method, this binds a
  // CppExampleObject to window.example.
  virtual void BindJSObjectsToWindow(WebFrame* frame) {
    example_bound_class_.BindToJavascript(frame, L"example");
    // We use the layoutTestController binding for notifyDone.
    TestShell::BindJSObjectsToWindow(frame);
  }

  // This is a public interface to TestShell's protected method, so it
  // can be called by our CreateEmptyWindow.
  bool PublicInitialize(const std::string& starting_url) {
    return Initialize(GURL(starting_url));
  }

  CppBindingExampleWithOptionalFallback example_bound_class_;
};

class CppBoundClassTest : public TestShellTest {
 protected:
   // Adapted from TestShell::CreateNewWindow, this creates an
   // ExampleTestShellWindow rather than a regular TestShell.
   virtual void CreateEmptyWindow() {
     ExampleTestShell* host = new ExampleTestShell(useFallback());
     ASSERT_TRUE(host != NULL);
     bool rv = host->PublicInitialize("about:blank");
     if (rv) {
       test_shell_ = host;
       TestShell::windowList()->push_back(host->mainWnd());
       webframe_ = test_shell_->webView()->mainFrame();
       ASSERT_TRUE(webframe_ != NULL);
     } else {
       delete host;
     }
   }

   // Wraps the given JavaScript snippet in <html><body><script> tags, then
   // loads it into a webframe so it is executed.
   void ExecuteJavaScript(const std::string& javascript) {
     std::string html = "<html><body>";
     html.append(TestShellTest::kJavascriptDelayExitScript);
     html.append("<script>");
     html.append(javascript);
     html.append("</script></body></html>");
     // The base URL doesn't matter.
     webframe_->loadHTMLString(html, GURL("about:blank"));

     test_shell_->WaitTestFinished();
    }

   // Executes the specified JavaScript and checks to be sure that the resulting
   // document text is exactly "SUCCESS".
   void CheckJavaScriptSuccess(const std::string& javascript) {
     ExecuteJavaScript(javascript);
     EXPECT_EQ(L"SUCCESS", webkit_glue::DumpDocumentText(webframe_));
   }

   // Executes the specified JavaScript and checks that the resulting document
   // text is empty.
   void CheckJavaScriptFailure(const std::string& javascript) {
     ExecuteJavaScript(javascript);
     EXPECT_EQ(L"", webkit_glue::DumpDocumentText(webframe_));
   }

   // Constructs a JavaScript snippet that evaluates and compares the left and
   // right expressions, printing "SUCCESS" to the page if they are equal and
   // printing their actual values if they are not.  Any strings in the
   // expressions should be enclosed in single quotes, and no double quotes
   // should appear in either expression (even if escaped). (If a test case
   // is added that needs fancier quoting, Json::valueToQuotedString could be
   // used here.  For now, it's not worth adding the dependency.)
   std::string BuildJSCondition(std::string left, std::string right) {
     return "var leftval = " + left + ";" +
            "var rightval = " + right + ";" +
            "if (leftval == rightval) {" +
            "  document.writeln('SUCCESS');" +
            "} else {" +
            "  document.writeln(\"" +
                 left + " [\" + leftval + \"] != " +
                 right + " [\" + rightval + \"]\");" +
            "}";
   }

protected:
  virtual bool useFallback() {
    return false;
  }

private:
  WebFrame* webframe_;
};

class CppBoundClassWithFallbackMethodTest : public CppBoundClassTest {
protected:
  virtual bool useFallback() {
    return true;
  }
};

// Ensures that the example object has been bound to JS.
TEST_F(CppBoundClassTest, ObjectExists) {
  std::string js = BuildJSCondition("typeof window.example", "'object'");
  CheckJavaScriptSuccess(js);

  // An additional check to test our test.
  js = BuildJSCondition("typeof window.invalid_object", "'undefined'");
  CheckJavaScriptSuccess(js);
}

TEST_F(CppBoundClassTest, PropertiesAreInitialized) {
  std::string js = BuildJSCondition("example.my_value", "10");
  CheckJavaScriptSuccess(js);

  js = BuildJSCondition("example.my_other_value", "'Reinitialized!'");
  CheckJavaScriptSuccess(js);
}

TEST_F(CppBoundClassTest, SubOject) {
  std::string js = BuildJSCondition("typeof window.example.sub_object",
                                    "'object'");
  CheckJavaScriptSuccess(js);

  js = BuildJSCondition("example.sub_object.sub_value", "'sub!'");
  CheckJavaScriptSuccess(js);
}

TEST_F(CppBoundClassTest, SetAndGetProperties) {
  // The property on the left will be set to the value on the right, then
  // checked to make sure it holds that same value.
  static const std::string tests[] = {
    "example.my_value", "7",
    "example.my_value", "'test'",
    "example.my_other_value", "3.14",
    "example.my_other_value", "false",
    "" // Array end marker: insert additional test pairs before this.
  };

  for (int i = 0; tests[i] != ""; i += 2) {
    std::string left = tests[i];
    std::string right = tests[i + 1];
    // left = right;
    std::string js = left;
    js.append(" = ");
    js.append(right);
    js.append(";");
    js.append(BuildJSCondition(left, right));
    CheckJavaScriptSuccess(js);
  }
}

TEST_F(CppBoundClassTest, SetAndGetPropertiesWithCallbacks) {
  // TODO(dglazkov): fix NPObject issues around failing property setters and
  // getters and add tests for situations when GetProperty or SetProperty fail.
  std::string js = "var result = 'SUCCESS';\n"
    "example.my_value_with_callback = 10;\n"
    "if (example.my_value_with_callback != 10)\n"
    "  result = 'FAIL: unable to set property.';\n"
    "example.my_value_with_callback = 11;\n"
    "if (example.my_value_with_callback != 11)\n"
    "  result = 'FAIL: unable to set property again';\n"
    "if (example.same != 42)\n"
    "  result = 'FAIL: same property should always be 42';\n"
    "example.same = 24;\n"
    "if (example.same != 42)\n"
    "  result = 'FAIL: same property should always be 42';\n"
    "document.writeln(result);\n";
  CheckJavaScriptSuccess(js);
}

TEST_F(CppBoundClassTest, InvokeMethods) {
  // The expression on the left is expected to return the value on the right.
  static const std::string tests[] = {
    "example.echoValue(true)", "true",
    "example.echoValue(13)", "13",
    "example.echoValue(2.718)", "2.718",
    "example.echoValue('yes')", "'yes'",
    "example.echoValue()", "null",     // Too few arguments

    "example.echoType(false)", "true",
    "example.echoType(19)", "7",
    "example.echoType(9.876)", "3.14159",
    "example.echoType('test string')", "'Success!'",
    "example.echoType()", "null",      // Too few arguments

    // Comparing floats that aren't integer-valued is usually problematic due
    // to rounding, but exact powers of 2 should also be safe.
    "example.plus(2.5, 18.0)", "20.5",
    "example.plus(2, 3.25)", "5.25",
    "example.plus(2, 3)", "5",
    "example.plus()", "null",             // Too few arguments
    "example.plus(1)", "null",            // Too few arguments
    "example.plus(1, 'test')", "null",    // Wrong argument type
    "example.plus('test', 2)", "null",    // Wrong argument type
    "example.plus('one', 'two')", "null", // Wrong argument type
    "" // Array end marker: insert additional test pairs before this.
  };

  for (int i = 0; tests[i] != ""; i+= 2) {
    std::string left = tests[i];
    std::string right = tests[i + 1];
    std::string js = BuildJSCondition(left, right);
    CheckJavaScriptSuccess(js);
  }

  std::string js = "example.my_value = 3.25; example.my_other_value = 1.25;";
  js.append(BuildJSCondition(
      "example.plus(example.my_value, example.my_other_value)", "4.5"));
  CheckJavaScriptSuccess(js);
}

// Tests that invoking a nonexistent method with no fallback method stops the
// script's execution
TEST_F(CppBoundClassTest,
       InvokeNonexistentMethodNoFallback) {
  std::string js = "example.nonExistentMethod();document.writeln('SUCCESS');";
  CheckJavaScriptFailure(js);
}

// Ensures existent methods can be invoked successfully when the fallback method
// is used
TEST_F(CppBoundClassWithFallbackMethodTest,
       InvokeExistentMethodsWithFallback) {
  std::string js = BuildJSCondition("example.echoValue(34)", "34");
  CheckJavaScriptSuccess(js);
}

}  // namespace