xref: /aosp_15_r20/system/nfc/tests/src/nfa_ce_main_test.cc (revision 7eba2f3b06c51ae21384f6a4f14577b668a869b3)
1 //
2 // Copyright (C) 2024 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #include <gtest/gtest.h>
18 #include <gmock/gmock.h>
19 #include <string>
20 #include "nfa_ce_main.cc"
21 
22 typedef unsigned int NFA_HANDLE;
23 
24 class MockNfcOperations {
25 public:
26     MOCK_METHOD(void, nfa_ce_restart_listen_check, (), ());
27     MOCK_METHOD(void, nfa_dm_delete_rf_discover, (NFA_HANDLE), ());
28 };
29 
30 // Tests for nfa_ce_evt_2_str
TEST(NfaCeEvtTest,EventToString)31 TEST(NfaCeEvtTest, EventToString) {
32     EXPECT_EQ(nfa_ce_evt_2_str(NFA_CE_API_CFG_LOCAL_TAG_EVT), "NFA_CE_API_CFG_LOCAL_TAG_EVT");
33     EXPECT_EQ(nfa_ce_evt_2_str(NFA_CE_API_REG_LISTEN_EVT), "NFA_CE_API_REG_LISTEN_EVT");
34     EXPECT_EQ(nfa_ce_evt_2_str(NFA_CE_API_DEREG_LISTEN_EVT), "NFA_CE_API_DEREG_LISTEN_EVT");
35     EXPECT_EQ(nfa_ce_evt_2_str(NFA_CE_API_CFG_ISODEP_TECH_EVT), "NFA_CE_API_CFG_ISODEP_TECH_EVT");
36     EXPECT_EQ(nfa_ce_evt_2_str(NFA_CE_ACTIVATE_NTF_EVT), "NFA_CE_ACTIVATE_NTF_EVT");
37     EXPECT_EQ(nfa_ce_evt_2_str(NFA_CE_DEACTIVATE_NTF_EVT), "NFA_CE_DEACTIVATE_NTF_EVT");
38     EXPECT_EQ(nfa_ce_evt_2_str(0x9999), "Unknown");
39     EXPECT_EQ(nfa_ce_evt_2_str(0x0000), "Unknown");
40     EXPECT_EQ(nfa_ce_evt_2_str(0xFFFF), "Unknown");
41     EXPECT_EQ(nfa_ce_evt_2_str(0x0100), "Unknown");
42     EXPECT_EQ(nfa_ce_evt_2_str(0x01FF), "Unknown");
43     EXPECT_EQ(nfa_ce_evt_2_str(0x1000), "Unknown");
44     EXPECT_EQ(nfa_ce_evt_2_str(0x2000), "Unknown");
45 
46 }
47 
48 // Tests for nfa_ce_proc_nfcc_power_mode
TEST(NfaCeProcNfccPowerModeTest,ProcessPowerMode)49 TEST(NfaCeProcNfccPowerModeTest, ProcessPowerMode) {
50     MockNfcOperations mock_ops;
51     nfa_ce_proc_nfcc_power_mode(NFA_DM_PWR_MODE_FULL);
52     nfa_ce_proc_nfcc_power_mode(0);
53     EXPECT_CALL(mock_ops, nfa_dm_delete_rf_discover(::testing::_)).Times(0);
54     nfa_ce_proc_nfcc_power_mode(0xFF);
55 }
56 
TEST(NfaCeProcNfccPowerModeTest,EdgeCases)57 TEST(NfaCeProcNfccPowerModeTest, EdgeCases) {
58     MockNfcOperations mock_ops;
59     EXPECT_CALL(mock_ops, nfa_dm_delete_rf_discover(::testing::_)).Times(0);
60     nfa_ce_proc_nfcc_power_mode(NFA_DM_PWR_MODE_FULL);
61     tNFA_CE_CB* p_cb = &nfa_ce_cb;
62     memset(p_cb->listen_info, 0, sizeof(p_cb->listen_info));
63     EXPECT_CALL(mock_ops, nfa_dm_delete_rf_discover(::testing::_)).Times(0);
64     nfa_ce_proc_nfcc_power_mode(0);
65     memset(p_cb->listen_info, 0xFF, sizeof(p_cb->listen_info));
66     nfa_ce_proc_nfcc_power_mode(0);
67 }
68 
TEST(NfaCeProcNfccPowerModeTest,NoListenInfo)69 TEST(NfaCeProcNfccPowerModeTest, NoListenInfo) {
70     MockNfcOperations mock_ops;
71     tNFA_CE_CB* p_cb = &nfa_ce_cb;
72     memset(p_cb->listen_info, 0, sizeof(p_cb->listen_info));
73     EXPECT_CALL(mock_ops, nfa_dm_delete_rf_discover(::testing::_)).Times(0);
74     nfa_ce_proc_nfcc_power_mode(0);
75 }
76 
TEST(NfaCeProcNfccPowerModeTest,SingleActiveListenEntry)77 TEST(NfaCeProcNfccPowerModeTest, SingleActiveListenEntry) {
78     MockNfcOperations mock_ops;
79     tNFA_CE_CB* p_cb = &nfa_ce_cb;
80     p_cb->listen_info[0].flags |= NFA_CE_LISTEN_INFO_IN_USE;
81     p_cb->listen_info[0].rf_disc_handle = 1;
82     nfa_ce_proc_nfcc_power_mode(0);
83 }
84 
TEST(NfaCeProcNfccPowerModeTest,TwoActiveListenEntries)85 TEST(NfaCeProcNfccPowerModeTest, TwoActiveListenEntries) {
86     MockNfcOperations mock_ops;
87     tNFA_CE_CB* p_cb = &nfa_ce_cb;
88     p_cb->listen_info[0].flags |= NFA_CE_LISTEN_INFO_IN_USE;
89     p_cb->listen_info[0].rf_disc_handle = 1;
90     p_cb->listen_info[1].flags |= NFA_CE_LISTEN_INFO_IN_USE;
91     p_cb->listen_info[1].rf_disc_handle = 2;
92     nfa_ce_proc_nfcc_power_mode(0);
93 }
94 
TEST(NfaCeProcNfccPowerModeTest,NoActiveListenEntries)95 TEST(NfaCeProcNfccPowerModeTest, NoActiveListenEntries) {
96     MockNfcOperations mock_ops;
97     tNFA_CE_CB* p_cb = &nfa_ce_cb;
98     memset(p_cb->listen_info, 0, sizeof(p_cb->listen_info));
99     EXPECT_CALL(mock_ops, nfa_dm_delete_rf_discover(::testing::_)).Times(0);
100     nfa_ce_proc_nfcc_power_mode(0);
101 }
102 
TEST(NfaCeProcNfccPowerModeTest,SingleDeactivatedListenEntry)103 TEST(NfaCeProcNfccPowerModeTest, SingleDeactivatedListenEntry) {
104     MockNfcOperations mock_ops;
105     tNFA_CE_CB* p_cb = &nfa_ce_cb;
106     memset(p_cb->listen_info, 0, sizeof(p_cb->listen_info));
107     p_cb->listen_info[0].flags &= ~NFA_CE_LISTEN_INFO_IN_USE;
108 
109     EXPECT_CALL(mock_ops, nfa_dm_delete_rf_discover(::testing::_)).Times(0);
110     nfa_ce_proc_nfcc_power_mode(0);
111 }
112 
TEST(NfaCeProcNfccPowerModeTest,MixedActiveAndInactiveListenEntries)113 TEST(NfaCeProcNfccPowerModeTest, MixedActiveAndInactiveListenEntries) {
114     MockNfcOperations mock_ops;
115     tNFA_CE_CB* p_cb = &nfa_ce_cb;
116     p_cb->listen_info[0].flags |= NFA_CE_LISTEN_INFO_IN_USE;
117     p_cb->listen_info[0].rf_disc_handle = 1;
118     p_cb->listen_info[1].flags &= ~NFA_CE_LISTEN_INFO_IN_USE;
119 
120     nfa_ce_proc_nfcc_power_mode(0);
121 }