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 #include "base/mac/scoped_nsobject.h"
7 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h"
8 #import "chrome/browser/ui/cocoa/hyperlink_text_view.h"
9 #import "chrome/browser/ui/cocoa/tab_contents/sad_tab_controller.h"
10 #import "chrome/browser/ui/cocoa/tab_contents/sad_tab_view.h"
11 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
12 #include "chrome/test/base/testing_profile.h"
14 @interface SadTabView (ExposedForTesting)
15 // Implementation is below.
16 - (HyperlinkTextView*)helpTextView;
19 @implementation SadTabView (ExposedForTesting)
20 - (HyperlinkTextView*)helpTextView {
27 class SadTabControllerTest : public ChromeRenderViewHostTestHarness {
29 SadTabControllerTest() : test_window_(nil) {
30 link_clicked_ = false;
33 virtual void SetUp() {
34 ChromeRenderViewHostTestHarness::SetUp();
35 // Inherting from ChromeRenderViewHostTestHarness means we can't inherit
36 // from from CocoaTest, so do a bootstrap and create test window.
37 CocoaTest::BootstrapCocoa();
38 test_window_ = [[CocoaTestHelperWindow alloc] init];
39 if (base::debug::BeingDebugged()) {
40 [test_window_ orderFront:nil];
42 [test_window_ orderBack:nil];
46 virtual void TearDown() {
49 ChromeRenderViewHostTestHarness::TearDown();
52 // Creates the controller and adds its view to contents, caller has ownership.
53 SadTabController* CreateController() {
54 SadTabController* controller =
55 [[SadTabController alloc] initWithWebContents:web_contents()];
56 EXPECT_TRUE(controller);
57 NSView* view = [controller view];
59 NSView* contentView = [test_window_ contentView];
60 [contentView addSubview:view];
65 HyperlinkTextView* GetHelpTextView(SadTabController* controller) {
66 SadTabView* view = static_cast<SadTabView*>([controller view]);
67 return ([view helpTextView]);
70 static bool link_clicked_;
71 CocoaTestHelperWindow* test_window_;
75 bool SadTabControllerTest::link_clicked_;
77 TEST_F(SadTabControllerTest, WithTabContents) {
78 base::scoped_nsobject<SadTabController> controller(CreateController());
79 EXPECT_TRUE(controller);
80 HyperlinkTextView* help = GetHelpTextView(controller);
84 TEST_F(SadTabControllerTest, WithoutTabContents) {
86 base::scoped_nsobject<SadTabController> controller(CreateController());
87 EXPECT_TRUE(controller);
88 HyperlinkTextView* help = GetHelpTextView(controller);
92 TEST_F(SadTabControllerTest, ClickOnLink) {
93 base::scoped_nsobject<SadTabController> controller(CreateController());
94 HyperlinkTextView* help = GetHelpTextView(controller);
96 EXPECT_FALSE(link_clicked_);
97 [help clickedOnLink:nil atIndex:0];
98 EXPECT_TRUE(link_clicked_);
103 @implementation NSApplication (SadTabControllerUnitTest)
104 // Add handler for the openLearnMoreAboutCrashLink: action to NSApp for testing
105 // purposes. Normally this would be sent up the responder tree correctly, but
106 // since tests run in the background, key window and main window are never set
107 // on NSApplication. Adding it to NSApplication directly removes the need for
108 // worrying about what the current window with focus is.
109 - (void)openLearnMoreAboutCrashLink:(id)sender {
110 SadTabControllerTest::link_clicked_ = true;