Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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. | |
| 4 | |
| 5 #import <Cocoa/Cocoa.h> | |
| 6 | |
| 7 #include "base/mac/mac_util.h" | |
| 8 #include "base/mac/scoped_nsobject.h" | |
| 9 #include "base/macros.h" | |
| 10 #import "chrome/browser/ui/cocoa/browser_window_controller.h" | |
| 11 #import "chrome/browser/ui/cocoa/fullscreen/fullscreen_menubar_tracker.h" | |
| 12 #import "chrome/browser/ui/cocoa/fullscreen/fullscreen_toolbar_animation_control ler.h" | |
| 13 #import "chrome/browser/ui/cocoa/fullscreen/fullscreen_toolbar_controller.h" | |
| 14 #import "chrome/browser/ui/cocoa/fullscreen/fullscreen_toolbar_mouse_tracker.h" | |
| 15 #import "chrome/browser/ui/cocoa/fullscreen/fullscreen_toolbar_visibility_lock_c ontroller.h" | |
| 16 #include "testing/gtest/include/gtest/gtest.h" | |
| 17 #import "testing/gtest_mac.h" | |
| 18 #import "third_party/ocmock/OCMock/OCMock.h" | |
| 19 #include "ui/base/cocoa/appkit_utils.h" | |
| 20 | |
| 21 ////////////////////////////////////////////////////////////////// | |
| 22 // MockFullscreenToolbarMouseTracker | |
| 23 // Mocks the mouse interactions with the toolbar. | |
| 24 | |
| 25 @interface MockFullscreenToolbarMouseTracker : FullscreenToolbarMouseTracker | |
| 26 | |
| 27 @property(nonatomic, assign) BOOL mouseInside; | |
| 28 | |
| 29 // Overridden to prevent a tracking area from being created. | |
| 30 - (void)updateTrackingArea; | |
| 31 | |
| 32 - (BOOL)mouseInsideTrackingArea; | |
| 33 | |
| 34 @end | |
| 35 | |
| 36 @implementation MockFullscreenToolbarMouseTracker | |
| 37 | |
| 38 @synthesize mouseInside = mouseInside_; | |
| 39 | |
| 40 - (void)updateTrackingArea { | |
| 41 } | |
| 42 | |
| 43 - (BOOL)mouseInsideTrackingArea { | |
| 44 return mouseInside_; | |
| 45 } | |
| 46 | |
| 47 @end | |
| 48 | |
| 49 ////////////////////////////////////////////////////////////////// | |
| 50 // MockFullscreenMenubarTracker | |
| 51 // Mocks the state of the menubar. | |
| 52 | |
| 53 @interface MockFullscreenMenubarTracker : FullscreenMenubarTracker { | |
| 54 CGFloat menubarFraction_; | |
| 55 FullscreenMenubarState menubarState_; | |
| 56 } | |
| 57 | |
| 58 - (CGFloat)menubarFraction; | |
| 59 - (FullscreenMenubarState)state; | |
| 60 - (void)setMenubarProgress:(CGFloat)progress; | |
| 61 | |
| 62 @end | |
| 63 | |
| 64 @implementation MockFullscreenMenubarTracker | |
| 65 | |
| 66 - (CGFloat)menubarFraction { | |
| 67 return menubarFraction_; | |
| 68 } | |
| 69 | |
| 70 - (FullscreenMenubarState)state { | |
| 71 return menubarState_; | |
| 72 } | |
| 73 | |
| 74 - (void)setMenubarProgress:(CGFloat)progress { | |
| 75 if (ui::IsCGFloatEqual(progress, 1.0)) | |
| 76 menubarState_ = FullscreenMenubarState::SHOWN; | |
| 77 else if (ui::IsCGFloatEqual(progress, 0.0)) | |
| 78 menubarState_ = FullscreenMenubarState::HIDDEN; | |
| 79 else if (progress < menubarFraction_) | |
| 80 menubarState_ = FullscreenMenubarState::HIDING; | |
| 81 else if (progress > menubarFraction_) | |
| 82 menubarState_ = FullscreenMenubarState::SHOWING; | |
| 83 menubarFraction_ = progress; | |
| 84 } | |
| 85 | |
| 86 @end | |
| 87 | |
| 88 namespace { | |
| 89 | |
| 90 class FullscreenToolbarControllerTest : public testing::Test { | |
| 91 public: | |
| 92 FullscreenToolbarControllerTest() {} | |
|
Robert Sesek
2016/11/22 21:02:44
You can omit this.
spqchan
2016/11/22 23:04:56
I removed this line and got an compilation error.
Robert Sesek
2016/11/23 18:11:36
Huh, that's new...
| |
| 93 void SetUp() override { | |
| 94 BOOL yes = YES; | |
| 95 BOOL no = NO; | |
| 96 | |
| 97 bwc_ = [OCMockObject mockForClass:[BrowserWindowController class]]; | |
| 98 [[[bwc_ stub] andReturnValue:OCMOCK_VALUE(yes)] | |
| 99 isKindOfClass:[BrowserWindowController class]]; | |
| 100 [[[bwc_ stub] andReturnValue:OCMOCK_VALUE(yes)] isInAppKitFullscreen]; | |
| 101 [[[bwc_ stub] andReturnValue:OCMOCK_VALUE(no)] isInImmersiveFullscreen]; | |
| 102 [[bwc_ stub] layoutSubviews]; | |
| 103 | |
| 104 controller_.reset( | |
| 105 [[FullscreenToolbarController alloc] initWithBrowserController:bwc_]); | |
| 106 SetToolbarStyle(FullscreenToolbarStyle::TOOLBAR_HIDDEN); | |
| 107 | |
| 108 menubar_tracker_ = [[MockFullscreenMenubarTracker alloc] | |
| 109 initWithFullscreenToolbarController:nil]; | |
| 110 [menubar_tracker_ setMenubarProgress:0.0]; | |
| 111 [controller_ setMenubarTracker:menubar_tracker_]; | |
| 112 | |
| 113 mouse_tracker_ = [[MockFullscreenToolbarMouseTracker alloc] init]; | |
| 114 [controller_ setMouseTracker:mouse_tracker_]; | |
| 115 | |
| 116 [controller_ animationController]->SetAnimationDuration(0.0); | |
| 117 | |
| 118 [controller_ setTestFullscreenMode:YES]; | |
| 119 } | |
| 120 | |
| 121 void TearDown() override { [controller_ setTestFullscreenMode:NO]; } | |
| 122 | |
| 123 void SetToolbarStyle(FullscreenToolbarStyle style) { | |
| 124 [controller_ setToolbarStyle:style]; | |
| 125 } | |
| 126 | |
| 127 void CheckLayout(CGFloat toolbarFraction, CGFloat menubarFraction) { | |
| 128 FullscreenToolbarLayout layout = [controller_ computeLayout]; | |
| 129 DCHECK_EQ(layout.toolbarFraction, toolbarFraction); | |
|
Robert Sesek
2016/11/22 21:02:44
nit: EXPECT_ ordering
spqchan
2016/11/22 23:04:56
Done.
| |
| 130 DCHECK_EQ(layout.menubarOffset, | |
| 131 menubarFraction * [controller_ toolbarVerticalOffset]); | |
| 132 } | |
| 133 | |
| 134 // A mock BrowserWindowController object. | |
| 135 id bwc_; | |
| 136 | |
| 137 // The FullscreenToolbarController object being tested. | |
| 138 base::scoped_nsobject<FullscreenToolbarController> controller_; | |
| 139 | |
| 140 // Mocks the state of the menubar. | |
| 141 MockFullscreenMenubarTracker* menubar_tracker_; // weak | |
| 142 | |
| 143 // Mocks the mouse interactions with the toolbar. | |
| 144 MockFullscreenToolbarMouseTracker* mouse_tracker_; // weak | |
| 145 | |
| 146 private: | |
| 147 DISALLOW_COPY_AND_ASSIGN(FullscreenToolbarControllerTest); | |
| 148 }; | |
| 149 | |
| 150 // Tests the toolbar fraction for the TOOLBAR_NONE and TOOLBAR_PRESENT | |
| 151 // styles. | |
| 152 TEST_F(FullscreenToolbarControllerTest, TestPresentAndNoneToolbarStyle) { | |
| 153 CheckLayout(0, 0); | |
| 154 | |
| 155 [controller_ setToolbarStyle:FullscreenToolbarStyle::TOOLBAR_NONE]; | |
| 156 CheckLayout(0, 0); | |
| 157 | |
| 158 [controller_ setToolbarStyle:FullscreenToolbarStyle::TOOLBAR_PRESENT]; | |
| 159 CheckLayout(1, 0); | |
| 160 } | |
| 161 | |
| 162 // Basic test that checks if the toolbar fraction for different menubar values. | |
| 163 // This test simulates the showing and hiding the menubar. | |
| 164 TEST_F(FullscreenToolbarControllerTest, TestHiddenToolbarWithMenubar) { | |
| 165 CheckLayout(0, 0); | |
| 166 | |
| 167 [menubar_tracker_ setMenubarProgress:0.5]; | |
| 168 CheckLayout(0.5, 0.5); | |
| 169 | |
| 170 [menubar_tracker_ setMenubarProgress:1]; | |
| 171 CheckLayout(1, 1); | |
| 172 | |
| 173 [menubar_tracker_ setMenubarProgress:0.5]; | |
| 174 CheckLayout(0.5, 0.5); | |
| 175 | |
| 176 [menubar_tracker_ setMenubarProgress:0]; | |
| 177 CheckLayout(0, 0); | |
| 178 } | |
| 179 | |
| 180 // Test that checks the visibility lock functions and the toolbar fraction. | |
| 181 TEST_F(FullscreenToolbarControllerTest, TestHiddenToolbarWithVisibilityLocks) { | |
| 182 FullscreenToolbarVisibilityLockController* locks = | |
| 183 [controller_ visibilityLockController]; | |
| 184 base::scoped_nsobject<NSObject> owner([[NSObject alloc] init]); | |
| 185 base::scoped_nsobject<NSObject> alternative_owner([[NSObject alloc] init]); | |
| 186 | |
| 187 [menubar_tracker_ setMenubarProgress:0]; | |
| 188 CheckLayout(0, 0); | |
| 189 | |
| 190 // Lock the toolbar visibility. Toolbar should be fully visible. | |
| 191 [locks lockToolbarVisibilityForOwner:owner.get() withAnimation:NO]; | |
| 192 DCHECK([locks isToolbarVisibilityLocked]); | |
|
Robert Sesek
2016/11/22 21:02:44
No DCHECKs in test. Use EXPECT_TRUE() instead.
spqchan
2016/11/22 23:04:56
Done.
| |
| 193 DCHECK([locks isToolbarVisibilityLockedForOwner:owner.get()]); | |
| 194 DCHECK(![locks isToolbarVisibilityLockedForOwner:alternative_owner.get()]); | |
| 195 CheckLayout(1, 0); | |
| 196 | |
| 197 // Show the menubar. | |
| 198 [menubar_tracker_ setMenubarProgress:1]; | |
| 199 CheckLayout(1, 1); | |
| 200 | |
| 201 // Hide the menubar. The toolbar should still be fully visible. | |
| 202 [menubar_tracker_ setMenubarProgress:0.5]; | |
| 203 CheckLayout(1, 0.5); | |
| 204 [menubar_tracker_ setMenubarProgress:0]; | |
| 205 CheckLayout(1, 0); | |
| 206 | |
| 207 // Release the lock. Toolbar should now be hidden. | |
| 208 [locks releaseToolbarVisibilityForOwner:owner.get() withAnimation:NO]; | |
| 209 DCHECK(![locks isToolbarVisibilityLocked]); | |
| 210 DCHECK(![locks isToolbarVisibilityLockedForOwner:owner.get()]); | |
| 211 CheckLayout(0, 0); | |
| 212 | |
| 213 // Lock and release the toolbar visibility with multiple owners. | |
| 214 [locks lockToolbarVisibilityForOwner:owner.get() withAnimation:NO]; | |
| 215 [locks lockToolbarVisibilityForOwner:alternative_owner.get() | |
| 216 withAnimation:NO]; | |
| 217 DCHECK([locks isToolbarVisibilityLocked]); | |
| 218 DCHECK([locks isToolbarVisibilityLockedForOwner:owner.get()]); | |
| 219 DCHECK([locks isToolbarVisibilityLockedForOwner:alternative_owner.get()]); | |
| 220 CheckLayout(1, 0); | |
| 221 | |
| 222 [locks releaseToolbarVisibilityForOwner:owner.get() withAnimation:NO]; | |
| 223 DCHECK([locks isToolbarVisibilityLocked]); | |
| 224 DCHECK(![locks isToolbarVisibilityLockedForOwner:owner.get()]); | |
| 225 DCHECK([locks isToolbarVisibilityLockedForOwner:alternative_owner.get()]); | |
| 226 CheckLayout(1, 0); | |
| 227 | |
| 228 [locks releaseToolbarVisibilityForOwner:alternative_owner.get() | |
| 229 withAnimation:NO]; | |
| 230 DCHECK(![locks isToolbarVisibilityLocked]); | |
| 231 DCHECK(![locks isToolbarVisibilityLockedForOwner:alternative_owner.get()]); | |
| 232 CheckLayout(0, 0); | |
| 233 } | |
| 234 | |
| 235 // Basic test that checks the toolbar fraction for different mouse tracking | |
| 236 // values. | |
| 237 TEST_F(FullscreenToolbarControllerTest, TestHiddenToolbarWithMouseTracking) { | |
| 238 CheckLayout(0, 0); | |
| 239 | |
| 240 [mouse_tracker_ setMouseInside:YES]; | |
| 241 CheckLayout(1, 0); | |
| 242 | |
| 243 [menubar_tracker_ setMenubarProgress:1]; | |
| 244 CheckLayout(1, 1); | |
| 245 | |
| 246 [menubar_tracker_ setMenubarProgress:0]; | |
| 247 CheckLayout(1, 0); | |
| 248 | |
| 249 [mouse_tracker_ setMouseInside:NO]; | |
| 250 CheckLayout(0, 0); | |
| 251 } | |
| 252 | |
| 253 // Test that checks the toolbar fraction with mouse tracking, menubar fraction, | |
| 254 // and visibility locks. | |
| 255 TEST_F(FullscreenToolbarControllerTest, TestHiddenToolbarWithMultipleFactors) { | |
| 256 FullscreenToolbarVisibilityLockController* locks = | |
| 257 [controller_ visibilityLockController]; | |
| 258 base::scoped_nsobject<NSObject> owner([[NSObject alloc] init]); | |
| 259 CheckLayout(0, 0); | |
| 260 | |
| 261 // Toolbar should be shown with the menubar. | |
| 262 [menubar_tracker_ setMenubarProgress:1]; | |
| 263 CheckLayout(1, 1); | |
| 264 | |
| 265 // Move the mouse to the toolbar and start hiding the menubar. Toolbar | |
| 266 // should be fully visible. | |
| 267 [mouse_tracker_ setMouseInside:YES]; | |
| 268 CheckLayout(1, 1); | |
| 269 [menubar_tracker_ setMenubarProgress:0.5]; | |
| 270 CheckLayout(1, 0.5); | |
| 271 | |
| 272 // Lock the toolbar's visibility. | |
| 273 [locks lockToolbarVisibilityForOwner:owner.get() withAnimation:NO]; | |
| 274 CheckLayout(1, 0.5); | |
| 275 | |
| 276 // Hide the menubar. Toolbar should be fully visible. | |
| 277 [menubar_tracker_ setMenubarProgress:0]; | |
| 278 CheckLayout(1, 0); | |
| 279 | |
| 280 // Lock the toolbar's visibility. Toolbar should be fully visible. | |
| 281 [locks releaseToolbarVisibilityForOwner:owner.get() withAnimation:NO]; | |
| 282 CheckLayout(1, 0); | |
| 283 | |
| 284 // Move the mouse away from the toolbar. Toolbar should hide. | |
| 285 [mouse_tracker_ setMouseInside:NO]; | |
| 286 CheckLayout(0, 0); | |
| 287 | |
| 288 // Lock the toolbar and move the mouse to it. | |
| 289 [locks lockToolbarVisibilityForOwner:owner.get() withAnimation:NO]; | |
| 290 [mouse_tracker_ setMouseInside:YES]; | |
| 291 CheckLayout(1, 0); | |
| 292 | |
| 293 // Move the mouse away from the toolbar. Toolbar should be fully visible. | |
| 294 [mouse_tracker_ setMouseInside:NO]; | |
| 295 CheckLayout(1, 0); | |
| 296 | |
| 297 // Release the toolbar. Toolbar should be hidden. | |
| 298 [locks releaseToolbarVisibilityForOwner:owner.get() withAnimation:NO]; | |
| 299 } | |
| 300 | |
| 301 } // namespace | |
| OLD | NEW |