Add ability to gather metrics to BubbleManager.
[chromium-blink-merge.git] / chrome / browser / ui / exclusive_access / fullscreen_controller_state_test.h
blob4d45a8cc49b88a01deadcdd853bc9940736009ea
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_UI_EXCLUSIVE_ACCESS_FULLSCREEN_CONTROLLER_STATE_TEST_H_
6 #define CHROME_BROWSER_UI_EXCLUSIVE_ACCESS_FULLSCREEN_CONTROLLER_STATE_TEST_H_
8 #include <sstream>
10 #include "base/basictypes.h"
11 #include "base/compiler_specific.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "build/build_config.h"
15 class Browser;
16 class FullscreenController;
17 class FullscreenNotificationObserver;
19 // Utility definition for mapping enum values to strings in switch statements.
20 #define ENUM_TO_STRING(enum) case enum: return #enum
22 // Test fixture used to test Fullscreen Controller through exhaustive sequences
23 // of events in unit and interactive tests.
25 // Because operating system window managers are too unreliable (they result in
26 // flakiness at around 1 out of 1000 runs) this fixture is designed to be run
27 // on testing infrastructure in unit tests mocking out the platforms' behavior.
28 // To verify that behavior interactive tests exist but are left disabled and
29 // only run manually when verifying the consistency of the
30 // FullscreenControllerTestWindow.
31 class FullscreenControllerStateTest {
32 public:
33 // Events names for FullscreenController methods.
34 enum Event {
35 TOGGLE_FULLSCREEN, // ToggleBrowserFullscreenMode()
36 TOGGLE_FULLSCREEN_CHROME, // ToggleBrowserFullscreenWithToolbar()
37 TAB_FULLSCREEN_TRUE, // ToggleFullscreenModeForTab(, true)
38 TAB_FULLSCREEN_FALSE, // ToggleFullscreenModeForTab(, false)
39 METRO_SNAP_TRUE, // SetMetroSnapMode(true)
40 METRO_SNAP_FALSE, // SetMetroSnapMode(false)
41 BUBBLE_EXIT_LINK, // ExitTabOrBrowserFullscreenToPreviousState()
42 BUBBLE_ALLOW, // OnAcceptFullscreenPermission()
43 BUBBLE_DENY, // OnDenyFullscreenPermission()
44 WINDOW_CHANGE, // ChangeWindowFullscreenState()
45 NUM_EVENTS,
46 EVENT_INVALID,
49 // Conceptual states of the Fullscreen Controller, these do not correspond
50 // to particular implemenation details.
51 enum State {
52 // The window is not in fullscreen.
53 STATE_NORMAL,
54 // User-initiated fullscreen.
55 STATE_BROWSER_FULLSCREEN_NO_CHROME,
56 // Mac User-initiated 'Lion Fullscreen' with browser chrome. OSX 10.7+ only.
57 STATE_BROWSER_FULLSCREEN_WITH_CHROME,
58 // Windows 8 Metro Snap mode, which puts the window at 20% screen-width.
59 // No TO_ state for Metro, as the windows implementation is only reentrant.
60 STATE_METRO_SNAP,
61 // HTML5 tab-initiated fullscreen.
62 STATE_TAB_FULLSCREEN,
63 // Both tab and browser fullscreen.
64 STATE_TAB_BROWSER_FULLSCREEN,
65 // Both tab and browser fullscreen, displayed without chrome, but exits tab
66 // fullscreen to STATE_BROWSER_FULLSCREEN_WITH_CHROME.
67 STATE_TAB_BROWSER_FULLSCREEN_CHROME,
68 // TO_ states are asynchronous states waiting for window state change
69 // before transitioning to their named state.
70 STATE_TO_NORMAL,
71 STATE_TO_BROWSER_FULLSCREEN_NO_CHROME,
72 STATE_TO_BROWSER_FULLSCREEN_WITH_CHROME,
73 STATE_TO_TAB_FULLSCREEN,
74 NUM_STATES,
75 STATE_INVALID,
78 static const int kMaxStateNameLength = 39;
80 FullscreenControllerStateTest();
81 virtual ~FullscreenControllerStateTest();
83 static const char* GetStateString(State state);
84 static const char* GetEventString(Event event);
86 // Returns true if FullscreenController::WindowFullscreenStateChanged()
87 // will be called and re-enter FullscreenController before
88 // FullscreenController methods complete.
89 static bool IsWindowFullscreenStateChangedReentrant();
91 // Returns true if |state| can be persistent. This is true for all of the
92 // states without "_TO_" in their name.
93 static bool IsPersistentState(State state);
95 // Causes Fullscreen Controller to transition to an arbitrary state.
96 void TransitionToState(State state);
98 // Makes one state change to approach |destination_state| via shortest path.
99 // Returns true if a state change is made.
100 // Repeated calls are needed to reach the destination.
101 bool TransitionAStepTowardState(State destination_state);
103 // Calls FullscreenController::ChangeWindowFullscreenState if needed because
104 // a mock BrowserWindow is being used.
105 virtual void ChangeWindowFullscreenState() {}
107 // Returns a description of the window's state, may return NULL.
108 // FullscreenControllerStateTest owns the returned pointer.
109 virtual const char* GetWindowStateString();
111 // Causes the |event| to occur and return true on success.
112 virtual bool InvokeEvent(Event event);
114 // Checks that window state matches the expected controller state.
115 virtual void VerifyWindowState();
117 // Wait for NOTIFICATION_FULLSCREEN_CHANGED if a notification should have been
118 // sent in transitioning to |state_| from the previous persistent state.
119 void MaybeWaitForNotification();
121 // Tests all states with all permutations of multiple events to detect
122 // lingering state issues that would bleed over to other states.
123 // I.E. for each state test all combinations of events E1, E2, E3.
125 // This produces coverage for event sequences that may happen normally but
126 // would not be exposed by traversing to each state via TransitionToState().
127 // TransitionToState() always takes the same path even when multiple paths
128 // exist.
129 void TestTransitionsForEachState();
131 // Log transition_table_ to a string for debugging.
132 std::string GetTransitionTableAsString() const;
133 // Log state_transitions_ to a string for debugging.
134 std::string GetStateTransitionsAsString() const;
136 protected:
137 // Set of enumerations (created with a helper macro) for _FALSE, _TRUE, and
138 // _NO_EXPECTATION values to be passed to VerifyWindowStateExpectations().
139 #define EXPECTATION_ENUM(enum_name, enum_prefix) \
140 enum enum_name { \
141 enum_prefix##_FALSE, \
142 enum_prefix##_TRUE, \
143 enum_prefix##_NO_EXPECTATION \
145 EXPECTATION_ENUM(FullscreenWithToolbarExpectation, FULLSCREEN_WITH_CHROME);
146 EXPECTATION_ENUM(FullscreenForBrowserExpectation, FULLSCREEN_FOR_BROWSER);
147 EXPECTATION_ENUM(FullscreenForTabExpectation, FULLSCREEN_FOR_TAB);
148 EXPECTATION_ENUM(InMetroSnapExpectation, IN_METRO_SNAP);
150 // Generated information about the transitions between states.
151 struct StateTransitionInfo {
152 StateTransitionInfo()
153 : event(EVENT_INVALID),
154 state(STATE_INVALID),
155 distance(NUM_STATES) {}
156 Event event; // The |Event| that will cause the state transition.
157 State state; // The adjacent |State| transitioned to; not the final state.
158 int distance; // Steps to final state. NUM_STATES represents unknown.
161 // Returns next transition info for shortest path from source to destination.
162 StateTransitionInfo NextTransitionInShortestPath(State source,
163 State destination,
164 int search_limit);
166 // Returns a detailed log of what FullscreenControllerStateTest has done
167 // up to this point, to be reported when tests fail.
168 std::string GetAndClearDebugLog();
170 // Returns true if the |state| & |event| pair should be skipped.
171 virtual bool ShouldSkipStateAndEventPair(State state, Event event);
173 // Returns true if a test should be skipped entirely, e.g. due to platform.
174 virtual bool ShouldSkipTest(State state, Event event);
176 // Runs one test of transitioning to a state and invoking an event.
177 virtual void TestStateAndEvent(State state, Event event);
179 // Checks that window state matches the expected controller state.
180 virtual void VerifyWindowStateExpectations(
181 FullscreenWithToolbarExpectation fullscreen_with_toolbar,
182 FullscreenForBrowserExpectation fullscreen_for_browser,
183 FullscreenForTabExpectation fullscreen_for_tab,
184 InMetroSnapExpectation in_metro_snap);
186 virtual Browser* GetBrowser() = 0;
187 FullscreenController* GetFullscreenController();
189 // The state the FullscreenController is expected to be in.
190 State state() const { return state_; }
192 private:
193 // The state the FullscreenController is expected to be in.
194 State state_;
196 // The state when the previous NOTIFICATION_FULLSCREEN_CHANGED notification
197 // was received.
198 State last_notification_received_state_;
200 // Listens for the NOTIFICATION_FULLSCREEN_CHANGED notification.
201 scoped_ptr<FullscreenNotificationObserver> fullscreen_notification_observer_;
203 // Human defined |State| that results given each [state][event] pair.
204 State transition_table_[NUM_STATES][NUM_EVENTS];
206 // Generated information about the transitions between states [from][to].
207 // View generated data with: out/Release/unit_tests
208 // --gtest_filter="FullscreenController*DebugLogStateTables"
209 // --gtest_also_run_disabled_tests
210 StateTransitionInfo state_transitions_[NUM_STATES][NUM_STATES];
212 // Log of operations reported on errors via GetAndClearDebugLog().
213 std::ostringstream debugging_log_;
215 DISALLOW_COPY_AND_ASSIGN(FullscreenControllerStateTest);
218 #endif // CHROME_BROWSER_UI_EXCLUSIVE_ACCESS_FULLSCREEN_CONTROLLER_STATE_TEST_H_