Skip to content

Commit ea46885

Browse files
committed
Add a test to validate locking
Test that DeepSleepLock and Timer lock deep sleep at the correct time and do no leave sleep locked after they are destroted.
1 parent f0ac234 commit ea46885

File tree

1 file changed

+132
-0
lines changed

1 file changed

+132
-0
lines changed
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
2+
/* mbed Microcontroller Library
3+
* Copyright (c) 2017 ARM Limited
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#if !DEVICE_SLEEP
19+
#error [NOT_SUPPORTED] Sleep not supported for this target
20+
#endif
21+
22+
#include "utest/utest.h"
23+
#include "unity/unity.h"
24+
#include "greentea-client/test_env.h"
25+
26+
#include "mbed.h"
27+
28+
using namespace utest::v1;
29+
30+
void deep_sleep_lock_lock_test()
31+
{
32+
TEST_ASSERT_EQUAL(true, sleep_manager_can_deep_sleep());
33+
{
34+
// Check basic usage works
35+
DeepSleepLock lock;
36+
TEST_ASSERT_EQUAL(false, sleep_manager_can_deep_sleep());
37+
}
38+
39+
TEST_ASSERT_EQUAL(true, sleep_manager_can_deep_sleep());
40+
{
41+
// Check that unlock and lock change can deep sleep as expected
42+
DeepSleepLock lock;
43+
TEST_ASSERT_EQUAL(false, sleep_manager_can_deep_sleep());
44+
lock.unlock();
45+
TEST_ASSERT_EQUAL(true, sleep_manager_can_deep_sleep());
46+
lock.lock();
47+
TEST_ASSERT_EQUAL(false, sleep_manager_can_deep_sleep());
48+
}
49+
50+
TEST_ASSERT_EQUAL(true, sleep_manager_can_deep_sleep());
51+
{
52+
// Check that unlock releases sleep based on count
53+
DeepSleepLock lock;
54+
lock.lock();
55+
lock.lock();
56+
lock.unlock();
57+
TEST_ASSERT_EQUAL(false, sleep_manager_can_deep_sleep());
58+
}
59+
60+
TEST_ASSERT_EQUAL(true, sleep_manager_can_deep_sleep());
61+
{
62+
// Check that unbalanced locks do not leave deep sleep locked
63+
DeepSleepLock lock;
64+
lock.lock();
65+
TEST_ASSERT_EQUAL(false, sleep_manager_can_deep_sleep());
66+
}
67+
TEST_ASSERT_EQUAL(true, sleep_manager_can_deep_sleep());
68+
69+
}
70+
71+
void timer_lock_test()
72+
{
73+
TEST_ASSERT_EQUAL(true, sleep_manager_can_deep_sleep());
74+
{
75+
// Just creating a timer object does not lock sleep
76+
Timer timer;
77+
TEST_ASSERT_EQUAL(true, sleep_manager_can_deep_sleep());
78+
}
79+
80+
TEST_ASSERT_EQUAL(true, sleep_manager_can_deep_sleep());
81+
{
82+
// Starting a timer does lock sleep
83+
Timer timer;
84+
timer.start();
85+
TEST_ASSERT_EQUAL(false, sleep_manager_can_deep_sleep());
86+
}
87+
88+
TEST_ASSERT_EQUAL(true, sleep_manager_can_deep_sleep());
89+
{
90+
// Stopping a timer after starting it allows sleep
91+
Timer timer;
92+
timer.start();
93+
timer.stop();
94+
TEST_ASSERT_EQUAL(true, sleep_manager_can_deep_sleep());
95+
}
96+
97+
TEST_ASSERT_EQUAL(true, sleep_manager_can_deep_sleep());
98+
{
99+
// Starting a timer multiple times still lets you sleep
100+
Timer timer;
101+
timer.start();
102+
timer.start();
103+
}
104+
TEST_ASSERT_EQUAL(true, sleep_manager_can_deep_sleep());
105+
106+
TEST_ASSERT_EQUAL(true, sleep_manager_can_deep_sleep());
107+
{
108+
// Stopping a timer multiple times still lets you sleep
109+
Timer timer;
110+
timer.start();
111+
timer.stop();
112+
timer.stop();
113+
TEST_ASSERT_EQUAL(true, sleep_manager_can_deep_sleep());
114+
}
115+
TEST_ASSERT_EQUAL(true, sleep_manager_can_deep_sleep());
116+
}
117+
118+
Case cases[] = {
119+
Case("DeepSleepLock lock test", deep_sleep_lock_lock_test),
120+
Case("timer lock test", timer_lock_test),
121+
};
122+
123+
utest::v1::status_t greentea_test_setup(const size_t number_of_cases) {
124+
GREENTEA_SETUP(20, "default_auto");
125+
return greentea_test_setup_handler(number_of_cases);
126+
}
127+
128+
Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler);
129+
130+
int main() {
131+
Harness::run(specification);
132+
}

0 commit comments

Comments
 (0)