summaryrefslogtreecommitdiffstats
path: root/remoting/protocol/clipboard_echo_filter_unittest.cc
blob: d92dc5cddb60c50bbf63d829950b9cd663c89a2c (plain)
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
// Copyright (c) 2012 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 "remoting/protocol/clipboard_echo_filter.h"

#include "remoting/proto/event.pb.h"
#include "remoting/protocol/protocol_mock_objects.h"
#include "remoting/protocol/test_event_matchers.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using ::testing::_;
using ::testing::InSequence;

namespace remoting {
namespace protocol {

using test::EqualsClipboardEvent;

static ClipboardEvent MakeClipboardEvent(const std::string& mime_type,
                                         const std::string& data) {
  ClipboardEvent event;
  event.set_mime_type(mime_type);
  event.set_data(data);
  return event;
}

// Check that the filter only filters out events identical to the latest
// clipboard item from the client.
TEST(ClipboardEchoFilterTest, FromClientBlocksIdenticalEventToClient) {
  MockClipboardStub client_stub;
  MockClipboardStub host_stub;

  {
    InSequence s;
    EXPECT_CALL(host_stub,
                InjectClipboardEvent(EqualsClipboardEvent("text", "a")));
    EXPECT_CALL(host_stub,
                InjectClipboardEvent(EqualsClipboardEvent("text", "b")));
    EXPECT_CALL(client_stub,
                InjectClipboardEvent(EqualsClipboardEvent("text", "a")));
    EXPECT_CALL(host_stub,
                InjectClipboardEvent(EqualsClipboardEvent("image", "a")));
    EXPECT_CALL(client_stub,
                InjectClipboardEvent(EqualsClipboardEvent("text", "a")));
  }

  ClipboardEchoFilter filter;
  filter.set_client_stub(&client_stub);
  filter.set_host_stub(&host_stub);

  filter.host_filter()->InjectClipboardEvent(
      MakeClipboardEvent("text", "a"));
  // The client has sent ("text", "a") to the host, so make sure the filter
  // will stop the host echoing that item back to the client.
  filter.client_filter()->InjectClipboardEvent(
      MakeClipboardEvent("text", "a"));
  filter.host_filter()->InjectClipboardEvent(
      MakeClipboardEvent("text", "b"));
  filter.client_filter()->InjectClipboardEvent(
      MakeClipboardEvent("text", "a"));
  filter.host_filter()->InjectClipboardEvent(
      MakeClipboardEvent("image", "a"));
  filter.client_filter()->InjectClipboardEvent(
      MakeClipboardEvent("text", "a"));
}

// Check that the filter will drop events sent to the host, if there is no host
// stub, whether or not there is a client stub.
TEST(ClipboardEchoFilterTest, NoHostStub) {
  MockClipboardStub client_stub;
  MockClipboardStub host_stub;

  EXPECT_CALL(host_stub,
              InjectClipboardEvent(EqualsClipboardEvent("text", "a")));

  ClipboardEchoFilter filter;
  ClipboardEvent event = MakeClipboardEvent("text", "a");
  filter.host_filter()->InjectClipboardEvent(event);

  filter.set_client_stub(&client_stub);
  filter.host_filter()->InjectClipboardEvent(event);

  filter.set_host_stub(&host_stub);
  filter.host_filter()->InjectClipboardEvent(event);
}

// Check that the filter will drop events sent to the client, if there is no
// client stub, whether or not there is a host stub.
TEST(ClipboardEchoFilter, NoClientStub) {
  MockClipboardStub client_stub;
  MockClipboardStub host_stub;

  EXPECT_CALL(client_stub,
              InjectClipboardEvent(EqualsClipboardEvent("text", "a")));

  ClipboardEchoFilter filter;
  ClipboardEvent event = MakeClipboardEvent("text", "a");
  filter.client_filter()->InjectClipboardEvent(event);

  filter.set_host_stub(&host_stub);
  filter.client_filter()->InjectClipboardEvent(event);

  filter.set_client_stub(&client_stub);
  filter.client_filter()->InjectClipboardEvent(event);
}

}  // namespace protocol
}  // namespace remoting