summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/platform/loader/fetch/resource_fetcher_properties_test.cc
blob: 8890de7be676291826cda7405b1f10db1fc89c37 (plain)
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
126
127
128
// Copyright 2019 The Chromium 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 "third_party/blink/renderer/platform/loader/fetch/resource_fetcher_properties.h"

#include "services/network/public/mojom/ip_address_space.mojom-blink.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/mojom/security_context/insecure_request_policy.mojom-blink.h"
#include "third_party/blink/public/mojom/service_worker/controller_service_worker_mode.mojom-blink.h"
#include "third_party/blink/renderer/platform/loader/fetch/fetch_client_settings_object.h"
#include "third_party/blink/renderer/platform/loader/fetch/fetch_client_settings_object_snapshot.h"
#include "third_party/blink/renderer/platform/loader/testing/test_resource_fetcher_properties.h"

namespace blink {

namespace {

class DetachableResourceFetcherPropertiesTest : public testing::Test {
 public:
  const FetchClientSettingsObjectSnapshot& CreateFetchClientSettingsObject(
      network::mojom::IPAddressSpace address_space) {
    return *MakeGarbageCollected<FetchClientSettingsObjectSnapshot>(
        KURL("https://example.com/foo.html"),
        KURL("https://example.com/foo.html"),
        SecurityOrigin::Create(KURL("https://example.com/")),
        network::mojom::ReferrerPolicy::kDefault,
        "https://example.com/foo.html", HttpsState::kModern,
        AllowedByNosniff::MimeTypeCheck::kStrict, address_space,
        mojom::blink::InsecureRequestPolicy::kLeaveInsecureRequestsAlone,
        FetchClientSettingsObject::InsecureNavigationsSet());
  }
};

TEST_F(DetachableResourceFetcherPropertiesTest, DetachWithDefaultValues) {
  const auto& original_client_settings_object =
      CreateFetchClientSettingsObject(network::mojom::IPAddressSpace::kPublic);
  auto& properties = *MakeGarbageCollected<DetachableResourceFetcherProperties>(
      *MakeGarbageCollected<TestResourceFetcherProperties>(
          original_client_settings_object));

  const auto& client_settings_object =
      properties.GetFetchClientSettingsObject();
  EXPECT_EQ(&original_client_settings_object, &client_settings_object);
  EXPECT_FALSE(properties.IsMainFrame());
  EXPECT_EQ(properties.GetControllerServiceWorkerMode(),
            mojom::ControllerServiceWorkerMode::kNoController);
  // We cannot call ServiceWorkerId as the service worker mode is kNoController.
  EXPECT_FALSE(properties.IsPaused());
  EXPECT_FALSE(properties.IsDetached());
  EXPECT_FALSE(properties.IsLoadComplete());
  EXPECT_FALSE(properties.ShouldBlockLoadingSubResource());
  EXPECT_FALSE(properties.IsSubframeDeprioritizationEnabled());
  EXPECT_EQ(scheduler::FrameStatus::kNone, properties.GetFrameStatus());

  properties.Detach();

  EXPECT_NE(&client_settings_object,
            &properties.GetFetchClientSettingsObject());
  EXPECT_EQ(properties.GetFetchClientSettingsObject().BaseUrl(),
            KURL("https://example.com/foo.html"));
  EXPECT_FALSE(properties.IsMainFrame());
  EXPECT_EQ(properties.GetControllerServiceWorkerMode(),
            mojom::ControllerServiceWorkerMode::kNoController);
  // We cannot call ServiceWorkerId as the service worker mode is kNoController.
  EXPECT_FALSE(properties.IsPaused());
  EXPECT_TRUE(properties.IsDetached());
  EXPECT_FALSE(properties.IsLoadComplete());
  EXPECT_TRUE(properties.ShouldBlockLoadingSubResource());
  EXPECT_FALSE(properties.IsSubframeDeprioritizationEnabled());
  EXPECT_EQ(scheduler::FrameStatus::kNone, properties.GetFrameStatus());
}

TEST_F(DetachableResourceFetcherPropertiesTest, DetachWithNonDefaultValues) {
  const auto& original_client_settings_object =
      CreateFetchClientSettingsObject(network::mojom::IPAddressSpace::kPublic);
  auto& original_properties =
      *MakeGarbageCollected<TestResourceFetcherProperties>(
          original_client_settings_object);
  auto& properties = *MakeGarbageCollected<DetachableResourceFetcherProperties>(
      original_properties);

  original_properties.SetIsMainFrame(true);
  original_properties.SetControllerServiceWorkerMode(
      mojom::ControllerServiceWorkerMode::kControlled);
  original_properties.SetServiceWorkerId(133);
  original_properties.SetIsPaused(true);
  original_properties.SetIsLoadComplete(true);
  original_properties.SetShouldBlockLoadingSubResource(true);
  original_properties.SetIsSubframeDeprioritizationEnabled(true);
  original_properties.SetFrameStatus(scheduler::FrameStatus::kMainFrameVisible);

  const auto& client_settings_object =
      properties.GetFetchClientSettingsObject();
  EXPECT_EQ(&original_client_settings_object, &client_settings_object);
  EXPECT_TRUE(properties.IsMainFrame());
  EXPECT_EQ(properties.GetControllerServiceWorkerMode(),
            mojom::ControllerServiceWorkerMode::kControlled);
  EXPECT_EQ(properties.ServiceWorkerId(), 133);
  EXPECT_TRUE(properties.IsPaused());
  EXPECT_FALSE(properties.IsDetached());
  EXPECT_TRUE(properties.IsLoadComplete());
  EXPECT_TRUE(properties.ShouldBlockLoadingSubResource());
  EXPECT_TRUE(properties.IsSubframeDeprioritizationEnabled());
  EXPECT_EQ(scheduler::FrameStatus::kMainFrameVisible,
            properties.GetFrameStatus());

  properties.Detach();

  EXPECT_NE(&client_settings_object,
            &properties.GetFetchClientSettingsObject());
  EXPECT_EQ(properties.GetFetchClientSettingsObject().BaseUrl(),
            KURL("https://example.com/foo.html"));
  EXPECT_TRUE(properties.IsMainFrame());
  EXPECT_EQ(properties.GetControllerServiceWorkerMode(),
            mojom::ControllerServiceWorkerMode::kNoController);
  // We cannot call ServiceWorkerId as the service worker mode is kNoController.
  EXPECT_TRUE(properties.IsPaused());
  EXPECT_TRUE(properties.IsDetached());
  EXPECT_TRUE(properties.IsLoadComplete());
  EXPECT_TRUE(properties.ShouldBlockLoadingSubResource());
  EXPECT_TRUE(properties.IsSubframeDeprioritizationEnabled());
  EXPECT_EQ(scheduler::FrameStatus::kNone, properties.GetFrameStatus());
}

}  // namespace

}  // namespace blink