Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2020 The Chromium Authors |
Fergal Daly | 6abbb2b | 2020-06-06 11:06:46 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Sreeja Kamishetty | d9f68db | 2022-08-03 19:42:47 | [diff] [blame] | 5 | #include "base/state_transitions.h" |
Fergal Daly | 6abbb2b | 2020-06-06 11:06:46 | [diff] [blame] | 6 | |
| 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 Kamishetty | d9f68db | 2022-08-03 19:42:47 | [diff] [blame] | 15 | namespace base { |
Fergal Daly | 6abbb2b | 2020-06-06 11:06:46 | [diff] [blame] | 16 | |
| 17 | enum class State { kState1 = 0, kState2, kState3, kState4 }; |
| 18 | |
| 19 | std::ostream& operator<<(std::ostream& o, const State& s) { |
| 20 | return o << static_cast<int>(s); |
| 21 | } |
| 22 | |
| 23 | TEST(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 | |
| 31 | TEST(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 | |
| 46 | TEST(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 | |
| 58 | TEST(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 Wang | 6028ac2 | 2022-01-15 19:18:41 | [diff] [blame] | 68 | #if !BUILDFLAG(IS_IOS) |
Fergal Daly | 6abbb2b | 2020-06-06 11:06:46 | [diff] [blame] | 69 | EXPECT_DEATH( |
| 70 | DCHECK_STATE_TRANSITION(&transitions, State::kState1, State::kState4), |
Peter Boström | 59a246e8 | 2025-03-17 03:55:33 | [diff] [blame] | 71 | "DCHECK failed.*Invalid transition: 0 -> 3"); |
Fergal Daly | 6abbb2b | 2020-06-06 11:06:46 | [diff] [blame] | 72 | // kState3 was omitted from the definition. |
| 73 | EXPECT_DEATH( |
| 74 | DCHECK_STATE_TRANSITION(&transitions, State::kState3, State::kState4), |
Peter Boström | 59a246e8 | 2025-03-17 03:55:33 | [diff] [blame] | 75 | "DCHECK failed.*Invalid transition: 2 -> 3"); |
Xiaohan Wang | 6028ac2 | 2022-01-15 19:18:41 | [diff] [blame] | 76 | #endif // !BUILDFLAG(IS_IOS) |
Fergal Daly | 6abbb2b | 2020-06-06 11:06:46 | [diff] [blame] | 77 | #endif // DCHECK_IS_ON() |
| 78 | } |
| 79 | |
| 80 | // Test that everything works OK with some other data type. |
| 81 | TEST(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 Kamishetty | d9f68db | 2022-08-03 19:42:47 | [diff] [blame] | 100 | } // namespace base |