chromium/chrome/browser/autofill/captured_sites_test_utils.cc

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

#include "chrome/browser/autofill/captured_sites_test_utils.h"

#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <utility>
#include <vector>

#include "base/base_switches.h"
#include "base/check_deref.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/json/json_reader.h"
#include "base/json/json_string_value_serializer.h"
#include "base/logging.h"
#include "base/path_service.h"
#include "base/process/launch.h"
#include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/single_thread_task_runner.h"
#include "base/task/thread_pool.h"
#include "base/threading/thread_restrictions.h"
#include "base/time/time.h"
#include "base/uuid.h"
#include "base/values.h"
#include "build/build_config.h"
#include "chrome/browser/autofill/autofill_uitest_util.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/test/base/interactive_test_utils.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/autofill/core/browser/autofill_test_utils.h"
#include "components/autofill/core/browser/autofill_type.h"
#include "components/autofill/core/browser/data_model/autofill_profile.h"
#include "components/autofill/core/browser/data_model/credit_card.h"
#include "components/autofill/core/browser/test_autofill_clock.h"
#include "components/autofill/core/common/autofill_clock.h"
#include "components/javascript_dialogs/app_modal_dialog_controller.h"
#include "components/javascript_dialogs/app_modal_dialog_view.h"
#include "components/permissions/permission_request_manager.h"
#include "components/variations/variations_switches.h"
#include "content/public/browser/browsing_data_remover.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/render_widget_host_view.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/browsing_data_remover_test_util.h"
#include "content/public/test/test_renderer_host.h"
#include "content/public/test/test_utils.h"
#include "ipc/ipc_channel_factory.h"
#include "ipc/ipc_logging.h"
#include "ipc/ipc_message_macros.h"
#include "ipc/ipc_sync_message.h"
#include "third_party/zlib/google/compression_utils.h"
#include "ui/events/keycodes/dom/dom_key.h"
#include "ui/events/keycodes/keyboard_code_conversion.h"
#include "ui/events/keycodes/keyboard_codes.h"

JSONParserOptions;
JSONReader;

namespace {

// The command-line flag to specify the command file flag.
const char kCommandFileFlag[] =;

// The command line flag to turn on verbose WPR logging.
const char kWebPageReplayVerboseFlag[] =;

// The maximum amount of time to wait for Chrome to finish autofilling a form.
const base::TimeDelta kAutofillActionWaitForVisualUpdateTimeout =;

// The number of tries the TestRecipeReplayer should perform when executing an
// Chrome Autofill action.
// Chrome Autofill can be flaky on some real-world pages. The Captured Site
// Automation Framework will retry an autofill action a couple times before
// concluding that Chrome Autofill does not work.
const int kAutofillActionNumRetries =;

// The public key hash for the certificate Web Page Replay (WPR) uses to serve
// HTTPS content.
// The Captured Sites Test Framework relies on WPR to serve captured site
// traffic. If a machine does not have the WPR certificate installed, Chrome
// will detect a server certificate validation failure when WPR serves Chrome
// HTTPS content. In response Chrome will block the WPR HTTPS content.
// The test framework avoids this problem by launching Chrome with the
// ignore-certificate-errors-spki-list flag set to the WPR certificate's
// public key hash. Doing so tells Chrome to ignore server certificate
// validation errors from WPR.
const char kWebPageReplayCertSPKI[] =;

const char kClockNotSetMessage[] =;

// Check and return that the caller wants verbose WPR output (off by default).
bool IsVerboseWprLoggingEnabled() {}

void PrintDebugInstructions(const base::FilePath& command_file_path) {}

std::optional<autofill::FieldType> StringToFieldType(std::string_view str) {}

// Command types to control and debug execution.
// * The |kAbsoluteLimit| and |kRelativeLimit| commands indicate that
//   execution shall not proceed if the next action's position is >= |param|
//   or >= current_index + |param|, respectively.
// * The |kSkipAction| command jumps |param| actions forward or backward.
// * The |kShowAction| command prints the |param| previous (if < 0) or
//   upcoming (if > 0) actions.
// * The |kWhereAmI| command prints the current execution position.
enum class ExecutionCommandType {};

struct ExecutionCommand {};

// Blockingly reads the content of |command_file_path|, parses it into
// ExecutionCommands, and returns the result.
std::vector<ExecutionCommand> ReadExecutionCommands(
    const base::FilePath& command_file_path) {}

struct ExecutionState {};

// Blockingly reads the commands from |command_file_path| and executes them.
// Execution primarily means manipulation of the |execution_state|, particularly
// `execution_state.limit`.
ExecutionState ProcessCommands(ExecutionState execution_state,
                               const base::Value::List* action_list,
                               const base::FilePath& command_file_path) {}

struct AllowNull {};

std::optional<std::string> FindPopulateString(
    const base::Value::Dict& container,
    std::string_view key_name,
    absl::variant<std::string_view, AllowNull> key_descriptor) {}

std::optional<std::vector<std::string>> FindPopulateStringVector(
    const base::Value::Dict& container,
    std::string_view key_name,
    absl::variant<std::string_view, AllowNull> key_descriptor) {}

}  // namespace

