File: feedback_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 (220 lines) | stat: -rw-r--r-- 8,443 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// 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 "chromeos/ash/components/specialized_features/feedback.h"

#include <memory>
#include <string>
#include <utility>

#include "base/check.h"
#include "base/compiler_specific.h"
#include "base/files/file_path.h"
#include "base/files/scoped_temp_dir.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "base/test/task_environment.h"
#include "base/test/test_future.h"
#include "components/feedback/feedback_uploader.h"
#include "components/feedback/proto/extension.pb.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"

namespace specialized_features {
namespace {

using ::base::test::InvokeFuture;
using ::testing::_;
using ::testing::HasSubstr;
using ::testing::Not;
using ::testing::WithArg;
using ::testing::WithoutArgs;

constexpr int kFakeProductId = 1;

class MockFeedbackUploader : public feedback::FeedbackUploader {
 public:
  MockFeedbackUploader(
      const base::FilePath& state_path,
      scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory)
      : FeedbackUploader(/*is_off_the_record=*/false,
                         state_path,
                         std::move(url_loader_factory)) {}

  // feedback::FeedbackUploader:
  MOCK_METHOD(void,
              QueueReport,
              (std::unique_ptr<std::string> data,
               bool has_email,
               int product_id),
              (override));

  base::WeakPtr<FeedbackUploader> AsWeakPtr() override {
    return weak_ptr_factory_.GetWeakPtr();
  }

 private:
  base::WeakPtrFactory<MockFeedbackUploader> weak_ptr_factory_{this};
};

// A `base::ScopedTempDir` which automatically creates itself upon construction.
// Required to use `MockFeedbackUploader` in `SpecializedFeaturesFeedbackTest`.
class ScopedAutocreatingTempDir {
 public:
  ScopedAutocreatingTempDir() { CHECK(scoped_temp_dir_.CreateUniqueTempDir()); }

  const base::FilePath& GetPath() const LIFETIME_BOUND {
    return scoped_temp_dir_.GetPath();
  }

 private:
  base::ScopedTempDir scoped_temp_dir_;
};

class SpecializedFeaturesFeedbackTest : public testing::Test {
 public:
  SpecializedFeaturesFeedbackTest()
      : uploader_(
            scoped_autocreating_temp_dir_.GetPath(),
            base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>(
                &url_loader_factory_)) {}

  MockFeedbackUploader& uploader() LIFETIME_BOUND { return uploader_; }

