Chromium Code Reviews| Index: mojo/apps/js/test/js_to_cpp_unittest.cc |
| diff --git a/mojo/apps/js/test/js_to_cpp_unittest.cc b/mojo/apps/js/test/js_to_cpp_unittest.cc |
| index 3b19142275a3623c89cb2e943a31b32602be5325..6e52d38922b02cea6899127bf14dea01b47e3b1c 100644 |
| --- a/mojo/apps/js/test/js_to_cpp_unittest.cc |
| +++ b/mojo/apps/js/test/js_to_cpp_unittest.cc |
| @@ -11,72 +11,198 @@ |
| #include "mojo/apps/js/mojo_runner_delegate.h" |
| #include "mojo/common/common_type_converters.h" |
| #include "mojo/common/test/test_utils.h" |
| +#include "mojo/public/cpp/bindings/allocation_scope.h" |
| #include "mojo/public/cpp/bindings/remote_ptr.h" |
| #include "mojo/public/cpp/environment/environment.h" |
| #include "mojo/public/cpp/system/core.h" |
| #include "mojo/public/cpp/system/macros.h" |
| -#include "mojo/public/interfaces/bindings/tests/sample_interfaces.mojom.h" |
| +#include "mojo/public/interfaces/bindings/tests/js_to_cpp.mojom.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| namespace mojo { |
| namespace js { |
| namespace { |
| +bool got_message = false; |
| +int message_count = 0; |
|
sky
2014/05/01 03:20:38
Can you scope these to JsToCppTest (or the subclas
Tom Sepez
2014/05/01 17:48:26
Moved to PingCppSideConnection / EchoCppSideConnec
|
| + |
| +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) |
| + |
| +bool IsRunningOnIsolatedBot() { |
| + // TODO(yzshen): Remove this check once isolated tests are supported on the |
| + // Chromium waterfall. (http://crbug.com/351214) |
| + const base::FilePath test_file_path( |
| + test::GetFilePathForJSResource( |
| + "mojo/public/interfaces/bindings/tests/sample_interfaces.mojom")); |
| + if (!base::PathExists(test_file_path)) { |
| + LOG(WARNING) << "Mojom binding files don't exist. Skipping the test."; |
| + return true; |
| + } |
| + return false; |
| +} |
| + |
| // Base Provider implementation class. It's expected that tests subclass and |
| // override the appropriate Provider functions. When test is done quit the |
| // run_loop(). |
| -class ProviderConnection : public sample::Provider { |
| +class CppSideConnection : public js_to_cpp::CppSide { |
| public: |
| - ProviderConnection() : run_loop_(NULL), client_(NULL) { |
| + CppSideConnection() : run_loop_(NULL), client_(NULL) { |
| } |
| - virtual ~ProviderConnection() {} |
| + virtual ~CppSideConnection() {} |
| void set_run_loop(base::RunLoop* run_loop) { run_loop_ = run_loop; } |
| base::RunLoop* run_loop() { return run_loop_; } |
| - void set_client(sample::ProviderClient* client) { client_ = client; } |
| - sample::ProviderClient* client() { return client_; } |
| + void set_client(js_to_cpp::JsSide* client) { client_ = client; } |
| + js_to_cpp::JsSide* client() { return client_; } |
| - // sample::Provider: |
| - virtual void EchoString(const String& a, |
| - const Callback<void(String)>& callback) OVERRIDE { |
| + // js_to_cpp::CppSide: |
| + virtual void StartTest() OVERRIDE { |
| NOTREACHED(); |
| } |
| - virtual void EchoStrings( |
| - const String& a, |
| - const String& b, |
| - const Callback<void(String, String)>& callback) OVERRIDE { |
| + |
| + virtual void PingResponse() OVERRIDE { |
| NOTREACHED(); |
| } |
| - virtual void EchoMessagePipeHandle( |
| - ScopedMessagePipeHandle a, |
| - const Callback<void(ScopedMessagePipeHandle)>& callback) OVERRIDE { |
| + |
| + virtual void EchoResponse(const js_to_cpp::EchoArgs& arg1, |
| + const js_to_cpp::EchoArgs& arg2) OVERRIDE { |
| NOTREACHED(); |
| } |
| - virtual void EchoEnum(sample::Enum a, |
| - const Callback<void(sample::Enum)>& callback) |
| - OVERRIDE { |
| - NOTREACHED(); |
| + |
| + protected: |
| + base::RunLoop* run_loop_; |
| + js_to_cpp::JsSide* client_; |
| + |
| + private: |
| + Environment environment; |
| + DISALLOW_COPY_AND_ASSIGN(CppSideConnection); |
| +}; |
| + |
| +// Trivial test to verify a message sent from JS is received. |
| +class PingCppSideConnection : public CppSideConnection { |
| + public: |
| + explicit PingCppSideConnection() {} |
| + virtual ~PingCppSideConnection() {} |
| + |
| + // js_to_cpp::CppSide: |
| + virtual void StartTest() OVERRIDE { |
| + client_->Ping(); |
| + } |
| + |
| + virtual void PingResponse() OVERRIDE { |
| + got_message = true; |
| + run_loop()->Quit(); |
| } |
| private: |
| - base::RunLoop* run_loop_; |
| - sample::ProviderClient* client_; |
| + DISALLOW_COPY_AND_ASSIGN(PingCppSideConnection); |
| +}; |
| - DISALLOW_COPY_AND_ASSIGN(ProviderConnection); |
| +// Test that parameters are passed with correct values. |
| +class EchoCppSideConnection : public CppSideConnection { |
| + public: |
| + explicit EchoCppSideConnection() {} |
| + virtual ~EchoCppSideConnection() {} |
| + |
| + // js_to_cpp::CppSide: |
| + virtual void StartTest() OVERRIDE { |
| + AllocationScope scope; |
| + js_to_cpp::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 void EchoResponse(const js_to_cpp::EchoArgs& arg1, |
| + const js_to_cpp::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(EchoCppSideConnection); |
| }; |
| class JsToCppTest : public testing::Test { |
| public: |
| JsToCppTest() {} |
| - void RunTest(const std::string& test, ProviderConnection* provider) { |
| - provider->set_run_loop(&run_loop_); |
| - InterfacePipe<sample::Provider, sample::ProviderClient> pipe; |
| - RemotePtr<sample::ProviderClient> provider_client; |
| - provider_client.reset(pipe.handle_to_peer.Pass(), provider); |
| - |
| - provider->set_client(provider_client.get()); |
| + void RunTest(const std::string& test, CppSideConnection* cpp_side) { |
| + cpp_side->set_run_loop(&run_loop_); |
| + InterfacePipe<js_to_cpp::CppSide, js_to_cpp::JsSide> pipe; |
| + RemotePtr<js_to_cpp::JsSide> js_side; |
| + js_side.reset(pipe.handle_to_peer.Pass(), cpp_side); |
| + cpp_side->set_client(js_side.get()); |
| gin::IsolateHolder instance(gin::IsolateHolder::kStrictMode); |
| apps::MojoRunnerDelegate delegate; |
| @@ -88,49 +214,29 @@ class JsToCppTest : public testing::Test { |
| } |
| private: |
| - Environment environment; |
| base::MessageLoop loop; |
| base::RunLoop run_loop_; |
| DISALLOW_COPY_AND_ASSIGN(JsToCppTest); |
| }; |
| -// Trivial test to verify a message sent from JS is received. |
| -class FromJsProviderConnection : public ProviderConnection { |
| - public: |
| - explicit FromJsProviderConnection() {} |
| - virtual ~FromJsProviderConnection() { |
| - } |
| - |
| - const base::string16& echo_string() const { return echo_string_; } |
| - |
| - // Provider: |
| - virtual void EchoString(const String& a, |
| - const Callback<void(String)>& callback) OVERRIDE { |
| - echo_string_ = a.To<base::string16>(); |
| - run_loop()->Quit(); |
| - } |
| - |
| - private: |
| - base::string16 echo_string_; |
| - |
| - DISALLOW_COPY_AND_ASSIGN(FromJsProviderConnection); |
| -}; |
| +TEST_F(JsToCppTest, Ping) { |
| + if (IsRunningOnIsolatedBot()) |
| + return; |
| + got_message = false; |
| + PingCppSideConnection cpp_side_connection; |
| + RunTest("mojo/apps/js/test/js_to_cpp_unittest", &cpp_side_connection); |
| + EXPECT_TRUE(got_message); |
| +} |
| -TEST_F(JsToCppTest, FromJS) { |
| - // TODO(yzshen): Remove this check once isolated tests are supported on the |
| - // Chromium waterfall. (http://crbug.com/351214) |
| - const base::FilePath test_file_path( |
| - test::GetFilePathForJSResource( |
| - "mojo/public/interfaces/bindings/tests/sample_interfaces.mojom")); |
| - if (!base::PathExists(test_file_path)) { |
| - LOG(WARNING) << "Mojom binding files don't exist. Skipping the test."; |
| +TEST_F(JsToCppTest, Echo) { |
| + if (IsRunningOnIsolatedBot()) |
| return; |
| - } |
| - FromJsProviderConnection provider; |
| - RunTest("mojo/apps/js/test/js_to_cpp_unittest", &provider); |
| - EXPECT_EQ("message", base::UTF16ToASCII(provider.echo_string())); |
| + message_count = 0; |
| + EchoCppSideConnection cpp_side_connection; |
| + RunTest("mojo/apps/js/test/js_to_cpp_unittest", &cpp_side_connection); |
| + EXPECT_EQ(kExpectedMessageCount, message_count); |
| } |
| } // namespace |