File: batch_upload_service_test_helper.h

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 (90 lines) | stat: -rw-r--r-- 4,270 bytes parent folder | download | duplicates (3)
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
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_PROFILES_BATCH_UPLOAD_BATCH_UPLOAD_SERVICE_TEST_HELPER_H_
#define CHROME_BROWSER_PROFILES_BATCH_UPLOAD_BATCH_UPLOAD_SERVICE_TEST_HELPER_H_

#include "chrome/browser/profiles/batch_upload/batch_upload_service.h"
#include "chrome/browser/ui/profiles/batch_upload_ui_delegate.h"
#include "components/sync/service/local_data_description.h"
#include "components/sync/test/mock_sync_service.h"

class BatchUploadDelegate;
class Profile;

namespace content {
class BrowserContext;
}  // namespace content

// The purpose of this class is to facilitate testing of the
// `BatchUploadService`. The main service relies on the `syncer::SyncService` to
// retrieve the local data to be used by the service, and given that the real
// SyncService is usually not used in tests, there is a need to always override
// the returned values that are used by the `BatchUploadService`.
// `BatchUploadServiceTestHelper` provides abstraction of that complexity by
// offering methods that constructs the service with the required testing
// dependencies, and easily control the retrieval of local data.
//
// Even though this test helper uses an internal `syncer::MockSyncService` which
// is also used by the constructed service, the tests using it can still use
// their own implementation of SyncService that may be tied to a profile, and
// will not affect this test helper/service, and vice versa.
class BatchUploadServiceTestHelper {
 public:
  BatchUploadServiceTestHelper();
  ~BatchUploadServiceTestHelper();

  // Setups up the TestingFactory to override the service constructed for
  // `profile` with a service that has the proper testing setup.
  // It is recommended for this function to be called in
  // `InProcessBrowserTest::SetUpBrowserContextKeyedServices()` during test
  // setup - if called after the service was already constructed, it will
  // override the existing service, potentially breaking other dependent
  // services. Even though `BatchUploadService` is not constructed at profile
  // initialization, it is still recommended to follow this pattern for
  // consistency.
  //  If `identity_manager` is nullptr, then the IdentityManager from the
  //  `profile` is used.
  // If `delegate` is not set, then the regular `BatchUploadUIDelegate` is used.
  void SetupBatchUploadTestingFactoryInProfile(
      Profile* profile,
      signin::IdentityManager* identity_manager = nullptr,
      std::unique_ptr<BatchUploadDelegate> delegate =
          std::make_unique<BatchUploadUIDelegate>());
  // Constructs a `BatchUploadService` instance that is not tied to any profile.
  std::unique_ptr<BatchUploadService> CreateBatchUploadService(
      signin::IdentityManager* identity_manager,
      std::unique_ptr<BatchUploadDelegate> delegate);

  // The following methods will affect the constructed BatchUploadService
  // through this class with the above methods.
  //
  // Simulates the addition of `item_count` local item(s) of `type`. Those items
  // will be returned when attempting to open the Batch Upload.
  // Returns the added description.
  const syncer::LocalDataDescription& SetReturnDescriptions(
      syncer::DataType type,
      size_t item_count);
  // Simulates the addition of 1 item of each available type. Those items will
  // be returned when attempting to open the Batch Upload.
  void SetLocalDataDescriptionForAllAvailableTypes();
  // Clears all fake local items.
  void ClearReturnDescriptions();
  // Returns the expected return description for `type`.
  syncer::LocalDataDescription& GetReturnDescription(syncer::DataType type);

  syncer::MockSyncService* GetSyncServiceMock() { return &mock_sync_service_; }

 private:
  std::unique_ptr<KeyedService> CreateBatchUploadServiceInternal(
      signin::IdentityManager* identity_manager,
      std::unique_ptr<BatchUploadDelegate> delegate,
      content::BrowserContext* browser_context);

  syncer::MockSyncService mock_sync_service_;
  std::map<syncer::DataType, syncer::LocalDataDescription>
      returned_descriptions_;
};

#endif  // CHROME_BROWSER_PROFILES_BATCH_UPLOAD_BATCH_UPLOAD_SERVICE_TEST_HELPER_H_