|
| 1 | +/* |
| 2 | + * Copyright (c) 2019 Arm Limited and affiliates. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 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 | +#include "gtest/gtest.h" |
| 19 | +#include "gmock/gmock.h" |
| 20 | +#include "drivers/PwmOut.h" |
| 21 | + |
| 22 | +using namespace mbed; |
| 23 | + |
| 24 | +class MbedPowerMgmtInterface { |
| 25 | +public: |
| 26 | + virtual ~MbedPowerMgmtInterface() {} |
| 27 | + virtual void sleep_manager_unlock_deep_sleep_internal() = 0; |
| 28 | + virtual void sleep_manager_lock_deep_sleep_internal() = 0; |
| 29 | +}; |
| 30 | + |
| 31 | + |
| 32 | +class MockMbedPowerMgmt : public MbedPowerMgmtInterface { |
| 33 | +public: |
| 34 | + virtual ~MockMbedPowerMgmt() {} |
| 35 | + MOCK_METHOD0(sleep_manager_unlock_deep_sleep_internal, void()); |
| 36 | + MOCK_METHOD0(sleep_manager_lock_deep_sleep_internal, void()); |
| 37 | + |
| 38 | + static MockMbedPowerMgmt *get_instance(bool create_instance) |
| 39 | + { |
| 40 | + if (instance == nullptr && create_instance) { |
| 41 | + instance = new MockMbedPowerMgmt(); |
| 42 | + } |
| 43 | + |
| 44 | + if (create_instance) { |
| 45 | + ++ref_counter; |
| 46 | + } |
| 47 | + |
| 48 | + return instance; |
| 49 | + } |
| 50 | + |
| 51 | + static void release_instance() |
| 52 | + { |
| 53 | + --ref_counter; |
| 54 | + |
| 55 | + if ((instance != nullptr) && (ref_counter == 0)) { |
| 56 | + delete instance; |
| 57 | + instance = nullptr; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + MockMbedPowerMgmt(MockMbedPowerMgmt const&) = delete; |
| 62 | + void operator=(MockMbedPowerMgmt const&) = delete; |
| 63 | +private: |
| 64 | + MockMbedPowerMgmt() {} |
| 65 | + |
| 66 | + static MockMbedPowerMgmt *instance; |
| 67 | + static int ref_counter; |
| 68 | +}; |
| 69 | +MockMbedPowerMgmt *MockMbedPowerMgmt::instance = nullptr; |
| 70 | +int MockMbedPowerMgmt::ref_counter = 0; |
| 71 | + |
| 72 | + |
| 73 | +void sleep_manager_unlock_deep_sleep(void) |
| 74 | +{ |
| 75 | + // Get the singleton if a test case has created one. |
| 76 | + // We do not want to increment the call count unless it's done by a test |
| 77 | + // case. |
| 78 | + MockMbedPowerMgmt *mbed_mgmt_mock = MockMbedPowerMgmt::get_instance(false); |
| 79 | + if (mbed_mgmt_mock != nullptr) { |
| 80 | + // Call only if the singleton was created by a test case. |
| 81 | + mbed_mgmt_mock->sleep_manager_unlock_deep_sleep_internal(); |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | + |
| 86 | +void sleep_manager_lock_deep_sleep(void) |
| 87 | +{ |
| 88 | + // Get the singleton if a test case has created one. |
| 89 | + // We do not want to increment the call count unless it's done by a test |
| 90 | + // case. |
| 91 | + MockMbedPowerMgmt *mbed_mgmt_mock = MockMbedPowerMgmt::get_instance(false); |
| 92 | + if (mbed_mgmt_mock != nullptr) { |
| 93 | + // Call only if the singleton was created by a test case. |
| 94 | + mbed_mgmt_mock->sleep_manager_lock_deep_sleep_internal(); |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | + |
| 99 | +class TestPwmOut : public testing::Test { |
| 100 | +protected: |
| 101 | + PwmOut *pwm_obj; |
| 102 | + MockMbedPowerMgmt *mbed_mgmt_mock; |
| 103 | + |
| 104 | + void SetUp() |
| 105 | + { |
| 106 | + pwm_obj = new PwmOut(PTC1); |
| 107 | + // Create a mock object singleton after the PwmOut object |
| 108 | + // instantiation so the sleep_manager_lock_deep_sleep call by the |
| 109 | + // constructor doesnot increment the call count. |
| 110 | + // Now it is ok because a test case is about to start. |
| 111 | + mbed_mgmt_mock = MockMbedPowerMgmt::get_instance(true); |
| 112 | + } |
| 113 | + |
| 114 | + void TearDown() |
| 115 | + { |
| 116 | + // Destroy the mock object singleton before the PwmOut destruction |
| 117 | + // because it will increment the sleep_manager_unlock_deep_sleep call |
| 118 | + // count. |
| 119 | + MockMbedPowerMgmt::release_instance(); |
| 120 | + delete pwm_obj; |
| 121 | + } |
| 122 | +}; |
| 123 | +// *INDENT-ON* |
| 124 | + |
| 125 | +/** Test if `suspend` unlocks deepsleep |
| 126 | +
|
| 127 | + Given an initialised Pmw with a deep sleep lock |
| 128 | + When the instance is suspended |
| 129 | + Then the deep sleep lock is released once |
| 130 | + */ |
| 131 | +TEST_F(TestPwmOut, test_suspend) |
| 132 | +{ |
| 133 | + using ::testing::Mock; |
| 134 | + |
| 135 | + EXPECT_CALL( |
| 136 | + *mbed_mgmt_mock, sleep_manager_unlock_deep_sleep_internal() |
| 137 | + ).Times(1); |
| 138 | + pwm_obj->suspend(); |
| 139 | + pwm_obj->suspend(); |
| 140 | + |
| 141 | + // Force gMock to verify a mock object singleton before it is destructed |
| 142 | + // by the teardown |
| 143 | + Mock::VerifyAndClearExpectations(mbed_mgmt_mock); |
| 144 | + // There will not be a leak since the destructor releases the singleton |
| 145 | + Mock::AllowLeak(mbed_mgmt_mock); |
| 146 | +} |
| 147 | + |
| 148 | +/** Test if `resume` lock deepsleep |
| 149 | +
|
| 150 | + Given an initialised Pmw in a suspended state |
| 151 | + When the instance is resumed |
| 152 | + Then the deep sleep lock is re-acquired once |
| 153 | + */ |
| 154 | +TEST_F(TestPwmOut, test_resume) |
| 155 | +{ |
| 156 | + using ::testing::Mock; |
| 157 | + |
| 158 | + EXPECT_CALL( |
| 159 | + *mbed_mgmt_mock, sleep_manager_lock_deep_sleep_internal() |
| 160 | + ).Times(1); |
| 161 | + |
| 162 | + pwm_obj->suspend(); |
| 163 | + pwm_obj->resume(); |
| 164 | + pwm_obj->resume(); |
| 165 | + |
| 166 | + // Force gMock to verify a mock object singleton before it is destructed |
| 167 | + // by the teardown |
| 168 | + Mock::VerifyAndClearExpectations(mbed_mgmt_mock); |
| 169 | + // There will not be a leak since the destructor releases the singleton |
| 170 | + Mock::AllowLeak(mbed_mgmt_mock); |
| 171 | +} |
| 172 | + |
| 173 | +/** Test if `suspend`/`resume` unlock/lock deepsleep multiple times |
| 174 | +
|
| 175 | + Given an initialised Pmw |
| 176 | + When the instance is suspended and then resumed |
| 177 | + Then the deep sleep lock can be unlocked and then locked again and again |
| 178 | + */ |
| 179 | +TEST_F(TestPwmOut, test_multiple_suspend_resume) |
| 180 | +{ |
| 181 | + using ::testing::Mock; |
| 182 | + |
| 183 | + const int suspend_resume_max_cycle = 3; |
| 184 | + |
| 185 | + EXPECT_CALL( |
| 186 | + *mbed_mgmt_mock, sleep_manager_unlock_deep_sleep_internal() |
| 187 | + ).Times(suspend_resume_max_cycle); |
| 188 | + EXPECT_CALL( |
| 189 | + *mbed_mgmt_mock, sleep_manager_lock_deep_sleep_internal() |
| 190 | + ).Times(suspend_resume_max_cycle); |
| 191 | + |
| 192 | + for (int i = 0; i < suspend_resume_max_cycle; i++) { |
| 193 | + pwm_obj->suspend(); |
| 194 | + pwm_obj->resume(); |
| 195 | + } |
| 196 | + |
| 197 | + // Force gMock to verify a mock object singleton before it is destructed |
| 198 | + // by the teardown |
| 199 | + Mock::VerifyAndClearExpectations(mbed_mgmt_mock); |
| 200 | + // There will not be a leak since the destructor releases the singleton |
| 201 | + Mock::AllowLeak(mbed_mgmt_mock); |
| 202 | +} |
0 commit comments