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
|
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_TEST_BASE_DEVTOOLS_LISTENER_H_
#define CHROME_TEST_BASE_DEVTOOLS_LISTENER_H_
#include <map>
#include <string>
#include <vector>
#include "base/memory/weak_ptr.h"
#include "base/values.h"
#include "content/public/browser/devtools_agent_host.h"
#include "content/public/browser/devtools_agent_host_client.h"
namespace coverage {
// Collects code coverage from a WebContents, during a browser test
// for example, using Chrome Devtools Protocol (CDP).
class DevToolsListener : public content::DevToolsAgentHostClient {
public:
// Attaches to host and enables CDP.
DevToolsListener(content::DevToolsAgentHost* host, uint32_t uuid);
~DevToolsListener() override;
// Host navigation starts code coverage.
void Navigated(content::DevToolsAgentHost* host);
// Returns true if host has started code coverage.
bool HasCoverage(content::DevToolsAgentHost* host);
// If host HasCoverage(), collect it and save it in |store|.
void GetCoverage(content::DevToolsAgentHost* host,
const base::FilePath& store,
const std::string& test);
// Detaches from host.
void Detach(content::DevToolsAgentHost* host);
// Returns a unique host identifier, with optional |prefix|.
static std::string HostString(content::DevToolsAgentHost* host,
const std::string& prefix = {});
// Creates coverage output directory and subdirectories.
static void SetupCoverageStore(const base::FilePath& store_path);
private:
// Starts CDP session on host.
void Start(content::DevToolsAgentHost* host);
// Starts JavaScript (JS) code coverage on host.
bool StartJSCoverage(content::DevToolsAgentHost* host);
// Collects JavaScript coverage from host and saves it in |store|.
void StopAndStoreJSCoverage(content::DevToolsAgentHost* host,
const base::FilePath& store,
const std::string& test);
// Stores JS scripts used during code execution on host.
void StoreScripts(content::DevToolsAgentHost* host,
const base::FilePath& store);
// Sends CDP commands to host.
void SendCommandMessage(content::DevToolsAgentHost* host,
const std::string& command);
// Awaits CDP response to command `id` and returns false if the host was
// closed whilst waiting, true otherwise.
bool AwaitCommandResponse(int id);
// Receives CDP messages from host.
void DispatchProtocolMessage(content::DevToolsAgentHost* host,
base::span<const uint8_t> message) override;
// Returns true if URL should be attached to.
bool MayAttachToURL(const GURL& url, bool is_webui) override;
// Called if host was shut down (closed).
void AgentHostClosed(content::DevToolsAgentHost* host) override;
// Repeatedly verify all the script IDs from the coverage entries are
// available and call `finished_callback` on completion (either retries
// exhausted or all scripts are available).
void VerifyAllScriptsAreParsedRepeatedly(
const base::Value::List* coverage_entries,
base::OnceClosure done_callback,
int retries);
std::vector<base::Value::Dict> scripts_;
base::Value::Dict script_coverage_;
std::map<std::string, std::string> script_hash_map_;
std::map<std::string, std::string> script_id_map_;
base::OnceClosure value_closure_;
base::Value::Dict value_;
int value_id_ = 0;
const std::string uuid_;
bool navigated_ = false;
bool attached_ = true;
bool all_scripts_parsed_ = false;
base::WeakPtrFactory<DevToolsListener> weak_ptr_factory_{this};
};
} // namespace coverage
#endif // CHROME_TEST_BASE_DEVTOOLS_LISTENER_H_
|