From 8327e1ff71a015c5e9ec06163f4a8ae0de07df9e Mon Sep 17 00:00:00 2001 From: Pritesh Nandgaonkar Date: Fri, 23 Nov 2018 03:40:41 -0800 Subject: [PATCH] Added background plugin tests in flipper client Summary: Added tests for flipper client which makes sure that didconnect is called in the case of background plugin. It also checks the case of a non background plugin. Reviewed By: jknoxville Differential Revision: D13152686 fbshipit-source-id: 7850f66a42d669243f656a1e1c26584869ee919f --- xplat/FlipperTestLib/FlipperPluginMock.h | 18 ++++++++-- xplat/FlipperTests/FlipperClientTests.cpp | 43 +++++++++++++++++++++-- 2 files changed, 57 insertions(+), 4 deletions(-) diff --git a/xplat/FlipperTestLib/FlipperPluginMock.h b/xplat/FlipperTestLib/FlipperPluginMock.h index 20a27b64f..02becf427 100644 --- a/xplat/FlipperTestLib/FlipperPluginMock.h +++ b/xplat/FlipperTestLib/FlipperPluginMock.h @@ -1,11 +1,10 @@ /* - * Copyright (c) 2018-present, Facebook, Inc. + * Copyright (c) Facebook, Inc. * * This source code is licensed under the MIT license found in the LICENSE * file in the root directory of this source tree. * */ - #pragma once #include @@ -35,6 +34,16 @@ class FlipperPluginMock : public FlipperPlugin { connectionCallback_(connectionCallback), disconnectionCallback_(disconnectionCallback) {} + FlipperPluginMock( + const std::string& identifier, + const ConnectionCallback& connectionCallback, + const DisconnectionCallback& disconnectionCallback, + bool runInBackground) + : identifier_(identifier), + runInBackground_(runInBackground), + connectionCallback_(connectionCallback), + disconnectionCallback_(disconnectionCallback) {} + std::string identifier() const override { return identifier_; } @@ -51,8 +60,13 @@ class FlipperPluginMock : public FlipperPlugin { } } + bool runInBackground() override { + return runInBackground_; + } + private: std::string identifier_; + bool runInBackground_ = false; ConnectionCallback connectionCallback_; DisconnectionCallback disconnectionCallback_; }; diff --git a/xplat/FlipperTests/FlipperClientTests.cpp b/xplat/FlipperTests/FlipperClientTests.cpp index 853f1eca9..1eb08eb91 100644 --- a/xplat/FlipperTests/FlipperClientTests.cpp +++ b/xplat/FlipperTests/FlipperClientTests.cpp @@ -1,11 +1,10 @@ /* - * Copyright (c) 2018-present, Facebook, Inc. + * Copyright (c) Facebook, Inc. * * This source code is licensed under the MIT license found in the LICENSE * file in the root directory of this source tree. * */ - #include #include #include @@ -289,6 +288,46 @@ TEST(FlipperClientTests, testExceptionUnknownApi) { "connection Unknown not found for method execute"); } +TEST(FlipperClientTests, testBackgroundPluginActivated) { + auto socket = new FlipperConnectionManagerMock; + FlipperClient client( + std::unique_ptr{socket}, state); + + bool pluginConnected = false; + const auto connectionCallback = [&](std::shared_ptr conn) { + pluginConnected = true; + }; + const auto disconnectionCallback = [&]() { pluginConnected = false; }; + auto plugin = std::make_shared( + "Test", connectionCallback, disconnectionCallback, true); + + client.addPlugin(plugin); + client.start(); + EXPECT_TRUE(pluginConnected); + client.stop(); + EXPECT_FALSE(pluginConnected); +} + +TEST(FlipperClientTests, testNonBackgroundPluginNotActivated) { + auto socket = new FlipperConnectionManagerMock; + FlipperClient client( + std::unique_ptr{socket}, state); + + bool pluginConnected = false; + const auto connectionCallback = [&](std::shared_ptr conn) { + pluginConnected = true; + }; + const auto disconnectionCallback = [&]() { pluginConnected = false; }; + auto plugin = std::make_shared( + "Test", connectionCallback, disconnectionCallback, false); + + client.addPlugin(plugin); + client.start(); + EXPECT_FALSE(pluginConnected); + client.stop(); + EXPECT_FALSE(pluginConnected); +} + } // namespace test } // namespace flipper } // namespace facebook