Commit c9d27bad authored by dconnelly's avatar dconnelly Committed by Commit bot

Add ManagePasswordsBubbleConfirmationViewController and unit tests.

BUG=394303

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

Cr-Commit-Position: refs/heads/master@{#291675}
parent c13470e3
// 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_CONFIRMATION_VIEW_CONTROLLER_H_
#define CHROME_BROWSER_UI_COCOA_PASSWORDS_MANAGE_PASSWORDS_BUBBLE_CONFIRMATION_VIEW_CONTROLLER_H_
#import <Cocoa/Cocoa.h>
#include "base/mac/scoped_nsobject.h"
#import "chrome/browser/ui/cocoa/passwords/manage_passwords_bubble_content_view_controller.h"
@class HyperlinkTextView;
class ManagePasswordsBubbleModel;
// Manages the view that confirms that the generated password was saved.
@interface ManagePasswordsBubbleConfirmationViewController
: ManagePasswordsBubbleContentViewController<NSTextViewDelegate> {
@private
ManagePasswordsBubbleModel* model_; // weak
id<ManagePasswordsBubbleContentViewDelegate> delegate_; // weak
base::scoped_nsobject<HyperlinkTextView> confirmationText_;
base::scoped_nsobject<NSButton> okButton_;
}
- (id)initWithModel:(ManagePasswordsBubbleModel*)model
delegate:(id<ManagePasswordsBubbleContentViewDelegate>)delegate;
@end
@interface ManagePasswordsBubbleConfirmationViewController (Testing)
@property(readonly) HyperlinkTextView* confirmationText;
@property(readonly) NSButton* okButton;
@end
#endif // CHROME_BROWSER_UI_COCOA_PASSWORDS_MANAGE_PASSWORDS_BUBBLE_CONFIRMATION_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.
#include <algorithm>
#import "chrome/browser/ui/cocoa/passwords/manage_passwords_bubble_confirmation_view_controller.h"
#include "base/strings/sys_string_conversions.h"
#include "chrome/browser/ui/chrome_style.h"
#import "chrome/browser/ui/cocoa/hyperlink_text_view.h"
#include "chrome/browser/ui/passwords/manage_passwords_bubble_model.h"
#include "grit/generated_resources.h"
#include "skia/ext/skia_utils_mac.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/font_list.h"
#include "ui/base/l10n/l10n_util.h"
using namespace password_manager::mac::ui;
@interface ManagePasswordsBubbleConfirmationViewController ()
- (void)onOKClicked:(id)sender;
@end
@implementation ManagePasswordsBubbleConfirmationViewController
- (id)initWithModel:(ManagePasswordsBubbleModel*)model
delegate:(id<ManagePasswordsBubbleContentViewDelegate>)delegate {
if ((self = [super initWithNibName:nil bundle:nil])) {
model_ = model;
delegate_ = delegate;
}
return self;
}
- (void)onOKClicked:(id)sender {
model_->OnOKClicked();
[delegate_ viewShouldDismiss];
}
- (BOOL)textView:(NSTextView*)textView
clickedOnLink:(id)link
atIndex:(NSUInteger)charIndex {
model_->OnRemoteManageLinkClicked();
[delegate_ viewShouldDismiss];
return YES;
}
- (void)loadView {
self.view = [[[NSView alloc] initWithFrame:NSZeroRect] autorelease];
// -----------------------------------
// | Title |
// | |
// | Saved! Click [here] to view. |
// | |
// | [OK] |
// -----------------------------------
// Create the elements and add them to the view.
// Title.
NSTextField* titleLabel =
[self addTitleLabel:base::SysUTF16ToNSString(model_->title())];
// Text.
confirmationText_.reset([[HyperlinkTextView alloc] initWithFrame:NSZeroRect]);
NSFont* font = ResourceBundle::GetSharedInstance()
.GetFontList(ResourceBundle::SmallFont)
.GetPrimaryFont()
.GetNativeFont();
NSColor* textColor = [NSColor blackColor];
[confirmationText_
setMessage:base::SysUTF16ToNSString(model_->save_confirmation_text())
withFont:font
messageColor:textColor];
NSColor* linkColor =
gfx::SkColorToCalibratedNSColor(chrome_style::GetLinkColor());
[confirmationText_
addLinkRange:model_->save_confirmation_link_range().ToNSRange()
withName:@""
linkColor:linkColor];
[confirmationText_ setDelegate:self];
[[confirmationText_ textContainer] setLineFragmentPadding:0.0f];
// Force the text to wrap to fit in the bubble size.
[confirmationText_ setVerticallyResizable:YES];
[confirmationText_
setFrameSize:NSMakeSize(kDesiredBubbleWidth - 2 * kFramePadding,
MAXFLOAT)];
[confirmationText_ sizeToFit];
// Create the link with no underlining.
[confirmationText_ setLinkTextAttributes:nil];
NSTextStorage* text = [confirmationText_ textStorage];
[text addAttribute:NSUnderlineStyleAttributeName
value:[NSNumber numberWithInt:NSUnderlineStyleNone]
range:model_->save_confirmation_link_range().ToNSRange()];
[[self view] addSubview:confirmationText_];
// OK button.
okButton_.reset([[self addButton:l10n_util::GetNSString(IDS_OK)
target:self
action:@selector(onOKClicked:)] retain]);
// Layout the elements, starting at the bottom and moving up.
const CGFloat width = kDesiredBubbleWidth;
// OK button goes on the bottom row and is right-aligned.
CGFloat curX = width - kFramePadding - NSWidth([okButton_ frame]);
CGFloat curY = kFramePadding;
[okButton_ setFrameOrigin:NSMakePoint(curX, curY)];
// Text goes on the next row and is shifted right.
curX = kFramePadding;
curY = NSMaxY([okButton_ frame]) + kUnrelatedControlVerticalPadding;
[confirmationText_ setFrameOrigin:NSMakePoint(curX, curY)];
// Title goes at the top after some padding.
curY = NSMaxY([confirmationText_ 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 ManagePasswordsBubbleConfirmationViewController (Testing)
- (HyperlinkTextView*)confirmationText {
return confirmationText_.get();
}
- (NSButton*)okButton {
return okButton_.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_confirmation_view_controller.h"
#include "base/mac/scoped_nsobject.h"
#include "base/strings/string16.h"
#include "base/strings/utf_string_conversions.h"
#import "chrome/browser/ui/cocoa/bubble_combobox.h"
#include "chrome/browser/ui/cocoa/cocoa_test_helper.h"
#include "chrome/browser/ui/cocoa/passwords/manage_passwords_controller_test.h"
#include "chrome/browser/ui/passwords/manage_passwords_bubble_model.h"
#include "chrome/browser/ui/passwords/manage_passwords_ui_controller_mock.h"
#include "chrome/browser/ui/passwords/save_password_refusal_combobox_model.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/gtest_mac.h"
@interface ManagePasswordsBubbleContentViewTestDelegate
: NSObject<ManagePasswordsBubbleContentViewDelegate> {
BOOL dismissed_;
}
@property(readonly) BOOL dismissed;
@end
@implementation ManagePasswordsBubbleContentViewTestDelegate
@synthesize dismissed = dismissed_;
- (void)viewShouldDismiss {
dismissed_ = YES;
}
@end
namespace {
class ManagePasswordsBubbleConfirmationViewControllerTest
: public ManagePasswordsControllerTest {
public:
ManagePasswordsBubbleConfirmationViewControllerTest() : controller_(nil) {}
virtual void SetUp() OVERRIDE {
ManagePasswordsControllerTest::SetUp();
delegate_.reset(
[[ManagePasswordsBubbleContentViewTestDelegate alloc] init]);
}
ManagePasswordsBubbleContentViewTestDelegate* delegate() {
return delegate_.get();
}
ManagePasswordsBubbleConfirmationViewController* controller() {
if (!controller_) {
controller_.reset([[ManagePasswordsBubbleConfirmationViewController alloc]
initWithModel:model()
delegate:delegate()]);
[controller_ loadView];
}
return controller_.get();
}
private:
base::scoped_nsobject<ManagePasswordsBubbleConfirmationViewController>
controller_;
base::scoped_nsobject<ManagePasswordsBubbleContentViewTestDelegate> delegate_;
};
TEST_F(ManagePasswordsBubbleConfirmationViewControllerTest,
ShouldDismissWhenOKClicked) {
[controller().okButton performClick:nil];
EXPECT_TRUE([delegate() dismissed]);
}
TEST_F(ManagePasswordsBubbleConfirmationViewControllerTest,
ShouldOpenPasswordsAndDismissWhenLinkClicked) {
[controller().confirmationText clickedOnLink:nil atIndex:0];
EXPECT_TRUE([delegate() dismissed]);
EXPECT_TRUE(ui_controller()->navigated_to_account_central_management_page());
}
} // namespace
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#import "chrome/browser/ui/cocoa/info_bubble_view.h" #import "chrome/browser/ui/cocoa/info_bubble_view.h"
#import "chrome/browser/ui/cocoa/info_bubble_window.h" #import "chrome/browser/ui/cocoa/info_bubble_window.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_confirmation_view_controller.h"
#include "ui/base/cocoa/window_size_constants.h" #include "ui/base/cocoa/window_size_constants.h"
@interface ManagePasswordsBubbleController () @interface ManagePasswordsBubbleController ()
...@@ -48,6 +49,11 @@ ...@@ -48,6 +49,11 @@
[[ManagePasswordsBubblePendingViewController alloc] [[ManagePasswordsBubblePendingViewController alloc]
initWithModel:model_ initWithModel:model_
delegate:self]); delegate:self]);
} else if (model_->state() == password_manager::ui::CONFIRMATION_STATE) {
currentController_.reset(
[[ManagePasswordsBubbleConfirmationViewController alloc]
initWithModel:model_
delegate:self]);
} }
[self performLayout]; [self performLayout];
} }
......
...@@ -10,6 +10,7 @@ ManagePasswordsUIControllerMock::ManagePasswordsUIControllerMock( ...@@ -10,6 +10,7 @@ ManagePasswordsUIControllerMock::ManagePasswordsUIControllerMock(
content::WebContents* contents) content::WebContents* contents)
: ManagePasswordsUIController(contents), : ManagePasswordsUIController(contents),
navigated_to_settings_page_(false), navigated_to_settings_page_(false),
navigated_to_account_central_management_page_(false),
saved_password_(false), saved_password_(false),
never_saved_password_(false) { never_saved_password_(false) {
contents->SetUserData(UserDataKey(), this); contents->SetUserData(UserDataKey(), this);
...@@ -23,6 +24,10 @@ void ManagePasswordsUIControllerMock:: ...@@ -23,6 +24,10 @@ void ManagePasswordsUIControllerMock::
navigated_to_settings_page_ = true; navigated_to_settings_page_ = true;
} }
void ManagePasswordsUIControllerMock::NavigateToAccountCentralManagementPage() {
navigated_to_account_central_management_page_ = true;
}
const autofill::PasswordForm& const autofill::PasswordForm&
ManagePasswordsUIControllerMock::PendingCredentials() const { ManagePasswordsUIControllerMock::PendingCredentials() const {
return pending_credentials_; return pending_credentials_;
......
...@@ -33,6 +33,11 @@ class ManagePasswordsUIControllerMock ...@@ -33,6 +33,11 @@ class ManagePasswordsUIControllerMock
return navigated_to_settings_page_; return navigated_to_settings_page_;
} }
virtual void NavigateToAccountCentralManagementPage() OVERRIDE;
bool navigated_to_account_central_management_page() const {
return navigated_to_account_central_management_page_;
}
// We don't have a FormManager in tests, so stub these out. // We don't have a FormManager in tests, so stub these out.
virtual void SavePasswordInternal() OVERRIDE; virtual void SavePasswordInternal() OVERRIDE;
bool saved_password() const { return saved_password_; } bool saved_password() const { return saved_password_; }
...@@ -58,6 +63,7 @@ class ManagePasswordsUIControllerMock ...@@ -58,6 +63,7 @@ class ManagePasswordsUIControllerMock
private: private:
bool navigated_to_settings_page_; bool navigated_to_settings_page_;
bool navigated_to_account_central_management_page_;
bool saved_password_; bool saved_password_;
bool never_saved_password_; bool never_saved_password_;
......
...@@ -631,6 +631,8 @@ ...@@ -631,6 +631,8 @@
'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_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.mm',
'browser/ui/cocoa/passwords/manage_passwords_bubble_content_view_controller.h', 'browser/ui/cocoa/passwords/manage_passwords_bubble_content_view_controller.h',
'browser/ui/cocoa/passwords/manage_passwords_bubble_content_view_controller.mm', 'browser/ui/cocoa/passwords/manage_passwords_bubble_content_view_controller.mm',
'browser/ui/cocoa/passwords/manage_passwords_bubble_controller.h', 'browser/ui/cocoa/passwords/manage_passwords_bubble_controller.h',
......
...@@ -1625,6 +1625,7 @@ ...@@ -1625,6 +1625,7 @@
'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_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_controller_unittest.mm', 'browser/ui/cocoa/passwords/manage_passwords_bubble_controller_unittest.mm',
'browser/ui/cocoa/passwords/manage_passwords_bubble_pending_view_controller_unittest.mm', 'browser/ui/cocoa/passwords/manage_passwords_bubble_pending_view_controller_unittest.mm',
'browser/ui/cocoa/passwords/manage_passwords_controller_test.h', 'browser/ui/cocoa/passwords/manage_passwords_controller_test.h',
......
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