| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef SERVICES_SHELL_PUBLIC_CPP_APPLICATION_TEST_BASE_H_ | 5 #ifndef SERVICES_SHELL_PUBLIC_CPP_APPLICATION_TEST_BASE_H_ |
| 6 #define SERVICES_SHELL_PUBLIC_CPP_APPLICATION_TEST_BASE_H_ | 6 #define SERVICES_SHELL_PUBLIC_CPP_APPLICATION_TEST_BASE_H_ |
| 7 | 7 |
| 8 #include <memory> |
| 9 |
| 8 #include "base/macros.h" | 10 #include "base/macros.h" |
| 9 #include "base/memory/scoped_ptr.h" | |
| 10 #include "mojo/public/cpp/bindings/array.h" | 11 #include "mojo/public/cpp/bindings/array.h" |
| 11 #include "mojo/public/cpp/bindings/string.h" | 12 #include "mojo/public/cpp/bindings/string.h" |
| 12 #include "services/shell/public/cpp/connector.h" | 13 #include "services/shell/public/cpp/connector.h" |
| 13 #include "services/shell/public/cpp/shell_client.h" | 14 #include "services/shell/public/cpp/shell_client.h" |
| 14 #include "services/shell/public/cpp/shell_connection.h" | 15 #include "services/shell/public/cpp/shell_connection.h" |
| 15 #include "services/shell/public/interfaces/shell_client.mojom.h" | 16 #include "services/shell/public/interfaces/shell_client.mojom.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 18 |
| 18 namespace mojo { | 19 namespace shell { |
| 19 | 20 |
| 20 class ShellConnection; | 21 class ShellConnection; |
| 21 | 22 |
| 22 namespace test { | 23 namespace test { |
| 23 | 24 |
| 24 // Run all application tests. This must be called after the environment is | 25 // Run all application tests. This must be called after the environment is |
| 25 // initialized, to support construction of a default run loop. | 26 // initialized, to support construction of a default run loop. |
| 26 MojoResult RunAllTests(MojoHandle shell_client_request_handle); | 27 MojoResult RunAllTests(MojoHandle shell_client_request_handle); |
| 27 | 28 |
| 28 // Used to configure the ShellConnection. This is used internally by | 29 // Used to configure the ShellConnection. This is used internally by |
| 29 // ApplicationTestBase, but useful if you do not want to subclass | 30 // ApplicationTestBase, but useful if you do not want to subclass |
| 30 // ApplicationTestBase. | 31 // ApplicationTestBase. |
| 31 class TestHelper { | 32 class TestHelper { |
| 32 public: | 33 public: |
| 33 explicit TestHelper(ShellClient* client); | 34 explicit TestHelper(ShellClient* client); |
| 34 ~TestHelper(); | 35 ~TestHelper(); |
| 35 | 36 |
| 36 Connector* connector() { return shell_connection_->connector(); } | 37 Connector* connector() { return shell_connection_->connector(); } |
| 37 const std::string& test_name() { return name_; } | 38 const std::string& test_name() { return name_; } |
| 38 const std::string& test_userid() { return userid_; } | 39 const std::string& test_userid() { return userid_; } |
| 39 uint32_t test_instance_id() { return instance_id_; } | 40 uint32_t test_instance_id() { return instance_id_; } |
| 40 | 41 |
| 41 private: | 42 private: |
| 42 // The application delegate used if GetShellClient is not overridden. | 43 // The application delegate used if GetShellClient is not overridden. |
| 43 ShellClient default_shell_client_; | 44 ShellClient default_shell_client_; |
| 44 | 45 |
| 45 // The application implementation instance, reconstructed for each test. | 46 // The application implementation instance, reconstructed for each test. |
| 46 scoped_ptr<ShellConnection> shell_connection_; | 47 std::unique_ptr<ShellConnection> shell_connection_; |
| 47 | 48 |
| 48 std::string name_; | 49 std::string name_; |
| 49 std::string userid_; | 50 std::string userid_; |
| 50 uint32_t instance_id_; | 51 uint32_t instance_id_; |
| 51 | 52 |
| 52 DISALLOW_COPY_AND_ASSIGN(TestHelper); | 53 DISALLOW_COPY_AND_ASSIGN(TestHelper); |
| 53 }; | 54 }; |
| 54 | 55 |
| 55 // A GTEST base class for application testing executed in mojo_shell. | 56 // A GTEST base class for application testing executed in mojo_shell. |
| 56 class ApplicationTestBase : public testing::Test { | 57 class ApplicationTestBase : public testing::Test { |
| 57 public: | 58 public: |
| 58 ApplicationTestBase(); | 59 ApplicationTestBase(); |
| 59 ~ApplicationTestBase() override; | 60 ~ApplicationTestBase() override; |
| 60 | 61 |
| 61 protected: | 62 protected: |
| 62 Connector* connector() { | 63 Connector* connector() { |
| 63 return test_helper_ ? test_helper_->connector() : nullptr; | 64 return test_helper_ ? test_helper_->connector() : nullptr; |
| 64 } | 65 } |
| 65 const std::string& test_name() const { | 66 const std::string& test_name() const { |
| 66 return test_helper_ ? test_helper_->test_name() : empty_; | 67 return test_helper_ ? test_helper_->test_name() : empty_; |
| 67 } | 68 } |
| 68 const std::string& test_userid() const { | 69 const std::string& test_userid() const { |
| 69 return test_helper_ ? test_helper_->test_userid() : inherit_user_id_; | 70 return test_helper_ ? test_helper_->test_userid() : inherit_user_id_; |
| 70 } | 71 } |
| 71 uint32_t test_instance_id() const { | 72 uint32_t test_instance_id() const { |
| 72 return test_helper_ ? test_helper_->test_instance_id() : | 73 return test_helper_ ? test_helper_->test_instance_id() |
| 73 shell::mojom::kInvalidInstanceID; | 74 : mojom::kInvalidInstanceID; |
| 74 } | 75 } |
| 75 | 76 |
| 76 // Get the ShellClient for the application to be tested. | 77 // Get the ShellClient for the application to be tested. |
| 77 virtual ShellClient* GetShellClient(); | 78 virtual ShellClient* GetShellClient(); |
| 78 | 79 |
| 79 // testing::Test: | 80 // testing::Test: |
| 80 void SetUp() override; | 81 void SetUp() override; |
| 81 void TearDown() override; | 82 void TearDown() override; |
| 82 | 83 |
| 83 // True by default, which indicates a MessageLoop will automatically be | 84 // True by default, which indicates a MessageLoop will automatically be |
| 84 // created for the application. Tests may override this function to prevent | 85 // created for the application. Tests may override this function to prevent |
| 85 // a default loop from being created. | 86 // a default loop from being created. |
| 86 virtual bool ShouldCreateDefaultRunLoop(); | 87 virtual bool ShouldCreateDefaultRunLoop(); |
| 87 | 88 |
| 88 private: | 89 private: |
| 89 scoped_ptr<TestHelper> test_helper_; | 90 std::unique_ptr<TestHelper> test_helper_; |
| 90 std::string empty_; | 91 std::string empty_; |
| 91 std::string inherit_user_id_ = shell::mojom::kInheritUserID; | 92 std::string inherit_user_id_ = mojom::kInheritUserID; |
| 92 | 93 |
| 93 DISALLOW_COPY_AND_ASSIGN(ApplicationTestBase); | 94 DISALLOW_COPY_AND_ASSIGN(ApplicationTestBase); |
| 94 }; | 95 }; |
| 95 | 96 |
| 96 } // namespace test | 97 } // namespace test |
| 97 | 98 |
| 98 } // namespace mojo | 99 } // namespace shell |
| 99 | 100 |
| 100 #endif // SERVICES_SHELL_PUBLIC_CPP_APPLICATION_TEST_BASE_H_ | 101 #endif // SERVICES_SHELL_PUBLIC_CPP_APPLICATION_TEST_BASE_H_ |
| OLD | NEW |