Commit 1a05bcff authored by Zach Trudo's avatar Zach Trudo Committed by Commit Bot

Create MockReportQueue for testing.

Bug: chromium:1078512
Change-Id: Ibbb9a3e2a85976bbbc136a053eb10e245a166f61
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2432853
Commit-Queue: Zach Trudo <zatrudo@google.com>
Reviewed-by: default avatarLeonid Baraz <lbaraz@chromium.org>
Cr-Commit-Position: refs/heads/master@{#812511}
parent 9c742f3f
......@@ -6418,6 +6418,8 @@ static_library("test_support") {
"notifications/stub_notification_display_service.h",
"plugins/plugin_test_utils.cc",
"plugins/plugin_test_utils.h",
"policy/messaging_layer/public/mock_report_queue.cc",
"policy/messaging_layer/public/mock_report_queue.h",
"predictors/loading_test_util.cc",
"predictors/loading_test_util.h",
"reputation/safety_tip_test_utils.cc",
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/policy/messaging_layer/public/mock_report_queue.h"
namespace reporting {
MockReportQueue::MockReportQueue()
: ReportQueue(/*config=*/nullptr,
/*storage=*/nullptr) {}
MockReportQueue::~MockReportQueue() = default;
} // namespace reporting
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_POLICY_MESSAGING_LAYER_PUBLIC_MOCK_REPORT_QUEUE_H_
#define CHROME_BROWSER_POLICY_MESSAGING_LAYER_PUBLIC_MOCK_REPORT_QUEUE_H_
#include "base/callback.h"
#include "base/values.h"
#include "chrome/browser/policy/messaging_layer/public/report_queue.h"
#include "chrome/browser/policy/messaging_layer/util/status.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "third_party/protobuf/src/google/protobuf/message_lite.h"
namespace reporting {
// A mock of ReportQueue for use in testing.
class MockReportQueue : public ReportQueue {
public:
// An EnqueueCallbacks are called on the completion of any |Enqueue| call.
// using EnqueueCallback = base::OnceCallback<void(Status)>;
MockReportQueue();
~MockReportQueue() override;
Status Enqueue(base::StringPiece record,
EnqueueCallback callback) const override {
return StringPieceEnqueue_(record, std::move(callback));
}
Status Enqueue(const base::Value& record,
EnqueueCallback callback) const override {
return ValueEnqueue_(record, std::move(callback));
}
Status Enqueue(google::protobuf::MessageLite* record,
EnqueueCallback callback) const override {
return MessageLiteEnqueue_(record, std::move(callback));
}
MOCK_METHOD(Status,
StringPieceEnqueue_,
(base::StringPiece record, EnqueueCallback callback),
(const));
MOCK_METHOD(Status,
ValueEnqueue_,
(const base::Value& record, EnqueueCallback callback),
(const));
MOCK_METHOD(Status,
MessageLiteEnqueue_,
(google::protobuf::MessageLite * record,
EnqueueCallback callback),
(const));
};
} // namespace reporting
#endif // CHROME_BROWSER_POLICY_MESSAGING_LAYER_PUBLIC_MOCK_REPORT_QUEUE_H_
......@@ -51,12 +51,12 @@ ReportQueue::ReportQueue(std::unique_ptr<ReportQueueConfiguration> config,
}
Status ReportQueue::Enqueue(base::StringPiece record,
EnqueueCallback callback) {
EnqueueCallback callback) const {
return AddRecord(record, std::move(callback));
}
Status ReportQueue::Enqueue(const base::Value& record,
EnqueueCallback callback) {
EnqueueCallback callback) const {
std::string json_record;
if (!base::JSONWriter::Write(record, &json_record)) {
return Status(error::INVALID_ARGUMENT,
......@@ -66,7 +66,7 @@ Status ReportQueue::Enqueue(const base::Value& record,
}
Status ReportQueue::Enqueue(google::protobuf::MessageLite* record,
EnqueueCallback callback) {
EnqueueCallback callback) const {
std::string protobuf_record;
if (!record->SerializeToString(&protobuf_record)) {
return Status(error::INVALID_ARGUMENT,
......@@ -77,7 +77,7 @@ Status ReportQueue::Enqueue(google::protobuf::MessageLite* record,
}
Status ReportQueue::AddRecord(base::StringPiece record,
EnqueueCallback callback) {
EnqueueCallback callback) const {
RETURN_IF_ERROR(config_->CheckPolicy());
if (!sequenced_task_runner_->PostTask(
FROM_HERE, base::BindOnce(&ReportQueue::SendRecordToStorage,
......@@ -89,12 +89,12 @@ Status ReportQueue::AddRecord(base::StringPiece record,
}
void ReportQueue::SendRecordToStorage(base::StringPiece record_data,
EnqueueCallback callback) {
EnqueueCallback callback) const {
storage_->AddRecord(config_->priority(), AugmentRecord(record_data),
std::move(callback));
}
Record ReportQueue::AugmentRecord(base::StringPiece record_data) {
Record ReportQueue::AugmentRecord(base::StringPiece record_data) const {
Record record;
record.set_data(std::string(record_data));
record.set_destination(config_->destination());
......
......@@ -46,7 +46,7 @@ class ReportQueue {
std::unique_ptr<ReportQueueConfiguration> config,
scoped_refptr<StorageModule> storage);
~ReportQueue();
virtual ~ReportQueue();
ReportQueue(const ReportQueue& other) = delete;
ReportQueue& operator=(const ReportQueue& other) = delete;
......@@ -60,25 +60,29 @@ class ReportQueue {
// UPLOAD_EVENTS : UploadEventsRequest
//
// |record| will be sent as a string with no conversion.
Status Enqueue(base::StringPiece record, EnqueueCallback callback);
virtual Status Enqueue(base::StringPiece record,
EnqueueCallback callback) const;
// |record| will be converted to a JSON string with base::JsonWriter::Write.
Status Enqueue(const base::Value& record, EnqueueCallback callback);
virtual Status Enqueue(const base::Value& record,
EnqueueCallback callback) const;
// |record| will be converted to a string with SerializeToString(). The
// handler is responsible for converting the record back to a proto with a
// ParseFromString() call.
Status Enqueue(google::protobuf::MessageLite* record,
EnqueueCallback callback);
virtual Status Enqueue(google::protobuf::MessageLite* record,
EnqueueCallback callback) const;
private:
protected:
ReportQueue(std::unique_ptr<ReportQueueConfiguration> config,
scoped_refptr<StorageModule> storage);
Status AddRecord(base::StringPiece record, EnqueueCallback callback);
void SendRecordToStorage(base::StringPiece record, EnqueueCallback callback);
private:
Status AddRecord(base::StringPiece record, EnqueueCallback callback) const;
void SendRecordToStorage(base::StringPiece record,
EnqueueCallback callback) const;
reporting::Record AugmentRecord(base::StringPiece record_data);
reporting::Record AugmentRecord(base::StringPiece record_data) const;
std::unique_ptr<ReportQueueConfiguration> config_;
scoped_refptr<StorageModule> storage_;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment