Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / components / domain_reliability / context_unittest.cc
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.
4
5 #include "components/domain_reliability/context.h"
6
7 #include <map>
8 #include <string>
9
10 #include "base/bind.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/message_loop/message_loop_proxy.h"
13 #include "components/domain_reliability/beacon.h"
14 #include "components/domain_reliability/dispatcher.h"
15 #include "components/domain_reliability/scheduler.h"
16 #include "components/domain_reliability/test_util.h"
17 #include "net/base/net_errors.h"
18 #include "net/url_request/url_request_test_util.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20
21 namespace domain_reliability {
22 namespace {
23
24 typedef std::vector<DomainReliabilityBeacon> BeaconVector;
25
26 DomainReliabilityBeacon MakeBeacon(MockableTime* time) {
27   DomainReliabilityBeacon beacon;
28   beacon.status = "ok";
29   beacon.chrome_error = net::OK;
30   beacon.server_ip = "127.0.0.1";
31   beacon.protocol = "HTTP";
32   beacon.http_response_code = 200;
33   beacon.elapsed = base::TimeDelta::FromMilliseconds(250);
34   beacon.start_time = time->NowTicks() - beacon.elapsed;
35   return beacon;
36 }
37
38 class DomainReliabilityContextTest : public testing::Test {
39  protected:
40   DomainReliabilityContextTest()
41       : dispatcher_(&time_),
42         params_(MakeTestSchedulerParams()),
43         uploader_(base::Bind(&DomainReliabilityContextTest::OnUploadRequest,
44                              base::Unretained(this))),
45         upload_reporter_string_("test-reporter"),
46         context_(&time_,
47                  params_,
48                  upload_reporter_string_,
49                  &dispatcher_,
50                  &uploader_,
51                  MakeTestConfig().Pass()),
52         upload_pending_(false) {}
53
54   TimeDelta min_delay() const { return params_.minimum_upload_delay; }
55   TimeDelta max_delay() const { return params_.maximum_upload_delay; }
56   TimeDelta retry_interval() const { return params_.upload_retry_interval; }
57   TimeDelta zero_delta() const { return TimeDelta::FromMicroseconds(0); }
58
59   bool upload_pending() { return upload_pending_; }
60
61   const std::string& upload_report() {
62     DCHECK(upload_pending_);
63     return upload_report_;
64   }
65
66   const GURL& upload_url() {
67     DCHECK(upload_pending_);
68     return upload_url_;
69   }
70
71   void CallUploadCallback(bool success) {
72     DCHECK(upload_pending_);
73     upload_callback_.Run(success);
74     upload_pending_ = false;
75   }
76
77   bool CheckNoBeacons() {
78     BeaconVector beacons;
79     context_.GetQueuedBeaconsForTesting(&beacons);
80     return beacons.empty();
81   }
82
83   bool CheckCounts(size_t index,
84                    unsigned expected_successful,
85                    unsigned expected_failed) {
86     unsigned successful, failed;
87     context_.GetRequestCountsForTesting(index, &successful, &failed);
88     return successful == expected_successful && failed == expected_failed;
89   }
90
91   MockTime time_;
92   DomainReliabilityDispatcher dispatcher_;
93   DomainReliabilityScheduler::Params params_;
94   MockUploader uploader_;
95   std::string upload_reporter_string_;
96   DomainReliabilityContext context_;
97
98  private:
99   void OnUploadRequest(
100       const std::string& report_json,
101       const GURL& upload_url,
102       const DomainReliabilityUploader::UploadCallback& callback) {
103     DCHECK(!upload_pending_);
104     upload_report_ = report_json;
105     upload_url_ = upload_url;
106     upload_callback_ = callback;
107     upload_pending_ = true;
108   }
109
110   bool upload_pending_;
111   std::string upload_report_;
112   GURL upload_url_;
113   DomainReliabilityUploader::UploadCallback upload_callback_;
114 };
115
116 TEST_F(DomainReliabilityContextTest, Create) {
117   EXPECT_TRUE(CheckNoBeacons());
118   EXPECT_TRUE(CheckCounts(0, 0, 0));
119   EXPECT_TRUE(CheckCounts(1, 0, 0));
120 }
121
122 TEST_F(DomainReliabilityContextTest, NoResource) {
123   GURL url("http://example/no_resource");
124   DomainReliabilityBeacon beacon = MakeBeacon(&time_);
125   context_.OnBeacon(url, beacon);
126
127   EXPECT_TRUE(CheckNoBeacons());
128   EXPECT_TRUE(CheckCounts(0, 0, 0));
129   EXPECT_TRUE(CheckCounts(1, 0, 0));
130 }
131
132 TEST_F(DomainReliabilityContextTest, NeverReport) {
133   GURL url("http://example/never_report");
134   DomainReliabilityBeacon beacon = MakeBeacon(&time_);
135   context_.OnBeacon(url, beacon);
136
137   EXPECT_TRUE(CheckNoBeacons());
138   EXPECT_TRUE(CheckCounts(0, 0, 0));
139   EXPECT_TRUE(CheckCounts(1, 1, 0));
140 }
141
142 TEST_F(DomainReliabilityContextTest, AlwaysReport) {
143   GURL url("http://example/always_report");
144   DomainReliabilityBeacon beacon = MakeBeacon(&time_);
145   context_.OnBeacon(url, beacon);
146
147   BeaconVector beacons;
148   context_.GetQueuedBeaconsForTesting(&beacons);
149   EXPECT_EQ(1u, beacons.size());
150   EXPECT_TRUE(CheckCounts(0, 1, 0));
151   EXPECT_TRUE(CheckCounts(1, 0, 0));
152 }
153
154 TEST_F(DomainReliabilityContextTest, ReportUpload) {
155   GURL url("http://example/always_report");
156   DomainReliabilityBeacon beacon = MakeBeacon(&time_);
157   context_.OnBeacon(url, beacon);
158
159   BeaconVector beacons;
160   context_.GetQueuedBeaconsForTesting(&beacons);
161   EXPECT_EQ(1u, beacons.size());
162   EXPECT_TRUE(CheckCounts(0, 1, 0));
163   EXPECT_TRUE(CheckCounts(1, 0, 0));
164
165   // N.B.: Assumes max_delay is 5 minutes.
166   const char* kExpectedReport = "{"
167       "\"config_version\":\"1\","
168       "\"entries\":[{\"http_response_code\":200,\"protocol\":\"HTTP\","
169           "\"request_age_ms\":300250,\"request_elapsed_ms\":250,"
170           "\"resource\":\"always_report\",\"server_ip\":\"127.0.0.1\","
171           "\"status\":\"ok\"}],"
172       "\"reporter\":\"test-reporter\","
173       "\"resources\":[{\"failed_requests\":0,\"name\":\"always_report\","
174           "\"successful_requests\":1}]}";
175
176   time_.Advance(max_delay());
177   EXPECT_TRUE(upload_pending());
178   EXPECT_EQ(kExpectedReport, upload_report());
179   EXPECT_EQ(GURL("https://exampleuploader/upload"), upload_url());
180   CallUploadCallback(true);
181
182   EXPECT_TRUE(CheckNoBeacons());
183   EXPECT_TRUE(CheckCounts(0, 0, 0));
184   EXPECT_TRUE(CheckCounts(1, 0, 0));
185 }
186
187 }  // namespace
188 }  // namespace domain_reliability