blob: 0f631b17ee27ec58ae9012fcad2926f46425dcf0 [file] [log] [blame]
asvitkinee6f7a58b12015-02-23 21:24:531// Copyright 2015 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/feedback/feedback_uploader_chrome.h"
6
asvitkine9a279832015-12-18 02:35:507#include <string>
8
avibc5337b2015-12-25 23:16:339#include "base/macros.h"
asvitkinee6f7a58b12015-02-23 21:24:5310#include "base/metrics/field_trial.h"
Ahmed Fakhryaf8ab052017-07-21 21:39:0711#include "base/run_loop.h"
12#include "base/task_scheduler/post_task.h"
13#include "base/task_scheduler/task_traits.h"
14#include "components/feedback/feedback_uploader_factory.h"
asvitkinee6f7a58b12015-02-23 21:24:5315#include "components/variations/variations_associated_data.h"
asvitkine9a279832015-12-18 02:35:5016#include "components/variations/variations_http_header_provider.h"
asvitkinee6f7a58b12015-02-23 21:24:5317#include "content/public/test/test_browser_context.h"
jamb84299e2016-04-12 16:58:5918#include "content/public/test/test_browser_thread_bundle.h"
asvitkinee6f7a58b12015-02-23 21:24:5319#include "net/url_request/test_url_fetcher_factory.h"
20#include "net/url_request/url_fetcher_delegate.h"
21#include "testing/gtest/include/gtest/gtest.h"
22
23namespace feedback {
24
Ahmed Fakhryaf8ab052017-07-21 21:39:0725namespace {
26
27constexpr base::TimeDelta kTestRetryDelay =
28 base::TimeDelta::FromMilliseconds(1);
29
30constexpr int kHttpPostSuccessNoContent = 204;
31constexpr int kHttpPostFailClientError = 400;
32constexpr int kHttpPostFailServerError = 500;
33
34} // namespace
35
asvitkinee6f7a58b12015-02-23 21:24:5336class FeedbackUploaderChromeTest : public ::testing::Test {
37 protected:
jamb84299e2016-04-12 16:58:5938 FeedbackUploaderChromeTest()
39 : browser_thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {}
asvitkinee6f7a58b12015-02-23 21:24:5340
41 ~FeedbackUploaderChromeTest() override {
42 // Clean up registered ids.
43 variations::testing::ClearAllVariationIDs();
44 }
45
46 // Registers a field trial with the specified name and group and an associated
47 // google web property variation id.
48 void CreateFieldTrialWithId(const std::string& trial_name,
49 const std::string& group_name,
50 int variation_id) {
51 variations::AssociateGoogleVariationID(
52 variations::GOOGLE_WEB_PROPERTIES, trial_name, group_name,
53 static_cast<variations::VariationID>(variation_id));
54 base::FieldTrialList::CreateFieldTrial(trial_name, group_name)->group();
55 }
56
57 private:
jamb84299e2016-04-12 16:58:5958 content::TestBrowserThreadBundle browser_thread_bundle_;
asvitkinee6f7a58b12015-02-23 21:24:5359
60 DISALLOW_COPY_AND_ASSIGN(FeedbackUploaderChromeTest);
61};
62
63TEST_F(FeedbackUploaderChromeTest, VariationHeaders) {
64 // Register a trial and variation id, so that there is data in variations
asvitkineb4ed78682015-03-12 18:18:5465 // headers. Also, the variations header provider may have been registered to
66 // observe some other field trial list, so reset it.
67 variations::VariationsHttpHeaderProvider::GetInstance()->ResetForTesting();
asvitkinee6f7a58b12015-02-23 21:24:5368 base::FieldTrialList field_trial_list_(NULL);
69 CreateFieldTrialWithId("Test", "Group1", 123);
70
71 content::TestBrowserContext context;
Ahmed Fakhryaf8ab052017-07-21 21:39:0772 FeedbackUploaderChrome uploader(
73 &context, FeedbackUploaderFactory::CreateUploaderTaskRunner());
asvitkinee6f7a58b12015-02-23 21:24:5374
75 net::TestURLFetcherFactory factory;
Ahmed Fakhryaf8ab052017-07-21 21:39:0776 uploader.QueueReport("test");
asvitkinee6f7a58b12015-02-23 21:24:5377
78 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
79 net::HttpRequestHeaders headers;
80 fetcher->GetExtraRequestHeaders(&headers);
81 std::string value;
82 EXPECT_TRUE(headers.GetHeader("X-Client-Data", &value));
83 EXPECT_FALSE(value.empty());
84 // The fetcher's delegate is responsible for freeing the fetcher (and itself).
85 fetcher->delegate()->OnURLFetchComplete(fetcher);
asvitkineb4ed78682015-03-12 18:18:5486
87 variations::VariationsHttpHeaderProvider::GetInstance()->ResetForTesting();
asvitkinee6f7a58b12015-02-23 21:24:5388}
89
Ahmed Fakhryaf8ab052017-07-21 21:39:0790TEST_F(FeedbackUploaderChromeTest, TestVariousServerResponses) {
91 content::TestBrowserContext context;
92 FeedbackUploader::SetMinimumRetryDelayForTesting(kTestRetryDelay);
93 FeedbackUploaderChrome uploader(
94 &context, FeedbackUploaderFactory::CreateUploaderTaskRunner());
95
96 EXPECT_EQ(kTestRetryDelay, uploader.retry_delay());
97 // Successful reports should not introduce any retries, and should not
98 // increase the backoff delay.
99 net::TestURLFetcherFactory factory;
100 factory.set_remove_fetcher_on_delete(true);
101 uploader.QueueReport("Successful report");
102 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
103 fetcher->set_response_code(kHttpPostSuccessNoContent);
104 fetcher->delegate()->OnURLFetchComplete(fetcher);
105 EXPECT_EQ(kTestRetryDelay, uploader.retry_delay());
106 EXPECT_TRUE(uploader.QueueEmpty());
107
108 // Failed reports due to client errors are not retried. No backoff delay
109 // should be doubled.
110 uploader.QueueReport("Client error failed report");
111 fetcher = factory.GetFetcherByID(0);
112 fetcher->set_response_code(kHttpPostFailClientError);
113 fetcher->delegate()->OnURLFetchComplete(fetcher);
114 EXPECT_EQ(kTestRetryDelay, uploader.retry_delay());
115 EXPECT_TRUE(uploader.QueueEmpty());
116
117 // Failed reports due to server errors are retried.
118 uploader.QueueReport("Server error failed report");
119 fetcher = factory.GetFetcherByID(0);
120 fetcher->set_response_code(kHttpPostFailServerError);
121 fetcher->delegate()->OnURLFetchComplete(fetcher);
122 EXPECT_EQ(kTestRetryDelay * 2, uploader.retry_delay());
123 EXPECT_FALSE(uploader.QueueEmpty());
124}
125
asvitkinee6f7a58b12015-02-23 21:24:53126} // namespace feedback