| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/debug/debugger.h" | 5 #include "base/debug/debugger.h" |
| 6 #import "base/mac/foundation_util.h" | 6 #import "base/mac/foundation_util.h" |
| 7 #include "base/mac/scoped_nsobject.h" | 7 #include "base/mac/scoped_nsobject.h" |
| 8 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h" | 8 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h" |
| 9 #import "chrome/browser/ui/cocoa/tab_contents/sad_tab_controller.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" | 10 #import "chrome/browser/ui/cocoa/tab_contents/sad_tab_view_cocoa.h" |
| 11 #include "chrome/common/url_constants.h" | 11 #include "chrome/common/url_constants.h" |
| 12 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 12 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 13 #include "chrome/test/base/testing_profile.h" | 13 #include "chrome/test/base/testing_profile.h" |
| 14 #include "content/public/browser/web_contents.h" |
| 15 #include "content/public/browser/web_contents_delegate.h" |
| 14 #import "ui/base/cocoa/controls/hyperlink_text_view.h" | 16 #import "ui/base/cocoa/controls/hyperlink_text_view.h" |
| 15 | 17 |
| 16 @interface SadTabView (ExposedForTesting) | 18 @interface SadTabView (ExposedForTesting) |
| 17 // Implementation is below. | 19 // Implementation is below. |
| 18 - (HyperlinkTextView*)helpTextView; | 20 - (HyperlinkTextView*)helpTextView; |
| 19 @end | 21 @end |
| 20 | 22 |
| 21 @implementation SadTabView (ExposedForTesting) | 23 @implementation SadTabView (ExposedForTesting) |
| 22 - (HyperlinkTextView*)helpTextView { | 24 - (HyperlinkTextView*)helpTextView { |
| 23 NSView* containerView = [[self subviews] lastObject]; | 25 NSView* containerView = [[self subviews] lastObject]; |
| 24 for (NSView* view in [containerView subviews]) { | 26 for (NSView* view in [containerView subviews]) { |
| 25 if (auto textView = base::mac::ObjCCast<HyperlinkTextView>(view)) | 27 if (auto textView = base::mac::ObjCCast<HyperlinkTextView>(view)) |
| 26 return textView; | 28 return textView; |
| 27 } | 29 } |
| 28 return nil; | 30 return nil; |
| 29 } | 31 } |
| 30 @end | 32 @end |
| 31 | 33 |
| 32 namespace { | 34 namespace { |
| 33 | 35 |
| 36 class ContentsDelegate : public content::WebContentsDelegate { |
| 37 public: |
| 38 content::WebContents* OpenURLFromTab( |
| 39 content::WebContents* source, |
| 40 const content::OpenURLParams& params) override { |
| 41 opened_url_ = params.url; |
| 42 return nullptr; |
| 43 } |
| 44 |
| 45 const GURL& OpenedURL() { return opened_url_; } |
| 46 |
| 47 private: |
| 48 GURL opened_url_; |
| 49 }; |
| 50 |
| 34 class SadTabControllerTest : public ChromeRenderViewHostTestHarness { | 51 class SadTabControllerTest : public ChromeRenderViewHostTestHarness { |
| 35 public: | 52 public: |
| 36 SadTabControllerTest() : test_window_(nil) { | 53 SadTabControllerTest() : test_window_(nil) {} |
| 37 link_clicked_ = false; | |
| 38 } | |
| 39 | 54 |
| 40 void SetUp() override { | 55 void SetUp() override { |
| 41 ChromeRenderViewHostTestHarness::SetUp(); | 56 ChromeRenderViewHostTestHarness::SetUp(); |
| 57 web_contents()->SetDelegate(&contents_delegate_); |
| 42 // Inherting from ChromeRenderViewHostTestHarness means we can't inherit | 58 // Inherting from ChromeRenderViewHostTestHarness means we can't inherit |
| 43 // from from CocoaTest, so do a bootstrap and create test window. | 59 // from from CocoaTest, so do a bootstrap and create test window. |
| 44 CocoaTest::BootstrapCocoa(); | 60 CocoaTest::BootstrapCocoa(); |
| 45 test_window_ = [[CocoaTestHelperWindow alloc] init]; | 61 test_window_ = [[CocoaTestHelperWindow alloc] init]; |
| 46 if (base::debug::BeingDebugged()) { | 62 if (base::debug::BeingDebugged()) { |
| 47 [test_window_ orderFront:nil]; | 63 [test_window_ orderFront:nil]; |
| 48 } else { | 64 } else { |
| 49 [test_window_ orderBack:nil]; | 65 [test_window_ orderBack:nil]; |
| 50 } | 66 } |
| 51 } | 67 } |
| (...skipping 15 matching lines...) Expand all Loading... |
| 67 [contentView addSubview:view]; | 83 [contentView addSubview:view]; |
| 68 | 84 |
| 69 return controller; | 85 return controller; |
| 70 } | 86 } |
| 71 | 87 |
| 72 HyperlinkTextView* GetHelpTextView(SadTabController* controller) { | 88 HyperlinkTextView* GetHelpTextView(SadTabController* controller) { |
| 73 SadTabView* view = static_cast<SadTabView*>([controller view]); | 89 SadTabView* view = static_cast<SadTabView*>([controller view]); |
| 74 return ([view helpTextView]); | 90 return ([view helpTextView]); |
| 75 } | 91 } |
| 76 | 92 |
| 77 static bool link_clicked_; | 93 ContentsDelegate contents_delegate_; |
| 78 CocoaTestHelperWindow* test_window_; | 94 CocoaTestHelperWindow* test_window_; |
| 79 }; | 95 }; |
| 80 | 96 |
| 81 // static | |
| 82 bool SadTabControllerTest::link_clicked_; | |
| 83 | |
| 84 TEST_F(SadTabControllerTest, ClickOnLink) { | 97 TEST_F(SadTabControllerTest, ClickOnLink) { |
| 85 base::scoped_nsobject<SadTabController> controller(CreateController()); | 98 base::scoped_nsobject<SadTabController> controller(CreateController()); |
| 86 HyperlinkTextView* help = GetHelpTextView(controller); | 99 HyperlinkTextView* help = GetHelpTextView(controller); |
| 87 EXPECT_TRUE(help); | 100 EXPECT_TRUE(help); |
| 88 EXPECT_FALSE(link_clicked_); | 101 EXPECT_TRUE(contents_delegate_.OpenedURL().is_empty()); |
| 89 [help clickedOnLink:@(chrome::kCrashReasonURL) atIndex:0]; | 102 [help clickedOnLink:@(chrome::kCrashReasonURL) atIndex:0]; |
| 90 EXPECT_TRUE(link_clicked_); | 103 EXPECT_EQ(contents_delegate_.OpenedURL(), GURL(chrome::kCrashReasonURL)); |
| 91 } | 104 } |
| 92 | 105 |
| 93 } // namespace | 106 } // namespace |
| 94 | |
| 95 @implementation NSApplication (SadTabControllerUnitTest) | |
| 96 // Add handler for the openLearnMoreAboutCrashLink: action to NSApp for testing | |
| 97 // purposes. Normally this would be sent up the responder tree correctly, but | |
| 98 // since tests run in the background, key window and main window are never set | |
| 99 // on NSApplication. Adding it to NSApplication directly removes the need for | |
| 100 // worrying about what the current window with focus is. | |
| 101 - (void)openLearnMoreAboutCrashLink:(id)sender { | |
| 102 SadTabControllerTest::link_clicked_ = true; | |
| 103 } | |
| 104 | |
| 105 @end | |
| OLD | NEW |