fixup! [M120 Migration] Notify media device state to webbrowser
[platform/framework/web/chromium-efl.git] / base / state_transitions_unittest.cc
1 // Copyright 2020 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/state_transitions.h"
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
15 namespace base {
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.
68 #if !BUILDFLAG(IS_IOS)
69   EXPECT_DEATH(
70       DCHECK_STATE_TRANSITION(&transitions, State::kState1, State::kState4),
71       "Check failed.*Invalid transition: 0 -> 3");
72   // kState3 was omitted from the definition.
73   EXPECT_DEATH(
74       DCHECK_STATE_TRANSITION(&transitions, State::kState3, State::kState4),
75       "Check failed.*Invalid transition: 2 -> 3");
76 #endif  // !BUILDFLAG(IS_IOS)
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
100 }  // namespace base