Skip to content

Commit 64e8363

Browse files
Teppo Järvelinadbridge
authored andcommitted
Cellular: added more unit tests for CellularDevice.
1 parent d5a6baf commit 64e8363

File tree

5 files changed

+128
-1
lines changed

5 files changed

+128
-1
lines changed

features/cellular/UNITTESTS/at/at_cellulardevice/at_cellulardevicetest.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,17 @@ TEST(AT_CellularDevice, test_AT_CellularDevice_close_information)
9797
unit->test_AT_CellularDevice_close_information();
9898
}
9999

100+
TEST(AT_CellularDevice, test_AT_CellularDevice_set_timeout)
101+
{
102+
unit->test_AT_CellularDevice_set_timeout();
103+
}
104+
105+
TEST(AT_CellularDevice, test_AT_CellularDevice_modem_debug_on)
106+
{
107+
unit->test_AT_CellularDevice_modem_debug_on();
108+
}
109+
110+
TEST(AT_CellularDevice, test_AT_CellularDevice_get_stack)
111+
{
112+
unit->test_AT_CellularDevice_get_stack();
113+
}

features/cellular/UNITTESTS/at/at_cellulardevice/test_at_cellulardevice.cpp

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,22 +112,56 @@ void Test_AT_CellularDevice::test_AT_CellularDevice_open_information()
112112

113113
void Test_AT_CellularDevice::test_AT_CellularDevice_close_network()
114114
{
115+
EventQueue que;
116+
AT_CellularDevice dev(que);
117+
FileHandle_stub fh1;
118+
ATHandler_stub::ref_count = 0;
115119

120+
CHECK(dev.open_network(&fh1));
121+
CHECK(ATHandler_stub::ref_count == 1);
122+
123+
dev.close_network();
116124
}
117125

118126
void Test_AT_CellularDevice::test_AT_CellularDevice_close_sms()
119127
{
128+
EventQueue que;
129+
AT_CellularDevice dev(que);
130+
FileHandle_stub fh1;
131+
ATHandler_stub::ref_count = 0;
132+
133+
CHECK(dev.open_sms(&fh1));
134+
CHECK(ATHandler_stub::ref_count == 1);
120135

136+
dev.close_sms();
121137
}
122138

123139
void Test_AT_CellularDevice::test_AT_CellularDevice_close_power()
124140
{
141+
EventQueue que;
142+
AT_CellularDevice dev(que);
143+
FileHandle_stub fh1;
144+
ATHandler_stub::ref_count = 0;
125145

146+
CHECK(dev.open_power(&fh1));
147+
CHECK(ATHandler_stub::ref_count == 1);
148+
149+
dev.close_power();
126150
}
127151

128152
void Test_AT_CellularDevice::test_AT_CellularDevice_close_sim()
129153
{
154+
EventQueue que;
155+
AT_CellularDevice dev(que);
156+
FileHandle_stub fh1;
157+
ATHandler_stub::ref_count = 0;
158+
159+
160+
CHECK(dev.open_sim(&fh1));
161+
dev.close_sms(); // this should not affect to refcount as it's not opened
162+
CHECK(ATHandler_stub::ref_count == 1);
130163

164+
dev.close_sim();
131165
}
132166

133167
void Test_AT_CellularDevice::test_AT_CellularDevice_close_information()
@@ -155,3 +189,61 @@ void Test_AT_CellularDevice::test_AT_CellularDevice_close_information()
155189
ATHandler_stub::fh_value = NULL;
156190
}
157191

