blob: e0464f3a3903d69cd251d650fc04df35809cbe83 [file] [log] [blame]
manuk4526a262019-08-13 20:46:251// Copyright 2019 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/omnibox/browser/document_suggestions_service.h"
6
7#include "base/bind.h"
8#include "base/memory/scoped_refptr.h"
9#include "base/metrics/field_trial.h"
10#include "base/run_loop.h"
11#include "base/strings/string_number_conversions.h"
12#include "base/strings/utf_string_conversions.h"
13#include "base/test/bind_test_util.h"
14#include "base/test/scoped_feature_list.h"
15#include "base/test/scoped_task_environment.h"
16#include "components/signin/public/identity_manager/identity_test_environment.h"
17#include "components/sync_preferences/testing_pref_service_syncable.h"
18#include "components/variations/net/variations_http_headers.h"
19#include "components/variations/variations_associated_data.h"
20#include "components/variations/variations_http_header_provider.h"
21#include "services/network/public/cpp/resource_request.h"
22#include "services/network/public/cpp/shared_url_loader_factory.h"
23#include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
24#include "services/network/test/test_url_loader_factory.h"
25#include "testing/gtest/include/gtest/gtest.h"
26
27namespace {
28
29variations::VariationID kVariationID = 123;
30
31void OnDocumentSuggestionsLoaderAvailable(
32 std::unique_ptr<network::SimpleURLLoader> loader) {}
33
34void OnURLLoadComplete(const network::SimpleURLLoader* source,
35 std::unique_ptr<std::string> response_body) {}
36
37class DocumentSuggestionsServiceTest : public testing::Test {
38 protected:
39 DocumentSuggestionsServiceTest()
40 : scoped_task_environment_(
41 base::test::ScopedTaskEnvironment::MainThreadType::UI),
42 shared_url_loader_factory_(
43 base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>(
44 &test_url_loader_factory_)),
45 identity_test_env_(&test_url_loader_factory_, &prefs_),
46 field_trial_list_(nullptr),
47 document_suggestions_service_(new DocumentSuggestionsService(
48 identity_test_env_.identity_manager(),
49 shared_url_loader_factory_)) {
50 // Set up identity manager.
51 identity_test_env_.SetPrimaryAccount("email");
52 identity_test_env_.SetRefreshTokenForPrimaryAccount();
53 identity_test_env_.SetAutomaticIssueOfAccessTokens(true);
54
55 // Set up a variation.
56 variations::VariationsHttpHeaderProvider::GetInstance()->ResetForTesting();
57 variations::AssociateGoogleVariationID(variations::GOOGLE_WEB_PROPERTIES,
58 "trial name", "group name",
59 kVariationID);
60 base::FieldTrialList::CreateFieldTrial("trial name", "group name")->group();
61 }
62
63 base::test::ScopedTaskEnvironment scoped_task_environment_;
64 network::TestURLLoaderFactory test_url_loader_factory_;
65 scoped_refptr<network::SharedURLLoaderFactory> shared_url_loader_factory_;
66 sync_preferences::TestingPrefServiceSyncable prefs_;
67 signin::IdentityTestEnvironment identity_test_env_;
68 base::FieldTrialList field_trial_list_;
69 std::unique_ptr<DocumentSuggestionsService> document_suggestions_service_;
70
71 DISALLOW_COPY_AND_ASSIGN(DocumentSuggestionsServiceTest);
72};
73
74TEST_F(DocumentSuggestionsServiceTest, VariationHeaders) {
75 test_url_loader_factory_.SetInterceptor(
76 base::BindLambdaForTesting([](const network::ResourceRequest& request) {
77 EXPECT_TRUE(variations::HasVariationsHeader(request));
78 std::string variation =
79 variations::VariationsHttpHeaderProvider::GetInstance()
80 ->GetVariationsString();
81 EXPECT_EQ(variation, " " + base::NumberToString(kVariationID) + " ");
82 }));
83
84 document_suggestions_service_->CreateDocumentSuggestionsRequest(
85 base::ASCIIToUTF16(""), false,
86 base::BindOnce(OnDocumentSuggestionsLoaderAvailable),
87 base::BindOnce(OnURLLoadComplete));
88
89 base::RunLoop().RunUntilIdle();
90}
91
92} // namespace