File: document_suggestions_service_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (168 lines) | stat: -rw-r--r-- 6,891 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/omnibox/browser/document_suggestions_service.h"

#include "base/functional/bind.h"
#include "base/json/json_reader.h"
#include "base/memory/scoped_refptr.h"
#include "base/metrics/field_trial.h"
#include "base/run_loop.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/bind.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "components/omnibox/common/omnibox_features.h"
#include "components/signin/public/identity_manager/account_capabilities_test_mutator.h"
#include "components/signin/public/identity_manager/identity_test_environment.h"
#include "components/signin/public/identity_manager/tribool.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "components/variations/net/variations_http_headers.h"
#include "components/variations/scoped_variations_ids_provider.h"
#include "components/variations/variations_associated_data.h"
#include "components/variations/variations_ids_provider.h"
#include "services/network/public/cpp/resource_request.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
#include "services/network/test/test_url_loader_factory.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

variations::VariationID kVariationID = 123;

void OnDocumentSuggestionsRequestAvailable(network::ResourceRequest* request) {}

void OnDocumentSuggestionsLoaderAvailable(
    std::unique_ptr<network::SimpleURLLoader> loader,
    const std::string& request_body) {}

void OnURLLoadComplete(const network::SimpleURLLoader* source,
                       std::unique_ptr<std::string> response_body) {}

class DocumentSuggestionsServiceTest : public testing::Test {
 protected:
  DocumentSuggestionsServiceTest()
      : task_environment_(base::test::TaskEnvironment::MainThreadType::UI),
        shared_url_loader_factory_(
            base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>(
                &test_url_loader_factory_)),
        identity_test_env_(&test_url_loader_factory_, &prefs_),
        document_suggestions_service_(new DocumentSuggestionsService(
            identity_test_env_.identity_manager(),
            shared_url_loader_factory_)) {
    // Set up a variation.
    variations::AssociateGoogleVariationID(
        variations::GOOGLE_WEB_PROPERTIES_ANY_CONTEXT, "trial name",
        "group name", kVariationID);
    base::FieldTrialList::CreateFieldTrial("trial name", "group name")
        ->Activate();
  }
  DocumentSuggestionsServiceTest(const DocumentSuggestionsServiceTest&) =
      delete;
  DocumentSuggestionsServiceTest& operator=(
      const DocumentSuggestionsServiceTest&) = delete;

  AccountInfo SetUpPrimaryAccount() {
    auto account_info = identity_test_env_.MakePrimaryAccountAvailable(
        "foo@gmail.com", signin::ConsentLevel::kSignin);
    identity_test_env_.SetRefreshTokenForPrimaryAccount();
    identity_test_env_.SetAutomaticIssueOfAccessTokens(true);
    return account_info;
  }

  base::test::TaskEnvironment task_environment_;
  variations::ScopedVariationsIdsProvider scoped_variations_ids_provider_{
      variations::VariationsIdsProvider::Mode::kUseSignedInState};
  network::TestURLLoaderFactory test_url_loader_factory_;
  scoped_refptr<network::SharedURLLoaderFactory> shared_url_loader_factory_;
  sync_preferences::TestingPrefServiceSyncable prefs_;
  signin::IdentityTestEnvironment identity_test_env_;
  std::unique_ptr<DocumentSuggestionsService> document_suggestions_service_;
};

TEST_F(DocumentSuggestionsServiceTest, EnsureVariationHeaders) {
  SetUpPrimaryAccount();

  network::ResourceRequest resource_request;
  test_url_loader_factory_.SetInterceptor(
      base::BindLambdaForTesting([&](const network::ResourceRequest& request) {
        resource_request = request;
      }));

  document_suggestions_service_->CreateDocumentSuggestionsRequest(
      u"", false, base::BindOnce(OnDocumentSuggestionsRequestAvailable),
      base::BindOnce(OnDocumentSuggestionsLoaderAvailable),
      base::BindOnce(OnURLLoadComplete));

  base::RunLoop().RunUntilIdle();

  EXPECT_TRUE(variations::HasVariationsHeader(resource_request));
  std::string variation =
      variations::VariationsIdsProvider::GetInstance()->GetVariationsString();
  EXPECT_EQ(variation, " " + base::NumberToString(kVariationID) + " ");
}

TEST_F(DocumentSuggestionsServiceTest, EnsureCookies) {
  SetUpPrimaryAccount();

  network::ResourceRequest resource_request;
  test_url_loader_factory_.SetInterceptor(
      base::BindLambdaForTesting([&](const network::ResourceRequest& request) {
        resource_request = request;
      }));

  document_suggestions_service_->CreateDocumentSuggestionsRequest(
      u"", false, base::BindOnce(OnDocumentSuggestionsRequestAvailable),
      base::BindOnce(OnDocumentSuggestionsLoaderAvailable),
      base::BindOnce(OnURLLoadComplete));

  base::RunLoop().RunUntilIdle();

  EXPECT_TRUE(resource_request.site_for_cookies.IsEquivalent(
      net::SiteForCookies::FromUrl(GURL("https://cloudsearch.googleapis.com"))))
      << resource_request.site_for_cookies.ToDebugString();
}

TEST_F(DocumentSuggestionsServiceTest, EnsurePrimaryAccount) {
  EXPECT_FALSE(document_suggestions_service_->HasPrimaryAccount());

  // Make the primary account available.
  SetUpPrimaryAccount();

  EXPECT_TRUE(document_suggestions_service_->HasPrimaryAccount());
}

TEST_F(DocumentSuggestionsServiceTest, EnsureIsSubjectToEnterprisePolicies) {
  EXPECT_EQ(signin::Tribool::kFalse,
            document_suggestions_service_
                ->account_is_subject_to_enterprise_policies());

  // Make the primary account available.
  auto account_info = SetUpPrimaryAccount();

  EXPECT_EQ(signin::Tribool::kUnknown,
            document_suggestions_service_
                ->account_is_subject_to_enterprise_policies());

  // Make the the primary account subject to Enterprise policies.
  AccountCapabilitiesTestMutator mutator(&account_info.capabilities);
  mutator.set_is_subject_to_enterprise_policies(true);
  identity_test_env_.UpdateAccountInfoForAccount(account_info);

  EXPECT_EQ(signin::Tribool::kTrue,
            document_suggestions_service_
                ->account_is_subject_to_enterprise_policies());

  // Make the the primary account NOT subject to Enterprise policies.
  mutator.set_is_subject_to_enterprise_policies(false);
  identity_test_env_.UpdateAccountInfoForAccount(account_info);

  EXPECT_EQ(signin::Tribool::kFalse,
            document_suggestions_service_
                ->account_is_subject_to_enterprise_policies());
}

}  // namespace