Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ function(APPLY_STANDARD_SETTINGS TARGET)
target_compile_definitions(${TARGET} PRIVATE "$<$<CONFIG:Debug>:_DEBUG>")
endfunction()

# Enable the test target.
set(include_test_plugin_tests TRUE)

# Flutter library and tool build rules.
set(FLUTTER_MANAGED_DIR "${CMAKE_CURRENT_SOURCE_DIR}/flutter")
add_subdirectory(${FLUTTER_MANAGED_DIR})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,88 @@ set(test_plugin_bundled_libraries
""
PARENT_SCOPE
)

# === Tests ===

if (${include_${PROJECT_NAME}_tests})
set(TEST_RUNNER "${PROJECT_NAME}_test")
enable_testing()
# TODO(stuartmorgan): Consider using a single shared, pre-checked-in googletest
# instance rather than downloading for each plugin. This approach makes sense
# for a template, but not for a monorepo with many plugins.
include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/release-1.11.0.zip
)
# Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
# Disable install commands for gtest so it doesn't end up in the bundle.
set(INSTALL_GTEST OFF CACHE BOOL "Disable installation of googletest" FORCE)

FetchContent_MakeAvailable(googletest)

# The plugin's C API is not very useful for unit testing, so build the sources
# directly into the test binary rather than using the DLL.
add_executable(${TEST_RUNNER}
# Tests.
test/multiple_arity_test.cpp
test/non_null_fields_test.cpp
test/nullable_returns_test.cpp
test/null_fields_test.cpp
test/pigeon_test.cpp
test/primitive_test.cpp
# Generated sources.
test/all_datatypes.g.cpp
test/all_datatypes.g.h
test/all_void.g.cpp
test/all_void.g.h
test/async_handlers.g.cpp
test/async_handlers.g.h
test/enum.g.cpp
test/enum.g.h
test/host2flutter.g.cpp
test/host2flutter.g.h
test/list.g.cpp
test/list.g.h
test/message.g.cpp
test/message.g.h
test/multiple_arity.g.cpp
test/multiple_arity.g.h
test/non_null_fields.g.cpp
test/non_null_fields.g.h
test/null_fields.g.cpp
test/null_fields.g.h
test/nullable_returns.g.cpp
test/nullable_returns.g.h
test/primitive.g.cpp
test/primitive.g.h
test/void_arg_flutter.g.cpp
test/void_arg_flutter.g.h
test/void_arg_host.g.cpp
test/void_arg_host.g.h
test/voidflutter.g.cpp
test/voidflutter.g.h
test/voidhost.g.cpp
test/voidhost.g.h
# Test utilities.
test/utils/echo_messenger.cpp
test/utils/echo_messenger.h
test/utils/fake_host_messenger.cpp
test/utils/fake_host_messenger.h

${PLUGIN_SOURCES}
)
apply_standard_settings(${TEST_RUNNER})
target_include_directories(${TEST_RUNNER} PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}")
target_link_libraries(${TEST_RUNNER} PRIVATE flutter_wrapper_plugin)
target_link_libraries(${TEST_RUNNER} PRIVATE gtest_main gmock)
# flutter_wrapper_plugin has link dependencies on the Flutter DLL.
add_custom_command(TARGET ${TEST_RUNNER} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
"${FLUTTER_LIBRARY}" $<TARGET_FILE_DIR:${TEST_RUNNER}>
)

include(GoogleTest)
gtest_discover_tests(${TEST_RUNNER})
endif()
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# TODO(stuartmorgan): Remove this, so that review will show the effects of
# changes on generated files. This will need a way to avoid unnecessary churn,
# such as a flag to suppress version stamp generation.
*.g.h
*.g.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ using flutter::EncodableValue;
// This gives useful test failure messages instead of silent crashes when the
// value isn't present, or has the wrong type.
template <class T>
const T *ExpectAndGet(const EncodableMap &map, const std::string &key) {
const T* ExpectAndGet(const EncodableMap& map, const std::string& key) {
auto it = map.find(EncodableValue(key));
EXPECT_TRUE(it != map.end()) << "Could not find value for '" << key << '"';
if (it == map.end()) {
return nullptr;
}
const T *value_ptr = std::get_if<T>(&(it->second));
const T* value_ptr = std::get_if<T>(&(it->second));
EXPECT_NE(value_ptr, nullptr)
<< "Value for '" << key << "' has incorrect type";
return value_ptr;
Expand All @@ -37,20 +37,20 @@ const T *ExpectAndGet(const EncodableMap &map, const std::string &key) {
class NullFieldsTest : public ::testing::Test {
protected:
// Wrapper for access to private NullFieldsSearchRequest map constructor.
NullFieldsSearchRequest RequestFromMap(const EncodableMap &map) {
NullFieldsSearchRequest RequestFromMap(const EncodableMap& map) {
return NullFieldsSearchRequest(map);
}

// Wrapper for access to private NullFieldsSearchRequest map constructor.
NullFieldsSearchReply ReplyFromMap(const EncodableMap &map) {
NullFieldsSearchReply ReplyFromMap(const EncodableMap& map) {
return NullFieldsSearchReply(map);
}
// Wrapper for access to private NullFieldsSearchRequest::ToEncodableMap.
EncodableMap MapFromRequest(const NullFieldsSearchRequest &request) {
EncodableMap MapFromRequest(const NullFieldsSearchRequest& request) {
return request.ToEncodableMap();
}
// Wrapper for access to private NullFieldsSearchRequest map constructor.
EncodableMap MapFromReply(const NullFieldsSearchReply &reply) {
EncodableMap MapFromReply(const NullFieldsSearchReply& reply) {
return reply.ToEncodableMap();
}
};
Expand Down Expand Up @@ -194,12 +194,12 @@ TEST_F(NullFieldsTest, ReplyToMapWithValues) {
EXPECT_EQ(map.size(), 5);
EXPECT_EQ(*ExpectAndGet<std::string>(map, "result"), "result");
EXPECT_EQ(*ExpectAndGet<std::string>(map, "error"), "error");
const EncodableList &indices = *ExpectAndGet<EncodableList>(map, "indices");
const EncodableList& indices = *ExpectAndGet<EncodableList>(map, "indices");
EXPECT_EQ(indices.size(), 3);
EXPECT_EQ(indices[0].LongValue(), 1L);
EXPECT_EQ(indices[1].LongValue(), 2L);
EXPECT_EQ(indices[2].LongValue(), 3L);
const EncodableMap &request_map = *ExpectAndGet<EncodableMap>(map, "request");
const EncodableMap& request_map = *ExpectAndGet<EncodableMap>(map, "request");
EXPECT_EQ(*ExpectAndGet<std::string>(request_map, "query"), "hello");
EXPECT_EQ(*ExpectAndGet<int>(map, "type"), 0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@
#include <string>

#include "message.g.h"
#include "windows_unit_tests_plugin.h"

namespace windows_unit_tests {
namespace test_plugin {
namespace test {

namespace {
Expand Down Expand Up @@ -80,18 +79,6 @@ class Writer : public flutter::ByteStreamWriter {
};
} // namespace

TEST(PigeonTests, Placeholder) {
std::unique_ptr<MockMethodResult> result =
std::make_unique<MockMethodResult>();

// Expect a success response.
EXPECT_CALL(*result, SuccessInternal(Pointee(EncodableValue(true))));

WindowsUnitTestsPlugin plugin;
plugin.HandleMethodCall(flutter::MethodCall<>("placeholder", nullptr),
std::move(result));
}

TEST(PigeonTests, CallInitialize) {
MockBinaryMessenger mock_messenger;
MockApi mock_api;
Expand Down Expand Up @@ -148,4 +135,4 @@ TEST(PigeonTests, CallSearch) {
}

} // namespace test
} // namespace windows_unit_tests
} // namespace test_plugin
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef PLATFORM_TESTS_WINDOWS_UNIT_TESTS_WINDOWS_TEST_UTILS_ECHO_MESSENGER_H_
#define PLATFORM_TESTS_WINDOWS_UNIT_TESTS_WINDOWS_TEST_UTILS_ECHO_MESSENGER_H_
#ifndef PLATFORM_TESTS_TEST_PLUGIN_WINDOWS_TEST_UTILS_ECHO_MESSENGER_H_
#define PLATFORM_TESTS_TEST_PLUGIN_WINDOWS_TEST_UTILS_ECHO_MESSENGER_H_

#include <flutter/binary_messenger.h>
#include <flutter/encodable_value.h>
Expand Down Expand Up @@ -32,4 +32,4 @@ class EchoMessenger : public flutter::BinaryMessenger {

} // namespace testing

#endif // PLATFORM_TESTS_WINDOWS_UNIT_TESTS_WINDOWS_TEST_UTILS_ECHO_MESSENGER_H_
#endif // PLATFORM_TESTS_TEST_PLUGIN_WINDOWS_TEST_UTILS_ECHO_MESSENGER_H_
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef PLATFORM_TESTS_WINDOWS_UNIT_TESTS_WINDOWS_TEST_UTILS_FAKE_HOST_MESSENGER_H_
#define PLATFORM_TESTS_WINDOWS_UNIT_TESTS_WINDOWS_TEST_UTILS_FAKE_HOST_MESSENGER_H_
#ifndef PLATFORM_TESTS_TEST_PLUGIN_WINDOWS_TEST_UTILS_FAKE_HOST_MESSENGER_H_
#define PLATFORM_TESTS_TEST_PLUGIN_WINDOWS_TEST_UTILS_FAKE_HOST_MESSENGER_H_

#include <flutter/binary_messenger.h>
#include <flutter/encodable_value.h>
Expand Down Expand Up @@ -48,4 +48,4 @@ class FakeHostMessenger : public flutter::BinaryMessenger {

} // namespace testing

#endif // PLATFORM_TESTS_WINDOWS_UNIT_TESTS_WINDOWS_TEST_UTILS_FAKE_HOST_MESSENGER_H_
#endif // PLATFORM_TESTS_TEST_PLUGIN_WINDOWS_TEST_UTILS_FAKE_HOST_MESSENGER_H_
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,20 @@
#include "test_plugin.h"

// This must be included before many other Windows headers.
#include <windows.h>

// For getPlatformVersion; remove unless needed for your plugin implementation.
#include <VersionHelpers.h>
#include <flutter/method_channel.h>
#include <flutter/plugin_registrar_windows.h>
#include <flutter/standard_method_codec.h>
#include <windows.h>

#include <memory>
#include <sstream>

namespace test_plugin {

// static
void TestPlugin::RegisterWithRegistrar(
flutter::PluginRegistrarWindows* registrar) {
auto channel =
std::make_unique<flutter::MethodChannel<flutter::EncodableValue>>(
registrar->messenger(), "test_plugin",
&flutter::StandardMethodCodec::GetInstance());

auto plugin = std::make_unique<TestPlugin>();

channel->SetMethodCallHandler(
[plugin_pointer = plugin.get()](const auto& call, auto result) {
plugin_pointer->HandleMethodCall(call, std::move(result));
});
// This plugin is currently a no-op since only unit tests have been set up.
// In the future, this will register Pigeon APIs used in integration tests.

registrar->AddPlugin(std::move(plugin));
}
Expand All @@ -40,23 +27,4 @@ TestPlugin::TestPlugin() {}

TestPlugin::~TestPlugin() {}

void TestPlugin::HandleMethodCall(
const flutter::MethodCall<flutter::EncodableValue>& method_call,
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result) {
if (method_call.method_name().compare("getPlatformVersion") == 0) {
std::ostringstream version_stream;
version_stream << "Windows ";
if (IsWindows10OrGreater()) {
version_stream << "10+";
} else if (IsWindows8OrGreater()) {
version_stream << "8";
} else if (IsWindows7OrGreater()) {
version_stream << "7";
}
result->Success(flutter::EncodableValue(version_stream.str()));
} else {
result->NotImplemented();
}
}

} // namespace test_plugin
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@ class TestPlugin : public flutter::Plugin {
// Disallow copy and assign.
TestPlugin(const TestPlugin&) = delete;
TestPlugin& operator=(const TestPlugin&) = delete;

private:
// Called when a method is called on this plugin's channel from Dart.
void HandleMethodCall(
const flutter::MethodCall<flutter::EncodableValue>& method_call,
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result);
};

} // namespace test_plugin
Expand Down

This file was deleted.

29 changes: 0 additions & 29 deletions packages/pigeon/platform_tests/windows_unit_tests/.gitignore

This file was deleted.

10 changes: 0 additions & 10 deletions packages/pigeon/platform_tests/windows_unit_tests/.metadata

This file was deleted.

This file was deleted.

25 changes: 0 additions & 25 deletions packages/pigeon/platform_tests/windows_unit_tests/LICENSE

This file was deleted.

13 changes: 0 additions & 13 deletions packages/pigeon/platform_tests/windows_unit_tests/README.md

This file was deleted.

Loading