summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/scheduler_integration_tests/scheduler_affecting_features_test.cc
blob: c3fe40679605b2e28ce67f89b39b8d2911449e7f (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code if governed by a BSD-style license that can be
// found in LICENSE file.

#include "base/run_loop.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/platform/web_url_loader_mock_factory.h"
#include "third_party/blink/public/web/web_script_source.h"
#include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/frame/web_local_frame_impl.h"
#include "third_party/blink/renderer/core/testing/fake_web_plugin.h"
#include "third_party/blink/renderer/core/testing/scoped_fake_plugin_registry.h"
#include "third_party/blink/renderer/core/testing/sim/sim_request.h"
#include "third_party/blink/renderer/core/testing/sim/sim_test.h"
#include "third_party/blink/renderer/platform/scheduler/public/page_scheduler.h"

using testing::_;

namespace blink {

class SchedulingAffectingFeaturesTest : public SimTest {
 public:
  PageScheduler* GetPageScheduler() {
    return MainFrameScheduler()->GetPageScheduler();
  }

  FrameScheduler* MainFrameScheduler() { return MainFrame().Scheduler(); }

  // Some features (e.g. document.load) are expected to appear in almost
  // any output. Filter them out to make most of the tests simpler.
  Vector<SchedulingPolicy::Feature> GetNonTrivialMainFrameFeatures() {
    Vector<SchedulingPolicy::Feature> result;
    for (SchedulingPolicy::Feature feature :
         MainFrameScheduler()
             ->GetActiveFeaturesTrackedForBackForwardCacheMetrics()) {
      if (feature == SchedulingPolicy::Feature::kDocumentLoaded)
        continue;
      if (feature == SchedulingPolicy::Feature::kOutstandingNetworkRequestFetch)
        continue;
      if (feature == SchedulingPolicy::Feature::kOutstandingNetworkRequestXHR)
        continue;
      if (feature ==
          SchedulingPolicy::Feature::kOutstandingNetworkRequestOthers) {
        continue;
      }
      result.push_back(feature);
    }
    return result;
  }
};

TEST_F(SchedulingAffectingFeaturesTest, WebSocketIsTracked) {
  SimRequest main_resource("https://example.com/", "text/html");

  LoadURL("https://example.com/");

  EXPECT_FALSE(GetPageScheduler()->OptedOutFromAggressiveThrottlingForTest());
  EXPECT_THAT(GetNonTrivialMainFrameFeatures(),
              testing::UnorderedElementsAre());

  main_resource.Complete(
      "<script>"
      "  var socket = new WebSocket(\"ws://www.example.com/websocket\");"
      "</script>");

  EXPECT_FALSE(GetPageScheduler()->OptedOutFromAggressiveThrottlingForTest());
  EXPECT_THAT(
      GetNonTrivialMainFrameFeatures(),
      testing::UnorderedElementsAre(SchedulingPolicy::Feature::kWebSocket));

  MainFrame().ExecuteScript(WebString("socket.close();"));

  EXPECT_FALSE(GetPageScheduler()->OptedOutFromAggressiveThrottlingForTest());
  EXPECT_THAT(GetNonTrivialMainFrameFeatures(),
              testing::UnorderedElementsAre());
}

TEST_F(SchedulingAffectingFeaturesTest, CacheControl_NoStore) {
  SimRequest::Params params;
  params.response_http_headers = {{"cache-control", "no-store"}};
  SimRequest main_resource("https://example.com/", "text/html", params);
  SimSubresourceRequest image_resource("https://example.com/image.png",
                                       "image/png", params);

  LoadURL("https://example.com/");

  main_resource.Complete("<img src=image.png>");

  EXPECT_THAT(
      GetNonTrivialMainFrameFeatures(),
      testing::UnorderedElementsAre(
          SchedulingPolicy::Feature::kMainResourceHasCacheControlNoStore));

  image_resource.Complete();

  EXPECT_THAT(
      GetNonTrivialMainFrameFeatures(),
      testing::UnorderedElementsAre(
          SchedulingPolicy::Feature::kMainResourceHasCacheControlNoStore,
          SchedulingPolicy::Feature::kSubresourceHasCacheControlNoStore));
}

TEST_F(SchedulingAffectingFeaturesTest, CacheControl_NoCache) {
  SimRequest::Params params;
  params.response_http_headers = {{"cache-control", "no-cache"}};
  SimRequest main_resource("https://example.com/", "text/html", params);
  SimSubresourceRequest image_resource("https://example.com/image.png",
                                       "image/png", params);
  LoadURL("https://example.com/");

  main_resource.Complete("<img src=image.png>");

  EXPECT_THAT(
      GetNonTrivialMainFrameFeatures(),
      testing::UnorderedElementsAre(
          SchedulingPolicy::Feature::kMainResourceHasCacheControlNoCache));

  image_resource.Complete();

  EXPECT_THAT(
      GetNonTrivialMainFrameFeatures(),
      testing::UnorderedElementsAre(
          SchedulingPolicy::Feature::kMainResourceHasCacheControlNoCache,
          SchedulingPolicy::Feature::kSubresourceHasCacheControlNoCache));
}

TEST_F(SchedulingAffectingFeaturesTest, CacheControl_Navigation) {
  SimRequest::Params params;
  params.response_http_headers = {{"cache-control", "no-cache,no-store"}};
  SimRequest main_resource1("https://foo.com/", "text/html", params);
  LoadURL("https://foo.com/");
  main_resource1.Complete();

  EXPECT_THAT(
      GetNonTrivialMainFrameFeatures(),
      testing::UnorderedElementsAre(
          SchedulingPolicy::Feature::kMainResourceHasCacheControlNoCache,
          SchedulingPolicy::Feature::kMainResourceHasCacheControlNoStore));

  SimRequest main_resource2("https://bar.com/", "text/html");
  LoadURL("https://bar.com/");
  main_resource2.Complete();

  EXPECT_THAT(GetNonTrivialMainFrameFeatures(),
              testing::UnorderedElementsAre());
}

TEST_F(SchedulingAffectingFeaturesTest, EventListener_PageShow) {
  SimRequest main_resource("https://foo.com/", "text/html");
  LoadURL("https://foo.com/");
  main_resource.Complete(
      "<script>"
      " window.addEventListener(\"pageshow\", () => {}); "
      "</script>");

  EXPECT_THAT(GetNonTrivialMainFrameFeatures(),
              testing::UnorderedElementsAre(
                  SchedulingPolicy::Feature::kPageShowEventListener));
}

TEST_F(SchedulingAffectingFeaturesTest, EventListener_PageHide) {
  SimRequest main_resource("https://foo.com/", "text/html");
  LoadURL("https://foo.com/");
  main_resource.Complete(
      "<script>"
      " window.addEventListener(\"pagehide\", () => {}); "
      "</script>");

  EXPECT_THAT(GetNonTrivialMainFrameFeatures(),
              testing::UnorderedElementsAre(
                  SchedulingPolicy::Feature::kPageHideEventListener));
}

TEST_F(SchedulingAffectingFeaturesTest, EventListener_BeforeUnload) {
  SimRequest main_resource("https://foo.com/", "text/html");
  LoadURL("https://foo.com/");
  main_resource.Complete(
      "<script>"
      " window.addEventListener(\"beforeunload\", () => {}); "
      "</script>");

  EXPECT_THAT(GetNonTrivialMainFrameFeatures(),
              testing::UnorderedElementsAre(
                  SchedulingPolicy::Feature::kBeforeUnloadEventListener));
}

TEST_F(SchedulingAffectingFeaturesTest, EventListener_Unload) {
  SimRequest main_resource("https://foo.com/", "text/html");
  LoadURL("https://foo.com/");
  main_resource.Complete(
      "<script>"
      " window.addEventListener(\"unload\", () => {}); "
      "</script>");

  EXPECT_THAT(GetNonTrivialMainFrameFeatures(),
              testing::UnorderedElementsAre(
                  SchedulingPolicy::Feature::kUnloadEventListener));
}

TEST_F(SchedulingAffectingFeaturesTest, EventListener_Freeze) {
  SimRequest main_resource("https://foo.com/", "text/html");
  LoadURL("https://foo.com/");
  main_resource.Complete(
      "<script>"
      " window.addEventListener(\"freeze\", () => {}); "
      "</script>");

  EXPECT_THAT(GetNonTrivialMainFrameFeatures(),
              testing::UnorderedElementsAre(
                  SchedulingPolicy::Feature::kFreezeEventListener));
}

TEST_F(SchedulingAffectingFeaturesTest, EventListener_Resume) {
  SimRequest main_resource("https://foo.com/", "text/html");
  LoadURL("https://foo.com/");
  main_resource.Complete(
      "<script>"
      " window.addEventListener(\"resume\", () => {}); "
      "</script>");

  EXPECT_THAT(GetNonTrivialMainFrameFeatures(),
              testing::UnorderedElementsAre(
                  SchedulingPolicy::Feature::kResumeEventListener));
}

TEST_F(SchedulingAffectingFeaturesTest, Plugins) {
  class PluginCreatingWebFrameClient
      : public frame_test_helpers::TestWebFrameClient {
   public:
    // WebLocalFrameClient overrides:
    WebPlugin* CreatePlugin(const WebPluginParams& params) override {
      return new FakeWebPlugin(params);
    }
  };

  ScopedFakePluginRegistry fake_plugins;
  SimRequest main_resource("https://example.com/", "text/html");
  LoadURL("https://example.com/");
  main_resource.Complete(
      "<object type='application/x-webkit-test-plugin'></object>");

  base::RunLoop().RunUntilIdle();

  EXPECT_THAT(GetNonTrivialMainFrameFeatures(),
              testing::UnorderedElementsAre(
                  SchedulingPolicy::Feature::kContainsPlugins));
}

TEST_F(SchedulingAffectingFeaturesTest, WebLocks) {
  SimRequest main_resource("https://foo.com/", "text/html");
  LoadURL("https://foo.com/");
  main_resource.Complete(
      "<script>"
      " navigator.locks.request('my_resource', async lock => {}); "
      "</script>");

  EXPECT_THAT(
      GetNonTrivialMainFrameFeatures(),
      testing::UnorderedElementsAre(SchedulingPolicy::Feature::kWebLocks));
}

}  // namespace blink