Commit 0af4ef74 authored by Chris Lu's avatar Chris Lu Committed by Commit Bot

Add JavaScript callback result and return value params to FakeWebFrame

This will enable tests to receive callback executions from FakeWebFrame
and configure what FakeWebFrame passes to callback. Also allows for
configuring CanCallJavascriptFunction to return false.

Change-Id: I5b84d88c2df0e30ec7d81b48b0cf9a5c66a18c51
Reviewed-on: https://chromium-review.googlesource.com/c/1482136
Commit-Queue: Chris Lu <thegreenfrog@chromium.org>
Reviewed-by: default avatarEugene But <eugenebut@chromium.org>
Cr-Commit-Position: refs/heads/master@{#636083}
parent 7bd0891a
......@@ -4,11 +4,18 @@
#include "ios/web/public/test/fakes/fake_web_frame.h"
#include <string>
#include <utility>
#include "base/bind.h"
#include "base/callback.h"
#include "base/json/json_writer.h"
#include "base/task/post_task.h"
#include "base/values.h"
#include "ios/web/public/web_task_traits.h"
namespace web {
FakeWebFrame::FakeWebFrame(const std::string& frame_id,
bool is_main_frame,
GURL security_origin)
......@@ -28,12 +35,15 @@ GURL FakeWebFrame::GetSecurityOrigin() const {
return security_origin_;
}
bool FakeWebFrame::CanCallJavaScriptFunction() const {
return true;
return can_call_function_;
}
bool FakeWebFrame::CallJavaScriptFunction(
const std::string& name,
const std::vector<base::Value>& parameters) {
if (!can_call_function_) {
return false;
}
last_javascript_call_ = std::string("__gCrWeb." + name + "(");
bool first = true;
for (auto& param : parameters) {
......@@ -46,7 +56,7 @@ bool FakeWebFrame::CallJavaScriptFunction(
last_javascript_call_ += paramString;
}
last_javascript_call_ += ");";
return false;
return can_call_function_;
}
bool FakeWebFrame::CallJavaScriptFunction(
......@@ -54,7 +64,20 @@ bool FakeWebFrame::CallJavaScriptFunction(
const std::vector<base::Value>& parameters,
base::OnceCallback<void(const base::Value*)> callback,
base::TimeDelta timeout) {
return CallJavaScriptFunction(name, parameters);
bool success = CallJavaScriptFunction(name, parameters);
if (!success) {
return false;
}
const base::Value* js_result = result_map_[name].get();
base::PostTaskWithTraits(FROM_HERE, {WebThread::UI},
base::BindOnce(std::move(callback), js_result));
return true;
}
void FakeWebFrame::AddJsResultForFunctionCall(
std::unique_ptr<base::Value> js_result,
const std::string& function_name) {
result_map_[function_name] = std::move(js_result);
}
} // namespace web
......@@ -5,6 +5,9 @@
#ifndef IOS_WEB_PUBLIC_TEST_FAKES_FAKE_WEB_FRAME_H_
#define IOS_WEB_PUBLIC_TEST_FAKES_FAKE_WEB_FRAME_H_
#include <map>
#include <memory>
#include "ios/web/public/web_state/web_frame.h"
namespace web {
......@@ -24,8 +27,9 @@ class FakeWebFrame : public WebFrame {
bool CallJavaScriptFunction(
const std::string& name,
const std::vector<base::Value>& parameters) override;
// This method will not call JavaScript and immediately return false.
// |callback| will not be called.
// This method will not call JavaScript and will return the value of
// |can_call_function_|. Will execute callback with value passed in to
// AddJsResultForFunctionCall(). If no such value exists, will pass null.
bool CallJavaScriptFunction(
std::string name,
const std::vector<base::Value>& parameters,
......@@ -33,7 +37,22 @@ class FakeWebFrame : public WebFrame {
base::TimeDelta timeout) override;
std::string last_javascript_call() { return last_javascript_call_; }
// Sets |js_result| that will be passed into callback for |name| function
// call. The same result will be pass regardless of call arguments.
void AddJsResultForFunctionCall(std::unique_ptr<base::Value> js_result,
const std::string& function_name);
// Sets return value |can_call_function_| of CanCallJavaScriptFunction(),
// which defaults to true.
void set_can_call_function(bool can_call_function) {
can_call_function_ = can_call_function;
}
private:
// Map holding values to be passed in CallJavaScriptFunction() callback. Keyed
// by JavaScript function |name| expected to be passed into
// CallJavaScriptFunction().
std::map<std::string, std::unique_ptr<base::Value>> result_map_;
// The frame identifier which uniquely identifies this frame across the
// application's lifetime.
std::string frame_id_;
......@@ -43,6 +62,8 @@ class FakeWebFrame : public WebFrame {
GURL security_origin_;
// The last Javascript script that was called, converted as a string.
std::string last_javascript_call_;
// The return value of CanCallJavaScriptFunction().
bool can_call_function_ = true;
};
} // namespace web
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment