Skip to content

Commit

Permalink
Refactor make_mock_engine into fl_test (flutter#21585)
Browse files Browse the repository at this point in the history
  • Loading branch information
anirudhb authored Oct 29, 2020
1 parent ed0f477 commit b457e2d
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 48 deletions.
13 changes: 1 addition & 12 deletions shell/platform/linux/fl_basic_message_channel_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,9 @@
#include "flutter/shell/platform/linux/fl_engine_private.h"
#include "flutter/shell/platform/linux/public/flutter_linux/fl_basic_message_channel.h"
#include "flutter/shell/platform/linux/public/flutter_linux/fl_standard_message_codec.h"
#include "flutter/shell/platform/linux/testing/fl_test.h"
#include "flutter/shell/platform/linux/testing/mock_renderer.h"

// Creates a mock engine that responds to platform messages.
static FlEngine* make_mock_engine() {
g_autoptr(FlDartProject) project = fl_dart_project_new();
g_autoptr(FlMockRenderer) renderer = fl_mock_renderer_new();
g_autoptr(FlEngine) engine = fl_engine_new(project, FL_RENDERER(renderer));
g_autoptr(GError) engine_error = nullptr;
EXPECT_TRUE(fl_engine_start(engine, &engine_error));
EXPECT_EQ(engine_error, nullptr);

return static_cast<FlEngine*>(g_object_ref(engine));
}

// Called when the message response is received in the SendMessage test.
static void echo_response_cb(GObject* object,
GAsyncResult* result,
Expand Down
13 changes: 1 addition & 12 deletions shell/platform/linux/fl_binary_messenger_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,9 @@
#include "flutter/shell/platform/linux/fl_binary_messenger_private.h"
#include "flutter/shell/platform/linux/fl_engine_private.h"
#include "flutter/shell/platform/linux/public/flutter_linux/fl_binary_messenger.h"
#include "flutter/shell/platform/linux/testing/fl_test.h"
#include "flutter/shell/platform/linux/testing/mock_renderer.h"

// Creates a mock engine that responds to platform messages.
static FlEngine* make_mock_engine() {
g_autoptr(FlDartProject) project = fl_dart_project_new();
g_autoptr(FlMockRenderer) renderer = fl_mock_renderer_new();
g_autoptr(FlEngine) engine = fl_engine_new(project, FL_RENDERER(renderer));
g_autoptr(GError) engine_error = nullptr;
EXPECT_TRUE(fl_engine_start(engine, &engine_error));
EXPECT_EQ(engine_error, nullptr);

return static_cast<FlEngine*>(g_object_ref(engine));
}

// Checks sending nullptr for a message works.
TEST(FlBinaryMessengerTest, SendNullptrMessage) {
g_autoptr(FlEngine) engine = make_mock_engine();
Expand Down
13 changes: 1 addition & 12 deletions shell/platform/linux/fl_key_event_plugin_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,9 @@
#include "flutter/shell/platform/linux/fl_engine_private.h"
#include "flutter/shell/platform/linux/public/flutter_linux/fl_basic_message_channel.h"
#include "flutter/shell/platform/linux/public/flutter_linux/fl_standard_message_codec.h"
#include "flutter/shell/platform/linux/testing/fl_test.h"
#include "flutter/shell/platform/linux/testing/mock_renderer.h"

// Creates a mock engine that responds to platform messages.
static FlEngine* make_mock_engine() {
g_autoptr(FlDartProject) project = fl_dart_project_new();
g_autoptr(FlMockRenderer) renderer = fl_mock_renderer_new();
g_autoptr(FlEngine) engine = fl_engine_new(project, FL_RENDERER(renderer));
g_autoptr(GError) engine_error = nullptr;
EXPECT_TRUE(fl_engine_start(engine, &engine_error));
EXPECT_EQ(engine_error, nullptr);

return static_cast<FlEngine*>(g_object_ref(engine));
}

const char* expected_value = nullptr;

// Called when the message response is received in the send_key_event test.
Expand Down
13 changes: 1 addition & 12 deletions shell/platform/linux/fl_method_channel_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,9 @@
#include "flutter/shell/platform/linux/public/flutter_linux/fl_basic_message_channel.h"
#include "flutter/shell/platform/linux/public/flutter_linux/fl_method_channel.h"
#include "flutter/shell/platform/linux/public/flutter_linux/fl_standard_method_codec.h"
#include "flutter/shell/platform/linux/testing/fl_test.h"
#include "flutter/shell/platform/linux/testing/mock_renderer.h"

// Creates a mock engine that responds to platform messages.
static FlEngine* make_mock_engine() {
g_autoptr(FlDartProject) project = fl_dart_project_new();
g_autoptr(FlMockRenderer) renderer = fl_mock_renderer_new();
g_autoptr(FlEngine) engine = fl_engine_new(project, FL_RENDERER(renderer));
g_autoptr(GError) engine_error = nullptr;
EXPECT_TRUE(fl_engine_start(engine, &engine_error));
EXPECT_EQ(engine_error, nullptr);

return static_cast<FlEngine*>(g_object_ref(engine));
}

// Called when when the method call response is received in the InvokeMethod
// test.
static void method_response_cb(GObject* object,
Expand Down
17 changes: 17 additions & 0 deletions shell/platform/linux/testing/fl_test.cc
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// FLUTTER_NOLINT

#include "gtest/gtest.h"

#include "flutter/shell/platform/linux/testing/fl_test.h"

#include "flutter/shell/platform/linux/fl_engine_private.h"
#include "flutter/shell/platform/linux/testing/mock_renderer.h"

static uint8_t hex_digit_to_int(char value) {
if (value >= '0' && value <= '9')
return value - '0';
Expand Down Expand Up @@ -39,3 +45,14 @@ gchar* bytes_to_hex_string(GBytes* bytes) {
g_string_append_printf(hex_string, "%02x", data[i]);
return g_string_free(hex_string, FALSE);
}

FlEngine* make_mock_engine() {
g_autoptr(FlDartProject) project = fl_dart_project_new();
g_autoptr(FlMockRenderer) renderer = fl_mock_renderer_new();
g_autoptr(FlEngine) engine = fl_engine_new(project, FL_RENDERER(renderer));
g_autoptr(GError) engine_error = nullptr;
EXPECT_TRUE(fl_engine_start(engine, &engine_error));
EXPECT_EQ(engine_error, nullptr);

return static_cast<FlEngine*>(g_object_ref(engine));
}
5 changes: 5 additions & 0 deletions shell/platform/linux/testing/fl_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#ifndef FLUTTER_SHELL_PLATFORM_LINUX_FL_TEST_H_
#define FLUTTER_SHELL_PLATFORM_LINUX_FL_TEST_H_

#include "flutter/shell/platform/linux/public/flutter_linux/fl_engine.h"

#include <glib.h>
#include <stdint.h>

Expand All @@ -18,6 +20,9 @@ GBytes* hex_string_to_bytes(const gchar* hex_string);
// Helper function to convert GBytes into a hexadecimal string (e.g. "01feab")
gchar* bytes_to_hex_string(GBytes* bytes);

// Creates a mock engine that responds to platform messages.
FlEngine* make_mock_engine();

G_END_DECLS

#endif // FLUTTER_SHELL_PLATFORM_LINUX_FL_TEST_H_

0 comments on commit b457e2d

Please sign in to comment.