1// Copyright (c) 2012 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5#include "remoting/protocol/clipboard_echo_filter.h" 6 7#include "remoting/proto/event.pb.h" 8#include "remoting/protocol/protocol_mock_objects.h" 9#include "testing/gmock/include/gmock/gmock.h" 10#include "testing/gtest/include/gtest/gtest.h" 11 12using ::testing::_; 13using ::testing::InSequence; 14 15namespace remoting { 16namespace protocol { 17 18MATCHER_P2(EqualsClipboardEvent, mime_type, data, "") { 19 return arg.mime_type() == mime_type && arg.data() == data; 20} 21 22static ClipboardEvent MakeClipboardEvent(const std::string& mime_type, 23 const std::string& data) { 24 ClipboardEvent event; 25 event.set_mime_type(mime_type); 26 event.set_data(data); 27 return event; 28} 29 30// Check that the filter only filters out events identical to the latest 31// clipboard item from the client. 32TEST(ClipboardEchoFilterTest, FromClientBlocksIdenticalEventToClient) { 33 MockClipboardStub client_stub; 34 MockClipboardStub host_stub; 35 36 { 37 InSequence s; 38 EXPECT_CALL(host_stub, 39 InjectClipboardEvent(EqualsClipboardEvent("text", "a"))); 40 EXPECT_CALL(host_stub, 41 InjectClipboardEvent(EqualsClipboardEvent("text", "b"))); 42 EXPECT_CALL(client_stub, 43 InjectClipboardEvent(EqualsClipboardEvent("text", "a"))); 44 EXPECT_CALL(host_stub, 45 InjectClipboardEvent(EqualsClipboardEvent("image", "a"))); 46 EXPECT_CALL(client_stub, 47 InjectClipboardEvent(EqualsClipboardEvent("text", "a"))); 48 } 49 50 ClipboardEchoFilter filter; 51 filter.set_client_stub(&client_stub); 52 filter.set_host_stub(&host_stub); 53 54 filter.host_filter()->InjectClipboardEvent( 55 MakeClipboardEvent("text", "a")); 56 // The client has sent ("text", "a") to the host, so make sure the filter 57 // will stop the host echoing that item back to the client. 58 filter.client_filter()->InjectClipboardEvent( 59 MakeClipboardEvent("text", "a")); 60 filter.host_filter()->InjectClipboardEvent( 61 MakeClipboardEvent("text", "b")); 62 filter.client_filter()->InjectClipboardEvent( 63 MakeClipboardEvent("text", "a")); 64 filter.host_filter()->InjectClipboardEvent( 65 MakeClipboardEvent("image", "a")); 66 filter.client_filter()->InjectClipboardEvent( 67 MakeClipboardEvent("text", "a")); 68} 69 70// Check that the filter will drop events sent to the host, if there is no host 71// stub, whether or not there is a client stub. 72TEST(ClipboardEchoFilterTest, NoHostStub) { 73 MockClipboardStub client_stub; 74 MockClipboardStub host_stub; 75 76 EXPECT_CALL(host_stub, 77 InjectClipboardEvent(EqualsClipboardEvent("text", "a"))); 78 79 ClipboardEchoFilter filter; 80 ClipboardEvent event = MakeClipboardEvent("text", "a"); 81 filter.host_filter()->InjectClipboardEvent(event); 82 83 filter.set_client_stub(&client_stub); 84 filter.host_filter()->InjectClipboardEvent(event); 85 86 filter.set_host_stub(&host_stub); 87 filter.host_filter()->InjectClipboardEvent(event); 88} 89 90// Check that the filter will drop events sent to the client, if there is no 91// client stub, whether or not there is a host stub. 92TEST(ClipboardEchoFilter, NoClientStub) { 93 MockClipboardStub client_stub; 94 MockClipboardStub host_stub; 95 96 EXPECT_CALL(client_stub, 97 InjectClipboardEvent(EqualsClipboardEvent("text", "a"))); 98 99 ClipboardEchoFilter filter; 100 ClipboardEvent event = MakeClipboardEvent("text", "a"); 101 filter.client_filter()->InjectClipboardEvent(event); 102 103 filter.set_host_stub(&host_stub); 104 filter.client_filter()->InjectClipboardEvent(event); 105 106 filter.set_client_stub(&client_stub); 107 filter.client_filter()->InjectClipboardEvent(event); 108} 109 110} // namespace protocol 111} // namespace remoting 112