namespace captured_sites_test_utils {

CapturedSiteParams::CapturedSiteParams() = default;
CapturedSiteParams::~CapturedSiteParams() = default;
CapturedSiteParams::CapturedSiteParams(const CapturedSiteParams& other) =
    default;

std::ostream& operator<<(std::ostream& os, const CapturedSiteParams& param) {}

// Iterate through Autofill's Web Page Replay capture file directory to look
// for captures sites and automation recipe files. Return a list of sites for
// which recipe-based testing is available.
std::vector<CapturedSiteParams> GetCapturedSites(
    const base::FilePath& replay_files_dir_path) {}

std::string FilePathToUTF8(const base::FilePath::StringType& str) {}

std::optional<base::FilePath> GetCommandFilePath() {}

void PrintInstructions(const char* test_file_name) {}

// FrameObserver --------------------------------------------------------------
IFrameWaiter::IFrameWaiter(content::WebContents* web_contents)
    :{}

IFrameWaiter::~IFrameWaiter() {}

content::RenderFrameHost* IFrameWaiter::WaitForFrameMatchingName(
    const std::string& name,
    const base::TimeDelta timeout) {}

content::RenderFrameHost* IFrameWaiter::WaitForFrameMatchingOrigin(
    const GURL origin,
    const base::TimeDelta timeout) {}

content::RenderFrameHost* IFrameWaiter::WaitForFrameMatchingUrl(
    const GURL url,
    const base::TimeDelta timeout) {}

void IFrameWaiter::RenderFrameCreated(
    content::RenderFrameHost* render_frame_host) {}

void IFrameWaiter::DidFinishLoad(content::RenderFrameHost* render_frame_host,
                                 const GURL& validated_url) {}

void IFrameWaiter::FrameNameChanged(content::RenderFrameHost* render_frame_host,
                                    const std::string& name) {}

bool IFrameWaiter::FrameHasOrigin(const GURL& origin,
                                  content::RenderFrameHost* frame) {}

// WebPageReplayServerWrapper -------------------------------------------------
WebPageReplayServerWrapper::WebPageReplayServerWrapper(
    const bool start_as_replay,
    int host_http_port,
    int host_https_port)
    :{}

WebPageReplayServerWrapper::~WebPageReplayServerWrapper() = default;

bool WebPageReplayServerWrapper::Start(
    const base::FilePath& capture_file_path) {}

bool WebPageReplayServerWrapper::Stop() {}

bool WebPageReplayServerWrapper::RunWebPageReplayCmdAndWaitForExit(
    const std::vector<std::string>& args,
    const base::TimeDelta& timeout) {}

bool WebPageReplayServerWrapper::RunWebPageReplayCmd(
    const std::vector<std::string>& args) {}

// ProfileDataController ------------------------------------------------------
ProfileDataController::ProfileDataController()
    :{}

ProfileDataController::~ProfileDataController() = default;

bool ProfileDataController::AddAutofillProfileInfo(
    const std::string& field_type,
    const std::string& field_value) {}

// TestRecipeReplayer ---------------------------------------------------------
TestRecipeReplayer::TestRecipeReplayer(
    Browser* browser,
    TestRecipeReplayChromeFeatureActionExecutor* feature_action_executor)
    :{}

TestRecipeReplayer::~TestRecipeReplayer() {}

bool TestRecipeReplayer::ReplayTest(
    const base::FilePath& capture_file_path,
    const base::FilePath& recipe_file_path,
    const std::optional<base::FilePath>& command_file_path) {}

const std::vector<testing::AssertionResult>
TestRecipeReplayer::GetValidationFailures() const {}

// Extracts the time of the wpr recording from the wpr archive file and
// overrides the autofill::AutofillClock to match that time.
bool TestRecipeReplayer::OverrideAutofillClock(
    const base::FilePath capture_file_path) {}

// static
void TestRecipeReplayer::SetUpHostResolverRules(
    base::CommandLine* command_line) {}

// static
void TestRecipeReplayer::SetUpCommandLine(base::CommandLine* command_line) {}

TestRecipeReplayChromeFeatureActionExecutor*
TestRecipeReplayer::feature_action_executor() {}

Browser* TestRecipeReplayer::browser() {}

WebPageReplayServerWrapper*
TestRecipeReplayer::web_page_replay_server_wrapper() {}

content::WebContents* TestRecipeReplayer::GetWebContents() {}

void TestRecipeReplayer::WaitTillPageIsIdle(
    base::TimeDelta continuous_paint_timeout) {}

bool TestRecipeReplayer::WaitForVisualUpdate(base::TimeDelta timeout) {}

void TestRecipeReplayer::CleanupSiteData() {}

bool TestRecipeReplayer::ReplayRecordedActions(
    const base::FilePath& recipe_file_path,
    const std::optional<base::FilePath>& command_file_path) {}

// Functions for deserializing and executing actions from the test recipe
// JSON object.
bool TestRecipeReplayer::InitializeBrowserToExecuteRecipe(
    base::Value::Dict& recipe) {}

bool TestRecipeReplayer::ExecuteAutofillAction(base::Value::Dict action) {}

bool TestRecipeReplayer::ExecuteClickAction(base::Value::Dict action) {}

bool TestRecipeReplayer::ExecuteClickIfNotSeenAction(base::Value::Dict action) {}

bool TestRecipeReplayer::ExecuteCloseTabAction(base::Value::Dict action) {}

bool TestRecipeReplayer::ExecuteCoolOffAction(base::Value::Dict action) {}

bool TestRecipeReplayer::ExecuteHoverAction(base::Value::Dict action) {}

bool TestRecipeReplayer::ExecuteForceLoadPage(base::Value::Dict action) {}

bool TestRecipeReplayer::ExecutePressEnterAction(base::Value::Dict action) {}

bool TestRecipeReplayer::ExecutePressEscapeAction(base::Value::Dict action) {}

bool TestRecipeReplayer::ExecutePressSpaceAction(base::Value::Dict action) {}

bool TestRecipeReplayer::ExecuteRunCommandAction(base::Value::Dict action) {}

bool TestRecipeReplayer::ExecuteSavePasswordAction(base::Value::Dict action) {}

bool TestRecipeReplayer::ExecuteSelectDropdownAction(base::Value::Dict action) {}

bool TestRecipeReplayer::ExecuteTypeAction(base::Value::Dict action) {}

bool TestRecipeReplayer::ExecuteTypePasswordAction(base::Value::Dict action) {}

bool TestRecipeReplayer::ExecuteUpdatePasswordAction(base::Value::Dict action) {}

bool TestRecipeReplayer::ExecuteValidateFieldValueAction(
    base::Value::Dict action) {}

bool TestRecipeReplayer::ExecuteValidateNoSavePasswordPromptAction(
    base::Value::Dict action) {}

bool TestRecipeReplayer::ExecuteValidatePasswordGenerationPromptAction(
    base::Value::Dict action) {}

void TestRecipeReplayer::ValidatePasswordGenerationPromptState(
    const content::ToRenderFrameHost& frame,
    const std::string& element_xpath,
    bool expect_to_be_shown) {}

bool TestRecipeReplayer::ExecuteValidateSaveFallbackAction(
    base::Value::Dict action) {}

bool TestRecipeReplayer::ExecuteWaitForStateAction(base::Value::Dict action) {}

bool TestRecipeReplayer::GetTargetHTMLElementXpathFromAction(
    const base::Value::Dict& action,
    std::string* xpath) {}

bool TestRecipeReplayer::GetTargetHTMLElementVisibilityEnumFromAction(
    const base::Value::Dict& action,
    int* visibility_enum_val) {}

bool TestRecipeReplayer::GetTargetFrameFromAction(
    const base::Value::Dict& action,
    content::RenderFrameHost** frame) {}

bool TestRecipeReplayer::ExtractFrameAndVerifyElement(
    const base::Value::Dict& action,
    std::string* xpath,
    content::RenderFrameHost** frame,
    bool set_focus,
    bool relaxed_visibility,
    bool ignore_failure) {}

bool TestRecipeReplayer::GetIFramePathFromAction(
    const base::Value::Dict& action,
    std::vector<std::string>* iframe_path) {}

bool TestRecipeReplayer::GetIFrameOffsetFromIFramePath(
    const std::vector<std::string>& iframe_path,
    content::RenderFrameHost* frame,
    gfx::Vector2d* offset) {}

bool TestRecipeReplayer::WaitForElementToBeReady(
    const std::string& xpath,
    const int visibility_enum_val,
    content::RenderFrameHost* frame,
    bool ignore_failure) {}

bool TestRecipeReplayer::WaitForStateChange(
    content::RenderFrameHost* frame,
    const std::vector<std::string>& state_assertions,
    const base::TimeDelta& timeout,
    bool ignore_failure) {}

bool TestRecipeReplayer::AllAssertionsPassed(
    const content::ToRenderFrameHost& frame,
    const std::vector<std::string>& assertions) {}

bool TestRecipeReplayer::ExecuteJavaScriptOnElementByXpath(
    const content::ToRenderFrameHost& frame,
    const std::string& element_xpath,
    const std::string& execute_function_body,
    const base::TimeDelta& time_to_wait_for_element) {}

bool TestRecipeReplayer::GetElementProperty(
    const content::ToRenderFrameHost& frame,
    const std::string& element_xpath,
    const std::string& get_property_function_body,
    std::string* property) {}

bool TestRecipeReplayer::ExpectElementPropertyEqualsAnyOf(
    const content::ToRenderFrameHost& frame,
    const std::string& element_xpath,
    const std::string& get_property_function_body,
    const std::vector<std::string>& expected_values,
    const std::string& validation_field,
    IgnoreCase ignore_case) {}

bool TestRecipeReplayer::ScrollElementIntoView(
    const std::string& element_xpath,
    content::RenderFrameHost* frame) {}

bool TestRecipeReplayer::PlaceFocusOnElement(
    const std::string& element_xpath,
    const std::vector<std::string> iframe_path,
    content::RenderFrameHost* frame) {}

bool TestRecipeReplayer::GetBoundingRectOfTargetElement(
    const std::string& target_element_xpath,
    content::RenderFrameHost* frame,
    gfx::Rect* output_rect) {}

bool TestRecipeReplayer::GetBoundingRectOfTargetElement(
    const std::string& target_element_xpath,
    const std::vector<std::string> iframe_path,
    content::RenderFrameHost* frame,
    gfx::Rect* output_rect) {}

bool TestRecipeReplayer::SimulateLeftMouseClickAt(
    const gfx::Point& point,
    content::RenderFrameHost* render_frame_host) {}

bool TestRecipeReplayer::SimulateMouseHoverAt(
    content::RenderFrameHost* render_frame_host,
    const gfx::Point& point) {}

void TestRecipeReplayer::SimulateKeyPressWrapper(
    content::WebContents* web_contents,
    ui::DomKey key) {}

bool TestRecipeReplayer::HasChromeStoredCredential(
    const base::Value::Dict& action,
    bool* stored_cred) {}

bool TestRecipeReplayer::SetupSavedAutofillProfile(
    base::Value::List saved_autofill_profile_container) {}

bool TestRecipeReplayer::SetupSavedPasswords(
    base::Value::List saved_password_list_container) {}

// TestRecipeReplayChromeFeatureActionExecutor --------------------------------
TestRecipeReplayChromeFeatureActionExecutor::
    TestRecipeReplayChromeFeatureActionExecutor() {}
TestRecipeReplayChromeFeatureActionExecutor::
    ~TestRecipeReplayChromeFeatureActionExecutor() {}

bool TestRecipeReplayChromeFeatureActionExecutor::AutofillForm(
    const std::string& focus_element_css_selector,
    const std::vector<std::string>& iframe_path,
    const int attempts,
    content::RenderFrameHost* frame,
    std::optional<autofill::FieldType> triggered_field_type) {}

bool TestRecipeReplayChromeFeatureActionExecutor::AddAutofillProfileInfo(
    const std::string& field_type,
    const std::string& field_value) {}

bool TestRecipeReplayChromeFeatureActionExecutor::SetupAutofillProfile() {}

bool TestRecipeReplayChromeFeatureActionExecutor::AddCredential(
    const std::string& origin,
    const std::string& username,
    const std::string& password) {}

bool TestRecipeReplayChromeFeatureActionExecutor::SavePassword() {}

bool TestRecipeReplayChromeFeatureActionExecutor::UpdatePassword() {}

bool TestRecipeReplayChromeFeatureActionExecutor::WaitForSaveFallback() {}

bool TestRecipeReplayChromeFeatureActionExecutor::
    IsChromeShowingPasswordGenerationPrompt() {}

bool TestRecipeReplayChromeFeatureActionExecutor::
    HasChromeShownSavePasswordPrompt() {}

bool TestRecipeReplayChromeFeatureActionExecutor::HasChromeStoredCredential(
    const std::string& origin,
    const std::string& username,
    const std::string& password) {}

}  // namespace captured_sites_test_utils