 private:
  base::test::TaskEnvironment task_environment_;
  network::TestURLLoaderFactory url_loader_factory_;
  ScopedAutocreatingTempDir scoped_autocreating_temp_dir_;
  MockFeedbackUploader uploader_;
};

TEST_F(SpecializedFeaturesFeedbackTest, SendFeedbackUsesProductId) {
  base::test::TestFuture<void> report_future;
  EXPECT_CALL(uploader(), QueueReport(_, _, kFakeProductId))
      .WillOnce(WithoutArgs(InvokeFuture(report_future)));

  SendFeedback(uploader(), kFakeProductId, "test description");
  EXPECT_TRUE(report_future.Wait());
}

TEST_F(SpecializedFeaturesFeedbackTest, SendFeedbackSendsDescription) {
  base::test::TestFuture<std::unique_ptr<std::string>> feedback_string_future;
  EXPECT_CALL(uploader(), QueueReport)
      .WillOnce(WithArg<0>(InvokeFuture(feedback_string_future)));

  SendFeedback(uploader(), kFakeProductId, "test description");

  std::unique_ptr<std::string> feedback_string = feedback_string_future.Take();
  ASSERT_TRUE(feedback_string);
  userfeedback::ExtensionSubmit feedback_data;
  feedback_data.ParseFromString(*feedback_string);
  EXPECT_EQ(feedback_data.common_data().description(), "test description");
}

TEST_F(SpecializedFeaturesFeedbackTest, SendFeedbackRedactsDescriptionStart) {
  base::test::TestFuture<std::unique_ptr<std::string>> feedback_string_future;
  EXPECT_CALL(uploader(), QueueReport)
      .WillOnce(WithArg<0>(InvokeFuture(feedback_string_future)));

  // This test fails without two leading spaces in the description due to a
  // limitation in `feedback::RedactionTool`.
  // TODO: b/367882164 - Fix this test by either fixing
  // `feedback::RedactionTool`, or working around it.
  SendFeedback(uploader(), kFakeProductId,
               "  4242 4242 4242 4242 is my credit card number");

  std::unique_ptr<std::string> feedback_string = feedback_string_future.Take();
  ASSERT_TRUE(feedback_string);
  userfeedback::ExtensionSubmit feedback_data;
  feedback_data.ParseFromString(*feedback_string);
  EXPECT_THAT(feedback_data.common_data().description(),
              Not(HasSubstr("4242 4242 4242 4242")));
}

TEST_F(SpecializedFeaturesFeedbackTest, SendFeedbackRedactsDescriptionEnd) {
  base::test::TestFuture<std::unique_ptr<std::string>> feedback_string_future;
  EXPECT_CALL(uploader(), QueueReport)
      .WillOnce(WithArg<0>(InvokeFuture(feedback_string_future)));

  // This test fails without a trailing new line in the description due to a
  // limitation in `feedback::RedactionTool`.
  // TODO: b/367882164 - Fix this test by either fixing
  // `feedback::RedactionTool`, or working around it.
  SendFeedback(uploader(), kFakeProductId,
               "My credit card number is 4242 4242 4242 4242\n");

  std::unique_ptr<std::string> feedback_string = feedback_string_future.Take();
  ASSERT_TRUE(feedback_string);
  userfeedback::ExtensionSubmit feedback_data;
  feedback_data.ParseFromString(*feedback_string);
  EXPECT_THAT(feedback_data.common_data().description(),
              Not(HasSubstr("4242 4242 4242 4242")));
}

TEST_F(SpecializedFeaturesFeedbackTest,
       SendFeedbackDoesNotIncludeScreenshotByDefault) {
  base::test::TestFuture<std::unique_ptr<std::string>> feedback_string_future;
  EXPECT_CALL(uploader(), QueueReport)
      .WillOnce(WithArg<0>(InvokeFuture(feedback_string_future)));

  SendFeedback(uploader(), kFakeProductId, "test description");

  std::unique_ptr<std::string> feedback_string = feedback_string_future.Take();
  ASSERT_TRUE(feedback_string);
  userfeedback::ExtensionSubmit feedback_data;
  feedback_data.ParseFromString(*feedback_string);
  EXPECT_FALSE(feedback_data.screenshot().has_binary_content());
}

TEST_F(SpecializedFeaturesFeedbackTest, SendFeedbackSendsScreenshot) {
  base::test::TestFuture<std::unique_ptr<std::string>> feedback_string_future;
  EXPECT_CALL(uploader(), QueueReport)
      .WillOnce(WithArg<0>(InvokeFuture(feedback_string_future)));

  SendFeedback(uploader(), kFakeProductId, "test description",
               "screenshotbytes");

  std::unique_ptr<std::string> feedback_string = feedback_string_future.Take();
  ASSERT_TRUE(feedback_string);
  userfeedback::ExtensionSubmit feedback_data;
  feedback_data.ParseFromString(*feedback_string);
  EXPECT_EQ(feedback_data.screenshot().binary_content(), "screenshotbytes");
}

TEST_F(SpecializedFeaturesFeedbackTest,
       SendFeedbackUsesDefaultScreenshotMimeType) {
  base::test::TestFuture<std::unique_ptr<std::string>> feedback_string_future;
  EXPECT_CALL(uploader(), QueueReport)
      .WillOnce(WithArg<0>(InvokeFuture(feedback_string_future)));

  SendFeedback(uploader(), kFakeProductId, "test description",
               "screenshotbytes");

  std::unique_ptr<std::string> feedback_string = feedback_string_future.Take();
  ASSERT_TRUE(feedback_string);
  userfeedback::ExtensionSubmit feedback_data;
  feedback_data.ParseFromString(*feedback_string);
  EXPECT_EQ(feedback_data.screenshot().mime_type(), "image/png");
}

TEST_F(SpecializedFeaturesFeedbackTest, SendFeedbackSendsScreenshotMimeType) {
  base::test::TestFuture<std::unique_ptr<std::string>> feedback_string_future;
  EXPECT_CALL(uploader(), QueueReport)
      .WillOnce(WithArg<0>(InvokeFuture(feedback_string_future)));

  SendFeedback(uploader(), kFakeProductId, "test description",
               "screenshotbytes", "image/jpeg");

  std::unique_ptr<std::string> feedback_string = feedback_string_future.Take();
  ASSERT_TRUE(feedback_string);
  userfeedback::ExtensionSubmit feedback_data;
  feedback_data.ParseFromString(*feedback_string);
  EXPECT_EQ(feedback_data.screenshot().mime_type(), "image/jpeg");
}

}  // namespace
}  // namespace specialized_features