web_engine_config_unittest.cc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright 2022 The Chromium 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. #include "fuchsia_web/webengine/browser/web_engine_config.h"
  5. #include "base/command_line.h"
  6. #include "base/metrics/field_trial.h"
  7. #include "base/values.h"
  8. #include "fuchsia_web/webengine/switches.h"
  9. #include "testing/gtest/include/gtest/gtest.h"
  10. namespace {
  11. constexpr char kCommandLineArgs[] = "command-line-args";
  12. base::Value CreateConfigWithSwitchValue(std::string switch_name,
  13. std::string switch_value) {
  14. base::Value::Dict config_dict;
  15. base::Value::Dict args;
  16. args.Set(switch_name, switch_value);
  17. config_dict.Set(kCommandLineArgs, std::move(args));
  18. return base::Value(std::move(config_dict));
  19. }
  20. } // namespace
  21. class WebEngineConfigTest : public testing::Test {
  22. public:
  23. WebEngineConfigTest() = default;
  24. ~WebEngineConfigTest() override = default;
  25. WebEngineConfigTest(const WebEngineConfigTest&) = delete;
  26. WebEngineConfigTest& operator=(const WebEngineConfigTest&) = delete;
  27. void SetUp() override {
  28. backup_field_trial_list_ = base::FieldTrialList::BackupInstanceForTesting();
  29. }
  30. void TearDown() override {
  31. base::FieldTrialList::RestoreInstanceForTesting(backup_field_trial_list_);
  32. backup_field_trial_list_ = nullptr;
  33. }
  34. private:
  35. base::FieldTrialList* backup_field_trial_list_ = nullptr;
  36. };
  37. TEST_F(WebEngineConfigTest, CommandLineArgs) {
  38. // Specify a configuration that sets valid args with valid strings.
  39. auto config = CreateConfigWithSwitchValue("renderer-process-limit", "0");
  40. base::CommandLine command(base::CommandLine::NO_PROGRAM);
  41. EXPECT_TRUE(UpdateCommandLineFromConfigFile(config, &command));
  42. EXPECT_EQ(command.GetSwitchValueASCII("renderer-process-limit"), "0");
  43. }
  44. TEST_F(WebEngineConfigTest, DisallowedCommandLineArgs) {
  45. // Specify a configuration that sets a disallowed command-line argument.
  46. auto config = CreateConfigWithSwitchValue("kittens-are-nice", "0");
  47. base::CommandLine command(base::CommandLine::NO_PROGRAM);
  48. EXPECT_TRUE(UpdateCommandLineFromConfigFile(config, &command));
  49. EXPECT_FALSE(command.HasSwitch("kittens-are-nice"));
  50. }
  51. TEST_F(WebEngineConfigTest, WronglyTypedCommandLineArgs) {
  52. base::Value::Dict config;
  53. // Specify a configuration that sets valid args with invalid value.
  54. base::Value::Dict args;
  55. args.Set("renderer-process-limit", false);
  56. config.Set(kCommandLineArgs, std::move(args));
  57. base::CommandLine command(base::CommandLine::NO_PROGRAM);
  58. EXPECT_FALSE(UpdateCommandLineFromConfigFile(base::Value(std::move(config)),
  59. &command));
  60. }
  61. TEST_F(WebEngineConfigTest, WithGoogleApiKeyValue) {
  62. constexpr char kDummyApiKey[] = "apikey123";
  63. auto config = CreateConfigWithSwitchValue("google-api-key", kDummyApiKey);
  64. base::CommandLine command(base::CommandLine::NO_PROGRAM);
  65. EXPECT_TRUE(UpdateCommandLineFromConfigFile(config, &command));
  66. EXPECT_EQ(command.GetSwitchValueASCII(switches::kGoogleApiKey), kDummyApiKey);
  67. }