command_line_top_host_provider_unittest.cc 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright 2019 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 "components/optimization_guide/core/command_line_top_host_provider.h"
  5. #include "base/command_line.h"
  6. #include "components/optimization_guide/core/optimization_guide_switches.h"
  7. #include "testing/gtest/include/gtest/gtest.h"
  8. namespace optimization_guide {
  9. TEST(CommandLineTopHostProviderTest, DoesNotCreateIfFlagNotEnabled) {
  10. ASSERT_FALSE(CommandLineTopHostProvider::CreateIfEnabled());
  11. }
  12. TEST(CommandLineTopHostProviderTest, DoesNotCreateIfSwitchEnabledButNoHosts) {
  13. base::CommandLine::ForCurrentProcess()->AppendSwitch(
  14. switches::kFetchHintsOverride);
  15. ASSERT_FALSE(CommandLineTopHostProvider::CreateIfEnabled());
  16. }
  17. TEST(CommandLineTopHostProviderTest, CreateIfFlagEnabledAndHasHosts) {
  18. base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
  19. switches::kFetchHintsOverride, "whatever.com");
  20. std::unique_ptr<CommandLineTopHostProvider> top_host_provider =
  21. CommandLineTopHostProvider::CreateIfEnabled();
  22. ASSERT_TRUE(top_host_provider);
  23. }
  24. TEST(CommandLineTopHostProviderTest,
  25. GetTopHostsMaxLessThanProvidedSizeReturnsEverything) {
  26. base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
  27. switches::kFetchHintsOverride, "whatever.com");
  28. std::unique_ptr<CommandLineTopHostProvider> top_host_provider =
  29. CommandLineTopHostProvider::CreateIfEnabled();
  30. ASSERT_TRUE(top_host_provider);
  31. std::vector<std::string> top_hosts = top_host_provider->GetTopHosts();
  32. EXPECT_EQ(1ul, top_hosts.size());
  33. EXPECT_EQ("whatever.com", top_hosts[0]);
  34. }
  35. TEST(CommandLineTopHostProviderTest,
  36. GetTopHostsMaxGreaterThanTotalVectorSizeReturnsFirstN) {
  37. base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
  38. switches::kFetchHintsOverride, "whatever.com,awesome.com");
  39. std::unique_ptr<CommandLineTopHostProvider> top_host_provider =
  40. CommandLineTopHostProvider::CreateIfEnabled();
  41. ASSERT_TRUE(top_host_provider);
  42. std::vector<std::string> top_hosts = top_host_provider->GetTopHosts();
  43. EXPECT_EQ(2u, top_hosts.size());
  44. EXPECT_EQ("whatever.com", top_hosts[0]);
  45. EXPECT_EQ("awesome.com", top_hosts[1]);
  46. }
  47. } // namespace optimization_guide