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 #import "chrome/browser/ui/cocoa/ssl_client_certificate_selector_cocoa.h"
7 #import <SecurityInterface/SFChooseIdentityPanel.h>
10 #import "base/mac/mac_util.h"
11 #include "base/macros.h"
12 #include "chrome/browser/ssl/ssl_client_certificate_selector.h"
13 #include "chrome/browser/ssl/ssl_client_certificate_selector_test.h"
14 #include "chrome/browser/ui/browser.h"
15 #include "chrome/browser/ui/browser_commands.h"
16 #include "chrome/browser/ui/tabs/tab_strip_model.h"
17 #include "components/web_modal/web_contents_modal_dialog_manager.h"
18 #include "content/public/browser/client_certificate_delegate.h"
19 #include "content/public/browser/web_contents.h"
20 #include "content/public/test/test_utils.h"
21 #include "ui/base/cocoa/window_size_constants.h"
23 using web_modal::WebContentsModalDialogManager;
27 class TestClientCertificateDelegate
28 : public content::ClientCertificateDelegate {
30 // Creates a ClientCertificateDelegate that sets |*destroyed| to true on
32 explicit TestClientCertificateDelegate(bool* destroyed)
33 : destroyed_(destroyed) {}
35 ~TestClientCertificateDelegate() override {
36 if (destroyed_ != nullptr)
40 // content::ClientCertificateDelegate.
41 void ContinueWithCertificate(net::X509Certificate* cert) override {
42 // TODO(davidben): Add a test which explicitly tests selecting a
43 // certificate, or selecting no certificate, since closing the dialog
44 // (normally by closing the tab) is not the same as explicitly selecting no
46 ADD_FAILURE() << "Certificate selected";
52 DISALLOW_COPY_AND_ASSIGN(TestClientCertificateDelegate);
57 typedef SSLClientCertificateSelectorTestBase
58 SSLClientCertificateSelectorCocoaTest;
60 // Flaky on 10.7; crbug.com/313243
61 IN_PROC_BROWSER_TEST_F(SSLClientCertificateSelectorCocoaTest, DISABLED_Basic) {
62 // TODO(kbr): re-enable: http://crbug.com/222296
63 if (base::mac::IsOSMountainLionOrLater())
66 content::WebContents* web_contents =
67 browser()->tab_strip_model()->GetActiveWebContents();
68 WebContentsModalDialogManager* web_contents_modal_dialog_manager =
69 WebContentsModalDialogManager::FromWebContents(web_contents);
70 EXPECT_FALSE(web_contents_modal_dialog_manager->IsDialogActive());
72 bool destroyed = false;
73 SSLClientCertificateSelectorCocoa* selector = [
74 [SSLClientCertificateSelectorCocoa alloc]
75 initWithBrowserContext:web_contents->GetBrowserContext()
76 certRequestInfo:auth_requestor_->cert_request_info_.get()
77 delegate:make_scoped_ptr(new TestClientCertificateDelegate(
79 [selector displayForWebContents:web_contents];
80 content::RunAllPendingInMessageLoop();
81 EXPECT_TRUE([selector panel]);
82 EXPECT_TRUE(web_contents_modal_dialog_manager->IsDialogActive());
84 WebContentsModalDialogManager::TestApi test_api(
85 web_contents_modal_dialog_manager);
86 test_api.CloseAllDialogs();
87 content::RunAllPendingInMessageLoop();
88 EXPECT_FALSE(web_contents_modal_dialog_manager->IsDialogActive());
90 EXPECT_TRUE(destroyed);
93 // Test that switching to another tab correctly hides the sheet.
94 IN_PROC_BROWSER_TEST_F(SSLClientCertificateSelectorCocoaTest, HideShow) {
95 content::WebContents* web_contents =
96 browser()->tab_strip_model()->GetActiveWebContents();
97 SSLClientCertificateSelectorCocoa* selector = [
98 [SSLClientCertificateSelectorCocoa alloc]
99 initWithBrowserContext:web_contents->GetBrowserContext()
100 certRequestInfo:auth_requestor_->cert_request_info_.get()
101 delegate:make_scoped_ptr(
102 new TestClientCertificateDelegate(nullptr))];
103 [selector displayForWebContents:web_contents];
104 content::RunAllPendingInMessageLoop();
106 NSWindow* sheetWindow = [[selector overlayWindow] attachedSheet];
107 NSRect sheetFrame = [sheetWindow frame];
108 EXPECT_EQ(1.0, [sheetWindow alphaValue]);
110 // Switch to another tab and verify that the sheet is hidden.
111 AddBlankTabAndShow(browser());
112 EXPECT_EQ(0.0, [sheetWindow alphaValue]);
113 EXPECT_TRUE(NSEqualRects(ui::kWindowSizeDeterminedLater,
114 [sheetWindow frame]));
116 // Switch back and verify that the sheet is shown.
117 chrome::SelectNumberedTab(browser(), 0);
118 EXPECT_EQ(1.0, [sheetWindow alphaValue]);
119 EXPECT_TRUE(NSEqualRects(sheetFrame, [sheetWindow frame]));