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
  263
  264
  265
  266
  267
  268
  269
  270
  271
  272
  273
  274
  275
  276
  277
  278
  279
  280
  281
  282
  283
  284
  285
  286
  287
  288
  289
  290
  291
  292
  293
  294
  295
  296
  297
  298
  299
  300
  301
  302
  303
  304
  305
  306
  307
  308
  309
  310
  311
  312
  313
  314
  315
  316
  317
  318
  319
  320
  321
  322
  323
  324
  325
  326
  327
  328
  329
  330
  331
  332
  333
  334
  335
  336
  337
  338
  339
  340
  341
  342
  343
  344
  345
  346
  347
  348
  349
  350
  351
  352
  353
  354
  355
  356
  357
  358
  359
  360
  361
  362
  363
  364
  365
  366
  367
  368
  369
  370
  371
  372
  373
  374
  375
  376
  377
  378
  379
  380
  381
  382
  383
  384
  385
  386
  387
  388
  389
  390
  391
  392
  393
  394
  395
  396
  397
  398
  399
  400
  401
  402
  403
  404
  405
  406
  407
  408
  409
  410
  411
  412
  413
  414
  415
  416
  417
  418
  419
  420
  421
  422
  423
  424
  425
  426
  427
  428
  429
  430
  431
  432
  433
  434
  435
  436
  437
  438
  439
  440
  441
  442
  443
  444
  445
  446
  447
  448
  449
  450

content / browser / preloading / preloading_decider_browsertest.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 "content/browser/preloading/preloading_decider.h"

#include "base/test/scoped_feature_list.h"
#include "base/timer/elapsed_timer.h"
#include "components/ukm/test_ukm_recorder.h"
#include "content/browser/preloading/preloading.h"
#include "content/browser/preloading/preloading_data_impl.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/content_browser_test.h"
#include "content/public/test/content_browser_test_utils.h"
#include "content/public/test/preloading_test_util.h"
#include "content/public/test/test_navigation_observer.h"
#include "content/shell/browser/shell.h"
#include "net/dns/mock_host_resolver.h"
#include "services/metrics/public/cpp/ukm_builders.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/mojom/preloading/anchor_element_interaction_host.mojom.h"

namespace content {

class PreloadingDeciderBrowserTest : public ContentBrowserTest {
 public:
  PreloadingDeciderBrowserTest() = default;
  ~PreloadingDeciderBrowserTest() override = default;

  void SetUp() override {
    feature_list_.InitWithFeaturesAndParameters(
        {
            {blink::features::kPreloadingHeuristicsMLModel,
             {{"enact_candidates", "true"}}},
            {blink::features::kPrerender2InNewTab, {}},
        },
        {
            // Disable the memory requirement of Prerender2 so the test can run
            // on any bot.
            {blink::features::kPrerender2MemoryControls},
        });

    ContentBrowserTest::SetUp();
  }

  void SetUpOnMainThread() override {
    ContentBrowserTest::SetUpOnMainThread();
    host_resolver()->AddRule("*", "127.0.0.1");
    https_server_ = std::make_unique<net::EmbeddedTestServer>(
        net::EmbeddedTestServer::TYPE_HTTPS);
    https_server_->AddDefaultHandlers(GetTestDataFilePath());

    ASSERT_TRUE(https_server_->Start());
  }

  WebContents* web_contents() { return shell()->web_contents(); }

  const GURL GetTestURL(const char* file) const {
    return https_server_->GetURL(file);
  }

  void ExpectCandidatesReceived() {
    // The renderer queues updates and waits for style to be clean.
    EXPECT_EQ(true, EvalJsAfterLifecycleUpdate(web_contents(), "", "true"));
    EXPECT_EQ(true, EvalJsAfterLifecycleUpdate(web_contents(), "", "true"));

    auto* preloading_decider = PreloadingDecider::GetOrCreateForCurrentDocument(
        web_contents()->GetPrimaryMainFrame());
    ASSERT_TRUE(preloading_decider);
    EXPECT_TRUE(preloading_decider->HasCandidatesForTesting());
  }

