chromium/chrome/browser/pdf/pdf_extension_js_test.cc

// 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 <memory>
#include <string>
#include <vector>

#include "base/base_paths.h"
#include "base/command_line.h"
#include "base/path_service.h"
#include "base/strings/stringprintf.h"
#include "base/test/icu_test_util.h"
#include "base/test/with_feature_override.h"
#include "build/branding_buildflags.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/pdf/pdf_extension_test_base.h"
#include "chrome/browser/pdf/pdf_extension_test_util.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/webui_url_constants.h"
#include "chrome/test/base/devtools_agent_coverage_observer.h"
#include "chrome/test/base/test_switches.h"
#include "chrome/test/base/ui_test_utils.h"
#include "chrome/test/base/web_ui_test_data_source.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "content/public/browser/render_process_host.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/scoped_time_zone.h"
#include "extensions/test/result_catcher.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "pdf/buildflags.h"
#include "pdf/pdf_features.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/resource/resource_bundle.h"
#include "url/gurl.h"

class PDFExtensionJSTestBase : public PDFExtensionTestBase {};

class PDFExtensionJSTest : public base::test::WithFeatureOverride,
                           public PDFExtensionJSTestBase {};

IN_PROC_BROWSER_TEST_P(PDFExtensionJSTest, Basic) {}

IN_PROC_BROWSER_TEST_P(PDFExtensionJSTest, BasicPlugin) {}

IN_PROC_BROWSER_TEST_P(PDFExtensionJSTest, PluginController) {}

IN_PROC_BROWSER_TEST_P(PDFExtensionJSTest, Viewport) {}

IN_PROC_BROWSER_TEST_P(PDFExtensionJSTest, Layout3) {}

IN_PROC_BROWSER_TEST_P(PDFExtensionJSTest, Layout4) {}

IN_PROC_BROWSER_TEST_P(PDFExtensionJSTest, Bookmark) {}

IN_PROC_BROWSER_TEST_P(PDFExtensionJSTest, Navigator) {}

IN_PROC_BROWSER_TEST_P(PDFExtensionJSTest, ParamsParser) {}

IN_PROC_BROWSER_TEST_P(PDFExtensionJSTest, ZoomManager) {}

IN_PROC_BROWSER_TEST_P(PDFExtensionJSTest, GestureDetector) {}

IN_PROC_BROWSER_TEST_P(PDFExtensionJSTest, SwipeDetector) {}

IN_PROC_BROWSER_TEST_P(PDFExtensionJSTest, TouchHandling) {}

IN_PROC_BROWSER_TEST_P(PDFExtensionJSTest, Elements) {}

IN_PROC_BROWSER_TEST_P(PDFExtensionJSTest, DownloadControls) {}

IN_PROC_BROWSER_TEST_P(PDFExtensionJSTest, Title) {}

IN_PROC_BROWSER_TEST_P(PDFExtensionJSTest, WhitespaceTitle) {}

IN_PROC_BROWSER_TEST_P(PDFExtensionJSTest, PageChange) {}

IN_PROC_BROWSER_TEST_P(PDFExtensionJSTest, PageSelector) {}

IN_PROC_BROWSER_TEST_P(PDFExtensionJSTest, ScrollWithFormFieldFocusedTest) {}

IN_PROC_BROWSER_TEST_P(PDFExtensionJSTest, Metrics) {}

IN_PROC_BROWSER_TEST_P(PDFExtensionJSTest, ViewerPasswordDialog) {}

IN_PROC_BROWSER_TEST_P(PDFExtensionJSTest, ArrayBufferAllocator) {}

IN_PROC_BROWSER_TEST_P(PDFExtensionJSTest, ViewerToolbar) {}

IN_PROC_BROWSER_TEST_P(PDFExtensionJSTest, ViewerPdfSidenav) {}

IN_PROC_BROWSER_TEST_P(PDFExtensionJSTest, ViewerThumbnailBar) {}

IN_PROC_BROWSER_TEST_P(PDFExtensionJSTest, ViewerThumbnail) {}

IN_PROC_BROWSER_TEST_P(PDFExtensionJSTest, ViewerAttachmentBar) {}

IN_PROC_BROWSER_TEST_P(PDFExtensionJSTest, ViewerAttachment) {}

IN_PROC_BROWSER_TEST_P(PDFExtensionJSTest, Fullscreen) {}

IN_PROC_BROWSER_TEST_P(PDFExtensionJSTest, PostMessageProxy) {}

#if BUILDFLAG(IS_CHROMEOS_ASH)
IN_PROC_BROWSER_TEST_P(PDFExtensionJSTest, Printing) {
  RunTestsInJsModule("printing_icon_test.js", "test.pdf");
}
#endif  // BUILDFLAG(IS_CHROMEOS_ASH)

#if BUILDFLAG(ENABLE_INK)
// TODO(crbug.com/41434927): Test times out under sanitizers.
// TODO(crbug.com/41495998): Test fails for
// testViewportToCameraConversion.
IN_PROC_BROWSER_TEST_P(PDFExtensionJSTest, DISABLED_AnnotationsFeatureEnabled) {
  // TODO(crbug.com/40268279): Remove this once the test passes for OOPIF PDF.
  if (UseOopif()) {
    GTEST_SKIP();
  }

  RunTestsInJsModule("annotations_feature_enabled_test.js", "test.pdf");
}

IN_PROC_BROWSER_TEST_P(PDFExtensionJSTest, AnnotationsToolbar) {
  // Although this test file does not require a PDF to be loaded, loading the
  // elements without loading a PDF is difficult.
  RunTestsInJsModule("annotations_toolbar_test.js", "test.pdf");
}

IN_PROC_BROWSER_TEST_P(PDFExtensionJSTest, ViewerToolbarDropdown) {
  // Although this test file does not require a PDF to be loaded, loading the
  // elements without loading a PDF is difficult.
  RunTestsInJsModule("viewer_toolbar_dropdown_test.js", "test.pdf");
}
#endif  // BUILDFLAG(ENABLE_INK)

// PDFExtensionJSTest with forced Pacific Time Zone.
class PDFExtensionPacificTimeZoneJSTest : public PDFExtensionJSTest {};

IN_PROC_BROWSER_TEST_P(PDFExtensionPacificTimeZoneJSTest,
                       ViewerPropertiesDialog) {}

class PDFExtensionContentSettingJSTest : public PDFExtensionJSTest {};

IN_PROC_BROWSER_TEST_P(PDFExtensionContentSettingJSTest, Beep) {}

IN_PROC_BROWSER_TEST_P(PDFExtensionContentSettingJSTest, NoBeep) {}

IN_PROC_BROWSER_TEST_P(PDFExtensionContentSettingJSTest, BeepThenNoBeep) {}

IN_PROC_BROWSER_TEST_P(PDFExtensionContentSettingJSTest, NoBeepThenBeep) {}

IN_PROC_BROWSER_TEST_P(PDFExtensionContentSettingJSTest, BeepCsp) {}

IN_PROC_BROWSER_TEST_P(PDFExtensionContentSettingJSTest, DISABLED_NoBeepCsp) {}

class PDFExtensionWebUICodeCacheJSTest : public PDFExtensionJSTest {};

// Regression test for https://crbug.com/1239148.
IN_PROC_BROWSER_TEST_P(PDFExtensionWebUICodeCacheJSTest, Basic) {}

// Service worker tests are regression tests for
// https://crbug.com/916514.
class PDFExtensionServiceWorkerJSTest : public PDFExtensionJSTest {};

// Test navigating to a PDF in the scope of a service worker with no fetch event
// handler.
IN_PROC_BROWSER_TEST_P(PDFExtensionServiceWorkerJSTest, NoFetchHandler) {}

// Test navigating to a PDF when a service worker intercepts the request and
// then falls back to network by not calling FetchEvent.respondWith().
IN_PROC_BROWSER_TEST_P(PDFExtensionServiceWorkerJSTest, NetworkFallback) {}

// Test navigating to a PDF when a service worker intercepts the request and
// provides a response.
IN_PROC_BROWSER_TEST_P(PDFExtensionServiceWorkerJSTest, Interception) {}

#if BUILDFLAG(ENABLE_PDF_INK2)
#if !BUILDFLAG(IS_CHROMEOS_ASH)
// Test behavior when Ink2 and annotation mode are disabled for the PDF viewer.
// Don't run this test on Ash, as annotation mode is always enabled there.
IN_PROC_BROWSER_TEST_P(PDFExtensionJSTest, Ink2Disabled) {}
#endif  // !BUILDFLAG(IS_CHROMEOS_ASH)

class PDFExtensionJSInk2Test : public PDFExtensionJSTest {};

IN_PROC_BROWSER_TEST_P(PDFExtensionJSInk2Test, Ink2) {}

IN_PROC_BROWSER_TEST_P(PDFExtensionJSInk2Test, Ink2Save) {}

IN_PROC_BROWSER_TEST_P(PDFExtensionJSInk2Test, Ink2SidePanel) {}

IN_PROC_BROWSER_TEST_P(PDFExtensionJSInk2Test, Ink2ViewerToolbar) {}

class PDFExtensionJSInk2BeforeUnloadTest : public PDFExtensionJSTestBase {};

IN_PROC_BROWSER_TEST_F(PDFExtensionJSInk2BeforeUnloadTest, Stroke) {}

IN_PROC_BROWSER_TEST_F(PDFExtensionJSInk2BeforeUnloadTest, Undo) {}

#endif  // BUILDFLAG(ENABLE_PDF_INK2)

// TODO(crbug.com/40268279): Stop testing both modes after OOPIF PDF viewer
// launches.
INSTANTIATE_FEATURE_OVERRIDE_TEST_SUITE();
INSTANTIATE_FEATURE_OVERRIDE_TEST_SUITE();
INSTANTIATE_FEATURE_OVERRIDE_TEST_SUITE();
INSTANTIATE_FEATURE_OVERRIDE_TEST_SUITE();
INSTANTIATE_FEATURE_OVERRIDE_TEST_SUITE();
INSTANTIATE_FEATURE_OVERRIDE_TEST_SUITE();