192+
void Test_AT_CellularDevice::test_AT_CellularDevice_set_timeout()
193+
{
194+
EventQueue que;
195+
AT_CellularDevice dev(que);
196+
FileHandle_stub fh1;
197+
ATHandler_stub::timeout = 0;
198+
ATHandler_stub::default_timeout = false;
199+
200+
// no interfaces open so settings timeout should not change anything
201+
dev.set_timeout(5000);
202+
CHECK(ATHandler_stub::timeout == 0);
203+
CHECK(ATHandler_stub::default_timeout == false);
204+
205+
CHECK(dev.open_sim(&fh1));
206+
CHECK(ATHandler_stub::ref_count == 1);
207+
208+
dev.set_timeout(5000);
209+
CHECK(ATHandler_stub::timeout == 5000);
210+
CHECK(ATHandler_stub::default_timeout == true);
211+
212+
dev.close_sim();
213+
}
214+
215+
void Test_AT_CellularDevice::test_AT_CellularDevice_modem_debug_on()
216+
{
217+
EventQueue que;
218+
AT_CellularDevice dev(que);
219+
FileHandle_stub fh1;
220+
ATHandler_stub::debug_on = false;
221+
222+
// no interfaces open so debug toggling should not affect
223+
dev.modem_debug_on(true);
224+
CHECK(ATHandler_stub::debug_on == false);
225+
226+
CHECK(dev.open_sim(&fh1));
227+
CHECK(ATHandler_stub::ref_count == 1);
228+
229+
dev.modem_debug_on(true);
230+
CHECK(ATHandler_stub::debug_on == true);
231+
232+
dev.close_sim();
233+
}
234+
235+
void Test_AT_CellularDevice::test_AT_CellularDevice_get_stack()
236+
{
237+
EventQueue que;
238+
AT_CellularDevice dev(que);
239+
FileHandle_stub fh1;
240+
241+
NetworkStack *stack = dev.get_stack();
242+
CHECK(stack == NULL);
243+
244+
CHECK(dev.open_network(&fh1));
245+
246+
stack = dev.get_stack();
247+
CHECK(stack == NULL); // Not in PPP so also null but this is got from the network class
248+
}
249+

features/cellular/UNITTESTS/at/at_cellulardevice/test_at_cellulardevice.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ class Test_AT_CellularDevice
4747
void test_AT_CellularDevice_close_sim();
4848

4949
void test_AT_CellularDevice_close_information();
50+
51+
void test_AT_CellularDevice_set_timeout();
52+
53+
void test_AT_CellularDevice_modem_debug_on();
54+
55+
void test_AT_CellularDevice_get_stack();
5056
};
5157

5258
#endif // TEST_AT_CELLULARDEVICE_H

features/cellular/UNITTESTS/stubs/ATHandler_stub.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ const int DEFAULT_AT_TIMEOUT = 1000; // at default timeout in milliseconds
3131
nsapi_error_t ATHandler_stub::nsapi_error_value = 0;
3232
uint8_t ATHandler_stub::nsapi_error_ok_counter = 0;
3333
int ATHandler_stub::int_value = -1;
34+
int ATHandler_stub::ref_count = 0;
35+
int ATHandler_stub::timeout = 0;
36+
bool ATHandler_stub::default_timeout = 0;
37+
bool ATHandler_stub::debug_on = 0;
3438
ssize_t ATHandler_stub::ssize_value = 0;
3539
char* ATHandler_stub::read_string_value = NULL;
3640
size_t ATHandler_stub::size_value = 0;
@@ -47,27 +51,32 @@ ATHandler::ATHandler(FileHandle *fh, EventQueue &queue, int timeout, const char
4751
_fileHandle(fh),
4852
_queue(queue)
4953
{
54+
ATHandler_stub::ref_count = 1;
5055
}
5156

5257
void ATHandler::enable_debug(bool enable)
5358
{
59+
ATHandler_stub::debug_on = enable;
5460
}
5561

5662
ATHandler::~ATHandler()
5763
{
64+
ATHandler_stub::ref_count = -909;
5865
}
5966

6067
void ATHandler::inc_ref_count()
6168
{
69+
ATHandler_stub::ref_count++;
6270
}
6371

6472
void ATHandler::dec_ref_count()
6573
{
74+
ATHandler_stub::ref_count--;
6675
}
6776

6877
int ATHandler::get_ref_count()
6978
{
70-
return ATHandler_stub::int_value;
79+
return ATHandler_stub::ref_count;
7180
}
7281

7382
FileHandle *ATHandler::get_file_handle()
@@ -113,6 +122,8 @@ nsapi_error_t ATHandler::unlock_return_error()
113122

114123
void ATHandler::set_at_timeout(uint32_t timeout_milliseconds, bool default_timeout)
115124
{
125+
ATHandler_stub::timeout = timeout_milliseconds;
126+
ATHandler_stub::default_timeout = default_timeout;
116127
}
117128

118129
void ATHandler::restore_at_timeout()

features/cellular/UNITTESTS/stubs/ATHandler_stub.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ namespace ATHandler_stub {
2828
extern nsapi_error_t nsapi_error_value;
2929
extern uint8_t nsapi_error_ok_counter;
3030
extern int int_value;
31+
extern int ref_count;
32+
extern int timeout;
33+
extern bool default_timeout;
34+
extern bool debug_on;
3135
extern ssize_t ssize_value;
3236
extern char* read_string_value;
3337
extern size_t size_value;

0 commit comments

Comments
 (0)