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"
21 class TestCertificateSelector
: public chrome::CertificateSelector
{
23 TestCertificateSelector(const net::CertificateList
& certificates
,
24 content::WebContents
* web_contents
)
25 : CertificateSelector(certificates
, web_contents
) {}
27 void Init() { InitWithText(base::ASCIIToUTF16("some arbitrary text")); }
30 DISALLOW_COPY_AND_ASSIGN(TestCertificateSelector
);
33 class CertificateSelectorTest
: public InProcessBrowserTest
{
35 void SetUpInProcessBrowserTestFixture() override
{
37 net::ImportCertFromFile(net::GetTestCertsDirectory(), "client_1.pem");
38 ASSERT_NE(nullptr, client_1_
);
41 net::ImportCertFromFile(net::GetTestCertsDirectory(), "client_2.pem");
42 ASSERT_NE(nullptr, client_2_
);
45 void SetUpOnMainThread() override
{
46 ASSERT_TRUE(content::WaitForLoadStop(
47 browser()->tab_strip_model()->GetActiveWebContents()));
49 net::CertificateList certificates
;
50 certificates
.push_back(client_1_
);
51 certificates
.push_back(client_2_
);
53 selector_
= new TestCertificateSelector(
54 certificates
, browser()->tab_strip_model()->GetActiveWebContents());
59 void TearDownOnMainThread() override
{
65 scoped_refptr
<net::X509Certificate
> client_1_
;
66 scoped_refptr
<net::X509Certificate
> client_2_
;
68 // The selector will be deleted when the browser is shutdown.
69 TestCertificateSelector
* selector_
= nullptr;
74 IN_PROC_BROWSER_TEST_F(CertificateSelectorTest
, GetSelectedCert
) {
75 EXPECT_EQ(client_1_
.get(), selector_
->GetSelectedCert());
76 EXPECT_TRUE(ui_test_utils::SendKeyPressSync(browser(), ui::VKEY_DOWN
, false,
77 false, false, false));
78 EXPECT_EQ(client_2_
.get(), selector_
->GetSelectedCert());
79 EXPECT_TRUE(ui_test_utils::SendKeyPressSync(browser(), ui::VKEY_UP
, false,
80 false, false, false));
81 EXPECT_EQ(client_1_
.get(), selector_
->GetSelectedCert());