• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright (C) 2025 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 "nfc_task.cc"
18 
19 #include <gmock/gmock.h>
20 #include <gtest/gtest.h>
21 
22 #include "gki.h"
23 #include "mock_gki_utils.h"
24 #include "nfc_int.h"
25 
26 class NfcTaskTest : public ::testing::Test {
SetUp()27   void SetUp() override { gki_utils = new MockGkiUtils(); }
TearDown()28   void TearDown() override {
29     delete gki_utils;
30     gki_utils = nullptr;
31   }
32 };
33 
34 // Test case 1: Empty timer list, non-NFC task
TEST_F(NfcTaskTest,EmptyListNonNfcTask)35 TEST_F(NfcTaskTest, EmptyListNonNfcTask) {
36   TIMER_LIST_ENT tle;
37   uint16_t type = 100;
38   uint32_t timeout = 5;
39   NFC_HDR* p_msg = (NFC_HDR*)malloc(NFC_HDR_SIZE);
40 
41   EXPECT_CALL(*((MockGkiUtils*)gki_utils),
42               timer_list_empty(&nfc_cb.timer_queue))
43       .WillOnce(testing::Return(true));
44   EXPECT_CALL(*((MockGkiUtils*)gki_utils), get_taskid())
45       .WillOnce(testing::Return(2));  // Not NFC_TASK
46   EXPECT_CALL(*((MockGkiUtils*)gki_utils), getbuf(NFC_HDR_SIZE))
47       .WillOnce(testing::Return(p_msg));
48   EXPECT_CALL(*((MockGkiUtils*)gki_utils),
49               send_msg(NFC_TASK, NFC_MBOX_ID, p_msg));
50   EXPECT_CALL(*((MockGkiUtils*)gki_utils),
51               remove_from_timer_list(&nfc_cb.timer_queue, &tle));
52   EXPECT_CALL(*((MockGkiUtils*)gki_utils),
53               add_to_timer_list(&nfc_cb.timer_queue, &tle));
54 
55   nfc_start_timer(&tle, type, timeout);
56 
57   ASSERT_EQ(tle.event, type);
58   ASSERT_EQ(tle.ticks, timeout);
59 
60   free(p_msg);
61 }
62 
63 // Test case 2: Empty timer list, NFC task
TEST_F(NfcTaskTest,EmptyListNfcTask)64 TEST_F(NfcTaskTest, EmptyListNfcTask) {
65   TIMER_LIST_ENT tle;
66   uint16_t type = 200;
67   uint32_t timeout = 10;
68 
69   EXPECT_CALL(*((MockGkiUtils*)gki_utils),
70               timer_list_empty(&nfc_cb.timer_queue))
71       .WillOnce(testing::Return(true));
72   EXPECT_CALL(*((MockGkiUtils*)gki_utils), get_taskid())
73       .WillOnce(testing::Return(NFC_TASK));
74   EXPECT_CALL(*((MockGkiUtils*)gki_utils),
75               start_timer(NFC_TIMER_ID, GKI_SECS_TO_TICKS(1), true));
76   EXPECT_CALL(*((MockGkiUtils*)gki_utils),
77               remove_from_timer_list(&nfc_cb.timer_queue, &tle));
78   EXPECT_CALL(*((MockGkiUtils*)gki_utils),
79               add_to_timer_list(&nfc_cb.timer_queue, &tle));
80 
81   nfc_start_timer(&tle, type, timeout);
82 
83   ASSERT_EQ(tle.event, type);
84   ASSERT_EQ(tle.ticks, timeout);
85 }
86 
87 // Test case 3: Non-empty timer list
TEST_F(NfcTaskTest,NonEmptyList)88 TEST_F(NfcTaskTest, NonEmptyList) {
89   TIMER_LIST_ENT tle;
90   uint16_t type = 300;
91   uint32_t timeout = 15;
92 
93   EXPECT_CALL(*((MockGkiUtils*)gki_utils),
94               timer_list_empty(&nfc_cb.timer_queue))
95       .WillOnce(testing::Return(false));
96   EXPECT_CALL(*((MockGkiUtils*)gki_utils),
97               remove_from_timer_list(&nfc_cb.timer_queue, &tle));
98   EXPECT_CALL(*((MockGkiUtils*)gki_utils),
99               add_to_timer_list(&nfc_cb.timer_queue, &tle));
100 
101   nfc_start_timer(&tle, type, timeout);
102 
103   ASSERT_EQ(tle.event, type);
104   ASSERT_EQ(tle.ticks, timeout);
105 }
106 
TEST_F(NfcTaskTest,TestStopQuickTimer)107 TEST_F(NfcTaskTest, TestStopQuickTimer) {
108   TIMER_LIST_ENT* mock_p_tle = new TIMER_LIST_ENT();
109   EXPECT_CALL(*((MockGkiUtils*)gki_utils),
110               remove_from_timer_list(testing::_, testing::_))
111       .Times(1);
112   EXPECT_CALL(*((MockGkiUtils*)gki_utils), stop_timer(testing::_)).Times(1);
113   nfc_stop_quick_timer(mock_p_tle);
114 }
115 
116 // Test case 1: Empty quick timer list, non-NFC task
TEST_F(NfcTaskTest,StartQuickTimerEmptyListNonNfcTask)117 TEST_F(NfcTaskTest, StartQuickTimerEmptyListNonNfcTask) {
118   TIMER_LIST_ENT tle;
119   uint16_t type = 100;
120   uint32_t timeout = 5;
121   NFC_HDR* p_msg = (NFC_HDR*)malloc(NFC_HDR_SIZE);
122 
123   EXPECT_CALL(*((MockGkiUtils*)gki_utils),
124               timer_list_empty(&nfc_cb.quick_timer_queue))
125       .WillOnce(testing::Return(true));
126   EXPECT_CALL(*((MockGkiUtils*)gki_utils), get_taskid())
127       .WillOnce(testing::Return(2));  // Not NFC_TASK
128   EXPECT_CALL(*((MockGkiUtils*)gki_utils), getbuf(NFC_HDR_SIZE))
129       .WillOnce(testing::Return(p_msg));
130   EXPECT_CALL(*((MockGkiUtils*)gki_utils),
131               send_msg(NFC_TASK, NFC_MBOX_ID, p_msg));
132   EXPECT_CALL(*((MockGkiUtils*)gki_utils),
133               remove_from_timer_list(&nfc_cb.quick_timer_queue, &tle));
134   EXPECT_CALL(*((MockGkiUtils*)gki_utils),
135               add_to_timer_list(&nfc_cb.quick_timer_queue, &tle));
136 
137   nfc_start_quick_timer(&tle, type, timeout);
138 
139   ASSERT_EQ(tle.event, type);
140   ASSERT_EQ(tle.ticks, timeout);
141 
142   free(p_msg);
143 }
144 
145 // Test case 2: Empty quick timer list, NFC task
TEST_F(NfcTaskTest,StartQuickTimerEmptyListNfcTask)146 TEST_F(NfcTaskTest, StartQuickTimerEmptyListNfcTask) {
147   TIMER_LIST_ENT tle;
148   uint16_t type = 200;
149   uint32_t timeout = 10;
150 
151   EXPECT_CALL(*((MockGkiUtils*)gki_utils),
152               timer_list_empty(&nfc_cb.quick_timer_queue))
153       .WillOnce(testing::Return(true));
154   EXPECT_CALL(*((MockGkiUtils*)gki_utils), get_taskid())
155       .WillOnce(testing::Return(NFC_TASK));
156   EXPECT_CALL(*((MockGkiUtils*)gki_utils),
157               start_timer(NFC_QUICK_TIMER_ID, testing::_, true));
158   EXPECT_CALL(*((MockGkiUtils*)gki_utils),
159               remove_from_timer_list(&nfc_cb.quick_timer_queue, &tle));
160   EXPECT_CALL(*((MockGkiUtils*)gki_utils),
161               add_to_timer_list(&nfc_cb.quick_timer_queue, &tle));
162 
163   nfc_start_quick_timer(&tle, type, timeout);
164 
165   ASSERT_EQ(tle.event, type);
166   ASSERT_EQ(tle.ticks, timeout);
167 }
168 
169 // Test case 3: Non-empty quick timer list
TEST_F(NfcTaskTest,StartQuickTimerNonEmptyList)170 TEST_F(NfcTaskTest, StartQuickTimerNonEmptyList) {
171   TIMER_LIST_ENT tle;
172   uint16_t type = 300;
173   uint32_t timeout = 15;
174 
175   EXPECT_CALL(*((MockGkiUtils*)gki_utils),
176               timer_list_empty(&nfc_cb.quick_timer_queue))
177       .WillOnce(testing::Return(false));
178   EXPECT_CALL(*((MockGkiUtils*)gki_utils),
179               remove_from_timer_list(&nfc_cb.quick_timer_queue, &tle));
180   EXPECT_CALL(*((MockGkiUtils*)gki_utils),
181               add_to_timer_list(&nfc_cb.quick_timer_queue, &tle));
182 
183   nfc_start_quick_timer(&tle, type, timeout);
184 
185   ASSERT_EQ(tle.event, type);
186   ASSERT_EQ(tle.ticks, timeout);
187 }
188