File: cluster_server_proxy_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 (180 lines) | stat: -rw-r--r-- 6,864 bytes parent folder | download | duplicates (5)
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
169
170
171
172
173
174
175
176
177
178
179
180
// Copyright 2024 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/commerce/core/compare/cluster_server_proxy.h"

#include <memory>
#include <optional>

#include "base/functional/callback.h"
#include "base/json/json_reader.h"
#include "base/json/json_writer.h"
#include "base/run_loop.h"
#include "base/test/gtest_util.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "base/values.h"
#include "components/commerce/core/commerce_feature_list.h"
#include "components/commerce/core/commerce_types.h"
#include "components/commerce/core/mock_account_checker.h"
#include "components/commerce/core/pref_names.h"
#include "components/commerce/core/test_utils.h"
#include "components/endpoint_fetcher/endpoint_fetcher.h"
#include "components/endpoint_fetcher/mock_endpoint_fetcher.h"
#include "components/prefs/testing_pref_service.h"
#include "components/signin/public/identity_manager/identity_test_environment.h"
#include "net/http/http_status_code.h"
#include "services/data_decoder/public/cpp/test_support/in_process_data_decoder.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/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using endpoint_fetcher::MockEndpointFetcher;
using testing::_;

namespace commerce {
namespace {

const char kTestServerUrl[] =
    "https://memex-pa.googleapis.com/v1/shopping/products:related";
const uint64_t kProduct1Id = 123u;
const uint64_t kProduct2Id = 234u;

const std::string kSimpleResponse = R"(
    {
      "productIds": [
        {
          "type": "TYPE_UNSPECIFIED",
          "identifier": "123"
        },
        {
          "type": "GLOBAL_PRODUCT_CLUSTER_ID",
          "identifier": "234"
        }
      ]
    })";

std::string GetPostData() {
  std::string post_data_string = R"(
    {
      "productIds": [
        {
          "identifier": "123",
          "type": "GLOBAL_PRODUCT_CLUSTER_ID"
        },
        {
          "identifier": "234",
          "type": "GLOBAL_PRODUCT_CLUSTER_ID"
        }
      ]
    })";
  std::optional<base::Value::Dict> dict =
      base::JSONReader::ReadDict(post_data_string);
  std::string post_data;
  base::JSONWriter::Write(dict.value(), &post_data);
  return post_data;
}

class FakeClusterServerProxy : public ClusterServerProxy {
 public:
  FakeClusterServerProxy(
      signin::IdentityManager* identity_manager,
      scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
      AccountChecker* account_checker)
      : ClusterServerProxy(identity_manager,
                           url_loader_factory,
                           account_checker) {}
  FakeClusterServerProxy(const FakeClusterServerProxy&) = delete;
  FakeClusterServerProxy operator=(const FakeClusterServerProxy&) = delete;
  ~FakeClusterServerProxy() override = default;
  MOCK_METHOD(std::unique_ptr<endpoint_fetcher::EndpointFetcher>,
              CreateEndpointFetcher,
              (const GURL& url, const std::string& post_data),
              (override));
};

class ClusterServerProxyTest : public testing::Test {
 protected:
  void SetUp() override {
    // Setup to pass CanFetchProductSpecificationsData check.
    scoped_feature_list_.InitAndEnableFeature(commerce::kProductSpecifications);
    account_checker_ = std::make_unique<commerce::MockAccountChecker>();
    prefs_ = std::make_unique<TestingPrefServiceSimple>();
    commerce::MockAccountChecker::RegisterCommercePrefs(prefs_->registry());
    account_checker_->SetPrefs(prefs_.get());
    commerce::EnableProductSpecificationsDataFetch(account_checker_.get(),
                                                   prefs_.get());
    fetcher_ = std::make_unique<MockEndpointFetcher>();
    scoped_refptr<network::SharedURLLoaderFactory> test_url_loader_factory =
        base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>(
            &test_url_loader_factory_);
    server_proxy_ = std::make_unique<FakeClusterServerProxy>(
        identity_test_env_.identity_manager(),
        std::move(test_url_loader_factory), account_checker_.get());
    ON_CALL(*server_proxy_, CreateEndpointFetcher).WillByDefault([this]() {
      return std::move(fetcher_);
    });
  }

 protected:
  base::test::TaskEnvironment task_environment_;
  signin::IdentityTestEnvironment identity_test_env_;
  network::TestURLLoaderFactory test_url_loader_factory_;
  data_decoder::test::InProcessDataDecoder in_process_data_decoder_;
  std::unique_ptr<MockEndpointFetcher> fetcher_;
  std::unique_ptr<commerce::MockAccountChecker> account_checker_;
  std::unique_ptr<TestingPrefServiceSimple> prefs_;
  std::unique_ptr<FakeClusterServerProxy> server_proxy_;
  base::test::ScopedFeatureList scoped_feature_list_;
};

TEST_F(ClusterServerProxyTest, TestGetComparableProducts) {
  fetcher_->SetFetchResponse(kSimpleResponse);
  EXPECT_CALL(*server_proxy_,
              CreateEndpointFetcher(GURL(kTestServerUrl), GetPostData()))
      .Times(1);
  base::RunLoop run_loop;
  server_proxy_->GetComparableProducts(
      std::vector<uint64_t>{kProduct1Id, kProduct2Id},
      base::BindOnce([](const std::vector<uint64_t>& product_cluster_ids) {
        ASSERT_EQ(product_cluster_ids.size(), 1u);
        ASSERT_EQ(product_cluster_ids[0], kProduct2Id);
      }).Then(run_loop.QuitClosure()));
  run_loop.Run();
}

TEST_F(ClusterServerProxyTest, TestGetComparableProductsWithServerError) {
  fetcher_->SetFetchResponse(kSimpleResponse, net::HTTP_NOT_FOUND);
  EXPECT_CALL(*server_proxy_,
              CreateEndpointFetcher(GURL(kTestServerUrl), GetPostData()))
      .Times(1);
  base::RunLoop run_loop;
  server_proxy_->GetComparableProducts(
      std::vector<uint64_t>{kProduct1Id, kProduct2Id},
      base::BindOnce([](const std::vector<uint64_t>& product_cluster_ids) {
        ASSERT_EQ(product_cluster_ids.size(), 0u);
      }).Then(run_loop.QuitClosure()));
  run_loop.Run();
}

TEST_F(ClusterServerProxyTest, TestGetComparableProductsNotEligible) {
  // Turning off MSBB should block fetches for new data from happening.
  account_checker_->SetAnonymizedUrlDataCollectionEnabled(false);

  fetcher_->SetFetchResponse(kSimpleResponse);
  EXPECT_CALL(*server_proxy_, CreateEndpointFetcher(testing::_, testing::_))
      .Times(0);
  base::RunLoop run_loop;
  server_proxy_->GetComparableProducts(
      std::vector<uint64_t>{kProduct1Id, kProduct2Id},
      base::BindOnce([](const std::vector<uint64_t>& product_cluster_ids) {
        ASSERT_EQ(product_cluster_ids.size(), 0u);
      }).Then(run_loop.QuitClosure()));
  run_loop.Run();
}

}  // namespace
}  // namespace commerce