Chromium Code Reviews| 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 struct ContentsDelegate : public content::WebContentsDelegate { | |
|
Avi (use Gerrit)
2016/08/12 23:06:22
This should be a class.
https://google.github.io/
Sidney San Martín
2016/08/12 23:35:10
Done.
| |
| 37 GURL opened_url; | |
| 38 | |
| 39 content::WebContents* OpenURLFromTab( | |
| 40 content::WebContents* source, | |
| 41 const content::OpenURLParams& params) override { | |
| 42 opened_url = params.url; | |
| 43 return nullptr; | |
| 44 } | |
| 45 }; | |
| 46 | |
| 34 class SadTabControllerTest : public ChromeRenderViewHostTestHarness { | 47 class SadTabControllerTest : public ChromeRenderViewHostTestHarness { |
| 35 public: | 48 public: |
| 36 SadTabControllerTest() : test_window_(nil) { | 49 SadTabControllerTest() : test_window_(nil) {} |
| 37 link_clicked_ = false; | |
| 38 } | |
| 39 | 50 |
| 40 void SetUp() override { | 51 void SetUp() override { |
| 41 ChromeRenderViewHostTestHarness::SetUp(); | 52 ChromeRenderViewHostTestHarness::SetUp(); |
| 53 web_contents()->SetDelegate(&contents_delegate_); | |
| 42 // Inherting from ChromeRenderViewHostTestHarness means we can't inherit | 54 // Inherting from ChromeRenderViewHostTestHarness means we can't inherit |
| 43 // from from CocoaTest, so do a bootstrap and create test window. | 55 // from from CocoaTest, so do a bootstrap and create test window. |
| 44 CocoaTest::BootstrapCocoa(); | 56 CocoaTest::BootstrapCocoa(); |
| 45 test_window_ = [[CocoaTestHelperWindow alloc] init]; | 57 test_window_ = [[CocoaTestHelperWindow alloc] init]; |
| 46 if (base::debug::BeingDebugged()) { | 58 if (base::debug::BeingDebugged()) { |
| 47 [test_window_ orderFront:nil]; | 59 [test_window_ orderFront:nil]; |
| 48 } else { | 60 } else { |
| 49 [test_window_ orderBack:nil]; | 61 [test_window_ orderBack:nil]; |
| 50 } | 62 } |
| 51 } | 63 } |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 67 [contentView addSubview:view]; | 79 [contentView addSubview:view]; |
| 68 | 80 |
| 69 return controller; | 81 return controller; |
| 70 } | 82 } |
| 71 | 83 |
| 72 HyperlinkTextView* GetHelpTextView(SadTabController* controller) { | 84 HyperlinkTextView* GetHelpTextView(SadTabController* controller) { |
| 73 SadTabView* view = static_cast<SadTabView*>([controller view]); | 85 SadTabView* view = static_cast<SadTabView*>([controller view]); |
| 74 return ([view helpTextView]); | 86 return ([view helpTextView]); |
| 75 } | 87 } |
| 76 | 88 |
| 77 static bool link_clicked_; | 89 ContentsDelegate contents_delegate_; |
| 78 CocoaTestHelperWindow* test_window_; | 90 CocoaTestHelperWindow* test_window_; |
| 79 }; | 91 }; |
| 80 | 92 |
| 81 // static | |
| 82 bool SadTabControllerTest::link_clicked_; | |
| 83 | |
| 84 TEST_F(SadTabControllerTest, ClickOnLink) { | 93 TEST_F(SadTabControllerTest, ClickOnLink) { |
| 85 base::scoped_nsobject<SadTabController> controller(CreateController()); | 94 base::scoped_nsobject<SadTabController> controller(CreateController()); |
| 86 HyperlinkTextView* help = GetHelpTextView(controller); | 95 HyperlinkTextView* help = GetHelpTextView(controller); |
| 87 EXPECT_TRUE(help); | 96 EXPECT_TRUE(help); |
| 88 EXPECT_FALSE(link_clicked_); | 97 EXPECT_TRUE(contents_delegate_.opened_url.is_empty()); |
| 89 [help clickedOnLink:@(chrome::kCrashReasonURL) atIndex:0]; | 98 [help clickedOnLink:@(chrome::kCrashReasonURL) atIndex:0]; |
| 90 EXPECT_TRUE(link_clicked_); | 99 EXPECT_EQ(contents_delegate_.opened_url, GURL(chrome::kCrashReasonURL)); |
| 91 } | 100 } |
| 92 | 101 |
| 93 } // namespace | 102 } // 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 |