Sergey Kuznetsov | ae59440 | 2019-04-19 16:08:05 | [diff] [blame] | 1 | // Copyright 2019 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include <string> |
| 6 | |
| 7 | #include "base/bind.h" |
| 8 | #include "base/macros.h" |
| 9 | #include "base/test/bind_test_util.h" |
| 10 | #include "components/services/patch/public/interfaces/constants.mojom.h" |
| 11 | #include "components/services/patch/public/interfaces/file_patcher.mojom.h" |
| 12 | #include "components/services/unzip/public/interfaces/constants.mojom.h" |
| 13 | #include "components/services/unzip/public/interfaces/unzipper.mojom.h" |
Ken Rockot | eff2b82 | 2019-06-20 22:56:53 | [diff] [blame] | 14 | #include "content/public/browser/system_connector.h" |
Sergey Kuznetsov | ae59440 | 2019-04-19 16:08:05 | [diff] [blame] | 15 | #include "content/public/test/test_browser_thread_bundle.h" |
| 16 | #include "content/public/test/test_utils.h" |
| 17 | #include "services/service_manager/public/cpp/connector.h" |
| 18 | #include "testing/gtest/include/gtest/gtest.h" |
| 19 | |
| 20 | namespace { |
| 21 | |
| 22 | class ServicesTest : public testing::Test { |
| 23 | public: |
| 24 | ServicesTest() |
| 25 | : thread_bundle_(content::TestBrowserThreadBundle::MainThreadType::IO) {} |
| 26 | |
Sergey Kuznetsov | ae59440 | 2019-04-19 16:08:05 | [diff] [blame] | 27 | template <typename Interface> |
| 28 | bool CanAccessInterfaceFromBrowser(const std::string& service_name) { |
| 29 | mojo::InterfacePtr<Interface> interface; |
| 30 | connector()->BindInterface(service_name, mojo::MakeRequest(&interface)); |
| 31 | |
| 32 | // If the service is present, the interface will be connected and |
| 33 | // FlushForTesting will complete without an error on the interface. |
| 34 | // Conversely if there is a problem connecting to the interface, we will |
| 35 | // always hit the error handler before FlushForTesting returns. |
| 36 | bool encountered_error = false; |
| 37 | interface.set_connection_error_handler( |
| 38 | base::BindLambdaForTesting([&] { encountered_error = true; })); |
| 39 | interface.FlushForTesting(); |
| 40 | return !encountered_error; |
| 41 | } |
| 42 | |
| 43 | private: |
| 44 | service_manager::Connector* connector() { |
Ken Rockot | eff2b82 | 2019-06-20 22:56:53 | [diff] [blame] | 45 | return content::GetSystemConnector(); |
Sergey Kuznetsov | ae59440 | 2019-04-19 16:08:05 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | content::TestBrowserThreadBundle thread_bundle_; |
| 49 | content::InProcessUtilityThreadHelper in_process_utility_thread_helper_; |
| 50 | |
| 51 | DISALLOW_COPY_AND_ASSIGN(ServicesTest); |
| 52 | }; |
| 53 | |
| 54 | } // namespace |
| 55 | |
| 56 | TEST_F(ServicesTest, ConnectToUnzip) { |
| 57 | EXPECT_TRUE(CanAccessInterfaceFromBrowser<unzip::mojom::Unzipper>( |
| 58 | unzip::mojom::kServiceName)); |
| 59 | } |
| 60 | |
| 61 | TEST_F(ServicesTest, ConnectToFilePatch) { |
| 62 | EXPECT_TRUE(CanAccessInterfaceFromBrowser<patch::mojom::FilePatcher>( |
| 63 | patch::mojom::kServiceName)); |
| 64 | } |