Infobar material design refresh: layout
[chromium-blink-merge.git] / chrome / browser / ui / views / certificate_selector_browsertest.cc
blob1e335eb95ac5bdfac310d716f9f11b5481ed2737
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.
5 #include "base/bind.h"
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"
20 namespace {
22 class TestCertificateSelector : public chrome::CertificateSelector {
23 public:
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())
30 on_destroy_.Run();
33 void Init() {
34 InitWithText(make_scoped_ptr(
35 new views::Label(base::ASCIIToUTF16("some arbitrary text"))));
38 bool Accept() override {
39 if (accepted_)
40 *accepted_ = true;
41 return CertificateSelector::Accept();
44 bool Cancel() override {
45 if (canceled_)
46 *canceled_ = true;
47 return CertificateSelector::Cancel();
50 void TrackState(bool* accepted, bool* canceled) {
51 accepted_ = accepted;
52 canceled_ = canceled;
55 void set_on_destroy(base::Closure on_destroy) { on_destroy_ = on_destroy; }
57 private:
58 bool* accepted_ = nullptr;
59 bool* canceled_ = nullptr;
60 base::Closure on_destroy_;
62 DISALLOW_COPY_AND_ASSIGN(TestCertificateSelector);
65 class CertificateSelectorTest : public InProcessBrowserTest {
66 public:
67 void SetUpInProcessBrowserTestFixture() override {
68 client_1_ =
69 net::ImportCertFromFile(net::GetTestCertsDirectory(), "client_1.pem");
70 ASSERT_NE(nullptr, client_1_);
72 client_2_ =
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());
87 selector_->Init();
88 selector_->Show();
91 protected:
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;
100 } // namespace
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);
117 base::RunLoop loop;
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.
124 loop.Run();
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);