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 #include "base/debug/debugger.h"
6 #import "base/mac/foundation_util.h"
7 #include "base/mac/scoped_nsobject.h"
8 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h"
9 #import "chrome/browser/ui/cocoa/tab_contents/sad_tab_controller.h"
10 #import "chrome/browser/ui/cocoa/tab_contents/sad_tab_view_cocoa.h"
11 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
12 #include "chrome/test/base/testing_profile.h"
13 #import "ui/base/cocoa/controls/hyperlink_text_view.h"
15 @interface SadTabView (ExposedForTesting)
16 // Implementation is below.
17 - (HyperlinkTextView*)helpTextView;
20 @implementation SadTabView (ExposedForTesting)
21 - (HyperlinkTextView*)helpTextView {
22 NSView* containerView = [[self subviews] lastObject];
23 for (NSView* view in [containerView subviews]) {
24 if (auto textView = base::mac::ObjCCast<HyperlinkTextView>(view))
33 class SadTabControllerTest : public ChromeRenderViewHostTestHarness {
35 SadTabControllerTest() : test_window_(nil) {
36 link_clicked_ = false;
39 void SetUp() override {
40 ChromeRenderViewHostTestHarness::SetUp();
41 // Inherting from ChromeRenderViewHostTestHarness means we can't inherit
42 // from from CocoaTest, so do a bootstrap and create test window.
43 CocoaTest::BootstrapCocoa();
44 test_window_ = [[CocoaTestHelperWindow alloc] init];
45 if (base::debug::BeingDebugged()) {
46 [test_window_ orderFront:nil];
48 [test_window_ orderBack:nil];
52 void TearDown() override {
55 ChromeRenderViewHostTestHarness::TearDown();
58 // Creates the controller and adds its view to contents, caller has ownership.
59 SadTabController* CreateController() {
60 SadTabController* controller =
61 [[SadTabController alloc] initWithWebContents:web_contents()];
62 EXPECT_TRUE(controller);
63 NSView* view = [controller view];
65 NSView* contentView = [test_window_ contentView];
66 [contentView addSubview:view];
71 HyperlinkTextView* GetHelpTextView(SadTabController* controller) {
72 SadTabView* view = static_cast<SadTabView*>([controller view]);
73 return ([view helpTextView]);
76 static bool link_clicked_;
77 CocoaTestHelperWindow* test_window_;
81 bool SadTabControllerTest::link_clicked_;
83 TEST_F(SadTabControllerTest, ClickOnLink) {
84 base::scoped_nsobject<SadTabController> controller(CreateController());
85 HyperlinkTextView* help = GetHelpTextView(controller);
87 EXPECT_FALSE(link_clicked_);
88 [help clickedOnLink:nil atIndex:0];
89 EXPECT_TRUE(link_clicked_);
94 @implementation NSApplication (SadTabControllerUnitTest)
95 // Add handler for the openLearnMoreAboutCrashLink: action to NSApp for testing
96 // purposes. Normally this would be sent up the responder tree correctly, but
97 // since tests run in the background, key window and main window are never set
98 // on NSApplication. Adding it to NSApplication directly removes the need for
99 // worrying about what the current window with focus is.
100 - (void)openLearnMoreAboutCrashLink:(id)sender {
101 SadTabControllerTest::link_clicked_ = true;