| Flutter macOS Embedder |

engine_switches_unittests.cc
Go to the documentation of this file.
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 #include "flutter/shell/platform/common/engine_switches.h"
6
7 #include "gtest/gtest.h"
8
9 namespace flutter {
10
11 namespace {
12 // Sets |key=value| in the environment of this process.
13 void SetEnvironmentVariable(const char* key, const char* value) {
14 #ifdef _WIN32
15 _putenv_s(key, value);
16 #else
17 setenv(key, value, 1);
18 #endif
19 }
20
21 // Removes |key| from the environment of this process, if present.
22 void ClearEnvironmentVariable(const char* key) {
23 #ifdef _WIN32
24 _putenv_s(key, "");
25 #else
26 unsetenv(key);
27 #endif
28 }
29 } // namespace
30
31 TEST(FlutterProjectBundle, SwitchesEmpty) {
32 // Clear the main environment variable, since test order is not guaranteed.
33 ClearEnvironmentVariable("FLUTTER_ENGINE_SWITCHES");
34
35 EXPECT_EQ(GetSwitchesFromEnvironment().size(), 0U);
36 }
37
38 #ifdef FLUTTER_RELEASE
39 TEST(FlutterProjectBundle, SwitchesIgnoredInRelease) {
40 SetEnvironmentVariable("FLUTTER_ENGINE_SWITCHES", "2");
41 SetEnvironmentVariable("FLUTTER_ENGINE_SWITCH_1", "abc");
42 SetEnvironmentVariable("FLUTTER_ENGINE_SWITCH_2", "foo=\"bar, baz\"");
43
44 std::vector<std::string> switches = GetSwitchesFromEnvironment();
45 EXPECT_EQ(switches.size(), 0U);
46 }
47 #endif // FLUTTER_RELEASE
48
49 #ifndef FLUTTER_RELEASE
50 TEST(FlutterProjectBundle, Switches) {
51 SetEnvironmentVariable("FLUTTER_ENGINE_SWITCHES", "2");
52 SetEnvironmentVariable("FLUTTER_ENGINE_SWITCH_1", "abc");
53 SetEnvironmentVariable("FLUTTER_ENGINE_SWITCH_2", "foo=\"bar, baz\"");
54
55 std::vector<std::string> switches = GetSwitchesFromEnvironment();
56 EXPECT_EQ(switches.size(), 2U);
57 EXPECT_EQ(switches[0], "--abc");
58 EXPECT_EQ(switches[1], "--foo=\"bar, baz\"");
59 }
60
61 TEST(FlutterProjectBundle, SwitchesExtraValues) {
62 SetEnvironmentVariable("FLUTTER_ENGINE_SWITCHES", "1");
63 SetEnvironmentVariable("FLUTTER_ENGINE_SWITCH_1", "abc");
64 SetEnvironmentVariable("FLUTTER_ENGINE_SWITCH_2", "foo=\"bar, baz\"");
65
66 std::vector<std::string> switches = GetSwitchesFromEnvironment();
67 EXPECT_EQ(switches.size(), 1U);
68 EXPECT_EQ(switches[0], "--abc");
69 }
70
71 TEST(FlutterProjectBundle, SwitchesMissingValues) {
72 SetEnvironmentVariable("FLUTTER_ENGINE_SWITCHES", "4");
73 SetEnvironmentVariable("FLUTTER_ENGINE_SWITCH_1", "abc");
74 SetEnvironmentVariable("FLUTTER_ENGINE_SWITCH_2", "foo=\"bar, baz\"");
75 ClearEnvironmentVariable("FLUTTER_ENGINE_SWITCH_3");
76 SetEnvironmentVariable("FLUTTER_ENGINE_SWITCH_4", "oops");
77
78 std::vector<std::string> switches = GetSwitchesFromEnvironment();
79 EXPECT_EQ(switches.size(), 3U);
80 EXPECT_EQ(switches[0], "--abc");
81 EXPECT_EQ(switches[1], "--foo=\"bar, baz\"");
82 // The missing switch should be skipped, leaving SWITCH_4 as the third
83 // switch in the array.
84 EXPECT_EQ(switches[2], "--oops");
85 }
86 #endif // !FLUTTER_RELEASE
87
88 } // namespace flutter
engine_switches.h
flutter
Definition: AccessibilityBridgeMac.h:16
flutter::GetSwitchesFromEnvironment
std::vector< std::string > GetSwitchesFromEnvironment()
Definition: engine_switches.cc:14
flutter::TEST
TEST(BasicMessageChannelTest, Registration)
Definition: basic_message_channel_unittests.cc:58