1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ios/chrome/browser/net/metrics_network_client.h"
7 #include "base/mac/scoped_nsobject.h"
8 #include "base/test/histogram_tester.h"
9 #include "net/base/net_errors.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 // Dummy client to be registered as underlying client for the
13 // MetricsNetworkClient.
14 @interface MetricsMockClient : CRNForwardingNetworkClient
17 @implementation MetricsMockClient
19 - (void)didFailWithNSErrorCode:(NSInteger)nsErrorCode
20 netErrorCode:(int)netErrorCode {
23 - (void)didFinishLoading {
30 // Name for the histogram the MetricsNetworkClient has to update.
31 const char kHistogramName[] = "Net.ErrorCodesForMainFrame3";
33 class MetricsNetworkClientTest : public testing::Test {
35 MetricsNetworkClientTest()
36 : histogram_tester_(), client_([[MetricsNetworkClient alloc] init]) {
37 // Setup a dummy underlying client to avoid DCHECKs.
38 base::scoped_nsobject<MetricsMockClient> underying_client(
39 [[MetricsMockClient alloc] init]);
40 [client_ setUnderlyingClient:underying_client];
43 // Returns true if there are no samples for "Net.ErrorCodesForMainFrame3".
44 void VerifyNoSamples() {
45 histogram_tester_.ExpectTotalCount(kHistogramName, 0);
49 base::HistogramTester histogram_tester_;
50 base::scoped_nsobject<MetricsNetworkClient> client_;
55 TEST_F(MetricsNetworkClientTest, HistogramUpdatedOnErrors) {
56 int net_error = net::ERR_FAILED;
58 // NSURLErrorCancelled errors must not update the histogram.
59 [client_ didFailWithNSErrorCode:NSURLErrorCancelled netErrorCode:net_error];
61 // Other iOS errors update the histogram.
62 [client_ didFailWithNSErrorCode:NSURLErrorCannotConnectToHost
63 netErrorCode:net_error];
64 // |net_error| is negative, the histogram reports the opposite value.
65 histogram_tester_.ExpectUniqueSample(kHistogramName, -net_error, 1);
68 TEST_F(MetricsNetworkClientTest, HistogramUpdatedOnSuccess) {
70 [client_ didFinishLoading];
71 histogram_tester_.ExpectUniqueSample(kHistogramName, -net::OK, 1);