flutter_flutter/shell/platform/common/cpp/engine_switches_unittests.cc
stuartmorgan 41ce791926
[macos] Allow engine flags via environment vars (#21468)
Replaces the (temporary) compile-time option to pass engine switches
with the ability to pass them temporarily at runtime via environment
variables. This moves the recently-added code for doing this on Windows
to a shared location for use by all desktop embeddings.

This is enabled only for debug/profile to avoid potential issues with
tampering with released applications, but if there is a need for that in
the future it could be added (potentially with a whitelist, as is
currently used for Dart VM flags).

Temporarily adds a way to enable mirrors as a compile time option,
as is already provided in the Linux embedding, to provide a migration
path for the one remaining known need for compile-time options
that has been raised in flutter/flutter#38569.
2020-09-29 09:36:30 -07:00

89 lines
2.8 KiB
C++

// 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.
#include "flutter/shell/platform/common/cpp/engine_switches.h"
#include "gtest/gtest.h"
namespace flutter {
namespace {
// Sets |key=value| in the environment of this process.
void SetEnvironmentVariable(const char* key, const char* value) {
#ifdef _WIN32
_putenv_s(key, value);
#else
setenv(key, value, 1);
#endif
}
// Removes |key| from the environment of this process, if present.
void ClearEnvironmentVariable(const char* key) {
#ifdef _WIN32
_putenv_s(key, "");
#else
unsetenv(key);
#endif
}
} // namespace
TEST(FlutterProjectBundle, SwitchesEmpty) {
// Clear the main environment variable, since test order is not guaranteed.
ClearEnvironmentVariable("FLUTTER_ENGINE_SWITCHES");
EXPECT_EQ(GetSwitchesFromEnvironment().size(), 0U);
}
#ifdef FLUTTER_RELEASE
TEST(FlutterProjectBundle, SwitchesIgnoredInRelease) {
SetEnvironmentVariable("FLUTTER_ENGINE_SWITCHES", "2");
SetEnvironmentVariable("FLUTTER_ENGINE_SWITCH_1", "abc");
SetEnvironmentVariable("FLUTTER_ENGINE_SWITCH_2", "foo=\"bar, baz\"");
std::vector<std::string> switches = GetSwitchesFromEnvironment();
EXPECT_EQ(switches.size(), 0U);
}
#endif // FLUTTER_RELEASE
#ifndef FLUTTER_RELEASE
TEST(FlutterProjectBundle, Switches) {
SetEnvironmentVariable("FLUTTER_ENGINE_SWITCHES", "2");
SetEnvironmentVariable("FLUTTER_ENGINE_SWITCH_1", "abc");
SetEnvironmentVariable("FLUTTER_ENGINE_SWITCH_2", "foo=\"bar, baz\"");
std::vector<std::string> switches = GetSwitchesFromEnvironment();
EXPECT_EQ(switches.size(), 2U);
EXPECT_EQ(switches[0], "--abc");
EXPECT_EQ(switches[1], "--foo=\"bar, baz\"");
}
TEST(FlutterProjectBundle, SwitchesExtraValues) {
SetEnvironmentVariable("FLUTTER_ENGINE_SWITCHES", "1");
SetEnvironmentVariable("FLUTTER_ENGINE_SWITCH_1", "abc");
SetEnvironmentVariable("FLUTTER_ENGINE_SWITCH_2", "foo=\"bar, baz\"");
std::vector<std::string> switches = GetSwitchesFromEnvironment();
EXPECT_EQ(switches.size(), 1U);
EXPECT_EQ(switches[0], "--abc");
}
TEST(FlutterProjectBundle, SwitchesMissingValues) {
SetEnvironmentVariable("FLUTTER_ENGINE_SWITCHES", "4");
SetEnvironmentVariable("FLUTTER_ENGINE_SWITCH_1", "abc");
SetEnvironmentVariable("FLUTTER_ENGINE_SWITCH_2", "foo=\"bar, baz\"");
ClearEnvironmentVariable("FLUTTER_ENGINE_SWITCH_3");
SetEnvironmentVariable("FLUTTER_ENGINE_SWITCH_4", "oops");
std::vector<std::string> switches = GetSwitchesFromEnvironment();
EXPECT_EQ(switches.size(), 3U);
EXPECT_EQ(switches[0], "--abc");
EXPECT_EQ(switches[1], "--foo=\"bar, baz\"");
// The missing switch should be skipped, leaving SWITCH_4 as the third
// switch in the array.
EXPECT_EQ(switches[2], "--oops");
}
#endif // !FLUTTER_RELEASE
} // namespace flutter