1 // Copyright 2015 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.
6 #include "base/files/file_path.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/browser/ui/browser.h"
9 #include "chrome/browser/ui/tabs/tab_strip_model.h"
10 #include "chrome/browser/ui/views/certificate_selector.h"
11 #include "chrome/test/base/in_process_browser_test.h"
12 #include "chrome/test/base/interactive_test_utils.h"
13 #include "content/public/test/browser_test_utils.h"
14 #include "net/base/test_data_directory.h"
15 #include "net/cert/x509_certificate.h"
16 #include "net/test/cert_test_util.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18 #include "ui/views/controls/label.h"
22 class TestCertificateSelector
: public chrome::CertificateSelector
{
24 TestCertificateSelector(const net::CertificateList
& certificates
,
25 content::WebContents
* web_contents
)
26 : CertificateSelector(certificates
, web_contents
) {}
28 ~TestCertificateSelector() override
{
29 if (!on_destroy_
.is_null())
34 InitWithText(make_scoped_ptr(
35 new views::Label(base::ASCIIToUTF16("some arbitrary text"))));
38 bool Accept() override
{
41 return CertificateSelector::Accept();
44 bool Cancel() override
{
47 return CertificateSelector::Cancel();
50 void TrackState(bool* accepted
, bool* canceled
) {
55 void set_on_destroy(base::Closure on_destroy
) { on_destroy_
= on_destroy
; }
58 bool* accepted_
= nullptr;
59 bool* canceled_
= nullptr;
60 base::Closure on_destroy_
;
62 DISALLOW_COPY_AND_ASSIGN(TestCertificateSelector
);
65 class CertificateSelectorTest
: public InProcessBrowserTest
{
67 void SetUpInProcessBrowserTestFixture() override
{
69 net::ImportCertFromFile(net::GetTestCertsDirectory(), "client_1.pem");
70 ASSERT_NE(nullptr, client_1_
);
73 net::ImportCertFromFile(net::GetTestCertsDirectory(), "client_2.pem");
74 ASSERT_NE(nullptr, client_2_
);
77 void SetUpOnMainThread() override
{
78 ASSERT_TRUE(content::WaitForLoadStop(
79 browser()->tab_strip_model()->GetActiveWebContents()));
81 net::CertificateList certificates
;
82 certificates
.push_back(client_1_
);
83 certificates
.push_back(client_2_
);
85 selector_
= new TestCertificateSelector(
86 certificates
, browser()->tab_strip_model()->GetActiveWebContents());
92 scoped_refptr
<net::X509Certificate
> client_1_
;
93 scoped_refptr
<net::X509Certificate
> client_2_
;
95 // The selector will be owned by the Views hierarchy and will at latest be
96 // deleted during the browser shutdown.
97 TestCertificateSelector
* selector_
= nullptr;
102 IN_PROC_BROWSER_TEST_F(CertificateSelectorTest
, GetSelectedCert
) {
103 EXPECT_EQ(client_1_
.get(), selector_
->GetSelectedCert());
104 EXPECT_TRUE(ui_test_utils::SendKeyPressSync(browser(), ui::VKEY_DOWN
, false,
105 false, false, false));
106 EXPECT_EQ(client_2_
.get(), selector_
->GetSelectedCert());
107 EXPECT_TRUE(ui_test_utils::SendKeyPressSync(browser(), ui::VKEY_UP
, false,
108 false, false, false));
109 EXPECT_EQ(client_1_
.get(), selector_
->GetSelectedCert());
112 IN_PROC_BROWSER_TEST_F(CertificateSelectorTest
, DoubleClick
) {
113 bool accepted
= false;
114 bool canceled
= false;
115 selector_
->TrackState(&accepted
, &canceled
);
118 selector_
->set_on_destroy(loop
.QuitClosure());
120 // Simulate double clicking on an entry in the certificate list.
121 selector_
->OnDoubleClick();
123 // Wait for the dialog to be closed and destroyed.
126 // Closing the dialog through a double click must call only the Accept()
127 // function and not Cancel().
128 EXPECT_TRUE(accepted
);
129 EXPECT_FALSE(canceled
);