File: mock_passage_embedder.h

package info (click to toggle)
chromium 140.0.7339.127-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,192,880 kB
  • sloc: cpp: 35,093,808; ansic: 7,161,670; javascript: 4,199,694; python: 1,441,797; asm: 949,904; xml: 747,503; pascal: 187,748; perl: 88,691; sh: 88,248; objc: 79,953; sql: 52,714; cs: 44,599; fortran: 24,137; makefile: 22,114; tcl: 15,277; php: 13,980; yacc: 9,000; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (98 lines) | stat: -rw-r--r-- 4,212 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
91
92
93
94
95
96
97
98
// 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_PERMISSIONS_TEST_MOCK_PASSAGE_EMBEDDER_H_
#define CHROME_BROWSER_PERMISSIONS_TEST_MOCK_PASSAGE_EMBEDDER_H_

#include <string>
#include <vector>

#include "base/run_loop.h"
#include "components/passage_embeddings/passage_embeddings_test_util.h"

namespace test {

class PassageEmbedderMock : public passage_embeddings::TestEmbedder {
 public:
  PassageEmbedderMock() = default;
  ~PassageEmbedderMock() override = default;

  // passage_embeddings::TestEmbedder:
  passage_embeddings::Embedder::TaskId ComputePassagesEmbeddings(
      passage_embeddings::PassagePriority priority,
      std::vector<std::string> passages,
      ComputePassagesEmbeddingsCallback callback) override;

  void set_status(passage_embeddings::ComputeEmbeddingsStatus status);

 private:
  passage_embeddings::ComputeEmbeddingsStatus status_ =
      passage_embeddings::ComputeEmbeddingsStatus::kSuccess;
};

// A mock that simulates a delayed execution of passage embeddings computation.
// This is useful for testing asynchronous client code, for example timeout
// logic. The computation is not executed upon calling
// `ComputePassagesEmbeddings`, but is rather stored. It can be invoked later
// by calling `ReleaseCallback`.
class DelayedPassageEmbedderMock : public PassageEmbedderMock {
 public:
  DelayedPassageEmbedderMock();
  ~DelayedPassageEmbedderMock() override;

  // passage_embeddings::TestEmbedder:
  // Overrides the base class implementation to simulate a delay. Instead of
  // computing the embeddings, it captures the arguments and the callback. The
  // actual computation (in this case a fake) is deferred until
  // `ReleaseCallback` is called.
  passage_embeddings::Embedder::TaskId ComputePassagesEmbeddings(
      passage_embeddings::PassagePriority priority,
      std::vector<std::string> passages,
      ComputePassagesEmbeddingsCallback callback) override;

  // Executes the pending embeddings computation. This will run the stored
  // callback and block until the entire asynchronous flow is complete.
  // This is necessary because the passage_embeddings::TestEmbedder uses a
  // task_runner internally to simulate an async model execution.
  void ReleaseCallback();

 private:
  // A wrapper for the `ComputePassagesEmbeddingsCallback`. This is invoked when
  // the underlying `PassageEmbedderMock` completes its computation. It forwards
  // the results to the original callback and quits the run loop to unblock the
  // test execution that called `ReleaseCallback`.
  void ComputePassageEmbeddingsCallbackWrapper(
      std::vector<std::string> passages,
      std::vector<passage_embeddings::Embedding> embeddings,
      TaskId task_id,
      passage_embeddings::ComputeEmbeddingsStatus status);

  // An internal helper method that is bound as a callback and executed when
  // `ReleaseCallback` is called. It initiates the fake computation by calling
  // the base class's `ComputePassagesEmbeddings`.
  void OnCallbackReleased(passage_embeddings::PassagePriority priority,
                          std::vector<std::string> passages,
                          ComputePassagesEmbeddingsCallback callback);

  // Stores the pending computation, which is a call to `OnCallbackReleased`
  // with all the necessary parameters. It is set in
  // `ComputePassagesEmbeddings` and run in `ReleaseCallback`.
  base::OnceCallback<void()> execution_callback_;

  // Stores the original callback from the `ComputePassagesEmbeddings` call, so
  // it can be invoked later when the computation is unblocked.
  ComputePassagesEmbeddingsCallback compute_embeddings_callback_;

  // A run loop used to make the asynchronous execution behave synchronously for
  // tests. `ReleaseCallback` runs this loop, and
  // `ComputePassageEmbeddingsCallbackWrapper` quits it.
  base::RunLoop model_execute_run_loop_for_testing_;

  // Used for creating the execution_callback_.
  base::WeakPtrFactory<DelayedPassageEmbedderMock> weak_ptr_factory_{this};
};

}  // namespace test

#endif  // CHROME_BROWSER_PERMISSIONS_TEST_MOCK_PASSAGE_EMBEDDER_H_