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 @@
#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 "chrome/browser/ui/views/window.h"
#include "googleurl/src/gurl.h"
#include "grit/generated_resources.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/views/controls/button/text_button.h"
......@@ -20,13 +16,14 @@
namespace browser {
// CryptoModulePasswordDialogView
////////////////////////////////////////////////////////////////////////////////
// CryptoModulePasswordDialogView, public:
CryptoModulePasswordDialogView::CryptoModulePasswordDialogView(
const std::string& slot_name,
browser::CryptoModulePasswordReason reason,
CryptoModulePasswordReason reason,
const std::string& server,
const base::Callback<void(const char*)>& callback)
const CryptoModulePasswordCallback& callback)
: callback_(callback) {
Init(server, slot_name, reason);
}
......@@ -34,10 +31,59 @@ CryptoModulePasswordDialogView::CryptoModulePasswordDialogView(
CryptoModulePasswordDialogView::~CryptoModulePasswordDialogView() {
}
void CryptoModulePasswordDialogView::Init(
const std::string& server,
const std::string& slot_name,
browser::CryptoModulePasswordReason reason) {
////////////////////////////////////////////////////////////////////////////////
// CryptoModulePasswordDialogView, private:
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.
std::string text;
const string16& server16 = UTF8ToUTF16(server);
......@@ -105,53 +151,6 @@ void CryptoModulePasswordDialogView::Init(
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(
const std::string& slot_name,
bool retry,
......
......@@ -7,57 +7,51 @@
#pragma once
#include <string>
#include <vector>
#include "base/callback.h"
#include "base/basictypes.h"
#include "base/gtest_prod_util.h"
#include "chrome/browser/ui/crypto_module_password_dialog.h"
#include "ui/views/controls/textfield/textfield_controller.h"
#include "ui/views/window/dialog_delegate.h"
namespace views {
class Textfield;
class Label;
class Textfield;
}
namespace browser {
// CryptoModulePasswordDialogView
// Dialog view for crypto module password interaction.
/////////////////////////////////////////////////////////////////////////
class CryptoModulePasswordDialogView : public views::DialogDelegateView,
public views::TextfieldController {
public:
CryptoModulePasswordDialogView(
const std::string& slot_name,
browser::CryptoModulePasswordReason reason,
const std::string& server,
const base::Callback<void(const char*)>& callback);
CryptoModulePasswordDialogView(const std::string& slot_name,
CryptoModulePasswordReason reason,
const std::string& server,
const CryptoModulePasswordCallback& callback);
virtual ~CryptoModulePasswordDialogView();
// views::DialogDelegate:
virtual bool Accept() OVERRIDE;
virtual bool Cancel() OVERRIDE;
virtual string16 GetDialogButtonLabel(
ui::DialogButton button) const OVERRIDE;
private:
FRIEND_TEST_ALL_PREFIXES(CryptoModulePasswordDialogViewTest, TestAccept);
// views::WidgetDelegate:
virtual views::View* GetInitiallyFocusedView() OVERRIDE;
virtual ui::ModalType GetModalType() const OVERRIDE;
virtual string16 GetWindowTitle() const OVERRIDE;
virtual views::View* GetContentsView() OVERRIDE;
// views::View:
virtual string16 GetWindowTitle() const OVERRIDE;
// views::DialogDelegate:
virtual string16 GetDialogButtonLabel(
ui::DialogButton button) const OVERRIDE;
virtual bool Cancel() OVERRIDE;
virtual bool Accept() OVERRIDE;
// views::TextfieldController:
virtual bool HandleKeyEvent(views::Textfield* sender,
const views::KeyEvent& keystroke) OVERRIDE;
virtual void ContentsChanged(views::Textfield* sender,
const string16& new_contents) OVERRIDE;
virtual bool HandleKeyEvent(views::Textfield* sender,
const views::KeyEvent& keystroke) OVERRIDE;
private:
// Initialize views and layout.
void Init(const std::string& server,
const std::string& slot_name,
......@@ -68,8 +62,7 @@ class CryptoModulePasswordDialogView : public views::DialogDelegateView,
views::Label* password_label_;
views::Textfield* password_entry_;
const base::Callback<void(const char*)> callback_;
FRIEND_TEST_ALL_PREFIXES(CryptoModulePasswordDialogViewTest, TestAccept);
const CryptoModulePasswordCallback callback_;
DISALLOW_COPY_AND_ASSIGN(CryptoModulePasswordDialogView);
};
......
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/views/crypto_module_password_dialog_view.h"
#include <string>
#include "base/basictypes.h"
#include "base/bind.h"
#include "base/utf_string_conversions.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 "ui/views/controls/textfield/textfield.h"
std::string kSlotName = "slot";
std::string kServer = "server";
namespace browser {
CryptoModulePasswordReason kReason = kCryptoModulePasswordKeygen;
class CryptoModulePasswordDialogViewTest : public testing::Test {
public:
CryptoModulePasswordDialogViewTest() {}
~CryptoModulePasswordDialogViewTest() {}
void Capture(const char* text) {
text_ = text;
}
void CreateDialogCrypto(const CryptoModulePasswordCallback& callback) {
dialog_.reset(new CryptoModulePasswordDialogView(
kSlotName, kReason, kServer, callback));
void CreateCryptoDialog(const CryptoModulePasswordCallback& callback) {
dialog_.reset(new CryptoModulePasswordDialogView("slot",
kCryptoModulePasswordKeygen, "server", callback));
}
browser::CryptoModulePasswordCallback* callback_;
CryptoModulePasswordCallback* callback_;
std::string text_;
scoped_ptr<CryptoModulePasswordDialogView> dialog_;
};
TEST_F(CryptoModulePasswordDialogViewTest, TestAccept) {
browser::CryptoModulePasswordCallback cb(
base::Bind(&browser::CryptoModulePasswordDialogViewTest::Capture,
CryptoModulePasswordCallback cb(
base::Bind(&CryptoModulePasswordDialogViewTest::Capture,
base::Unretained(this)));
CreateDialogCrypto(cb);
CreateCryptoDialog(cb);
EXPECT_EQ(dialog_->password_entry_, dialog_->GetInitiallyFocusedView());
EXPECT_TRUE(dialog_->GetModalType() != ui::MODAL_TYPE_NONE);
const std::string kPassword = "diAl0g";
dialog_->password_entry_->SetText(UTF8ToUTF16(kPassword));
dialog_->password_entry_->SetText(ASCIIToUTF16(kPassword));
EXPECT_TRUE(dialog_->Accept());
EXPECT_EQ(kPassword, text_);
const string16 empty;
EXPECT_EQ(empty, dialog_->password_entry_->text());
}
} // 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