1 // Copyright (c) 2013 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/certificate_viewer.h"
7 #include "chrome/browser/ui/browser.h"
8 #include "chrome/browser/ui/browser_commands.h"
9 #include "chrome/browser/ui/cocoa/certificate_viewer_mac.h"
10 #include "chrome/browser/ui/tabs/tab_strip_model.h"
11 #include "chrome/test/base/in_process_browser_test.h"
12 #include "components/web_modal/web_contents_modal_dialog_manager.h"
13 #include "content/public/browser/web_contents.h"
14 #include "content/public/test/test_utils.h"
15 #include "net/base/test_data_directory.h"
16 #include "net/cert/x509_certificate.h"
17 #include "net/test/cert_test_util.h"
18 #include "ui/base/cocoa/window_size_constants.h"
20 using web_modal::WebContentsModalDialogManager;
22 typedef InProcessBrowserTest SSLCertificateViewerCocoaTest;
25 scoped_refptr<net::X509Certificate> GetSampleCertificate() {
26 return net::ImportCertFromFile(net::GetTestCertsDirectory(),
31 IN_PROC_BROWSER_TEST_F(SSLCertificateViewerCocoaTest, Basic) {
32 scoped_refptr<net::X509Certificate> cert = GetSampleCertificate();
33 ASSERT_TRUE(cert.get());
34 content::WebContents* web_contents =
35 browser()->tab_strip_model()->GetActiveWebContents();
36 gfx::NativeWindow window = web_contents->GetTopLevelNativeWindow();
37 WebContentsModalDialogManager* web_contents_modal_dialog_manager =
38 WebContentsModalDialogManager::FromWebContents(web_contents);
39 EXPECT_FALSE(web_contents_modal_dialog_manager->IsDialogActive());
41 ShowCertificateViewer(web_contents, window, cert.get());
43 content::RunAllPendingInMessageLoop();
44 EXPECT_TRUE(web_contents_modal_dialog_manager->IsDialogActive());
46 WebContentsModalDialogManager::TestApi test_api(
47 web_contents_modal_dialog_manager);
48 test_api.CloseAllDialogs();
49 content::RunAllPendingInMessageLoop();
50 EXPECT_FALSE(web_contents_modal_dialog_manager->IsDialogActive());
53 // Test that switching to another tab correctly hides the sheet.
54 IN_PROC_BROWSER_TEST_F(SSLCertificateViewerCocoaTest, HideShow) {
55 scoped_refptr<net::X509Certificate> cert = GetSampleCertificate();
56 ASSERT_TRUE(cert.get());
57 content::WebContents* web_contents =
58 browser()->tab_strip_model()->GetActiveWebContents();
60 SSLCertificateViewerCocoa* viewer =
61 [[SSLCertificateViewerCocoa alloc] initWithCertificate:cert.get()];
62 [viewer displayForWebContents:web_contents];
64 content::RunAllPendingInMessageLoop();
66 NSWindow* sheetWindow = [[viewer overlayWindow] attachedSheet];
67 NSRect sheetFrame = [sheetWindow frame];
68 EXPECT_EQ(1.0, [sheetWindow alphaValue]);
70 // Switch to another tab and verify that the sheet is hidden.
71 AddBlankTabAndShow(browser());
72 EXPECT_EQ(0.0, [sheetWindow alphaValue]);
74 NSEqualRects(ui::kWindowSizeDeterminedLater, [sheetWindow frame]));
76 // Switch back and verify that the sheet is shown.
77 chrome::SelectNumberedTab(browser(), 0);
78 EXPECT_EQ(1.0, [sheetWindow alphaValue]);
79 EXPECT_TRUE(NSEqualRects(sheetFrame, [sheetWindow frame]));