 private:
  content::test::PreloadingConfigOverride preloading_config_override_;
  base::test::ScopedFeatureList feature_list_;
  std::unique_ptr<net::EmbeddedTestServer> https_server_;
};

IN_PROC_BROWSER_TEST_F(PreloadingDeciderBrowserTest,
                       SetIsNavigationInDomainCallback) {
  base::HistogramTester histogram_tester;
  ASSERT_TRUE(NavigateToURL(shell(),
                            GetTestURL("/preloading/preloading_decider.html")));
  ExpectCandidatesReceived();

  // Now navigate to another page
  TestNavigationObserver nav_observer(web_contents());
  EXPECT_TRUE(ExecJs(web_contents(), "document.getElementById('bar').click()"));
  nav_observer.Wait();

  histogram_tester.ExpectBucketCount(
      "Preloading.Predictor.SpeculationRules.Recall",
      PredictorConfusionMatrix::kFalseNegative, 1);
  histogram_tester.ExpectBucketCount(
      "Preloading.Predictor.UrlPointerDownOnAnchor.Recall",
      PredictorConfusionMatrix::kFalseNegative, 1);
  histogram_tester.ExpectBucketCount(
      "Preloading.Predictor.UrlPointerHoverOnAnchor.Recall",
      PredictorConfusionMatrix::kFalseNegative, 1);
  histogram_tester.ExpectBucketCount(
      "Preloading.Predictor.PreloadingHeuristicsMLModel.Recall",
      PredictorConfusionMatrix::kFalseNegative, 1);
}

class PreloadingDeciderNonEagerBrowserTest
    : public PreloadingDeciderBrowserTest,
      public ::testing::WithParamInterface<
          ::testing::tuple<PreloadingPredictor, PreloadingType>> {
 public:
  static std::string DescribeParams(
      const testing::TestParamInfo<ParamType>& info) {
    const auto& [predictor, type] = info.param;
    return base::StringPrintf(
        "%s_%s", std::string(predictor.name()).c_str(),
        std::string(PreloadingTypeToString(type)).c_str());
  }

  static constexpr PreloadingPredictor kNonEagerPredictors[] = {
      preloading_predictor::kUrlPointerDownOnAnchor,
      preloading_predictor::kUrlPointerHoverOnAnchor,
      preloading_predictor::kPreloadingHeuristicsMLModel,
  };

  static constexpr PreloadingType kPreloadingTypes[] = {
      PreloadingType::kPrefetch,
      PreloadingType::kPrerender,
  };

  PreloadingPredictor predictor() const {
    return ::testing::get<0>(GetParam());
  }

  PreloadingType type() const { return ::testing::get<1>(GetParam()); }
};

INSTANTIATE_TEST_SUITE_P(
    All,
    PreloadingDeciderNonEagerBrowserTest,
    ::testing::Combine(
        ::testing::ValuesIn(
            PreloadingDeciderNonEagerBrowserTest::kNonEagerPredictors),
        ::testing::ValuesIn(
            PreloadingDeciderNonEagerBrowserTest::kPreloadingTypes)),
    PreloadingDeciderNonEagerBrowserTest::DescribeParams);

IN_PROC_BROWSER_TEST_P(PreloadingDeciderNonEagerBrowserTest,
                       EnactModerateCandidate) {
  base::ScopedMockElapsedTimersForTest mock_elapsed_timer;
  base::HistogramTester histogram_tester;
  ukm::TestAutoSetUkmRecorder ukm_recorder;

  ASSERT_TRUE(NavigateToURL(
      shell(), GetTestURL("/preloading/preloading_decider_moderate.html")));
  ExpectCandidatesReceived();

  std::string next_page_id;
  GURL next_page_url;
  switch (type()) {
    case PreloadingType::kPrefetch:
      next_page_id = "b";
      next_page_url = GetTestURL("/title1.html?b");
      break;
    case PreloadingType::kPrerender:
      next_page_id = "c";
      next_page_url = GetTestURL("/title1.html?c");
      break;
    default:
      FAIL();
  }

  // Trigger the non-eager predictor.
  auto* preloading_decider = PreloadingDecider::GetOrCreateForCurrentDocument(
      web_contents()->GetPrimaryMainFrame());
  ASSERT_TRUE(preloading_decider);
  if (predictor() == preloading_predictor::kUrlPointerDownOnAnchor) {
    preloading_decider->OnPointerDown(next_page_url);
  } else if (predictor() == preloading_predictor::kUrlPointerHoverOnAnchor) {
    preloading_decider->OnPointerHover(
        next_page_url,
        blink::mojom::AnchorElementPointerData::New(true, 0.0, 0.0));
  } else if (predictor() ==
             preloading_predictor::kPreloadingHeuristicsMLModel) {
    preloading_decider->OnPreloadingHeuristicsModelDone(next_page_url,
                                                        /*score=*/1.0);
  } else {
    FAIL();
  }

  TestNavigationObserver nav_observer(web_contents());
  EXPECT_TRUE(
      ExecJs(web_contents(),
             JsReplace("document.getElementById($1).click()", next_page_id)));
  nav_observer.Wait();

  const ukm::SourceId source_id = nav_observer.next_page_ukm_source_id();

  const std::string type_str{PreloadingTypeToString(type())};
  const char* type_cstr = type_str.c_str();

  // For non-eager predictors, there are two PreloadingPredictors that
  // contribute to a preloading attempt. One creates a candidate, but does not
  // start the preloading attempt. The other starts the attempt. We assert below
  // that both predictors have appropriate attribution in the recorded metrics.
  {
    const std::string rule_predictor_str{
        content_preloading_predictor::kSpeculationRules.name()};
    const char* rule_predictor_cstr = rule_predictor_str.c_str();

    // We intentionally don't record a prediction for non-eager speculation
    // rules. They aren't predictions per se, but a declaration to the browser
    // that preloading would be safe.
    histogram_tester.ExpectTotalCount(
        base::StringPrintf("Preloading.Predictor.%s.Precision",
                           rule_predictor_cstr),
        0);
    histogram_tester.ExpectUniqueSample(
        base::StringPrintf("Preloading.Predictor.%s.Recall",
                           rule_predictor_cstr),
        PredictorConfusionMatrix::kFalseNegative, 1);

    histogram_tester.ExpectUniqueSample(
        base::StringPrintf("Preloading.%s.Attempt.%s.Precision", type_cstr,
                           rule_predictor_cstr),
        PredictorConfusionMatrix::kTruePositive, 1);
    histogram_tester.ExpectUniqueSample(
        base::StringPrintf("Preloading.%s.Attempt.%s.Recall", type_cstr,
                           rule_predictor_cstr),
        PredictorConfusionMatrix::kTruePositive, 1);
    histogram_tester.ExpectUniqueSample(
        base::StringPrintf("Preloading.%s.Attempt.%s.TriggeringOutcome",
                           type_cstr, rule_predictor_cstr),
        PreloadingTriggeringOutcome::kSuccess, 1);

    test::PreloadingAttemptUkmEntryBuilder attempt_entry_builder(
        content_preloading_predictor::kSpeculationRules);
    ukm::TestUkmRecorder::HumanReadableUkmEntry expected_attempt_entry =
        attempt_entry_builder.BuildEntry(
            source_id, type(), PreloadingEligibility::kEligible,
            PreloadingHoldbackStatus::kAllowed,
            PreloadingTriggeringOutcome::kSuccess,
            PreloadingFailureReason::kUnspecified, /*accurate=*/true,
            base::ScopedMockElapsedTimersForTest::kMockElapsedTime,
            blink::mojom::SpeculationEagerness::kModerate);
    std::vector<ukm::TestUkmRecorder::HumanReadableUkmEntry> attempts =
        ukm_recorder.GetEntries(ukm::builders::Preloading_Attempt::kEntryName,
                                test::kPreloadingAttemptUkmMetrics);
    std::erase_if(attempts, [&](const auto& entry) {
      return entry.metrics.at(
                 ukm::builders::Preloading_Attempt::kPreloadingPredictorName) !=
             content_preloading_predictor::kSpeculationRules.ukm_value();
    });
    ASSERT_EQ(attempts.size(), 1u);
    EXPECT_EQ(attempts[0], expected_attempt_entry)
        << test::ActualVsExpectedUkmEntryToString(attempts[0],
                                                  expected_attempt_entry);

    std::vector<ukm::TestUkmRecorder::HumanReadableUkmEntry> predictions =
        ukm_recorder.GetEntries(
            ukm::builders::Preloading_Prediction::kEntryName,
            test::kPreloadingPredictionUkmMetrics);
    std::erase_if(predictions, [&](const auto& entry) {
      return entry.metrics.at(ukm::builders::Preloading_Prediction::
                                  kPreloadingPredictorName) !=
             content_preloading_predictor::kSpeculationRules.ukm_value();
    });
    EXPECT_TRUE(predictions.empty());
  }

  {
    const std::string predictor_str{predictor().name()};
    const char* predictor_cstr = predictor_str.c_str();

    histogram_tester.ExpectUniqueSample(
        base::StringPrintf("Preloading.Predictor.%s.Precision", predictor_cstr),
        PredictorConfusionMatrix::kTruePositive, 1);
    histogram_tester.ExpectUniqueSample(
        base::StringPrintf("Preloading.Predictor.%s.Recall", predictor_cstr),
        PredictorConfusionMatrix::kTruePositive, 1);
    histogram_tester.ExpectUniqueSample(
        base::StringPrintf("Preloading.%s.Attempt.%s.Precision", type_cstr,
                           predictor_cstr),
        PredictorConfusionMatrix::kTruePositive, 1);
    histogram_tester.ExpectUniqueSample(
        base::StringPrintf("Preloading.%s.Attempt.%s.Recall", type_cstr,
                           predictor_cstr),
        PredictorConfusionMatrix::kTruePositive, 1);
    histogram_tester.ExpectUniqueSample(
        base::StringPrintf("Preloading.%s.Attempt.%s.TriggeringOutcome",
                           type_cstr, predictor_cstr),
        PreloadingTriggeringOutcome::kSuccess, 1);

    constexpr bool accurate = true;
    constexpr int confidence = 100;

    test::PreloadingAttemptUkmEntryBuilder attempt_entry_builder(predictor());
    ukm::TestUkmRecorder::HumanReadableUkmEntry expected_attempt_entry =
        attempt_entry_builder.BuildEntry(
            source_id, type(), PreloadingEligibility::kEligible,
            PreloadingHoldbackStatus::kAllowed,
            PreloadingTriggeringOutcome::kSuccess,
            PreloadingFailureReason::kUnspecified, accurate,
            base::ScopedMockElapsedTimersForTest::kMockElapsedTime,
            blink::mojom::SpeculationEagerness::kModerate);
    std::vector<ukm::TestUkmRecorder::HumanReadableUkmEntry> attempts =
        ukm_recorder.GetEntries(ukm::builders::Preloading_Attempt::kEntryName,
                                test::kPreloadingAttemptUkmMetrics);
    std::erase_if(attempts, [&](const auto& entry) {
      return entry.metrics.at(
                 ukm::builders::Preloading_Attempt::kPreloadingPredictorName) !=
             predictor().ukm_value();
    });
    ASSERT_EQ(attempts.size(), 1u);
    EXPECT_EQ(attempts[0], expected_attempt_entry)
        << test::ActualVsExpectedUkmEntryToString(attempts[0],
                                                  expected_attempt_entry);

    test::PreloadingPredictionUkmEntryBuilder prediction_entry_builder(
        predictor());
    ukm::TestUkmRecorder::HumanReadableUkmEntry expected_prediction_entry =
        prediction_entry_builder.BuildEntry(source_id, confidence, accurate);
    std::vector<ukm::TestUkmRecorder::HumanReadableUkmEntry> predictions =
        ukm_recorder.GetEntries(
            ukm::builders::Preloading_Prediction::kEntryName,
            test::kPreloadingPredictionUkmMetrics);
    std::erase_if(predictions, [&](const auto& entry) {
      return entry.metrics.at(ukm::builders::Preloading_Prediction::
                                  kPreloadingPredictorName) !=
             predictor().ukm_value();
    });
    ASSERT_EQ(predictions.size(), 1u);
    EXPECT_EQ(predictions[0], expected_prediction_entry)
        << test::ActualVsExpectedUkmEntryToString(predictions[0],
                                                  expected_prediction_entry);
  }
}

IN_PROC_BROWSER_TEST_F(PreloadingDeciderBrowserTest,
                       EnactModerateNewTabPrerender) {
  base::HistogramTester histogram_tester;
  ASSERT_TRUE(NavigateToURL(
      shell(), GetTestURL("/preloading/preloading_decider_moderate.html")));
  ExpectCandidatesReceived();

  const GURL new_tab_url = GetTestURL("/title1.html?d");

  auto* preloading_decider = PreloadingDecider::GetOrCreateForCurrentDocument(
      web_contents()->GetPrimaryMainFrame());
  ASSERT_TRUE(preloading_decider);

  TestNavigationObserver nav_observer(new_tab_url);
  nav_observer.StartWatchingNewWebContents();

  preloading_decider->OnPointerDown(new_tab_url);

  EXPECT_TRUE(ExecJs(web_contents(), "document.getElementById('d').click()"));
  nav_observer.Wait();

  // Also navigate the current tab away, so any of its metrics are flushed.
  ASSERT_TRUE(NavigateToURL(shell(), GetTestURL("/title2.html")));

  {
    const std::string rule_predictor_str{
        content_preloading_predictor::kSpeculationRules.name()};
    const char* rule_predictor_cstr = rule_predictor_str.c_str();

    // We intentionally don't record a prediction for non-eager speculation
    // rules. They aren't predictions per se, but a declaration to the browser
    // that preloading would be safe.
    histogram_tester.ExpectTotalCount(
        base::StringPrintf("Preloading.Predictor.%s.Precision",
                           rule_predictor_cstr),
        0);
    histogram_tester.ExpectUniqueSample(
        base::StringPrintf("Preloading.Predictor.%s.Recall",
                           rule_predictor_cstr),
        PredictorConfusionMatrix::kFalseNegative, 1);

    histogram_tester.ExpectUniqueSample(
        base::StringPrintf("Preloading.Prerender.Attempt.%s.Precision",
                           rule_predictor_cstr),
        PredictorConfusionMatrix::kTruePositive, 1);
    histogram_tester.ExpectUniqueSample(
        base::StringPrintf("Preloading.Prerender.Attempt.%s.Recall",
                           rule_predictor_cstr),
        PredictorConfusionMatrix::kTruePositive, 1);
  }

  {
    const std::string predictor_str{
        preloading_predictor::kUrlPointerDownOnAnchor.name()};
    const char* predictor_cstr = predictor_str.c_str();

    histogram_tester.ExpectUniqueSample(
        base::StringPrintf("Preloading.Predictor.%s.Precision", predictor_cstr),
        PredictorConfusionMatrix::kTruePositive, 1);
    histogram_tester.ExpectUniqueSample(
        base::StringPrintf("Preloading.Predictor.%s.Recall", predictor_cstr),
        PredictorConfusionMatrix::kTruePositive, 1);
    histogram_tester.ExpectUniqueSample(
        base::StringPrintf("Preloading.Prerender.Attempt.%s.Precision",
                           predictor_cstr),
        PredictorConfusionMatrix::kTruePositive, 1);
    histogram_tester.ExpectUniqueSample(
        base::StringPrintf("Preloading.Prerender.Attempt.%s.Recall",
                           predictor_cstr),
        PredictorConfusionMatrix::kTruePositive, 1);
  }
}

IN_PROC_BROWSER_TEST_F(PreloadingDeciderBrowserTest, PredictionWithoutAttempt) {
  base::HistogramTester histogram_tester;
  ASSERT_TRUE(NavigateToURL(
      shell(), GetTestURL("/preloading/preloading_decider_moderate.html")));
  ExpectCandidatesReceived();

  auto* preloading_decider = PreloadingDecider::GetOrCreateForCurrentDocument(
      web_contents()->GetPrimaryMainFrame());
  ASSERT_TRUE(preloading_decider);

  TestNavigationObserver nav_observer(web_contents());
  preloading_decider->OnPointerDown(GetTestURL("/title1.html?a"));
  EXPECT_TRUE(ExecJs(web_contents(), "document.getElementById('a').click()"));
  nav_observer.Wait();

  {
    const std::string predictor_str{
        preloading_predictor::kUrlPointerDownOnAnchor.name()};
    const char* predictor_cstr = predictor_str.c_str();

    histogram_tester.ExpectUniqueSample(
        base::StringPrintf("Preloading.Predictor.%s.Precision", predictor_cstr),
        PredictorConfusionMatrix::kTruePositive, 1);
    histogram_tester.ExpectUniqueSample(
        base::StringPrintf("Preloading.Predictor.%s.Recall", predictor_cstr),
        PredictorConfusionMatrix::kTruePositive, 1);
    histogram_tester.ExpectTotalCount(
        base::StringPrintf("Preloading.Prerender.Attempt.%s.Precision",
                           predictor_cstr),
        0);
    histogram_tester.ExpectUniqueSample(
        base::StringPrintf("Preloading.Prerender.Attempt.%s.Recall",
                           predictor_cstr),
        PredictorConfusionMatrix::kFalseNegative, 1);
  }
}

}  // namespace content