File: content_capture_test_helper.h

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,122,156 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 (169 lines) | stat: -rw-r--r-- 5,531 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
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
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_CONTENT_CAPTURE_BROWSER_CONTENT_CAPTURE_TEST_HELPER_H_
#define COMPONENTS_CONTENT_CAPTURE_BROWSER_CONTENT_CAPTURE_TEST_HELPER_H_

#include <vector>

#include "components/content_capture/browser/content_capture_consumer.h"
#include "components/content_capture/browser/content_capture_receiver.h"
#include "components/content_capture/browser/onscreen_content_provider.h"

namespace content_capture {

// Fake ContentCaptureSender to call ContentCaptureReceiver mojom interface.
class FakeContentCaptureSender {
 public:
  FakeContentCaptureSender();
  virtual ~FakeContentCaptureSender();

  void Bind(content::RenderFrameHost* frame);

  void DidCompleteBatchCaptureContent();

  void DidCaptureContent(const ContentCaptureData& captured_content,
                         bool first_data);

  void DidUpdateContent(const ContentCaptureData& captured_content);

  void DidRemoveContent(const std::vector<int64_t>& data);

 private:
  mojo::AssociatedRemote<mojom::ContentCaptureReceiver>
      content_capture_receiver_;
};

class SessionRemovedTestHelper {
 public:
  SessionRemovedTestHelper();
  virtual ~SessionRemovedTestHelper();

  void DidRemoveSession(const ContentCaptureSession& data);

  const std::vector<ContentCaptureSession>& removed_sessions() const {
    return removed_sessions_;
  }

  void Reset();

 private:
  std::vector<ContentCaptureSession> removed_sessions_;
};

// The helper class implements ContentCaptureConsumer and keeps the
// result for verification.
class ContentCaptureConsumerHelper : public ContentCaptureConsumer {
 public:
  explicit ContentCaptureConsumerHelper(
      SessionRemovedTestHelper* session_removed_test_helper);

  ~ContentCaptureConsumerHelper() override;

  // ContentCaptureConsumer
  void FlushCaptureContent(const ContentCaptureSession& session,
                           const ContentCaptureFrame& data) override;

  void DidCaptureContent(const ContentCaptureSession& parent_session,
                         const ContentCaptureFrame& data) override;

  void DidUpdateContent(const ContentCaptureSession& parent_session,
                        const ContentCaptureFrame& data) override;

  void DidRemoveContent(const ContentCaptureSession& session,
                        const std::vector<int64_t>& ids) override;

  void DidRemoveSession(const ContentCaptureSession& data) override;

  void DidUpdateTitle(const ContentCaptureFrame& main_frame) override;

  void DidUpdateFavicon(const ContentCaptureFrame& main_frame) override;

  bool ShouldCapture(const GURL& url) override;

  const ContentCaptureSession& parent_session() const {
    return parent_session_;
  }

  const ContentCaptureSession& updated_parent_session() const {
    return updated_parent_session_;
  }

  const ContentCaptureSession& session() const { return session_; }

  const ContentCaptureFrame& captured_data() const { return captured_data_; }

  const ContentCaptureFrame& updated_data() const { return updated_data_; }

  const std::vector<ContentCaptureSession>& removed_sessions() const {
    return removed_sessions_;
  }

  const std::vector<int64_t>& removed_ids() const { return removed_ids_; }
  const std::u16string& updated_title() const { return updated_title_; }

  void Reset();

 private:
  ContentCaptureSession parent_session_;
  ContentCaptureSession updated_parent_session_;
  ContentCaptureSession session_;
  ContentCaptureFrame captured_data_;
  ContentCaptureFrame updated_data_;
  std::vector<int64_t> removed_ids_;
  std::vector<ContentCaptureSession> removed_sessions_;
  raw_ptr<SessionRemovedTestHelper> session_removed_test_helper_;
  std::u16string updated_title_;
};

class ContentCaptureTestHelper {
 public:
  ContentCaptureTestHelper();
  ContentCaptureTestHelper(const ContentCaptureTestHelper&) = delete;
  ContentCaptureTestHelper& operator=(const ContentCaptureTestHelper&) = delete;
  virtual ~ContentCaptureTestHelper();

  void CreateProviderAndConsumer(
      content::WebContents* web_contents,
      SessionRemovedTestHelper* session_removed_helper = nullptr);

  void InitTestData(const std::u16string& data1, const std::u16string& data2);

  OnscreenContentProvider* onscreen_content_provider() const {
    return onscreen_content_provider_;
  }

  ContentCaptureConsumerHelper* content_capture_consumer() const {
    return content_capture_consumer_.get();
  }

  const ContentCaptureData& test_data() const { return test_data_; }
  const ContentCaptureData& test_data2() const { return test_data2_; }
  const ContentCaptureData& test_data_change() const {
    return test_data_change_;
  }
  const ContentCaptureData& test_data_update() const {
    return test_data_update_;
  }

 private:
  raw_ptr<OnscreenContentProvider, DanglingUntriaged>
      onscreen_content_provider_ = nullptr;
  std::unique_ptr<ContentCaptureConsumerHelper> content_capture_consumer_;

  ContentCaptureData test_data_;
  ContentCaptureData test_data2_;
  ContentCaptureData test_data_change_;
  ContentCaptureData test_data_update_;
};

void VerifySession(const ContentCaptureSession& expected,
                   const ContentCaptureSession& result);

ContentCaptureFrame GetExpectedTestData(const ContentCaptureData& data,
                                        int64_t expected_id);

}  // namespace content_capture

#endif  // COMPONENTS_CONTENT_CAPTURE_BROWSER_CONTENT_CAPTURE_TEST_HELPER_H_