asvitkine | e6f7a58b1 | 2015-02-23 21:24:53 | [diff] [blame] | 1 | // 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 | |
asvitkine | 9a27983 | 2015-12-18 02:35:50 | [diff] [blame] | 7 | #include <string> |
| 8 | |
avi | bc5337b | 2015-12-25 23:16:33 | [diff] [blame] | 9 | #include "base/macros.h" |
asvitkine | e6f7a58b1 | 2015-02-23 21:24:53 | [diff] [blame] | 10 | #include "base/metrics/field_trial.h" |
Ahmed Fakhry | af8ab05 | 2017-07-21 21:39:07 | [diff] [blame] | 11 | #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" |
asvitkine | e6f7a58b1 | 2015-02-23 21:24:53 | [diff] [blame] | 15 | #include "components/variations/variations_associated_data.h" |
asvitkine | 9a27983 | 2015-12-18 02:35:50 | [diff] [blame] | 16 | #include "components/variations/variations_http_header_provider.h" |
asvitkine | e6f7a58b1 | 2015-02-23 21:24:53 | [diff] [blame] | 17 | #include "content/public/test/test_browser_context.h" |
jam | b84299e | 2016-04-12 16:58:59 | [diff] [blame] | 18 | #include "content/public/test/test_browser_thread_bundle.h" |
asvitkine | e6f7a58b1 | 2015-02-23 21:24:53 | [diff] [blame] | 19 | #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 | |
| 23 | namespace feedback { |
| 24 | |
Ahmed Fakhry | af8ab05 | 2017-07-21 21:39:07 | [diff] [blame] | 25 | namespace { |
| 26 | |
| 27 | constexpr base::TimeDelta kTestRetryDelay = |
| 28 | base::TimeDelta::FromMilliseconds(1); |
| 29 | |
| 30 | constexpr int kHttpPostSuccessNoContent = 204; |
| 31 | constexpr int kHttpPostFailClientError = 400; |
| 32 | constexpr int kHttpPostFailServerError = 500; |
| 33 | |
| 34 | } // namespace |
| 35 | |
asvitkine | e6f7a58b1 | 2015-02-23 21:24:53 | [diff] [blame] | 36 | class FeedbackUploaderChromeTest : public ::testing::Test { |
| 37 | protected: |
jam | b84299e | 2016-04-12 16:58:59 | [diff] [blame] | 38 | FeedbackUploaderChromeTest() |
| 39 | : browser_thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {} |
asvitkine | e6f7a58b1 | 2015-02-23 21:24:53 | [diff] [blame] | 40 | |
| 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: |
jam | b84299e | 2016-04-12 16:58:59 | [diff] [blame] | 58 | content::TestBrowserThreadBundle browser_thread_bundle_; |
asvitkine | e6f7a58b1 | 2015-02-23 21:24:53 | [diff] [blame] | 59 | |
| 60 | DISALLOW_COPY_AND_ASSIGN(FeedbackUploaderChromeTest); |
| 61 | }; |
| 62 | |
| 63 | TEST_F(FeedbackUploaderChromeTest, VariationHeaders) { |
| 64 | // Register a trial and variation id, so that there is data in variations |
asvitkine | b4ed7868 | 2015-03-12 18:18:54 | [diff] [blame] | 65 | // 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(); |
asvitkine | e6f7a58b1 | 2015-02-23 21:24:53 | [diff] [blame] | 68 | base::FieldTrialList field_trial_list_(NULL); |
| 69 | CreateFieldTrialWithId("Test", "Group1", 123); |
| 70 | |
| 71 | content::TestBrowserContext context; |
Ahmed Fakhry | af8ab05 | 2017-07-21 21:39:07 | [diff] [blame] | 72 | FeedbackUploaderChrome uploader( |
| 73 | &context, FeedbackUploaderFactory::CreateUploaderTaskRunner()); |
asvitkine | e6f7a58b1 | 2015-02-23 21:24:53 | [diff] [blame] | 74 | |
| 75 | net::TestURLFetcherFactory factory; |
Ahmed Fakhry | af8ab05 | 2017-07-21 21:39:07 | [diff] [blame] | 76 | uploader.QueueReport("test"); |
asvitkine | e6f7a58b1 | 2015-02-23 21:24:53 | [diff] [blame] | 77 | |
| 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); |
asvitkine | b4ed7868 | 2015-03-12 18:18:54 | [diff] [blame] | 86 | |
| 87 | variations::VariationsHttpHeaderProvider::GetInstance()->ResetForTesting(); |
asvitkine | e6f7a58b1 | 2015-02-23 21:24:53 | [diff] [blame] | 88 | } |
| 89 | |
Ahmed Fakhry | af8ab05 | 2017-07-21 21:39:07 | [diff] [blame] | 90 | TEST_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 | |
asvitkine | e6f7a58b1 | 2015-02-23 21:24:53 | [diff] [blame] | 126 | } // namespace feedback |