1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
// Copyright (c) 2011 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.
#include "base/mac/scoped_nsobject.h"
#include "base/memory/scoped_ptr.h"
#include "base/strings/string_util.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/fullscreen.h"
#include "chrome/browser/ui/bookmarks/bookmark_utils.h"
#import "chrome/browser/ui/cocoa/browser_window_cocoa.h"
#import "chrome/browser/ui/cocoa/browser_window_controller.h"
#include "chrome/browser/ui/cocoa/cocoa_profile_test.h"
#include "chrome/test/base/testing_profile.h"
#include "content/public/browser/notification_details.h"
#include "testing/gtest/include/gtest/gtest.h"
#import "third_party/ocmock/OCMock/OCMock.h"
#import "third_party/ocmock/gtest_support.h"
// Main test class.
class BrowserWindowCocoaTest : public CocoaProfileTest {
void SetUp() override {
CocoaProfileTest::SetUp();
ASSERT_TRUE(browser());
controller_ = [[BrowserWindowController alloc] initWithBrowser:browser()
takeOwnership:NO];
}
void TearDown() override {
[controller_ close];
CocoaProfileTest::TearDown();
}
public:
BrowserWindowController* controller_;
};
TEST_F(BrowserWindowCocoaTest, TestBookmarkBarVisible) {
scoped_ptr<BrowserWindowCocoa> bwc(
new BrowserWindowCocoa(browser(), controller_));
bool before = bwc->IsBookmarkBarVisible();
chrome::ToggleBookmarkBarWhenVisible(profile());
EXPECT_NE(before, bwc->IsBookmarkBarVisible());
chrome::ToggleBookmarkBarWhenVisible(profile());
EXPECT_EQ(before, bwc->IsBookmarkBarVisible());
}
// Tests that BrowserWindowCocoa::Close mimics the behavior of
// -[NSWindow performClose:].
class BrowserWindowCocoaCloseTest : public CocoaProfileTest {
public:
BrowserWindowCocoaCloseTest()
: controller_(
[OCMockObject mockForClass:[BrowserWindowController class]]),
window_([OCMockObject mockForClass:[NSWindow class]]) {
[[[controller_ stub] andReturn:nil] overlayWindow];
}
void CreateAndCloseBrowserWindow() {
BrowserWindowCocoa browser_window(browser(), controller_);
browser_window.Close();
}
id ValueYES() {
BOOL v = YES;
return OCMOCK_VALUE(v);
}
id ValueNO() {
BOOL v = NO;
return OCMOCK_VALUE(v);
}
protected:
id controller_;
id window_;
};
TEST_F(BrowserWindowCocoaCloseTest, DelegateRespondsYes) {
[[[window_ stub] andReturn:controller_] delegate];
[[[controller_ stub] andReturn:window_] window];
[[[controller_ stub] andReturnValue:ValueYES()] windowShouldClose:window_];
[[window_ expect] orderOut:nil];
[[window_ expect] close];
CreateAndCloseBrowserWindow();
EXPECT_OCMOCK_VERIFY(controller_);
EXPECT_OCMOCK_VERIFY(window_);
}
TEST_F(BrowserWindowCocoaCloseTest, DelegateRespondsNo) {
[[[window_ stub] andReturn:controller_] delegate];
[[[controller_ stub] andReturn:window_] window];
[[[controller_ stub] andReturnValue:ValueNO()] windowShouldClose:window_];
// Window should not be closed.
CreateAndCloseBrowserWindow();
EXPECT_OCMOCK_VERIFY(controller_);
EXPECT_OCMOCK_VERIFY(window_);
}
// NSWindow does not implement |-windowShouldClose:|, but subclasses can
// implement it, and |-performClose:| will invoke it if implemented.
@interface BrowserWindowCocoaCloseWindow : NSWindow
- (BOOL)windowShouldClose:(id)window;
@end
@implementation BrowserWindowCocoaCloseWindow
- (BOOL)windowShouldClose:(id)window {
return YES;
}
@end
TEST_F(BrowserWindowCocoaCloseTest, WindowRespondsYes) {
window_ = [OCMockObject mockForClass:[BrowserWindowCocoaCloseWindow class]];
[[[window_ stub] andReturn:nil] delegate];
[[[controller_ stub] andReturn:window_] window];
[[[window_ stub] andReturnValue:ValueYES()] windowShouldClose:window_];
[[window_ expect] orderOut:nil];
[[window_ expect] close];
CreateAndCloseBrowserWindow();
EXPECT_OCMOCK_VERIFY(controller_);
EXPECT_OCMOCK_VERIFY(window_);
}
TEST_F(BrowserWindowCocoaCloseTest, WindowRespondsNo) {
window_ = [OCMockObject mockForClass:[BrowserWindowCocoaCloseWindow class]];
[[[window_ stub] andReturn:nil] delegate];
[[[controller_ stub] andReturn:window_] window];
[[[window_ stub] andReturnValue:ValueNO()] windowShouldClose:window_];
// Window should not be closed.
CreateAndCloseBrowserWindow();
EXPECT_OCMOCK_VERIFY(controller_);
EXPECT_OCMOCK_VERIFY(window_);
}
TEST_F(BrowserWindowCocoaCloseTest, DelegateRespondsYesWindowRespondsNo) {
window_ = [OCMockObject mockForClass:[BrowserWindowCocoaCloseWindow class]];
[[[window_ stub] andReturn:controller_] delegate];
[[[controller_ stub] andReturn:window_] window];
[[[controller_ stub] andReturnValue:ValueYES()] windowShouldClose:window_];
[[[window_ stub] andReturnValue:ValueNO()] windowShouldClose:window_];
[[window_ expect] orderOut:nil];
[[window_ expect] close];
CreateAndCloseBrowserWindow();
EXPECT_OCMOCK_VERIFY(controller_);
EXPECT_OCMOCK_VERIFY(window_);
}
TEST_F(BrowserWindowCocoaCloseTest, DelegateRespondsNoWindowRespondsYes) {
window_ = [OCMockObject mockForClass:[BrowserWindowCocoaCloseWindow class]];
[[[window_ stub] andReturn:controller_] delegate];
[[[controller_ stub] andReturn:window_] window];
[[[controller_ stub] andReturnValue:ValueNO()] windowShouldClose:window_];
[[[window_ stub] andReturnValue:ValueYES()] windowShouldClose:window_];
// Window should not be closed.
CreateAndCloseBrowserWindow();
EXPECT_OCMOCK_VERIFY(controller_);
EXPECT_OCMOCK_VERIFY(window_);
}
TEST_F(BrowserWindowCocoaCloseTest, NoResponseFromDelegateNorWindow) {
[[[window_ stub] andReturn:nil] delegate];
[[[controller_ stub] andReturn:window_] window];
[[window_ expect] orderOut:nil];
[[window_ expect] close];
CreateAndCloseBrowserWindow();
EXPECT_OCMOCK_VERIFY(controller_);
EXPECT_OCMOCK_VERIFY(window_);
}
// TODO(???): test other methods of BrowserWindowCocoa
|