chromium/chrome/browser/preloading/prefetch/prefetch_browsertest.cc

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

#include "base/path_service.h"
#include "base/test/scoped_feature_list.h"
#include "chrome/browser/preloading/prefetch/chrome_prefetch_manager.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/test/base/chrome_test_utils.h"
#include "chrome/test/base/platform_browser_test.h"
#include "content/public/common/content_features.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/prefetch_test_util.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "testing/gtest/include/gtest/gtest.h"

#if BUILDFLAG(IS_ANDROID)
#include "chrome/browser/flags/android/chrome_feature_list.h"
#endif  // BUILDFLAG(IS_ANDROID)

namespace {

class PrefetchBrowserTest : public PlatformBrowserTest {};

#if BUILDFLAG(IS_ANDROID)
IN_PROC_BROWSER_TEST_F(PrefetchBrowserTest, CCTPrefetch) {
  content::test::TestPrefetchWatcher test_prefetch_watcher;

  const GURL initial_url = GetURL("/empty.html");
  const GURL prefetch_url = GetURL("/simple.html");
  ASSERT_TRUE(NavigateToURL(initial_url));

  auto* chrome_prefetch_manager =
      ChromePrefetchManager::GetOrCreateForWebContents(GetActiveWebContents());
  chrome_prefetch_manager->StartPrefetchFromCCT(prefetch_url, false,
                                                std::nullopt);

  content::test::PrefetchContainerIdForTesting prefetch_container_id =
      test_prefetch_watcher.WaitUntilPrefetchResponseCompleted(std::nullopt,
                                                               prefetch_url);

  ASSERT_TRUE(NavigateToURL(prefetch_url));

  EXPECT_TRUE(test_prefetch_watcher.PrefetchUsedInLastNavigation());
  EXPECT_EQ(
      test_prefetch_watcher.GetPrefetchContainerIdForTestingInLastNavigation(),
      prefetch_container_id);
}
#endif  // BUILDFLAG(IS_ANDROID)

}  // namespace