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.
5 #include "components/feedback/feedback_uploader_chrome.h"
7 #include "base/message_loop/message_loop.h"
8 #include "base/metrics/field_trial.h"
9 #include "components/variations/variations_associated_data.h"
10 #include "content/public/test/test_browser_context.h"
11 #include "net/url_request/test_url_fetcher_factory.h"
12 #include "net/url_request/url_fetcher_delegate.h"
13 #include "testing/gtest/include/gtest/gtest.h"
17 class FeedbackUploaderChromeTest
: public ::testing::Test
{
19 FeedbackUploaderChromeTest() {}
21 ~FeedbackUploaderChromeTest() override
{
22 // Clean up registered ids.
23 variations::testing::ClearAllVariationIDs();
26 // Registers a field trial with the specified name and group and an associated
27 // google web property variation id.
28 void CreateFieldTrialWithId(const std::string
& trial_name
,
29 const std::string
& group_name
,
31 variations::AssociateGoogleVariationID(
32 variations::GOOGLE_WEB_PROPERTIES
, trial_name
, group_name
,
33 static_cast<variations::VariationID
>(variation_id
));
34 base::FieldTrialList::CreateFieldTrial(trial_name
, group_name
)->group();
38 base::MessageLoopForUI message_loop_
;
40 DISALLOW_COPY_AND_ASSIGN(FeedbackUploaderChromeTest
);
43 TEST_F(FeedbackUploaderChromeTest
, VariationHeaders
) {
44 // Register a trial and variation id, so that there is data in variations
46 base::FieldTrialList
field_trial_list_(NULL
);
47 CreateFieldTrialWithId("Test", "Group1", 123);
49 content::TestBrowserContext context
;
50 FeedbackUploaderChrome
uploader(&context
);
52 net::TestURLFetcherFactory factory
;
53 uploader
.DispatchReport("test");
55 net::TestURLFetcher
* fetcher
= factory
.GetFetcherByID(0);
56 net::HttpRequestHeaders headers
;
57 fetcher
->GetExtraRequestHeaders(&headers
);
59 EXPECT_TRUE(headers
.GetHeader("X-Client-Data", &value
));
60 EXPECT_FALSE(value
.empty());
61 // The fetcher's delegate is responsible for freeing the fetcher (and itself).
62 fetcher
->delegate()->OnURLFetchComplete(fetcher
);
65 } // namespace feedback