Skip to content

Commit

Permalink
[ozone] Add clipboard testing support.
Browse files Browse the repository at this point in the history
Mock out clipboard delegate and provide a simple mocked
system clipboard implementation to test clipboard_ozone

Change-Id: I4d65e1496930cb14a32396be974e8f84353288d0
  • Loading branch information
msisov committed Dec 3, 2018
1 parent 5a815d7 commit 087ad68
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
4 changes: 4 additions & 0 deletions chrome/test/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -4795,6 +4795,10 @@ if (!is_android) {
if (is_linux) {
data += [ "$root_out_dir/libppapi_tests.so" ]
}

if (use_ozone) {
sources += [ "//ui/base/clipboard/mock_clipboard_delegate.h", ]
}

defines = [ "HAS_OUT_OF_PROC_TEST_RUNNER" ]
ldflags = []
Expand Down
16 changes: 15 additions & 1 deletion ui/base/clipboard/clipboard_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
#include "ui/events/platform/platform_event_source.h"
#endif

#if defined(USE_OZONE)
#include "ui/base/clipboard/mock_clipboard_delegate.h"

ui::ClipboardDelegate* g_clipboard_delegate_ = nullptr;
#endif

namespace ui {

struct PlatformClipboardTraits {
Expand All @@ -19,7 +25,15 @@ struct PlatformClipboardTraits {
}
#endif

static Clipboard* Create() { return Clipboard::GetForCurrentThread(); }
static Clipboard* Create() {
auto clipboard = Clipboard::GetForCurrentThread();
#if defined(USE_OZONE)
DCHECK(!g_clipboard_delegate_);
g_clipboard_delegate_ = new MockClipboardDelegate();
clipboard->SetDelegate(g_clipboard_delegate_);
#endif
return clipboard;
}

static void Destroy(Clipboard* clipboard) {
ASSERT_EQ(Clipboard::GetForCurrentThread(), clipboard);
Expand Down
52 changes: 52 additions & 0 deletions ui/base/clipboard/mock_clipboard_delegate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2018 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 UI_BASE_CLIPBOARD_MOCK_CLIPBOARD_DELEGATE_
#define UI_BASE_CLIPBOARD_MOCK_CLIPBOARD_DELEGATE_

#include "base/callback.h"
#include "ui/ozone/public/clipboard_delegate.h"

namespace ui {

// Delegate class, which mocks out system clipboard.
// TODO(msisov): move the declaration details to .cc file.
class MockClipboardDelegate : public ClipboardDelegate {
public:
MockClipboardDelegate() = default;
~MockClipboardDelegate() = default;

void OfferClipboardData(const DataMap& data_map,
OfferDataClosure callback) override {
offered_data_map_ = data_map;
std::move(callback).Run();
}

void RequestClipboardData(const std::string& mime_type,
DataMap* data_map,
RequestDataClosure callback) override {
auto it = offered_data_map_.find(mime_type);
DCHECK(it != offered_data_map_.end());
*data_map = offered_data_map_;
std::move(callback).Run(std::move(it->second));
}

void GetAvailableMimeTypes(GetMimeTypesClosure callback) override {
std::vector<std::string> mime_types;
for (const auto& item : offered_data_map_) {
mime_types.push_back(item.first);
}
std::move(callback).Run(std::move(mime_types));
}

bool IsSelectionOwner() override {
return !offered_data_map_.empty();
}
private:
ClipboardDelegate::DataMap offered_data_map_;
};

} // namespace ui

#endif // UI_OZONE_PUBLIC_CLIPBOARD_DELEGATE_H_

0 comments on commit 087ad68

Please sign in to comment.