Chromium Code Reviews| Index: chrome/browser/ui/cocoa/fullscreen/fullscreen_toolbar_controller_unittest.mm |
| diff --git a/chrome/browser/ui/cocoa/fullscreen/fullscreen_toolbar_controller_unittest.mm b/chrome/browser/ui/cocoa/fullscreen/fullscreen_toolbar_controller_unittest.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..ad6c99a0ed45d0f5b8cacfe70714922a8bbbc3c0 |
| --- /dev/null |
| +++ b/chrome/browser/ui/cocoa/fullscreen/fullscreen_toolbar_controller_unittest.mm |
| @@ -0,0 +1,301 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#import <Cocoa/Cocoa.h> |
| + |
| +#include "base/mac/mac_util.h" |
| +#include "base/mac/scoped_nsobject.h" |
| +#include "base/macros.h" |
| +#import "chrome/browser/ui/cocoa/browser_window_controller.h" |
| +#import "chrome/browser/ui/cocoa/fullscreen/fullscreen_menubar_tracker.h" |
| +#import "chrome/browser/ui/cocoa/fullscreen/fullscreen_toolbar_animation_controller.h" |
| +#import "chrome/browser/ui/cocoa/fullscreen/fullscreen_toolbar_controller.h" |
| +#import "chrome/browser/ui/cocoa/fullscreen/fullscreen_toolbar_mouse_tracker.h" |
| +#import "chrome/browser/ui/cocoa/fullscreen/fullscreen_toolbar_visibility_lock_controller.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| +#import "testing/gtest_mac.h" |
| +#import "third_party/ocmock/OCMock/OCMock.h" |
| +#include "ui/base/cocoa/appkit_utils.h" |
| + |
| +////////////////////////////////////////////////////////////////// |
| +// MockFullscreenToolbarMouseTracker |
| +// Mocks the mouse interactions with the toolbar. |
| + |
| +@interface MockFullscreenToolbarMouseTracker : FullscreenToolbarMouseTracker |
| + |
| +@property(nonatomic, assign) BOOL mouseInside; |
| + |
| +// Overridden to prevent a tracking area from being created. |
| +- (void)updateTrackingArea; |
| + |
| +- (BOOL)mouseInsideTrackingArea; |
| + |
| +@end |
| + |
| +@implementation MockFullscreenToolbarMouseTracker |
| + |
| +@synthesize mouseInside = mouseInside_; |
| + |
| +- (void)updateTrackingArea { |
| +} |
| + |
| +- (BOOL)mouseInsideTrackingArea { |
| + return mouseInside_; |
| +} |
| + |
| +@end |
| + |
| +////////////////////////////////////////////////////////////////// |
| +// MockFullscreenMenubarTracker |
| +// Mocks the state of the menubar. |
| + |
| +@interface MockFullscreenMenubarTracker : FullscreenMenubarTracker { |
| + CGFloat menubarFraction_; |
| + FullscreenMenubarState menubarState_; |
| +} |
| + |
| +- (CGFloat)menubarFraction; |
| +- (FullscreenMenubarState)state; |
| +- (void)setMenubarProgress:(CGFloat)progress; |
| + |
| +@end |
| + |
| +@implementation MockFullscreenMenubarTracker |
| + |
| +- (CGFloat)menubarFraction { |
| + return menubarFraction_; |
| +} |
| + |
| +- (FullscreenMenubarState)state { |
| + return menubarState_; |
| +} |
| + |
| +- (void)setMenubarProgress:(CGFloat)progress { |
| + if (ui::IsCGFloatEqual(progress, 1.0)) |
| + menubarState_ = FullscreenMenubarState::SHOWN; |
| + else if (ui::IsCGFloatEqual(progress, 0.0)) |
| + menubarState_ = FullscreenMenubarState::HIDDEN; |
| + else if (progress < menubarFraction_) |
| + menubarState_ = FullscreenMenubarState::HIDING; |
| + else if (progress > menubarFraction_) |
| + menubarState_ = FullscreenMenubarState::SHOWING; |
| + menubarFraction_ = progress; |
| +} |
| + |
| +@end |
| + |
| +namespace { |
| + |
| +// The amount by which the toolbar is offset downwards (to avoid the menu). |
| +// Copied from FullscreenToolbarController. |
| +const CGFloat kToolbarVerticalOffset = -22; |
|
erikchen
2016/11/21 18:45:04
maybe we should expose this as well to avoid dupli
spqchan
2016/11/22 20:18:42
Done.
|
| + |
| +class FullscreenToolbarControllerTest : public testing::Test { |
| + public: |
| + FullscreenToolbarControllerTest() {} |
| + void SetUp() override { |
| + BOOL yes = YES; |
| + BOOL no = NO; |
| + |
| + id bwc = [OCMockObject mockForClass:[BrowserWindowController class]]; |
|
erikchen
2016/11/21 18:45:05
should we retain bwc for the duration of this test
spqchan
2016/11/22 20:18:42
Done.
|
| + [[[bwc stub] andReturnValue:OCMOCK_VALUE(yes)] |
| + isKindOfClass:[BrowserWindowController class]]; |
| + [[[bwc stub] andReturnValue:OCMOCK_VALUE(yes)] isInAppKitFullscreen]; |
| + [[[bwc stub] andReturnValue:OCMOCK_VALUE(no)] isInImmersiveFullscreen]; |
| + [[bwc stub] layoutSubviews]; |
| + |
| + controller_.reset( |
| + [[FullscreenToolbarController alloc] initWithBrowserController:bwc]); |
| + SetToolbarStyle(FullscreenToolbarStyle::TOOLBAR_HIDDEN); |
| + |
| + menubar_tracker_ = [[MockFullscreenMenubarTracker alloc] |
|
erikchen
2016/11/21 18:45:04
should we be using scoped_nsobject here?
spqchan
2016/11/22 20:18:42
No, FullscreenToolbarController will be holding it
Robert Sesek
2016/11/22 21:02:43
I think it's a little atypical for a setter to tra
|
| + initWithFullscreenToolbarController:nil]; |
| + [menubar_tracker_ setMenubarProgress:0.0]; |
| + [controller_ setMenubarTracker:menubar_tracker_]; |
| + |
| + mouse_tracker_ = [[MockFullscreenToolbarMouseTracker alloc] init]; |
| + [controller_ setMouseTracker:mouse_tracker_]; |
| + |
| + [controller_ animationController]->SetAnimationDuration(0.0); |
| + |
| + [controller_ setTestFullscreenMode:YES]; |
| + } |
| + |
| + void TearDown() override { [controller_ setTestFullscreenMode:NO]; } |
| + |
| + void SetToolbarStyle(FullscreenToolbarStyle style) { |
| + [controller_ setToolbarStyle:style]; |
| + } |
| + |
| + void CheckLayout(CGFloat toolbarFraction, CGFloat menubarFraction) { |
| + FullscreenToolbarLayout layout = [controller_ computeLayout]; |
| + DCHECK_EQ(layout.toolbarFraction, toolbarFraction); |
| + DCHECK_EQ(layout.menubarOffset, menubarFraction * kToolbarVerticalOffset); |
| + } |
| + |
| + // The FullscreenToolbarController object being tested. |
| + base::scoped_nsobject<FullscreenToolbarController> controller_; |
| + |
| + // Mocks the state of the menubar. |
| + MockFullscreenMenubarTracker* menubar_tracker_; |
| + |
| + // Mocks the mouse interactions with the toolbar. |
| + MockFullscreenToolbarMouseTracker* mouse_tracker_; |
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(FullscreenToolbarControllerTest); |
| +}; |
| + |
| +// Tests the toolbar fraction for the TOOLBAR_NONE and TOOLBAR_PRESENT |
| +// styles. |
| +TEST_F(FullscreenToolbarControllerTest, TestPresentAndNoneToolbarStyle) { |
| + CheckLayout(0, 0); |
| + |
| + [controller_ setToolbarStyle:FullscreenToolbarStyle::TOOLBAR_NONE]; |
| + CheckLayout(0, 0); |
| + |
| + [controller_ setToolbarStyle:FullscreenToolbarStyle::TOOLBAR_PRESENT]; |
| + CheckLayout(1, 0); |
| +} |
| + |
| +// Basic test that checks if the toolbar fraction for different menubar values. |
| +// This test simulates the showing and hiding the menubar. |
| +TEST_F(FullscreenToolbarControllerTest, TestHiddenToolbarWithMenubar) { |
| + CheckLayout(0, 0); |
| + |
| + [menubar_tracker_ setMenubarProgress:0.5]; |
| + CheckLayout(0.5, 0.5); |
| + |
| + [menubar_tracker_ setMenubarProgress:1]; |
| + CheckLayout(1, 1); |
| + |
| + [menubar_tracker_ setMenubarProgress:0.5]; |
| + CheckLayout(0.5, 0.5); |
| + |
| + [menubar_tracker_ setMenubarProgress:0]; |
| + CheckLayout(0, 0); |
| +} |
| + |
| +// Test that checks the visibility lock functions and the toolbar fraction. |
| +TEST_F(FullscreenToolbarControllerTest, TestHiddenToolbarWithVisibilityLocks) { |
| + FullscreenToolbarVisibilityLockController* locks = |
| + [controller_ visibilityLockController]; |
| + base::scoped_nsobject<NSObject> owner([[NSObject alloc] init]); |
| + base::scoped_nsobject<NSObject> alternative_owner([[NSObject alloc] init]); |
| + |
| + [menubar_tracker_ setMenubarProgress:0]; |
| + CheckLayout(0, 0); |
| + |
| + // Lock the toolbar visibility. Toolbar should be fully visible. |
| + [locks lockToolbarVisibilityForOwner:owner.get() withAnimation:NO]; |
| + DCHECK([locks isToolbarVisibilityLocked]); |
| + DCHECK([locks isToolbarVisibilityLockedForOwner:owner.get()]); |
| + DCHECK(![locks isToolbarVisibilityLockedForOwner:alternative_owner.get()]); |
| + CheckLayout(1, 0); |
| + |
| + // Show the menubar. |
| + [menubar_tracker_ setMenubarProgress:1]; |
| + CheckLayout(1, 1); |
| + |
| + // Hide the menubar. The toolbar should still be fully visible. |
| + [menubar_tracker_ setMenubarProgress:0.5]; |
| + CheckLayout(1, 0.5); |
| + [menubar_tracker_ setMenubarProgress:0]; |
| + CheckLayout(1, 0); |
| + |
| + // Release the lock. Toolbar should now be hidden. |
| + [locks releaseToolbarVisibilityForOwner:owner.get() withAnimation:NO]; |
| + DCHECK(![locks isToolbarVisibilityLocked]); |
| + DCHECK(![locks isToolbarVisibilityLockedForOwner:owner.get()]); |
| + CheckLayout(0, 0); |
| + |
| + // Lock and release the toolbar visibility with multiple owners. |
| + [locks lockToolbarVisibilityForOwner:owner.get() withAnimation:NO]; |
| + [locks lockToolbarVisibilityForOwner:alternative_owner.get() |
| + withAnimation:NO]; |
| + DCHECK([locks isToolbarVisibilityLocked]); |
| + DCHECK([locks isToolbarVisibilityLockedForOwner:owner.get()]); |
| + DCHECK([locks isToolbarVisibilityLockedForOwner:alternative_owner.get()]); |
| + CheckLayout(1, 0); |
| + |
| + [locks releaseToolbarVisibilityForOwner:owner.get() withAnimation:NO]; |
| + DCHECK([locks isToolbarVisibilityLocked]); |
| + DCHECK(![locks isToolbarVisibilityLockedForOwner:owner.get()]); |
| + DCHECK([locks isToolbarVisibilityLockedForOwner:alternative_owner.get()]); |
| + CheckLayout(1, 0); |
| + |
| + [locks releaseToolbarVisibilityForOwner:alternative_owner.get() |
| + withAnimation:NO]; |
| + DCHECK(![locks isToolbarVisibilityLocked]); |
| + DCHECK(![locks isToolbarVisibilityLockedForOwner:alternative_owner.get()]); |
| + CheckLayout(0, 0); |
| +} |
| + |
| +// Basic test that checks the toolbar fraction for different mouse tracking |
| +// values. |
| +TEST_F(FullscreenToolbarControllerTest, TestHiddenToolbarWithMouseTracking) { |
| + CheckLayout(0, 0); |
| + |
| + [mouse_tracker_ setMouseInside:YES]; |
| + CheckLayout(1, 0); |
| + |
| + [menubar_tracker_ setMenubarProgress:1]; |
| + CheckLayout(1, 1); |
| + |
| + [menubar_tracker_ setMenubarProgress:0]; |
| + CheckLayout(1, 0); |
| + |
| + [mouse_tracker_ setMouseInside:NO]; |
| + CheckLayout(0, 0); |
| +} |
| + |
| +// Test that checks the toolbar fraction with mouse tracking, menubar fraction, |
| +// and visibility locks. |
| +TEST_F(FullscreenToolbarControllerTest, TestHiddenToolbarWithMultipleFactors) { |
| + FullscreenToolbarVisibilityLockController* locks = |
| + [controller_ visibilityLockController]; |
| + base::scoped_nsobject<NSObject> owner([[NSObject alloc] init]); |
| + CheckLayout(0, 0); |
| + |
| + // Toolbar should be shown with the menubar. |
| + [menubar_tracker_ setMenubarProgress:1]; |
| + CheckLayout(1, 1); |
| + |
| + // Move the mouse to the toolbar and start hiding the menubar. Toolbar |
| + // should be fully visible. |
| + [mouse_tracker_ setMouseInside:YES]; |
| + CheckLayout(1, 1); |
| + [menubar_tracker_ setMenubarProgress:0.5]; |
| + CheckLayout(1, 0.5); |
| + |
| + // Lock the toolbar's visibility. |
| + [locks lockToolbarVisibilityForOwner:owner.get() withAnimation:NO]; |
| + CheckLayout(1, 0.5); |
| + |
| + // Hide the menubar. Toolbar should be fully visible. |
| + [menubar_tracker_ setMenubarProgress:0]; |
| + CheckLayout(1, 0); |
| + |
| + // Lock the toolbar's visibility. Toolbar should be fully visible. |
| + [locks releaseToolbarVisibilityForOwner:owner.get() withAnimation:NO]; |
| + CheckLayout(1, 0); |
| + |
| + // Move the mouse away from the toolbar. Toolbar should hide. |
| + [mouse_tracker_ setMouseInside:NO]; |
| + CheckLayout(0, 0); |
| + |
| + // Lock the toolbar and move the mouse to it. |
| + [locks lockToolbarVisibilityForOwner:owner.get() withAnimation:NO]; |
| + [mouse_tracker_ setMouseInside:YES]; |
| + CheckLayout(1, 0); |
| + |
| + // Move the mouse away from the toolbar. Toolbar should be fully visible. |
| + [mouse_tracker_ setMouseInside:NO]; |
| + CheckLayout(1, 0); |
| + |
| + // Release the toolbar. Toolbar should be hidden. |
| + [locks releaseToolbarVisibilityForOwner:owner.get() withAnimation:NO]; |
| +} |
| + |
| +} // namespace |