Chromium Code Reviews| Index: examples/python/python_apptest.cc |
| diff --git a/examples/python/python_apptest.cc b/examples/python/python_apptest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..5b64cf23a92e0ed27b796a3ac04de0f6f27ec8d9 |
| --- /dev/null |
| +++ b/examples/python/python_apptest.cc |
| @@ -0,0 +1,84 @@ |
| +// Copyright 2014 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 <stdio.h> |
| + |
| +#include "base/run_loop.h" |
| +#include "examples/python/echo_service.mojom.h" |
| +#include "mojo/public/c/system/main.h" |
| +#include "mojo/public/cpp/application/application_delegate.h" |
| +#include "mojo/public/cpp/application/application_impl.h" |
| +#include "mojo/public/cpp/application/application_runner.h" |
| +#include "mojo/public/cpp/application/application_test_base.h" |
| +#include "mojo/public/cpp/utility/run_loop.h" |
| + |
| +namespace mojo { |
| +namespace examples { |
| + |
| +class PythonApplicationTest : public test::ApplicationTestBase { |
|
qsr
2015/01/07 15:14:45
Did you think about using //example/apptest instea
etiennej
2015/01/08 11:23:12
//example/apptest requires more support code: in p
|
| + public: |
| + PythonApplicationTest() : ApplicationTestBase() {} |
| + ~PythonApplicationTest() override {} |
| + |
| + protected: |
| + // ApplicationTestBase: |
| + void SetUp() override { |
| + ApplicationTestBase::SetUp(); |
| + |
| + application_impl()->ConnectToService("mojo:py_service", &echo_service_); |
| + } |
| + |
| + EchoServicePtr echo_service_; |
| + |
| + private: |
| + MOJO_DISALLOW_COPY_AND_ASSIGN(PythonApplicationTest); |
| +}; |
| + |
| +class ResponseRecorder { |
| + public: |
| + void set_record(const String& value) { |
| + has_record_ = true; |
| + value_ = value; |
| + } |
| + |
| + bool has_record() { |
| + return has_record_; |
| + } |
| + |
| + const String& get_record() { |
| + return value_; |
| + } |
| + |
| + private: |
| + String value_; |
| + bool has_record_ = false; |
| +}; |
| + |
| +class ResponseCallback : public mojo::Callback<void(const String&)>::Runnable { |
| + public: |
| + ResponseCallback(ResponseRecorder* recorder) : recorder_(recorder) {}; |
| + ~ResponseCallback() {}; |
| + |
| + void Run(const String& value) const override { |
| + recorder_->set_record(value); |
| + base::MessageLoop::current()->QuitWhenIdle(); |
| + } |
| + |
| + ResponseRecorder* recorder_; |
| +}; |
| + |
| +TEST_F(PythonApplicationTest, CallMethodOnService) { |
| + // Verify that we can call a method on a python service and get an answer. |
| + const String& test_string = "Hello, World!"; |
| + ResponseRecorder responseRecoder; |
| + ResponseCallback responseCallback(&responseRecoder); |
| + echo_service_->EchoString(test_string, responseCallback); |
| + base::RunLoop().Run(); |
| + EXPECT_TRUE(responseRecoder.has_record()); |
| + EXPECT_TRUE(test_string == responseRecoder.get_record()); |
| +} |
| + |
| +} // namespace examples |
| +} // namespace mojo |
| + |