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
|
// Copyright 2014 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 "device/hid/hid_device_filter.h"
#include "device/hid/hid_device_info.h"
#include "device/hid/test_report_descriptors.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace device {
namespace {
#if defined(OS_MACOSX)
const uint64_t kTestDeviceId = 42;
#else
const char* kTestDeviceId = "device1";
#endif
} // namespace
class HidFilterTest : public testing::Test {
public:
void SetUp() override {
device_info_ = new HidDeviceInfo(
kTestDeviceId, 0x046d, 0xc31c, "Test Keyboard", "123ABC",
kHIDBusTypeUSB,
std::vector<uint8>(kKeyboard, kKeyboard + kKeyboardSize));
}
protected:
scoped_refptr<HidDeviceInfo> device_info_;
};
TEST_F(HidFilterTest, MatchAny) {
HidDeviceFilter filter;
ASSERT_TRUE(filter.Matches(device_info_));
}
TEST_F(HidFilterTest, MatchVendorId) {
HidDeviceFilter filter;
filter.SetVendorId(0x046d);
ASSERT_TRUE(filter.Matches(device_info_));
}
TEST_F(HidFilterTest, MatchVendorIdNegative) {
HidDeviceFilter filter;
filter.SetVendorId(0x18d1);
ASSERT_FALSE(filter.Matches(device_info_));
}
TEST_F(HidFilterTest, MatchProductId) {
HidDeviceFilter filter;
filter.SetVendorId(0x046d);
filter.SetProductId(0xc31c);
ASSERT_TRUE(filter.Matches(device_info_));
}
TEST_F(HidFilterTest, MatchProductIdNegative) {
HidDeviceFilter filter;
filter.SetVendorId(0x046d);
filter.SetProductId(0x0801);
ASSERT_FALSE(filter.Matches(device_info_));
}
TEST_F(HidFilterTest, MatchUsagePage) {
HidDeviceFilter filter;
filter.SetUsagePage(HidUsageAndPage::kPageGenericDesktop);
ASSERT_TRUE(filter.Matches(device_info_));
}
TEST_F(HidFilterTest, MatchUsagePageNegative) {
HidDeviceFilter filter;
filter.SetUsagePage(HidUsageAndPage::kPageLed);
ASSERT_FALSE(filter.Matches(device_info_));
}
TEST_F(HidFilterTest, MatchVendorAndUsagePage) {
HidDeviceFilter filter;
filter.SetVendorId(0x046d);
filter.SetUsagePage(HidUsageAndPage::kPageGenericDesktop);
ASSERT_TRUE(filter.Matches(device_info_));
}
TEST_F(HidFilterTest, MatchUsageAndPage) {
HidDeviceFilter filter;
filter.SetUsagePage(HidUsageAndPage::kPageGenericDesktop);
filter.SetUsage(HidUsageAndPage::kGenericDesktopKeyboard);
ASSERT_TRUE(filter.Matches(device_info_));
}
TEST_F(HidFilterTest, MatchUsageAndPageNegative) {
HidDeviceFilter filter;
filter.SetUsagePage(HidUsageAndPage::kPageGenericDesktop);
filter.SetUsage(0x02);
ASSERT_FALSE(filter.Matches(device_info_));
}
TEST_F(HidFilterTest, MatchEmptyFilterListNegative) {
std::vector<HidDeviceFilter> filters;
ASSERT_FALSE(HidDeviceFilter::MatchesAny(device_info_, filters));
}
TEST_F(HidFilterTest, MatchFilterList) {
std::vector<HidDeviceFilter> filters;
HidDeviceFilter filter;
filter.SetUsagePage(HidUsageAndPage::kPageGenericDesktop);
filters.push_back(filter);
ASSERT_TRUE(HidDeviceFilter::MatchesAny(device_info_, filters));
}
TEST_F(HidFilterTest, MatchFilterListNegative) {
std::vector<HidDeviceFilter> filters;
HidDeviceFilter filter;
filter.SetUsagePage(HidUsageAndPage::kPageLed);
filters.push_back(filter);
ASSERT_FALSE(HidDeviceFilter::MatchesAny(device_info_, filters));
}
} // namespace device
|