Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(410)

Unified Diff: components/domain_reliability/context_unittest.cc

Issue 1511243003: Domain Reliability: Make context upload test cases less brittle (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Make some requested changes Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: components/domain_reliability/context_unittest.cc
diff --git a/components/domain_reliability/context_unittest.cc b/components/domain_reliability/context_unittest.cc
index 5b42d2187b50a1849d398f5c9bf890df4d07e5b9..fd2ab1cb60203593acf33c35866970d28ceb8381 100644
--- a/components/domain_reliability/context_unittest.cc
+++ b/components/domain_reliability/context_unittest.cc
@@ -8,6 +8,7 @@
#include <string>
#include "base/bind.h"
+#include "base/json/json_reader.h"
#include "base/memory/scoped_ptr.h"
#include "components/domain_reliability/beacon.h"
#include "components/domain_reliability/dispatcher.h"
@@ -21,6 +22,10 @@
namespace domain_reliability {
namespace {
+using base::DictionaryValue;
+using base::ListValue;
+using base::Value;
+
typedef std::vector<const DomainReliabilityBeacon*> BeaconVector;
scoped_ptr<DomainReliabilityBeacon> MakeBeacon(MockableTime* time) {
@@ -39,6 +44,38 @@ scoped_ptr<DomainReliabilityBeacon> MakeBeacon(MockableTime* time) {
return beacon.Pass();
}
+template <typename ValueType,
+ bool (DictionaryValue::* GetValueType)(const std::string&,
+ ValueType*) const>
+struct HasValue {
+ bool operator()(const DictionaryValue& dict,
+ const std::string& key,
+ ValueType expected_value) {
+ ValueType actual_value;
+ bool got_value = (dict.*GetValueType)(key, &actual_value);
+ EXPECT_TRUE(got_value);
+ if (got_value)
+ EXPECT_EQ(expected_value, actual_value);
+ return got_value && (expected_value == actual_value);
+ }
+};
+
+HasValue<std::string, &DictionaryValue::GetString> HasStringValue;
+HasValue<int, &DictionaryValue::GetInteger> HasIntegerValue;
+HasValue<bool, &DictionaryValue::GetBoolean> HasBooleanValue;
+
+bool GetEntryFromReport(const Value* report,
+ size_t index,
+ const DictionaryValue** entry_out) {
+ const DictionaryValue* report_dict;
+ const ListValue* entries;
+
+ return report &&
+ report->GetAsDictionary(&report_dict) &&
+ report_dict->GetList("entries", &entries) &&
+ entries->GetDictionary(index, entry_out);
+}
+
class DomainReliabilityContextTest : public testing::Test {
protected:
DomainReliabilityContextTest()
@@ -208,23 +245,27 @@ TEST_F(DomainReliabilityContextTest, ReportUpload) {
context_.GetQueuedBeaconsForTesting(&beacons);
EXPECT_EQ(1u, beacons.size());
- // N.B.: Assumes max_delay is 5 minutes.
- const char* kExpectedReport = "{"
- "\"entries\":["
- "{\"failure_data\":{\"custom_error\":\"net::ERR_CONNECTION_RESET\"},"
- "\"network_changed\":false,\"protocol\":\"HTTP\","
- "\"quic_broken\":true,\"request_age_ms\":300250,"
- "\"request_elapsed_ms\":250,"
- "\"server_ip\":\"127.0.0.1\",\"status\":\"tcp.connection_reset\","
- "\"url\":\"https://localhost/\","
- "\"was_proxied\":false}],\"reporter\":\"test-reporter\"}";
-
time_.Advance(max_delay());
EXPECT_TRUE(upload_pending());
- EXPECT_EQ(kExpectedReport, upload_report());
EXPECT_EQ(0, upload_max_depth());
EXPECT_EQ(GURL("https://exampleuploader/upload"), upload_url());
+ scoped_ptr<Value> value = base::JSONReader::Read(upload_report());
+ const DictionaryValue* entry;
+ ASSERT_TRUE(GetEntryFromReport(value.get(), 0, &entry));
+ EXPECT_TRUE(HasStringValue(*entry, "failure_data.custom_error",
+ "net::ERR_CONNECTION_RESET"));
+ EXPECT_TRUE(HasBooleanValue(*entry, "network_changed", false));
+ EXPECT_TRUE(HasStringValue(*entry, "protocol", "HTTP"));
+ EXPECT_TRUE(HasBooleanValue(*entry, "quic_broken", true));
+ // N.B.: Assumes max_delay is 5 minutes.
+ EXPECT_TRUE(HasIntegerValue(*entry, "request_age_ms", 300250));
+ EXPECT_TRUE(HasIntegerValue(*entry, "request_elapsed_ms", 250));
+ EXPECT_TRUE(HasStringValue(*entry, "server_ip", "127.0.0.1"));
+ EXPECT_TRUE(HasStringValue(*entry, "status", "tcp.connection_reset"));
+ EXPECT_TRUE(HasStringValue(*entry, "url", "https://localhost/"));
+ EXPECT_TRUE(HasBooleanValue(*entry, "was_proxied", false));
+
DomainReliabilityUploader::UploadResult result;
result.status = DomainReliabilityUploader::UploadResult::SUCCESS;
CallUploadCallback(result);
@@ -232,32 +273,25 @@ TEST_F(DomainReliabilityContextTest, ReportUpload) {
EXPECT_TRUE(CheckNoBeacons());
}
-TEST_F(DomainReliabilityContextTest, Upload_NetworkChanged) {
+TEST_F(DomainReliabilityContextTest, NetworkChanged) {
context_.OnBeacon(MakeBeacon(&time_));
BeaconVector beacons;
context_.GetQueuedBeaconsForTesting(&beacons);
EXPECT_EQ(1u, beacons.size());
- // N.B.: Assumes max_delay is 5 minutes.
- const char* kExpectedReport = "{"
- "\"entries\":["
- "{\"failure_data\":{\"custom_error\":\"net::ERR_CONNECTION_RESET\"},"
- "\"network_changed\":true,\"protocol\":\"HTTP\","
- "\"quic_broken\":true,\"request_age_ms\":300250,"
- "\"request_elapsed_ms\":250,"
- "\"server_ip\":\"127.0.0.1\",\"status\":\"tcp.connection_reset\","
- "\"url\":\"https://localhost/\","
- "\"was_proxied\":false}],\"reporter\":\"test-reporter\"}";
-
// Simulate a network change after the request but before the upload.
last_network_change_time_ = time_.NowTicks();
time_.Advance(max_delay());
EXPECT_TRUE(upload_pending());
- EXPECT_EQ(kExpectedReport, upload_report());
EXPECT_EQ(0, upload_max_depth());
EXPECT_EQ(GURL("https://exampleuploader/upload"), upload_url());
+ scoped_ptr<Value> value = base::JSONReader::Read(upload_report());
+ const DictionaryValue* entry;
+ ASSERT_TRUE(GetEntryFromReport(value.get(), 0, &entry));
+ EXPECT_TRUE(HasBooleanValue(*entry, "network_changed", true));
+
DomainReliabilityUploader::UploadResult result;
result.status = DomainReliabilityUploader::UploadResult::SUCCESS;
CallUploadCallback(result);
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698