Commit 66919aa3 authored by dconnelly's avatar dconnelly Committed by Commit bot

Add ManagePasswordsBubbleBlacklistViewController and unit tests.

BUG=328847

Review URL: https://codereview.chromium.org/446073002

Cr-Commit-Position: refs/heads/master@{#291921}
parent aeffd18e
// Copyright 2014 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.
#ifndef CHROME_BROWSER_UI_COCOA_PASSWORDS_MANAGE_PASSWORDS_BUBBLE_BLACKLIST_VIEW_CONTROLLER_H_
#define CHROME_BROWSER_UI_COCOA_PASSWORDS_MANAGE_PASSWORDS_BUBBLE_BLACKLIST_VIEW_CONTROLLER_H_
#import <Cocoa/Cocoa.h>
#include "base/mac/scoped_nsobject.h"
#include "chrome/browser/ui/cocoa/passwords/manage_passwords_bubble_content_view_controller.h"
@class BubbleCombobox;
class ManagePasswordsBubbleModel;
@class ManagePasswordItemViewController;
// Manages the view that informs the user that the current site is blacklisted
// from the password manager and offers to un-blacklist it.
@interface ManagePasswordsBubbleBlacklistViewController
: ManagePasswordsBubbleContentViewController {
@private
ManagePasswordsBubbleModel* model_; // weak
id<ManagePasswordsBubbleContentViewDelegate> delegate_; // weak
base::scoped_nsobject<NSButton> doneButton_;
base::scoped_nsobject<NSButton> undoBlacklistButton_;
}
- (id)initWithModel:(ManagePasswordsBubbleModel*)model
delegate:(id<ManagePasswordsBubbleContentViewDelegate>)delegate;
@end
@interface ManagePasswordsBubbleBlacklistViewController (Testing)
@property(readonly) NSButton* doneButton;
@property(readonly) NSButton* undoBlacklistButton;
@end
#endif // CHROME_BROWSER_UI_COCOA_PASSWORDS_MANAGE_PASSWORDS_BUBBLE_BLACKLIST_VIEW_CONTROLLER_H_
// Copyright 2014 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.
#import "chrome/browser/ui/cocoa/passwords/manage_passwords_bubble_blacklist_view_controller.h"
#include "base/strings/sys_string_conversions.h"
#include "chrome/browser/ui/passwords/manage_passwords_bubble_model.h"
#include "grit/generated_resources.h"
#import "third_party/google_toolbox_for_mac/src/AppKit/GTMUILocalizerAndLayoutTweaker.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/models/combobox_model.h"
using namespace password_manager::mac::ui;
@interface ManagePasswordsBubbleBlacklistViewController ()
- (void)onDoneClicked:(id)sender;
- (void)onUndoBlacklistClicked:(id)sender;
@end
@implementation ManagePasswordsBubbleBlacklistViewController
- (id)initWithModel:(ManagePasswordsBubbleModel*)model
delegate:(id<ManagePasswordsBubbleContentViewDelegate>)delegate {
if ((self = [super initWithNibName:nil bundle:nil])) {
model_ = model;
delegate_ = delegate;
}
return self;
}
- (void)onDoneClicked:(id)sender {
model_->OnDoneClicked();
[delegate_ viewShouldDismiss];
}
- (void)onUndoBlacklistClicked:(id)sender {
model_->OnUnblacklistClicked();
[delegate_ viewShouldDismiss];
}
- (void)loadView {
self.view = [[[NSView alloc] initWithFrame:NSZeroRect] autorelease];
// -----------------------------------
// | Title |
// | |
// | Blacklisted! |
// | |
// | [Done] [Undo] |
// -----------------------------------
// Create the elements and add them to the view.
// Title.
NSTextField* titleLabel =
[self addTitleLabel:base::SysUTF16ToNSString(model_->title())];
// Blacklist explanation.
NSTextField* explanationLabel =
[self addLabel:l10n_util::GetNSString(IDS_MANAGE_PASSWORDS_BLACKLISTED)];
// Done button.
doneButton_.reset([[self addButton:l10n_util::GetNSString(IDS_DONE)
target:self
action:@selector(onDoneClicked:)] retain]);
// Undo button.
undoBlacklistButton_.reset([[self
addButton:l10n_util::GetNSString(IDS_PASSWORD_MANAGER_UNBLACKLIST_BUTTON)
target:self
action:@selector(onUndoBlacklistClicked:)] retain]);
// Compute the bubble width using the title and explanation labels.
// The explanation label can wrap to multiple lines
const CGFloat contentWidth =
std::max(NSWidth([titleLabel frame]), kDesiredBubbleWidth);
[explanationLabel setFrameSize:NSMakeSize(contentWidth, 0)];
[GTMUILocalizerAndLayoutTweaker
sizeToFitFixedWidthTextField:explanationLabel];
const CGFloat width = kFramePadding + contentWidth + kFramePadding;
// Layout the elements, starting at the bottom and moving up.
// Buttons go on the bottom row and are right-aligned.
// Start with [Undo].
CGFloat curX = width - kFramePadding - NSWidth([undoBlacklistButton_ frame]);
CGFloat curY = kFramePadding;
[undoBlacklistButton_ setFrameOrigin:NSMakePoint(curX, curY)];
// [Done] goes to the left of [Undo].
curX = NSMinX([undoBlacklistButton_ frame]) -
kRelatedControlHorizontalPadding -
NSWidth([doneButton_ frame]);
[doneButton_ setFrameOrigin:NSMakePoint(curX, curY)];
// Explanation label goes on the next row and is shifted right.
curX = kFramePadding;
curY = NSMaxY([doneButton_ frame]) + kUnrelatedControlVerticalPadding;
[explanationLabel setFrameOrigin:NSMakePoint(curX, curY)];
// Title goes at the top after some padding.
curY = NSMaxY([explanationLabel frame]) + kUnrelatedControlVerticalPadding;
[titleLabel setFrameOrigin:NSMakePoint(curX, curY)];
// Update the bubble size.
const CGFloat height =
NSMaxY([titleLabel frame]) + kFramePadding;
[self.view setFrame:NSMakeRect(0, 0, width, height)];
}
@end
@implementation ManagePasswordsBubbleBlacklistViewController (Testing)
- (NSButton*)doneButton {
return doneButton_.get();
}
- (NSButton*)undoBlacklistButton {
return undoBlacklistButton_.get();
}
@end
// Copyright 2014 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.
#import "chrome/browser/ui/cocoa/passwords/manage_passwords_bubble_blacklist_view_controller.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/ui/cocoa/cocoa_test_helper.h"
#import "chrome/browser/ui/cocoa/passwords/manage_passwords_bubble_blacklist_view_controller.h"
#include "chrome/browser/ui/cocoa/passwords/manage_passwords_controller_test.h"
#include "chrome/browser/ui/passwords/manage_passwords_ui_controller_mock.h"
#include "components/password_manager/core/common/password_manager_ui.h"
// Helper delegate for testing the blacklist view of the password management
// bubble.
@interface ManagePasswordsBubbleBlacklistViewTestDelegate
: NSObject<ManagePasswordsBubbleContentViewDelegate> {
BOOL dismissed_;
}
@property(readonly) BOOL dismissed;
@end
@implementation ManagePasswordsBubbleBlacklistViewTestDelegate
@synthesize dismissed = dismissed_;
- (void)viewShouldDismiss {
dismissed_ = YES;
}
@end
namespace {
// Tests for the blacklist view of the password management bubble.
class ManagePasswordsBubbleBlacklistViewControllerTest
: public ManagePasswordsControllerTest {
public:
ManagePasswordsBubbleBlacklistViewControllerTest() : controller_(nil) {}
virtual void SetUp() OVERRIDE {
ManagePasswordsControllerTest::SetUp();
delegate_.reset(
[[ManagePasswordsBubbleBlacklistViewTestDelegate alloc] init]);
}
ManagePasswordsBubbleBlacklistViewTestDelegate* delegate() {
return delegate_.get();
}
ManagePasswordsBubbleBlacklistViewController* controller() {
if (!controller_) {
controller_.reset([[ManagePasswordsBubbleBlacklistViewController alloc]
initWithModel:model()
delegate:delegate()]);
[controller_ loadView];
}
return controller_.get();
}
private:
base::scoped_nsobject<ManagePasswordsBubbleBlacklistViewController>
controller_;
base::scoped_nsobject<ManagePasswordsBubbleBlacklistViewTestDelegate>
delegate_;
};
TEST_F(ManagePasswordsBubbleBlacklistViewControllerTest,
ShouldDismissWhenDoneClicked) {
NSButton* doneButton = controller().doneButton;
[doneButton performClick:nil];
EXPECT_TRUE(delegate().dismissed);
}
TEST_F(ManagePasswordsBubbleBlacklistViewControllerTest,
ShouldDismissAndUnblacklistWhenUnblacklistClicked) {
ui_controller()->SetState(password_manager::ui::BLACKLIST_STATE);
// Unblacklisting requires passwords to exist for the site.
autofill::PasswordForm form;
form.username_value = base::ASCIIToUTF16("username");
form.password_value = base::ASCIIToUTF16("password");
autofill::ConstPasswordFormMap map;
map[base::ASCIIToUTF16("username")] = &form;
ui_controller()->SetPasswordFormMap(map);
EXPECT_EQ(password_manager::ui::BLACKLIST_STATE, ui_controller()->state());
NSButton* undoButton = controller().undoBlacklistButton;
[undoButton performClick:nil];
EXPECT_TRUE(delegate().dismissed);
EXPECT_NE(password_manager::ui::BLACKLIST_STATE, ui_controller()->state());
}
} // namespace
...@@ -37,7 +37,10 @@ class ManagePasswordsBubbleCocoaTest : public CocoaProfileTest { ...@@ -37,7 +37,10 @@ class ManagePasswordsBubbleCocoaTest : public CocoaProfileTest {
// Create the test UIController here so that it's bound to // Create the test UIController here so that it's bound to
// |test_web_contents_| and therefore accessible to the model. // |test_web_contents_| and therefore accessible to the model.
new ManagePasswordsUIControllerMock(webContents_); ManagePasswordsUIControllerMock* ui_controller =
new ManagePasswordsUIControllerMock(webContents_);
// Set the initial state.
ui_controller->SetState(password_manager::ui::PENDING_PASSWORD_STATE);
} }
content::WebContents* webContents() { return webContents_; } content::WebContents* webContents() { return webContents_; }
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#import "chrome/browser/ui/cocoa/info_bubble_window.h" #import "chrome/browser/ui/cocoa/info_bubble_window.h"
#import "chrome/browser/ui/cocoa/passwords/manage_passwords_bubble_manage_view_controller.h" #import "chrome/browser/ui/cocoa/passwords/manage_passwords_bubble_manage_view_controller.h"
#include "chrome/browser/ui/cocoa/location_bar/location_bar_view_mac.h" #include "chrome/browser/ui/cocoa/location_bar/location_bar_view_mac.h"
#import "chrome/browser/ui/cocoa/passwords/manage_passwords_bubble_blacklist_view_controller.h"
#import "chrome/browser/ui/cocoa/passwords/manage_passwords_bubble_confirmation_view_controller.h" #import "chrome/browser/ui/cocoa/passwords/manage_passwords_bubble_confirmation_view_controller.h"
#include "ui/base/cocoa/window_size_constants.h" #include "ui/base/cocoa/window_size_constants.h"
...@@ -43,7 +44,6 @@ ...@@ -43,7 +44,6 @@
- (void)updateState { - (void)updateState {
// Find the next view controller. // Find the next view controller.
// TODO(dconnelly): Handle other states once they're implemented.
currentController_.reset(); currentController_.reset();
if (password_manager::ui::IsPendingState(model_->state())) { if (password_manager::ui::IsPendingState(model_->state())) {
currentController_.reset( currentController_.reset(
...@@ -59,6 +59,13 @@ ...@@ -59,6 +59,13 @@
currentController_.reset([[ManagePasswordsBubbleManageViewController alloc] currentController_.reset([[ManagePasswordsBubbleManageViewController alloc]
initWithModel:model_ initWithModel:model_
delegate:self]); delegate:self]);
} else if (model_->state() == password_manager::ui::BLACKLIST_STATE) {
currentController_.reset(
[[ManagePasswordsBubbleBlacklistViewController alloc]
initWithModel:model_
delegate:self]);
} else {
NOTREACHED();
} }
[self performLayout]; [self performLayout];
} }
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
#import "chrome/browser/ui/cocoa/passwords/manage_passwords_bubble_manage_view_controller.h" #import "chrome/browser/ui/cocoa/passwords/manage_passwords_bubble_manage_view_controller.h"
#import "chrome/browser/ui/cocoa/passwords/manage_passwords_bubble_pending_view_controller.h" #import "chrome/browser/ui/cocoa/passwords/manage_passwords_bubble_pending_view_controller.h"
#include "chrome/browser/ui/cocoa/passwords/manage_passwords_controller_test.h" #include "chrome/browser/ui/cocoa/passwords/manage_passwords_controller_test.h"
#import "chrome/browser/ui/cocoa/passwords/manage_passwords_bubble_blacklist_view_controller.h"
#include "chrome/browser/ui/passwords/manage_passwords_bubble_model.h" #include "chrome/browser/ui/passwords/manage_passwords_bubble_model.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
#include "testing/gtest_mac.h" #include "testing/gtest_mac.h"
...@@ -89,4 +90,11 @@ TEST_F(ManagePasswordsBubbleControllerTest, ...@@ -89,4 +90,11 @@ TEST_F(ManagePasswordsBubbleControllerTest,
[[controller() currentController] class]); [[controller() currentController] class]);
} }
TEST_F(ManagePasswordsBubbleControllerTest,
BlacklistStateShouldHaveBlacklistView) {
model()->set_state(password_manager::ui::BLACKLIST_STATE);
EXPECT_EQ([ManagePasswordsBubbleBlacklistViewController class],
[[controller() currentController] class]);
}
} // namespace } // namespace
...@@ -629,6 +629,8 @@ ...@@ -629,6 +629,8 @@
'browser/ui/cocoa/panels/panel_window_controller_cocoa.mm', 'browser/ui/cocoa/panels/panel_window_controller_cocoa.mm',
'browser/ui/cocoa/passwords/manage_password_item_view_controller.h', 'browser/ui/cocoa/passwords/manage_password_item_view_controller.h',
'browser/ui/cocoa/passwords/manage_password_item_view_controller.mm', 'browser/ui/cocoa/passwords/manage_password_item_view_controller.mm',
'browser/ui/cocoa/passwords/manage_passwords_bubble_blacklist_view_controller.h',
'browser/ui/cocoa/passwords/manage_passwords_bubble_blacklist_view_controller.mm',
'browser/ui/cocoa/passwords/manage_passwords_bubble_cocoa.h', 'browser/ui/cocoa/passwords/manage_passwords_bubble_cocoa.h',
'browser/ui/cocoa/passwords/manage_passwords_bubble_cocoa.mm', 'browser/ui/cocoa/passwords/manage_passwords_bubble_cocoa.mm',
'browser/ui/cocoa/passwords/manage_passwords_bubble_confirmation_view_controller.h', 'browser/ui/cocoa/passwords/manage_passwords_bubble_confirmation_view_controller.h',
......
...@@ -1623,6 +1623,7 @@ ...@@ -1623,6 +1623,7 @@
'browser/ui/cocoa/one_click_signin_bubble_controller_unittest.mm', 'browser/ui/cocoa/one_click_signin_bubble_controller_unittest.mm',
'browser/ui/cocoa/panels/panel_cocoa_unittest.mm', 'browser/ui/cocoa/panels/panel_cocoa_unittest.mm',
'browser/ui/cocoa/passwords/manage_password_item_view_controller_unittest.mm', 'browser/ui/cocoa/passwords/manage_password_item_view_controller_unittest.mm',
'browser/ui/cocoa/passwords/manage_passwords_bubble_blacklist_view_controller_unittest.mm',
'browser/ui/cocoa/passwords/manage_passwords_bubble_cocoa_unittest.mm', 'browser/ui/cocoa/passwords/manage_passwords_bubble_cocoa_unittest.mm',
'browser/ui/cocoa/passwords/manage_passwords_bubble_confirmation_view_controller_unittest.mm', 'browser/ui/cocoa/passwords/manage_passwords_bubble_confirmation_view_controller_unittest.mm',
'browser/ui/cocoa/passwords/manage_passwords_bubble_controller_unittest.mm', 'browser/ui/cocoa/passwords/manage_passwords_bubble_controller_unittest.mm',
......
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