1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125

headless / lib / browser / headless_client_hints_controller_delegate_unittest.cc [blame]

// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "headless/lib/browser/headless_client_hints_controller_delegate.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/user_agent/user_agent_metadata.h"

using network::mojom::WebClientHintsType::kUAArch;
using network::mojom::WebClientHintsType::kUAModel;
using network::mojom::WebClientHintsType::kUAPlatform;

namespace headless {

TEST(HeadlessClientHintsControllerDelegateTests, GetNetworkQualityTracker) {
  HeadlessClientHintsControllerDelegate delegate;
  EXPECT_EQ(delegate.GetNetworkQualityTracker(), nullptr);
}

TEST(HeadlessClientHintsControllerDelegateTests, PersistentClientHints) {
  HeadlessClientHintsControllerDelegate delegate;
  auto kOrigin = url::Origin::Create(GURL("https://example.com"));
  delegate.PersistClientHints(kOrigin, nullptr, {kUAArch, kUAPlatform});

  blink::EnabledClientHints enabled_client_hints;
  delegate.GetAllowedClientHintsFromSource(kOrigin, &enabled_client_hints);
  EXPECT_EQ(enabled_client_hints.GetEnabledHints(),
            std::vector({kUAArch, kUAPlatform}));

  delegate.PersistClientHints(kOrigin, nullptr, {kUAPlatform, kUAModel});
  delegate.GetAllowedClientHintsFromSource(kOrigin, &enabled_client_hints);
  EXPECT_EQ(enabled_client_hints.GetEnabledHints(),
            std::vector({kUAPlatform, kUAModel}));
}

TEST(HeadlessClientHintsControllerDelegateTests, PersistentClientHintsNotSet) {
  HeadlessClientHintsControllerDelegate delegate;
  auto kOrigin = url::Origin::Create(GURL("https://example.com"));

  blink::EnabledClientHints enabled_client_hints;
  delegate.GetAllowedClientHintsFromSource(kOrigin, &enabled_client_hints);
  EXPECT_TRUE(enabled_client_hints.GetEnabledHints().empty());
}

TEST(HeadlessClientHintsControllerDelegateTests,
     PersistentClientHintsEmptyOrigin) {
  HeadlessClientHintsControllerDelegate delegate;
  auto kOrigin = url::Origin();
  delegate.PersistClientHints(kOrigin, nullptr, {kUAArch, kUAPlatform});

  // Hints are not persisted for empty origins.
  blink::EnabledClientHints enabled_client_hints;
  delegate.GetAllowedClientHintsFromSource(kOrigin, &enabled_client_hints);
  EXPECT_TRUE(enabled_client_hints.GetEnabledHints().empty());
}

TEST(HeadlessClientHintsControllerDelegateTests,
     PersistentClientHintsInsecureOrigin) {
  HeadlessClientHintsControllerDelegate delegate;
  auto kOrigin = url::Origin::Create(GURL("http://example.com"));
  delegate.PersistClientHints(kOrigin, nullptr, {kUAArch, kUAPlatform});

  // Hints are not persisted for insecure origins.
  blink::EnabledClientHints enabled_client_hints;
  delegate.GetAllowedClientHintsFromSource(kOrigin, &enabled_client_hints);
  EXPECT_TRUE(enabled_client_hints.GetEnabledHints().empty());
}

TEST(HeadlessClientHintsControllerDelegateTests,
     PersistentClientHintsDifferentOrigins) {
  HeadlessClientHintsControllerDelegate delegate;
  auto kOrigin1 = url::Origin::Create(GURL("https://one.example.com"));
  auto kOrigin2 = url::Origin::Create(GURL("https://two.example.com"));
  delegate.PersistClientHints(kOrigin1, nullptr, {kUAArch, kUAPlatform});
  delegate.PersistClientHints(kOrigin2, nullptr, {kUAPlatform, kUAModel});

  blink::EnabledClientHints enabled_client_hints;
  delegate.GetAllowedClientHintsFromSource(kOrigin1, &enabled_client_hints);
  EXPECT_EQ(enabled_client_hints.GetEnabledHints(),
            std::vector({kUAArch, kUAPlatform}));

  delegate.GetAllowedClientHintsFromSource(kOrigin2, &enabled_client_hints);
  EXPECT_EQ(enabled_client_hints.GetEnabledHints(),
            std::vector({kUAPlatform, kUAModel}));
}

TEST(HeadlessClientHintsControllerDelegateTests,
     PersistentClientHintsAdditional) {
  HeadlessClientHintsControllerDelegate delegate;
  auto kOrigin = url::Origin::Create(GURL("https://example.com"));
  delegate.PersistClientHints(kOrigin, nullptr, {kUAArch, kUAPlatform});
  delegate.SetAdditionalClientHints({kUAPlatform, kUAModel});

  blink::EnabledClientHints enabled_client_hints;
  delegate.GetAllowedClientHintsFromSource(kOrigin, &enabled_client_hints);
  EXPECT_EQ(enabled_client_hints.GetEnabledHints(),
            std::vector({kUAArch, kUAPlatform, kUAModel}));

  delegate.ClearAdditionalClientHints();

  delegate.GetAllowedClientHintsFromSource(kOrigin, &enabled_client_hints);
  EXPECT_EQ(enabled_client_hints.GetEnabledHints(),
            std::vector({kUAArch, kUAPlatform}));
}

TEST(HeadlessClientHintsControllerDelegateTests, IsJavaScriptAllowed) {
  HeadlessClientHintsControllerDelegate delegate;
  EXPECT_TRUE(delegate.IsJavaScriptAllowed(GURL(), nullptr));
}

TEST(HeadlessClientHintsControllerDelegateTests, GetUserAgentMetadata) {
  HeadlessClientHintsControllerDelegate delegate;
  EXPECT_EQ(delegate.GetUserAgentMetadata(),
            HeadlessBrowser::GetUserAgentMetadata());
}

TEST(HeadlessClientHintsControllerDelegateTests,
     MostRecentMainFrameViewportSize) {
  HeadlessClientHintsControllerDelegate delegate;
  EXPECT_EQ(delegate.GetMostRecentMainFrameViewportSize(), gfx::Size(800, 600));
  delegate.SetMostRecentMainFrameViewportSize(gfx::Size(640, 480));
  EXPECT_EQ(delegate.GetMostRecentMainFrameViewportSize(), gfx::Size(640, 480));
}

}  // namespace headless