],
cflags: ["-DBUILDCFG"],
}
-
-// btif state machine unit tests for target
-// ========================================================
-cc_test {
- name: "net_test_btif_state_machine",
- defaults: ["fluoride_defaults"],
- include_dirs: btifCommonIncludes,
- host_supported: true,
- srcs: [
- "test/btif_state_machine_test.cc"
- ],
- header_libs: ["libbluetooth_headers"],
- shared_libs: [
- "liblog",
- "libcutils",
- ],
- static_libs: [
- "libbluetooth-types",
- "libosi",
- ],
- cflags: ["-DBUILDCFG"],
-}
#include "btif_av_co.h"
#include "btif_profile_queue.h"
#include "btif_rc.h"
-#include "btif_state_machine.h"
#include "btif_util.h"
#include "btu.h"
+#include "common/state_machine.h"
#include "osi/include/allocator.h"
#include "osi/include/osi.h"
#include "osi/include/properties.h"
// different than Open state. Suspend flags are needed however to prevent
// media task from trying to restart stream during remote Suspend or while
// we are in the process of a local Suspend.
-class BtifAvStateMachine : public BtifStateMachine {
+class BtifAvStateMachine : public bluetooth::common::StateMachine {
public:
enum {
kStateIdle, // AVDTP disconnected
"leaky_bonded_queue_unittest.cc",
"message_loop_thread_unittest.cc",
"metrics_unittest.cc",
+ "state_machine_unittest.cc",
"time_util_unittest.cc",
],
shared_libs: [
* limitations under the License.
*/
-#ifndef BTIF_STATE_MACHINE_H
-#define BTIF_STATE_MACHINE_H
+#pragma once
#include <map>
#include <utility>
#include <base/logging.h>
+namespace bluetooth {
+
+namespace common {
+
/**
- * State machine used by BTIF components.
+ * State machine used by Bluetooth native stack.
*/
-class BtifStateMachine {
+class StateMachine {
public:
enum { kStateInvalid = -1 };
* A class to represent the state in the State Machine.
*/
class State {
- friend class BtifStateMachine;
+ friend class StateMachine;
public:
/**
* @param sm the State Machine to use
* @param state_id the unique State ID. It should be a non-negative number.
*/
- State(BtifStateMachine& sm, int state_id) : sm_(sm), state_id_(state_id) {}
+ State(StateMachine& sm, int state_id) : sm_(sm), state_id_(state_id) {}
virtual ~State() = default;
*
* @param dest_state the state to transition to. It cannot be nullptr.
*/
- void TransitionTo(BtifStateMachine::State* dest_state) {
+ void TransitionTo(StateMachine::State* dest_state) {
sm_.TransitionTo(dest_state);
}
private:
- BtifStateMachine& sm_;
+ StateMachine& sm_;
int state_id_;
};
- BtifStateMachine()
+ StateMachine()
: initial_state_(nullptr),
previous_state_(nullptr),
current_state_(nullptr) {}
- ~BtifStateMachine() {
+ ~StateMachine() {
for (auto& kv : states_) delete kv.second;
}
*
* @param dest_state the state to transition to. It cannot be nullptr.
*/
- void TransitionTo(BtifStateMachine::State* dest_state) {
+ void TransitionTo(StateMachine::State* dest_state) {
if (current_state_ != nullptr) {
current_state_->OnExit();
}
std::map<int, State*> states_;
};
-#endif // BTIF_STATE_MACHINE_H
+} // namespace common
+
+} // namespace bluetooth
#include <gtest/gtest.h>
-#include "btif/include/btif_state_machine.h"
+#include "common/state_machine.h"
+
+using bluetooth::common::StateMachine;
namespace {
static constexpr uint32_t kInvalidEvent = 0xffffffff;
static char dataTwo = 2;
} // namespace
-class BtifStateMachineImpl : public BtifStateMachine {
+class StateMachineImpl : public StateMachine {
public:
enum {
kStateZero,
class StateZero : public State {
public:
- StateZero(BtifStateMachine& sm)
+ StateZero(StateMachine& sm)
: State(sm, kStateZero),
on_enter_(false),
on_exit_(false),
class StateOne : public State {
public:
- StateOne(BtifStateMachine& sm)
+ StateOne(StateMachine& sm)
: State(sm, kStateOne),
on_enter_(false),
on_exit_(false),
class StateTwo : public State {
public:
- StateTwo(BtifStateMachine& sm)
+ StateTwo(StateMachine& sm)
: State(sm, kStateTwo),
on_enter_(false),
on_exit_(false),
void* data_;
};
- BtifStateMachineImpl() {
+ StateMachineImpl() {
state_zero_ = new StateZero(*this);
state_one_ = new StateOne(*this);
state_two_ = new StateTwo(*this);
StateTwo* state_two_;
};
-class BtifStateMachineTest : public ::testing::Test {
+class StateMachineTest : public ::testing::Test {
protected:
- BtifStateMachineTest() {}
+ StateMachineTest() {}
void SetUp() override { sm_.Start(); }
void TearDown() override { sm_.Quit(); }
- BtifStateMachineImpl sm_;
+ StateMachineImpl sm_;
};
-TEST_F(BtifStateMachineTest, test_initial_state) {
+TEST_F(StateMachineTest, test_initial_state) {
ASSERT_EQ(sm_.kStateZero, sm_.StateId());
ASSERT_EQ(sm_.kStateInvalid, sm_.PreviousStateId());
}
-TEST_F(BtifStateMachineTest, test_invalid_state) {
+TEST_F(StateMachineTest, test_invalid_state) {
sm_.Quit();
ASSERT_EQ(sm_.kStateInvalid, sm_.StateId());
ASSERT_EQ(sm_.kStateInvalid, sm_.PreviousStateId());
ASSERT_EQ(sm_.kStateInvalid, sm_.PreviousStateId());
}
-TEST_F(BtifStateMachineTest, test_transition_to) {
+TEST_F(StateMachineTest, test_transition_to) {
// Initial state: StateZero
ASSERT_EQ(sm_.kStateZero, sm_.StateId());
ASSERT_EQ(sm_.kStateInvalid, sm_.PreviousStateId());
ASSERT_FALSE(sm_.state_two_->on_exit_);
}
-TEST_F(BtifStateMachineTest, test_process_event) {
+TEST_F(StateMachineTest, test_process_event) {
// Initial state: StateZero
ASSERT_EQ(sm_.kStateZero, sm_.StateId());
ASSERT_EQ(sm_.kStateInvalid, sm_.PreviousStateId());
'bluetooth_test_common',
'bluetoothtbd_test',
'net_test_avrcp',
- 'net_test_btif_state_machine',
'net_test_btcore',
'net_test_types',
'net_test_btpackets',
net_test_bta
net_test_btif
net_test_btif_profile_queue
- net_test_btif_state_machine
net_test_device
net_test_hci
net_test_stack