#ifndef NET_NQE_NETWORK_QUALITY_ESTIMATOR_TEST_UTIL_H_
#define NET_NQE_NETWORK_QUALITY_ESTIMATOR_TEST_UTIL_H_
#include <stdint.h>
#include <map>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <vector>
#include "base/files/file_path.h"
#include "base/time/time.h"
#include "net/base/network_change_notifier.h"
#include "net/log/net_log.h"
#include "net/log/test_net_log.h"
#include "net/nqe/effective_connection_type.h"
#include "net/nqe/network_quality_estimator.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
namespace net {
class TestNetworkQualityEstimator : public NetworkQualityEstimator {
public:
TestNetworkQualityEstimator();
explicit TestNetworkQualityEstimator(
const std::map<std::string, std::string>& variation_params);
TestNetworkQualityEstimator(
const std::map<std::string, std::string>& variation_params,
bool allow_local_host_requests_for_tests,
bool allow_smaller_responses_for_tests);
TestNetworkQualityEstimator(
const std::map<std::string, std::string>& variation_params,
bool allow_local_host_requests_for_tests,
bool allow_smaller_responses_for_tests,
bool suppress_notifications_for_testing);
explicit TestNetworkQualityEstimator(
std::unique_ptr<NetworkQualityEstimatorParams> params);
TestNetworkQualityEstimator(const TestNetworkQualityEstimator&) = delete;
TestNetworkQualityEstimator& operator=(const TestNetworkQualityEstimator&) =
delete;
~TestNetworkQualityEstimator() override;
void RunOneRequest();
void SimulateNetworkChange(
NetworkChangeNotifier::ConnectionType new_connection_type,
const std::string& network_id);
const GURL GetEchoURL();
const GURL GetRedirectURL();
void set_effective_connection_type(EffectiveConnectionType type) {
effective_connection_type_ = type;
}
EffectiveConnectionType GetEffectiveConnectionType() const override;
void set_recent_effective_connection_type(EffectiveConnectionType type) {
DCHECK(!start_time_null_http_rtt_ && !recent_http_rtt_ &&
!start_time_null_transport_rtt_ && !recent_transport_rtt_ &&
!start_time_null_downlink_throughput_kbps_ &&
!recent_downlink_throughput_kbps_);
recent_effective_connection_type_ = type;
}
EffectiveConnectionType GetRecentEffectiveConnectionTypeUsingMetrics(
base::TimeDelta* http_rtt,
base::TimeDelta* transport_rtt,
base::TimeDelta* end_to_end_rtt,
int32_t* downstream_throughput_kbps,
size_t* observations_count,
size_t* end_to_end_rtt_observation_count) const override;
void NotifyObserversOfRTTOrThroughputComputed() const override;
void NotifyRTTAndThroughputEstimatesObserverIfPresent(
RTTAndThroughputEstimatesObserver* observer) const override;
void SetStartTimeNullHttpRtt(const base::TimeDelta http_rtt);
void set_recent_http_rtt(const base::TimeDelta& recent_http_rtt) {
DCHECK(!effective_connection_type_ && !recent_effective_connection_type_);
recent_http_rtt_ = recent_http_rtt;
}
bool GetRecentRTT(nqe::internal::ObservationCategory observation_category,
const base::TimeTicks& start_time,
base::TimeDelta* rtt,
size_t* observations_count) const override;
void SetStartTimeNullTransportRtt(const base::TimeDelta transport_rtt);
void set_recent_transport_rtt(const base::TimeDelta& recent_transport_rtt) {
DCHECK(!effective_connection_type_ && !recent_effective_connection_type_);
recent_transport_rtt_ = recent_transport_rtt;
}
std::optional<base::TimeDelta> GetTransportRTT() const override;
void set_start_time_null_downlink_throughput_kbps(
int32_t downlink_throughput_kbps) {
start_time_null_downlink_throughput_kbps_ = downlink_throughput_kbps;
}
void set_recent_downlink_throughput_kbps(
int32_t recent_downlink_throughput_kbps) {
DCHECK(!effective_connection_type_ && !recent_effective_connection_type_);
recent_downlink_throughput_kbps_ = recent_downlink_throughput_kbps;
}
bool GetRecentDownlinkThroughputKbps(const base::TimeTicks& start_time,
int32_t* kbps) const override;
base::TimeDelta GetRTTEstimateInternal(
base::TimeTicks start_time,
nqe::internal::ObservationCategory observation_category,
int percentile,
size_t* observations_count) const override;
void set_rtt_estimate_internal(base::TimeDelta value) {
rtt_estimate_internal_ = value;
}
void set_start_time_null_end_to_end_rtt(const base::TimeDelta rtt) {
DCHECK(!effective_connection_type_ && !recent_effective_connection_type_);
start_time_null_end_to_end_rtt_ = rtt;
}
void set_start_time_null_end_to_end_rtt_observation_count(size_t count) {
end_to_end_rtt_observation_count_at_last_ect_computation_ = count;
}
int GetEntriesCount(NetLogEventType type) const;
std::string GetNetLogLastStringValue(NetLogEventType type,
const std::string& key) const;
int GetNetLogLastIntegerValue(NetLogEventType type,
const std::string& key) const;
void NotifyObserversOfRTTOrThroughputEstimatesComputed(
const net::nqe::internal::NetworkQuality& network_quality);
void SetAndNotifyObserversOfEffectiveConnectionType(
EffectiveConnectionType type);
void SetAndNotifyObserversOfP2PActiveConnectionsCountChange(uint32_t count);
void SetTransportRTTAtastECTSampleCount(size_t count) {
transport_rtt_observation_count_last_ect_computation_ = count;
}
size_t ping_rtt_received_count() const { return ping_rtt_received_count_; }
const NetworkQualityEstimatorParams* params() const;
void RecordSpdyPingLatency(const HostPortPair& host_port_pair,
base::TimeDelta rtt) override;
using NetworkQualityEstimator::SetTickClockForTesting;
using NetworkQualityEstimator::OnConnectionTypeChanged;
using NetworkQualityEstimator::OnUpdatedTransportRTTAvailable;
using NetworkQualityEstimator::AddAndNotifyObserversOfRTT;
using NetworkQualityEstimator::AddAndNotifyObserversOfThroughput;
using NetworkQualityEstimator::IsHangingRequest;
private:
class LocalHttpTestServer : public EmbeddedTestServer {
public:
explicit LocalHttpTestServer(const base::FilePath& document_root);
};
nqe::internal::NetworkID GetCurrentNetworkID() const override;
std::optional<net::EffectiveConnectionType> GetOverrideECT() const override;
net::RecordingNetLogObserver net_log_observer_;
std::optional<EffectiveConnectionType> effective_connection_type_;
std::optional<EffectiveConnectionType> recent_effective_connection_type_;
NetworkChangeNotifier::ConnectionType current_network_type_ =
NetworkChangeNotifier::CONNECTION_UNKNOWN;
std::string current_network_id_;
std::optional<base::TimeDelta> start_time_null_http_rtt_;
std::optional<base::TimeDelta> recent_http_rtt_;
std::optional<base::TimeDelta> start_time_null_transport_rtt_;
std::optional<base::TimeDelta> recent_transport_rtt_;
std::optional<int32_t> start_time_null_downlink_throughput_kbps_;
std::optional<int32_t> recent_downlink_throughput_kbps_;
std::optional<base::TimeDelta> rtt_estimate_internal_;
std::optional<base::TimeDelta> start_time_null_end_to_end_rtt_;
const bool suppress_notifications_for_testing_;
size_t ping_rtt_received_count_ = 0;
std::optional<size_t> transport_rtt_observation_count_last_ect_computation_;
LocalHttpTestServer embedded_test_server_;
};
}
#endif