Commit 83d9dc34 authored by tfarina@chromium.org's avatar tfarina@chromium.org

views: Make everything in crypto module password dialog private except the ctor.

R=pkasting@chromium.org

Review URL: https://chromiumcodereview.appspot.com/9956157

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@132516 0039d316-1c4b-4281-b951-d872f2087c98
parent 7c95c1a8
...@@ -4,11 +4,7 @@ ...@@ -4,11 +4,7 @@
#include "chrome/browser/ui/views/crypto_module_password_dialog_view.h" #include "chrome/browser/ui/views/crypto_module_password_dialog_view.h"
#include "base/basictypes.h"
#include "base/bind.h"
#include "base/utf_string_conversions.h" #include "base/utf_string_conversions.h"
#include "chrome/browser/ui/views/window.h"
#include "googleurl/src/gurl.h"
#include "grit/generated_resources.h" #include "grit/generated_resources.h"
#include "ui/base/l10n/l10n_util.h" #include "ui/base/l10n/l10n_util.h"
#include "ui/views/controls/button/text_button.h" #include "ui/views/controls/button/text_button.h"
...@@ -20,13 +16,14 @@ ...@@ -20,13 +16,14 @@
namespace browser { namespace browser {
// CryptoModulePasswordDialogView
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// CryptoModulePasswordDialogView, public:
CryptoModulePasswordDialogView::CryptoModulePasswordDialogView( CryptoModulePasswordDialogView::CryptoModulePasswordDialogView(
const std::string& slot_name, const std::string& slot_name,
browser::CryptoModulePasswordReason reason, CryptoModulePasswordReason reason,
const std::string& server, const std::string& server,
const base::Callback<void(const char*)>& callback) const CryptoModulePasswordCallback& callback)
: callback_(callback) { : callback_(callback) {
Init(server, slot_name, reason); Init(server, slot_name, reason);
} }
...@@ -34,10 +31,59 @@ CryptoModulePasswordDialogView::CryptoModulePasswordDialogView( ...@@ -34,10 +31,59 @@ CryptoModulePasswordDialogView::CryptoModulePasswordDialogView(
CryptoModulePasswordDialogView::~CryptoModulePasswordDialogView() { CryptoModulePasswordDialogView::~CryptoModulePasswordDialogView() {
} }
void CryptoModulePasswordDialogView::Init( ////////////////////////////////////////////////////////////////////////////////
const std::string& server, // CryptoModulePasswordDialogView, private:
const std::string& slot_name,
browser::CryptoModulePasswordReason reason) { views::View* CryptoModulePasswordDialogView::GetInitiallyFocusedView() {
return password_entry_;
}
ui::ModalType CryptoModulePasswordDialogView::GetModalType() const {
return ui::MODAL_TYPE_WINDOW;
}
string16 CryptoModulePasswordDialogView::GetWindowTitle() const {
return l10n_util::GetStringUTF16(IDS_CRYPTO_MODULE_AUTH_DIALOG_TITLE);
}
views::View* CryptoModulePasswordDialogView::GetContentsView() {
return this;
}
string16 CryptoModulePasswordDialogView::GetDialogButtonLabel(
ui::DialogButton button) const {
return l10n_util::GetStringUTF16(button == ui::DIALOG_BUTTON_OK ?
IDS_CRYPTO_MODULE_AUTH_DIALOG_OK_BUTTON_LABEL : IDS_CANCEL);
}
bool CryptoModulePasswordDialogView::Cancel() {
callback_.Run(static_cast<const char*>(NULL));
const string16 empty;
password_entry_->SetText(empty);
return true;
}
bool CryptoModulePasswordDialogView::Accept() {
callback_.Run(UTF16ToUTF8(password_entry_->text()).c_str());
const string16 empty;
password_entry_->SetText(empty);
return true;
}
void CryptoModulePasswordDialogView::ContentsChanged(
views::Textfield* sender,
const string16& new_contents) {
}
bool CryptoModulePasswordDialogView::HandleKeyEvent(
views::Textfield* sender,
const views::KeyEvent& keystroke) {
return false;
}
void CryptoModulePasswordDialogView::Init(const std::string& server,
const std::string& slot_name,
CryptoModulePasswordReason reason) {
// Select an appropriate text for the reason. // Select an appropriate text for the reason.
std::string text; std::string text;
const string16& server16 = UTF8ToUTF16(server); const string16& server16 = UTF8ToUTF16(server);
...@@ -105,53 +151,6 @@ void CryptoModulePasswordDialogView::Init( ...@@ -105,53 +151,6 @@ void CryptoModulePasswordDialogView::Init(
layout->AddView(password_entry_); layout->AddView(password_entry_);
} }
views::View* CryptoModulePasswordDialogView::GetInitiallyFocusedView() {
return password_entry_;
}
ui::ModalType CryptoModulePasswordDialogView::GetModalType() const {
return ui::MODAL_TYPE_WINDOW;
}
views::View* CryptoModulePasswordDialogView::GetContentsView() {
return this;
}
string16 CryptoModulePasswordDialogView::GetDialogButtonLabel(
ui::DialogButton button) const {
return l10n_util::GetStringUTF16(button == ui::DIALOG_BUTTON_OK ?
IDS_CRYPTO_MODULE_AUTH_DIALOG_OK_BUTTON_LABEL : IDS_CANCEL);
}
bool CryptoModulePasswordDialogView::Accept() {
callback_.Run(UTF16ToUTF8(password_entry_->text()).c_str());
const string16 empty;
password_entry_->SetText(empty);
return true;
}
bool CryptoModulePasswordDialogView::Cancel() {
callback_.Run(static_cast<const char*>(NULL));
const string16 empty;
password_entry_->SetText(empty);
return true;
}
bool CryptoModulePasswordDialogView::HandleKeyEvent(
views::Textfield* sender,
const views::KeyEvent& keystroke) {
return false;
}
void CryptoModulePasswordDialogView::ContentsChanged(
views::Textfield* sender,
const string16& new_contents) {
}
string16 CryptoModulePasswordDialogView::GetWindowTitle() const {
return l10n_util::GetStringUTF16(IDS_CRYPTO_MODULE_AUTH_DIALOG_TITLE);
}
void ShowCryptoModulePasswordDialog( void ShowCryptoModulePasswordDialog(
const std::string& slot_name, const std::string& slot_name,
bool retry, bool retry,
......
...@@ -7,57 +7,51 @@ ...@@ -7,57 +7,51 @@
#pragma once #pragma once
#include <string> #include <string>
#include <vector>
#include "base/callback.h" #include "base/basictypes.h"
#include "base/gtest_prod_util.h" #include "base/gtest_prod_util.h"
#include "chrome/browser/ui/crypto_module_password_dialog.h" #include "chrome/browser/ui/crypto_module_password_dialog.h"
#include "ui/views/controls/textfield/textfield_controller.h" #include "ui/views/controls/textfield/textfield_controller.h"
#include "ui/views/window/dialog_delegate.h" #include "ui/views/window/dialog_delegate.h"
namespace views { namespace views {
class Textfield;
class Label; class Label;
class Textfield;
} }
namespace browser { namespace browser {
// CryptoModulePasswordDialogView
// Dialog view for crypto module password interaction.
/////////////////////////////////////////////////////////////////////////
class CryptoModulePasswordDialogView : public views::DialogDelegateView, class CryptoModulePasswordDialogView : public views::DialogDelegateView,
public views::TextfieldController { public views::TextfieldController {
public: public:
CryptoModulePasswordDialogView( CryptoModulePasswordDialogView(const std::string& slot_name,
const std::string& slot_name, CryptoModulePasswordReason reason,
browser::CryptoModulePasswordReason reason, const std::string& server,
const std::string& server, const CryptoModulePasswordCallback& callback);
const base::Callback<void(const char*)>& callback);
virtual ~CryptoModulePasswordDialogView(); virtual ~CryptoModulePasswordDialogView();
// views::DialogDelegate: private:
virtual bool Accept() OVERRIDE; FRIEND_TEST_ALL_PREFIXES(CryptoModulePasswordDialogViewTest, TestAccept);
virtual bool Cancel() OVERRIDE;
virtual string16 GetDialogButtonLabel(
ui::DialogButton button) const OVERRIDE;
// views::WidgetDelegate: // views::WidgetDelegate:
virtual views::View* GetInitiallyFocusedView() OVERRIDE; virtual views::View* GetInitiallyFocusedView() OVERRIDE;
virtual ui::ModalType GetModalType() const OVERRIDE; virtual ui::ModalType GetModalType() const OVERRIDE;
virtual string16 GetWindowTitle() const OVERRIDE;
virtual views::View* GetContentsView() OVERRIDE; virtual views::View* GetContentsView() OVERRIDE;
// views::View: // views::DialogDelegate:
virtual string16 GetWindowTitle() const OVERRIDE; virtual string16 GetDialogButtonLabel(
ui::DialogButton button) const OVERRIDE;
virtual bool Cancel() OVERRIDE;
virtual bool Accept() OVERRIDE;
// views::TextfieldController: // views::TextfieldController:
virtual bool HandleKeyEvent(views::Textfield* sender,
const views::KeyEvent& keystroke) OVERRIDE;
virtual void ContentsChanged(views::Textfield* sender, virtual void ContentsChanged(views::Textfield* sender,
const string16& new_contents) OVERRIDE; const string16& new_contents) OVERRIDE;
virtual bool HandleKeyEvent(views::Textfield* sender,
const views::KeyEvent& keystroke) OVERRIDE;
private:
// Initialize views and layout. // Initialize views and layout.
void Init(const std::string& server, void Init(const std::string& server,
const std::string& slot_name, const std::string& slot_name,
...@@ -68,8 +62,7 @@ class CryptoModulePasswordDialogView : public views::DialogDelegateView, ...@@ -68,8 +62,7 @@ class CryptoModulePasswordDialogView : public views::DialogDelegateView,
views::Label* password_label_; views::Label* password_label_;
views::Textfield* password_entry_; views::Textfield* password_entry_;
const base::Callback<void(const char*)> callback_; const CryptoModulePasswordCallback callback_;
FRIEND_TEST_ALL_PREFIXES(CryptoModulePasswordDialogViewTest, TestAccept);
DISALLOW_COPY_AND_ASSIGN(CryptoModulePasswordDialogView); DISALLOW_COPY_AND_ASSIGN(CryptoModulePasswordDialogView);
}; };
......
// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include "chrome/browser/ui/views/crypto_module_password_dialog_view.h"
#include <string> #include <string>
#include "base/basictypes.h"
#include "base/bind.h" #include "base/bind.h"
#include "base/utf_string_conversions.h" #include "base/utf_string_conversions.h"
#include "chrome/browser/ui/crypto_module_password_dialog.h" #include "chrome/browser/ui/crypto_module_password_dialog.h"
#include "chrome/browser/ui/views/crypto_module_password_dialog_view.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
#include "ui/views/controls/textfield/textfield.h" #include "ui/views/controls/textfield/textfield.h"
std::string kSlotName = "slot";
std::string kServer = "server";
namespace browser { namespace browser {
CryptoModulePasswordReason kReason = kCryptoModulePasswordKeygen;
class CryptoModulePasswordDialogViewTest : public testing::Test { class CryptoModulePasswordDialogViewTest : public testing::Test {
public: public:
CryptoModulePasswordDialogViewTest() {} CryptoModulePasswordDialogViewTest() {}
~CryptoModulePasswordDialogViewTest() {} ~CryptoModulePasswordDialogViewTest() {}
void Capture(const char* text) { void Capture(const char* text) {
text_ = text; text_ = text;
} }
void CreateDialogCrypto(const CryptoModulePasswordCallback& callback) {
dialog_.reset(new CryptoModulePasswordDialogView( void CreateCryptoDialog(const CryptoModulePasswordCallback& callback) {
kSlotName, kReason, kServer, callback)); dialog_.reset(new CryptoModulePasswordDialogView("slot",
kCryptoModulePasswordKeygen, "server", callback));
} }
browser::CryptoModulePasswordCallback* callback_;
CryptoModulePasswordCallback* callback_;
std::string text_; std::string text_;
scoped_ptr<CryptoModulePasswordDialogView> dialog_; scoped_ptr<CryptoModulePasswordDialogView> dialog_;
}; };
TEST_F(CryptoModulePasswordDialogViewTest, TestAccept) { TEST_F(CryptoModulePasswordDialogViewTest, TestAccept) {
browser::CryptoModulePasswordCallback cb( CryptoModulePasswordCallback cb(
base::Bind(&browser::CryptoModulePasswordDialogViewTest::Capture, base::Bind(&CryptoModulePasswordDialogViewTest::Capture,
base::Unretained(this))); base::Unretained(this)));
CreateDialogCrypto(cb); CreateCryptoDialog(cb);
EXPECT_EQ(dialog_->password_entry_, dialog_->GetInitiallyFocusedView()); EXPECT_EQ(dialog_->password_entry_, dialog_->GetInitiallyFocusedView());
EXPECT_TRUE(dialog_->GetModalType() != ui::MODAL_TYPE_NONE); EXPECT_TRUE(dialog_->GetModalType() != ui::MODAL_TYPE_NONE);
const std::string kPassword = "diAl0g"; const std::string kPassword = "diAl0g";
dialog_->password_entry_->SetText(UTF8ToUTF16(kPassword)); dialog_->password_entry_->SetText(ASCIIToUTF16(kPassword));
EXPECT_TRUE(dialog_->Accept()); EXPECT_TRUE(dialog_->Accept());
EXPECT_EQ(kPassword, text_); EXPECT_EQ(kPassword, text_);
const string16 empty; const string16 empty;
EXPECT_EQ(empty, dialog_->password_entry_->text()); EXPECT_EQ(empty, dialog_->password_entry_->text());
} }
} // namespace browser } // namespace browser
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment