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
|
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/android/traffic_stats.h"
#include "base/run_loop.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "net/url_request/url_request_test_util.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
namespace net {
namespace {
TEST(TrafficStatsAndroidTest, BasicsTest) {
EmbeddedTestServer embedded_test_server;
embedded_test_server.ServeFilesFromDirectory(
base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
ASSERT_TRUE(embedded_test_server.Start());
int64_t tx_bytes_before_request = -1;
int64_t rx_bytes_before_request = -1;
EXPECT_TRUE(
android::traffic_stats::GetTotalTxBytes(&tx_bytes_before_request));
EXPECT_GE(tx_bytes_before_request, 0);
EXPECT_TRUE(
android::traffic_stats::GetTotalRxBytes(&rx_bytes_before_request));
EXPECT_GE(rx_bytes_before_request, 0);
TestDelegate test_delegate;
TestURLRequestContext context(false);
scoped_ptr<URLRequest> request(
context.CreateRequest(embedded_test_server.GetURL("/echo.html"),
DEFAULT_PRIORITY, &test_delegate));
request->Start();
base::RunLoop().Run();
// Bytes should increase because of the network traffic.
int64_t tx_bytes_after_request = -1;
int64_t rx_bytes_after_request = -1;
EXPECT_TRUE(android::traffic_stats::GetTotalTxBytes(&tx_bytes_after_request));
EXPECT_GT(tx_bytes_after_request, tx_bytes_before_request);
EXPECT_TRUE(android::traffic_stats::GetTotalRxBytes(&rx_bytes_after_request));
EXPECT_GT(rx_bytes_after_request, rx_bytes_before_request);
}
TEST(TrafficStatsAndroidTest, UIDBasicsTest) {
EmbeddedTestServer embedded_test_server;
embedded_test_server.ServeFilesFromDirectory(
base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
ASSERT_TRUE(embedded_test_server.Start());
int64_t tx_bytes_before_request = -1;
int64_t rx_bytes_before_request = -1;
EXPECT_TRUE(
android::traffic_stats::GetCurrentUidTxBytes(&tx_bytes_before_request));
EXPECT_GE(tx_bytes_before_request, 0);
EXPECT_TRUE(
android::traffic_stats::GetCurrentUidRxBytes(&rx_bytes_before_request));
EXPECT_GE(rx_bytes_before_request, 0);
TestDelegate test_delegate;
TestURLRequestContext context(false);
scoped_ptr<URLRequest> request(
context.CreateRequest(embedded_test_server.GetURL("/echo.html"),
DEFAULT_PRIORITY, &test_delegate));
request->Start();
base::RunLoop().Run();
// Bytes should increase because of the network traffic.
int64_t tx_bytes_after_request = -1;
int64_t rx_bytes_after_request = -1;
EXPECT_TRUE(
android::traffic_stats::GetCurrentUidTxBytes(&tx_bytes_after_request));
EXPECT_GT(tx_bytes_after_request, tx_bytes_before_request);
EXPECT_TRUE(
android::traffic_stats::GetCurrentUidRxBytes(&rx_bytes_after_request));
EXPECT_GT(rx_bytes_after_request, rx_bytes_before_request);
}
} // namespace
} // namespace net
|