|
| 1 | +// Copyright 2013 The Flutter 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 | +// Included first as it collides with the X11 headers. |
| 6 | +#include "gtest/gtest.h" |
| 7 | + |
| 8 | +#include "flutter/shell/platform/embedder/test_utils/proc_table_replacement.h" |
| 9 | +#include "flutter/shell/platform/linux/fl_engine_private.h" |
| 10 | +#include "flutter/shell/platform/linux/public/flutter_linux/fl_engine.h" |
| 11 | +#include "flutter/shell/platform/linux/public/flutter_linux/fl_json_message_codec.h" |
| 12 | +#include "flutter/shell/platform/linux/public/flutter_linux/fl_value.h" |
| 13 | +#include "flutter/shell/platform/linux/testing/fl_test.h" |
| 14 | + |
| 15 | +static void test_system_theme(const gchar* color, const gchar* expected_theme) { |
| 16 | + gtk_init(nullptr, nullptr); |
| 17 | + |
| 18 | + GdkScreen* screen = gdk_screen_get_default(); |
| 19 | + GtkWidget* window = gtk_window_new(GTK_WINDOW_TOPLEVEL); |
| 20 | + |
| 21 | + g_autofree gchar* css = g_strdup_printf("* { color: %s; }", color); |
| 22 | + g_autoptr(GtkCssProvider) css_provider = gtk_css_provider_new(); |
| 23 | + gtk_css_provider_load_from_data(css_provider, css, -1, NULL); |
| 24 | + |
| 25 | + gtk_style_context_add_provider_for_screen(screen, |
| 26 | + GTK_STYLE_PROVIDER(css_provider), |
| 27 | + GTK_STYLE_PROVIDER_PRIORITY_USER); |
| 28 | + |
| 29 | + g_autoptr(FlEngine) engine = make_mock_engine(); |
| 30 | + FlutterEngineProcTable* embedder_api = fl_engine_get_embedder_api(engine); |
| 31 | + |
| 32 | + g_autofree gchar* system_theme = nullptr; |
| 33 | + embedder_api->SendPlatformMessage = MOCK_ENGINE_PROC( |
| 34 | + SendPlatformMessage, |
| 35 | + ([&system_theme](auto engine, const FlutterPlatformMessage* message) { |
| 36 | + EXPECT_STREQ(message->channel, "flutter/settings"); |
| 37 | + |
| 38 | + g_autoptr(FlJsonMessageCodec) codec = fl_json_message_codec_new(); |
| 39 | + g_autoptr(GBytes) data = |
| 40 | + g_bytes_new(message->message, message->message_size); |
| 41 | + g_autoptr(GError) error = nullptr; |
| 42 | + g_autoptr(FlValue) settings = fl_message_codec_decode_message( |
| 43 | + FL_MESSAGE_CODEC(codec), data, &error); |
| 44 | + EXPECT_NE(settings, nullptr); |
| 45 | + EXPECT_EQ(error, nullptr); |
| 46 | + |
| 47 | + FlValue* value = fl_value_lookup_string(settings, "platformBrightness"); |
| 48 | + EXPECT_NE(value, nullptr); |
| 49 | + |
| 50 | + system_theme = g_strdup(fl_value_get_string(value)); |
| 51 | + |
| 52 | + return kSuccess; |
| 53 | + })); |
| 54 | + |
| 55 | + EXPECT_TRUE(fl_engine_start(engine, nullptr)); |
| 56 | + EXPECT_STREQ(expected_theme, system_theme); |
| 57 | + |
| 58 | + gtk_style_context_remove_provider_for_screen( |
| 59 | + screen, GTK_STYLE_PROVIDER(css_provider)); |
| 60 | + gtk_widget_destroy(window); |
| 61 | +} |
| 62 | + |
| 63 | +TEST(FlSettingsPluginTest, TestLightSystemTheme) { |
| 64 | + test_system_theme("#111", "light"); |
| 65 | +} |
| 66 | + |
| 67 | +TEST(FlSettingsPluginTest, TestDarkSystemTheme) { |
| 68 | + test_system_theme("#eee", "dark"); |
| 69 | +} |
0 commit comments