blob: c27c8360430509648ecfdb8102c81680c1e35aec [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2020 The Chromium Authors
Fergal Daly6abbb2b2020-06-06 11:06:462// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Sreeja Kamishettyd9f68db2022-08-03 19:42:475#include "base/state_transitions.h"
Fergal Daly6abbb2b2020-06-06 11:06:466
7#include <ostream>
8#include <string>
9
10#include "base/test/gtest_util.h"
11#include "build/build_config.h"
12#include "testing/gmock/include/gmock/gmock.h"
13#include "testing/gtest/include/gtest/gtest.h"
14
Sreeja Kamishettyd9f68db2022-08-03 19:42:4715namespace base {
Fergal Daly6abbb2b2020-06-06 11:06:4616
17enum class State { kState1 = 0, kState2, kState3, kState4 };
18
19std::ostream& operator<<(std::ostream& o, const State& s) {
20 return o << static_cast<int>(s);
21}
22
23TEST(StateTransitionsTest, Constructor) {
24 // No expectations, just make sure the constructor works.
25 const StateTransitions<State> transitions({
26 {State::kState1, {State::kState2, State::kState3}},
27 {State::kState2, {State::kState3, State::kState4}},
28 });
29}
30
31TEST(StateTransitionsTest, GetValidTransitions) {
32 const StateTransitions<State> transitions({
33 {State::kState1, {State::kState2, State::kState3}},
34 {State::kState2, {State::kState3, State::kState4}},
35 });
36 EXPECT_THAT(transitions.GetValidTransitions(State::kState1),
37 testing::ElementsAre(State::kState2, State::kState3));
38 EXPECT_THAT(transitions.GetValidTransitions(State::kState2),
39 testing::ElementsAre(State::kState3, State::kState4));
40 EXPECT_THAT(transitions.GetValidTransitions(State::kState3),
41 testing::ElementsAre());
42 EXPECT_THAT(transitions.GetValidTransitions(State::kState4),
43 testing::ElementsAre());
44}
45
46TEST(StateTransitionsTest, IsTransitionValid) {
47 const StateTransitions<State> transitions({
48 {State::kState1, {State::kState2, State::kState3}},
49 {State::kState2, {State::kState3, State::kState4}},
50 });
51 ASSERT_TRUE(transitions.IsTransitionValid(State::kState1, State::kState2));
52 ASSERT_TRUE(transitions.IsTransitionValid(State::kState2, State::kState3));
53 ASSERT_FALSE(transitions.IsTransitionValid(State::kState1, State::kState4));
54 // kState3 was omitted from the definition.
55 ASSERT_FALSE(transitions.IsTransitionValid(State::kState3, State::kState4));
56}
57
58TEST(StateTransitionsTest, DCHECK_STATE_TRANSITION) {
59 const StateTransitions<State> transitions({
60 {State::kState1, {State::kState2, State::kState3}},
61 {State::kState2, {State::kState3, State::kState4}},
62 });
63 DCHECK_STATE_TRANSITION(&transitions, State::kState1, State::kState2);
64 DCHECK_STATE_TRANSITION(&transitions, State::kState2, State::kState3);
65
66#if DCHECK_IS_ON()
67 // EXPECT_DEATH is not defined on IOS.
Xiaohan Wang6028ac22022-01-15 19:18:4168#if !BUILDFLAG(IS_IOS)
Fergal Daly6abbb2b2020-06-06 11:06:4669 EXPECT_DEATH(
70 DCHECK_STATE_TRANSITION(&transitions, State::kState1, State::kState4),
Peter Boström59a246e82025-03-17 03:55:3371 "DCHECK failed.*Invalid transition: 0 -> 3");
Fergal Daly6abbb2b2020-06-06 11:06:4672 // kState3 was omitted from the definition.
73 EXPECT_DEATH(
74 DCHECK_STATE_TRANSITION(&transitions, State::kState3, State::kState4),
Peter Boström59a246e82025-03-17 03:55:3375 "DCHECK failed.*Invalid transition: 2 -> 3");
Xiaohan Wang6028ac22022-01-15 19:18:4176#endif // !BUILDFLAG(IS_IOS)
Fergal Daly6abbb2b2020-06-06 11:06:4677#endif // DCHECK_IS_ON()
78}
79
80// Test that everything works OK with some other data type.
81TEST(StateTransitionsTest, NonEnum) {
82 const StateTransitions<std::string> transitions({
83 {"state1", {"state2", "state3"}},
84 {"state2", {"state3", "state4"}},
85 });
86 ASSERT_TRUE(transitions.IsTransitionValid("state1", "state2"));
87 ASSERT_TRUE(transitions.IsTransitionValid("state2", "state3"));
88 ASSERT_FALSE(transitions.IsTransitionValid("state1", "state4"));
89 // kState3 was omitted from the definition.
90 ASSERT_FALSE(transitions.IsTransitionValid("state3", "state4"));
91 DCHECK_STATE_TRANSITION(&transitions, "state1", "state2");
92 DCHECK_STATE_TRANSITION(&transitions, "state2", "state3");
93
94 // Try some states that are not in the specification at all.
95 ASSERT_FALSE(transitions.IsTransitionValid("foo", "state2"));
96 ASSERT_FALSE(transitions.IsTransitionValid("state1", "foo"));
97 ASSERT_FALSE(transitions.IsTransitionValid("foo", "bar"));
98}
99
Sreeja Kamishettyd9f68db2022-08-03 19:42:47100} // namespace base