Skip to content

Commit

Permalink
Move existing Mojo JS <--> CPP tests out of webui test harness.
Browse files Browse the repository at this point in the history
The mojo webui test in content_browsertests was not the proper place to
perform bindings-related fuzzing (though it was initially convenient).  As a
 first step, move the existing tests to the new "framework" sky added in
  https://src.chromium.org/viewvc/chrome?revision=267208&view=revision

This also restores the webui code to something near its original purpose,
although I've kept a few of the hooks that may be useful as real webui 
tests are added.

R=sky@chromium.org

Review URL: https://codereview.chromium.org/268593002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@267703 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
tsepez@chromium.org committed May 2, 2014
1 parent 0a54cd5 commit b4d4bbb
Show file tree
Hide file tree
Showing 7 changed files with 273 additions and 278 deletions.
152 changes: 0 additions & 152 deletions content/browser/webui/web_ui_mojo_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,34 +32,6 @@ namespace content {
namespace {

bool got_message = false;
int message_count = 0;

const int kExpectedMessageCount = 100;

// Negative numbers with different values in each byte, the last of
// which can survive promotion to double and back.
const int8 kExpectedInt8Value = -65;
const int16 kExpectedInt16Value = -16961;
const int32 kExpectedInt32Value = -1145258561;
const int64 kExpectedInt64Value = -77263311946305LL;

// Positive numbers with different values in each byte, the last of
// which can survive promotion to double and back.
const uint8 kExpectedUInt8Value = 65;
const uint16 kExpectedUInt16Value = 16961;
const uint32 kExpectedUInt32Value = 1145258561;
const uint64 kExpectedUInt64Value = 77263311946305LL;

// Double/float values, including special case constants.
const double kExpectedDoubleVal = 3.14159265358979323846;
const double kExpectedDoubleInf = std::numeric_limits<double>::infinity();
const double kExpectedDoubleNan = std::numeric_limits<double>::quiet_NaN();
const float kExpectedFloatVal = static_cast<float>(kExpectedDoubleVal);
const float kExpectedFloatInf = std::numeric_limits<float>::infinity();
const float kExpectedFloatNan = std::numeric_limits<float>::quiet_NaN();

// NaN has the property that it is not equal to itself.
#define EXPECT_NAN(x) EXPECT_NE(x, x)

// The bindings for the page are generated from a .mojom file. This code looks
// up the generated file from disk and returns it.
Expand Down Expand Up @@ -97,11 +69,6 @@ class BrowserTargetImpl : public mojo::BrowserTarget {
NOTREACHED();
}

virtual void EchoResponse(const mojo::EchoArgs& arg1,
const mojo::EchoArgs& arg2) OVERRIDE {
NOTREACHED();
}

protected:
mojo::RemotePtr<mojo::RendererTarget> client_;
base::RunLoop* run_loop_;
Expand Down Expand Up @@ -131,76 +98,6 @@ class PingBrowserTargetImpl : public BrowserTargetImpl {
DISALLOW_COPY_AND_ASSIGN(PingBrowserTargetImpl);
};

class EchoBrowserTargetImpl : public BrowserTargetImpl {
public:
EchoBrowserTargetImpl(mojo::ScopedRendererTargetHandle handle,
base::RunLoop* run_loop)
: BrowserTargetImpl(handle, run_loop) {
mojo::AllocationScope scope;
mojo::EchoArgs::Builder builder;
builder.set_si64(kExpectedInt64Value);
builder.set_si32(kExpectedInt32Value);
builder.set_si16(kExpectedInt16Value);
builder.set_si8(kExpectedInt8Value);
builder.set_ui64(kExpectedUInt64Value);
builder.set_ui32(kExpectedUInt32Value);
builder.set_ui16(kExpectedUInt16Value);
builder.set_ui8(kExpectedUInt8Value);
builder.set_float_val(kExpectedFloatVal);
builder.set_float_inf(kExpectedFloatInf);
builder.set_float_nan(kExpectedFloatNan);
builder.set_double_val(kExpectedDoubleVal);
builder.set_double_inf(kExpectedDoubleInf);
builder.set_double_nan(kExpectedDoubleNan);
builder.set_name("coming");
mojo::Array<mojo::String>::Builder string_array(3);
string_array[0] = "one";
string_array[1] = "two";
string_array[2] = "three";
builder.set_string_array(string_array.Finish());
client_->Echo(builder.Finish());
}

virtual ~EchoBrowserTargetImpl() {}

// mojo::BrowserTarget overrides:
// Check the response, and quit the RunLoop after N calls.
virtual void EchoResponse(const mojo::EchoArgs& arg1,
const mojo::EchoArgs& arg2) OVERRIDE {
EXPECT_EQ(kExpectedInt64Value, arg1.si64());
EXPECT_EQ(kExpectedInt32Value, arg1.si32());
EXPECT_EQ(kExpectedInt16Value, arg1.si16());
EXPECT_EQ(kExpectedInt8Value, arg1.si8());
EXPECT_EQ(kExpectedUInt64Value, arg1.ui64());
EXPECT_EQ(kExpectedUInt32Value, arg1.ui32());
EXPECT_EQ(kExpectedUInt16Value, arg1.ui16());
EXPECT_EQ(kExpectedUInt8Value, arg1.ui8());
EXPECT_EQ(kExpectedFloatVal, arg1.float_val());
EXPECT_EQ(kExpectedFloatInf, arg1.float_inf());
EXPECT_NAN(arg1.float_nan());
EXPECT_EQ(kExpectedDoubleVal, arg1.double_val());
EXPECT_EQ(kExpectedDoubleInf, arg1.double_inf());
EXPECT_NAN(arg1.double_nan());
EXPECT_EQ(std::string("coming"), arg1.name().To<std::string>());
EXPECT_EQ(std::string("one"), arg1.string_array()[0].To<std::string>());
EXPECT_EQ(std::string("two"), arg1.string_array()[1].To<std::string>());
EXPECT_EQ(std::string("three"), arg1.string_array()[2].To<std::string>());

EXPECT_EQ(-1, arg2.si64());
EXPECT_EQ(-1, arg2.si32());
EXPECT_EQ(-1, arg2.si16());
EXPECT_EQ(-1, arg2.si8());
EXPECT_EQ(std::string("going"), arg2.name().To<std::string>());

message_count += 1;
if (message_count == kExpectedMessageCount)
run_loop_->Quit();
}

private:
DISALLOW_COPY_AND_ASSIGN(EchoBrowserTargetImpl);
};

// WebUIController that sets up mojo bindings.
class TestWebUIController : public WebUIController {
public:
Expand Down Expand Up @@ -242,27 +139,6 @@ class PingTestWebUIController : public TestWebUIController {
DISALLOW_COPY_AND_ASSIGN(PingTestWebUIController);
};

// TestWebUIController that additionally creates the echo test BrowserTarget
// implementation at the right time.
class EchoTestWebUIController : public TestWebUIController {
public:
EchoTestWebUIController(WebUI* web_ui, base::RunLoop* run_loop)
: TestWebUIController(web_ui, run_loop) {
}

// WebUIController overrides:
virtual void RenderViewCreated(RenderViewHost* render_view_host) OVERRIDE {
mojo::InterfacePipe<mojo::BrowserTarget, mojo::RendererTarget> pipe;
browser_target_.reset(new EchoBrowserTargetImpl(
pipe.handle_to_peer.Pass(), run_loop_));
render_view_host->SetWebUIHandle(
mojo::ScopedMessagePipeHandle(pipe.handle_to_self.release()));
}

private:
DISALLOW_COPY_AND_ASSIGN(EchoTestWebUIController);
};

// WebUIControllerFactory that creates TestWebUIController.
class TestWebUIControllerFactory : public WebUIControllerFactory {
public:
Expand All @@ -274,8 +150,6 @@ class TestWebUIControllerFactory : public WebUIControllerFactory {
WebUI* web_ui, const GURL& url) const OVERRIDE {
if (url.query() == "ping")
return new PingTestWebUIController(web_ui, run_loop_);
if (url.query() == "echo")
return new EchoTestWebUIController(web_ui, run_loop_);
return NULL;
}
virtual WebUI::TypeID GetWebUIType(BrowserContext* browser_context,
Expand Down Expand Up @@ -341,31 +215,5 @@ IN_PROC_BROWSER_TEST_F(WebUIMojoTest, EndToEndPing) {
EXPECT_TRUE(got_message);
}

// Loads a webui page that contains mojo bindings and verifies that
// parameters are passed back correctly from JavaScript.
IN_PROC_BROWSER_TEST_F(WebUIMojoTest, EndToEndEcho) {
// Currently there is no way to have a generated file included in the isolate
// files. If the bindings file doesn't exist assume we're on such a bot and
// pass.
// TODO(sky): remove this conditional when isolates support copying from gen.
const base::FilePath test_file_path(
mojo::test::GetFilePathForJSResource(
"content/test/data/web_ui_test_mojo_bindings.mojom"));
if (!base::PathExists(test_file_path)) {
LOG(WARNING) << " mojom binding file doesn't exist, assuming on isolate";
return;
}

message_count = 0;
ASSERT_TRUE(test_server()->Start());
base::RunLoop run_loop;
factory()->set_run_loop(&run_loop);
GURL test_url(test_server()->GetURL("files/web_ui_mojo.html?echo"));
NavigateToURL(shell(), test_url);
// RunLoop is quit when response received from page.
run_loop.Run();
EXPECT_EQ(kExpectedMessageCount, message_count);
}

} // namespace
} // namespace content
29 changes: 1 addition & 28 deletions content/test/data/web_ui_mojo.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ define('main', [
'mojo/public/js/bindings/connection',
'content/test/data/web_ui_test_mojo_bindings.mojom',
], function (connection, bindings) {
var retainedConnection, kIterations = 100, kBadValue = 13;
var retainedConnection;

function RendererTargetTest(bindings) {
this.bindings_ = bindings;
Expand All @@ -23,33 +23,6 @@ define('main', [
this.bindings_.pingResponse();
};

RendererTargetTest.prototype.echo = function (arg) {
var i;

// Ensure negative values are negative.
if (arg.si64 > 0)
arg.si64 = kBadValue;

if (arg.si32 > 0)
arg.si32 = kBadValue;

if (arg.si16 > 0)
arg.si16 = kBadValue;

if (arg.si8 > 0)
arg.si8 = kBadValue;

for (i = 0; i < kIterations; ++i) {
arg2 = new bindings.EchoArgs();
arg2.si64 = -1;
arg2.si32 = -1;
arg2.si16 = -1;
arg2.si8 = -1;
arg2.name = "going";
this.bindings_.echoResponse(arg, arg2);
}
};

return function(handle) {
retainedConnection = new connection.Connection(
handle, RendererTargetTest, bindings.BrowserTargetProxy);
Expand Down
23 changes: 0 additions & 23 deletions content/test/data/web_ui_test_mojo_bindings.mojom
Original file line number Diff line number Diff line change
@@ -1,36 +1,13 @@
module mojo {

// This struct encompasses all of the basic types, so that they
// may be sent from C++ to JS and back for validation.
struct EchoArgs {
int64 si64;
int32 si32;
int16 si16;
int8 si8;
uint64 ui64;
uint32 ui32;
uint16 ui16;
uint8 ui8;
float float_val;
float float_inf;
float float_nan;
double double_val;
double double_inf;
double double_nan;
string name;
string[] string_array;
};

[Peer=RendererTarget]
interface BrowserTarget {
PingResponse();
EchoResponse(EchoArgs arg1, EchoArgs arg2);
};

[Peer=BrowserTarget]
interface RendererTarget {
Ping();
Echo(EchoArgs arg);
};

}
37 changes: 37 additions & 0 deletions mojo/apps/js/test/js_to_cpp.mojom
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module js_to_cpp {

// This struct encompasses all of the basic types, so that they
// may be sent from C++ to JS and back for validation.
struct EchoArgs {
int64 si64;
int32 si32;
int16 si16;
int8 si8;
uint64 ui64;
uint32 ui32;
uint16 ui16;
uint8 ui8;
float float_val;
float float_inf;
float float_nan;
double double_val;
double double_inf;
double double_nan;
string name;
string[] string_array;
};

[Peer=JsSide]
interface CppSide {
StartTest(); // Sent to notify that the JS side is now ready.
PingResponse();
EchoResponse(EchoArgs arg1, EchoArgs arg2);
};

[Peer=CppSide]
interface JsSide {
Ping();
Echo(EchoArgs arg);
};

}
Loading

0 comments on commit b4d4bbb

Please sign in to comment.