Commit 732d346c authored by Nico Weber's avatar Nico Weber Committed by Commit Bot

mac: Remove unused ManagePasswordsDecoration and SelectedKeywordDecoration.

Bug: 832676
Change-Id: I844589ee9726882c5a19b4d7e0ffd2543118d6a4
Reviewed-on: https://chromium-review.googlesource.com/1240167Reviewed-by: default avatarElly Fong-Jones <ellyjones@chromium.org>
Commit-Queue: Nico Weber <thakis@chromium.org>
Cr-Commit-Position: refs/heads/master@{#593564}
parent 98d260d4
......@@ -204,12 +204,8 @@ jumbo_split_static_library("ui") {
"cocoa/location_bar/location_bar_decoration.mm",
"cocoa/location_bar/location_bar_view_mac.h",
"cocoa/location_bar/location_bar_view_mac.mm",
"cocoa/location_bar/manage_passwords_decoration.h",
"cocoa/location_bar/manage_passwords_decoration.mm",
"cocoa/location_bar/page_info_bubble_decoration.h",
"cocoa/location_bar/page_info_bubble_decoration.mm",
"cocoa/location_bar/selected_keyword_decoration.h",
"cocoa/location_bar/selected_keyword_decoration.mm",
"cocoa/location_bar/zoom_decoration.h",
"cocoa/location_bar/zoom_decoration.mm",
"cocoa/main_menu_item.h",
......
......@@ -13,9 +13,6 @@ class BubbleDialogDelegateView;
class LocationBarDecoration;
// Returns the manage password icon decoration in the omnibox.
LocationBarDecoration* GetManagePasswordDecoration(gfx::NativeWindow window);
// Returns the page info decoration in the omnibox.
LocationBarDecoration* GetPageInfoDecoration(gfx::NativeWindow window);
......
......@@ -12,7 +12,6 @@
#include "chrome/browser/ui/cocoa/l10n_util.h"
#import "chrome/browser/ui/cocoa/location_bar/location_bar_decoration.h"
#import "chrome/browser/ui/cocoa/location_bar/location_bar_view_mac.h"
#import "chrome/browser/ui/cocoa/location_bar/manage_passwords_decoration.h"
#import "chrome/browser/ui/cocoa/location_bar/page_info_bubble_decoration.h"
#include "ui/views/bubble/bubble_dialog_delegate_view.h"
#include "ui/views/widget/widget_observer.h"
......@@ -76,13 +75,6 @@ class BubbleAnchorHelper final : public views::WidgetObserver {
} // namespace
LocationBarDecoration* GetManagePasswordDecoration(gfx::NativeWindow window) {
BrowserWindowController* window_controller =
[BrowserWindowController browserWindowControllerForWindow:window];
LocationBarViewMac* location_bar = [window_controller locationBarBridge];
return location_bar ? location_bar->manage_passwords_decoration() : nullptr;
}
LocationBarDecoration* GetPageInfoDecoration(gfx::NativeWindow window) {
BrowserWindowController* window_controller =
[BrowserWindowController browserWindowControllerForWindow:window];
......
......@@ -10,7 +10,6 @@
#import "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field_cell.h"
#import "chrome/browser/ui/cocoa/location_bar/location_bar_decoration.h"
#import "chrome/browser/ui/cocoa/location_bar/page_info_bubble_decoration.h"
#import "chrome/browser/ui/cocoa/location_bar/selected_keyword_decoration.h"
#import "chrome/browser/ui/cocoa/test/cocoa_test_helper.h"
#import "chrome/browser/ui/cocoa/test/scoped_force_rtl_mac.h"
#include "testing/gmock/include/gmock/gmock.h"
......@@ -95,13 +94,6 @@ TEST_F(AutocompleteTextFieldCellTest, FocusedDisplay) {
// indicate that they should be omitted.
const CGFloat kVeryWide = 1000.0;
SelectedKeywordDecoration selected_keyword_decoration;
selected_keyword_decoration.SetVisible(true);
selected_keyword_decoration.SetKeyword(base::ASCIIToUTF16("Google"), false);
[cell addLeadingDecoration:&selected_keyword_decoration];
EXPECT_NE(selected_keyword_decoration.GetWidthForSpace(kVeryWide),
LocationBarDecoration::kOmittedWidth);
// TODO(shess): This really wants a |LocationBarViewMac|, but only a
// few methods reference it, so this works well enough. But
// something better would be nice.
......
......@@ -61,10 +61,6 @@ class BubbleDecoration : public LocationBarDecoration {
base::scoped_nsobject<NSMutableDictionary> attributes_;
private:
friend class SelectedKeywordDecorationTest;
FRIEND_TEST_ALL_PREFIXES(SelectedKeywordDecorationTest,
UsesPartialKeywordIfNarrow);
// Contains any Retina-only baseline adjustment for |label_|.
CGFloat retina_baseline_offset_;
......
......@@ -25,9 +25,7 @@
@class AutocompleteTextField;
class CommandUpdater;
class LocationBarDecoration;
class ManagePasswordsDecoration;
class Profile;
class SelectedKeywordDecoration;
class PageInfoBubbleDecoration;
class ZoomDecoration;
class ZoomDecorationTest;
......@@ -169,10 +167,6 @@ class LocationBarViewMac : public LocationBar,
return page_info_decoration_.get();
}
ManagePasswordsDecoration* manage_passwords_decoration() {
return manage_passwords_decoration_.get();
}
ZoomDecoration* zoom_decoration() const { return zoom_decoration_.get(); }
Browser* browser() const { return browser_; }
......@@ -226,9 +220,6 @@ class LocationBarViewMac : public LocationBar,
AutocompleteTextField* field_; // owned by tab controller
// A decoration that shows the keyword-search bubble on the left.
std::unique_ptr<SelectedKeywordDecoration> selected_keyword_decoration_;
// A decoration that shows an icon to the left of the address. If applicable,
// it'll also show information about the current page.
std::unique_ptr<PageInfoBubbleDecoration> page_info_decoration_;
......@@ -237,9 +228,6 @@ class LocationBarViewMac : public LocationBar,
// levels.
std::unique_ptr<ZoomDecoration> zoom_decoration_;
// The right-hand-side button to manage passwords associated with a page.
std::unique_ptr<ManagePasswordsDecoration> manage_passwords_decoration_;
Browser* browser_;
// Used to change the visibility of the star decoration.
......
......@@ -29,9 +29,7 @@
#import "chrome/browser/ui/cocoa/l10n_util.h"
#import "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field.h"
#import "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field_cell.h"
#import "chrome/browser/ui/cocoa/location_bar/manage_passwords_decoration.h"
#import "chrome/browser/ui/cocoa/location_bar/page_info_bubble_decoration.h"
#import "chrome/browser/ui/cocoa/location_bar/selected_keyword_decoration.h"
#import "chrome/browser/ui/cocoa/location_bar/zoom_decoration.h"
#import "chrome/browser/ui/cocoa/omnibox/omnibox_view_mac.h"
#import "chrome/browser/ui/cocoa/toolbar/toolbar_controller.h"
......@@ -92,11 +90,8 @@ LocationBarViewMac::LocationBarViewMac(AutocompleteTextField* field,
ChromeOmniboxEditController(command_updater),
omnibox_view_(new OmniboxViewMac(this, profile, command_updater, field)),
field_(field),
selected_keyword_decoration_(new SelectedKeywordDecoration()),
page_info_decoration_(new PageInfoBubbleDecoration(this)),
zoom_decoration_(new ZoomDecoration(this)),
manage_passwords_decoration_(
new ManagePasswordsDecoration(command_updater, this)),
browser_(browser),
location_bar_visible_(true),
is_width_available_for_security_verbose_(false),
......@@ -163,12 +158,6 @@ void LocationBarViewMac::UpdateContentSettingsIcons() {
}
void LocationBarViewMac::UpdateManagePasswordsIconAndBubble() {
WebContents* web_contents = GetWebContents();
if (!web_contents)
return;
ManagePasswordsUIController::FromWebContents(web_contents)
->UpdateIconAndBubbleState(manage_passwords_decoration_->icon());
OnDecorationsChanged();
}
void LocationBarViewMac::UpdateSaveCreditCardIcon() {
......@@ -277,13 +266,10 @@ void LocationBarViewMac::Layout() {
// the constructor. I am still wrestling with how best to deal with
// right-hand decorations, which are not a static set.
[cell clearDecorations];
[cell addLeadingDecoration:selected_keyword_decoration_.get()];
[cell addLeadingDecoration:page_info_decoration_.get()];
[cell addTrailingDecoration:zoom_decoration_.get()];
[cell addTrailingDecoration:manage_passwords_decoration_.get()];
// By default only the location icon is visible.
selected_keyword_decoration_->SetVisible(false);
page_info_decoration_->SetVisible(true);
// Get the keyword to use for keyword-search and hinting.
......@@ -305,16 +291,6 @@ void LocationBarViewMac::Layout() {
NSString* a11y_description = @"";
if (!keyword.empty() && !is_keyword_hint) {
// Switch from location icon to keyword mode.
selected_keyword_decoration_->SetVisible(true);
page_info_decoration_->SetVisible(false);
selected_keyword_decoration_->SetKeyword(short_name, is_extension_keyword);
// Note: the first time through this code path the
// |selected_keyword_decoration_| has no image set because under Material
// Design we need to set its color, which we cannot do until we know the
// theme (by being installed in a browser window).
selected_keyword_decoration_->SetImage(GetKeywordImage(keyword));
a11y_description = selected_keyword_decoration_->GetAccessibilityLabel();
} else if (!keyword.empty() && is_keyword_hint) {
} else {
UpdatePageInfoText();
......@@ -642,10 +618,8 @@ std::vector<LocationBarDecoration*> LocationBarViewMac::GetDecorations() {
// TODO(ellyjones): page actions and keyword hints are not included right
// now. Keyword hints have no useful tooltip (issue 752592), and page actions
// are likewise.
decorations.push_back(selected_keyword_decoration_.get());
decorations.push_back(page_info_decoration_.get());
decorations.push_back(zoom_decoration_.get());
decorations.push_back(manage_passwords_decoration_.get());
return decorations;
}
......
// 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_LOCATION_BAR_MANAGE_PASSWORDS_DECORATION_H_
#define CHROME_BROWSER_UI_COCOA_LOCATION_BAR_MANAGE_PASSWORDS_DECORATION_H_
#import <Cocoa/Cocoa.h>
#include <memory>
#include "base/compiler_specific.h"
#include "base/macros.h"
#include "chrome/browser/ui/cocoa/location_bar/image_decoration.h"
#include "chrome/browser/ui/passwords/manage_passwords_icon.h"
class CommandUpdater;
class LocationBarViewMac;
class ManagePasswordsDecoration;
// Cocoa implementation of ManagePasswordsIcon that delegates to
// ManagePasswordsDecoration.
class ManagePasswordsIconCocoa : public ManagePasswordsIcon {
public:
ManagePasswordsIconCocoa(ManagePasswordsDecoration* decoration);
virtual ~ManagePasswordsIconCocoa();
void UpdateVisibleUI() override;
void OnChangingState() override;
int tooltip_text_id() { return tooltip_text_id_; }
private:
ManagePasswordsDecoration* decoration_; // weak, owns us
};
// Manage passwords icon on the right side of the field. This appears when
// password management is available on the current page.
class ManagePasswordsDecoration : public ImageDecoration {
public:
explicit ManagePasswordsDecoration(CommandUpdater* command_updater,
LocationBarViewMac* location_bar);
~ManagePasswordsDecoration() override;
// Implement |LocationBarDecoration|
AcceptsPress AcceptsMousePress() override;
bool OnMousePressed(NSRect frame, NSPoint location) override;
NSString* GetToolTip() override;
NSPoint GetBubblePointInFrame(NSRect frame) override;
// Updates the decoration according to icon state changes.
void UpdateVisibleUI();
// Closes the bubble if it's currently displayed.
void HideBubble();
// Accessor for the platform-independent interface.
ManagePasswordsIconCocoa* icon() { return icon_.get(); }
protected:
// Overridden from LocationBarDecoration:
const gfx::VectorIcon* GetMaterialVectorIcon() const override;
private:
// Triggers a redraw after a state change.
void OnChange();
// Updates child view states.
void UpdateUIState();
// Whether a bubble is already showing. Handles both views and Cocoa
// bubbles.
bool IsBubbleShowing();
// Shows the manage passwords bubble.
CommandUpdater* command_updater_; // Weak, owned by Browser.
// Displays all the decorations.
LocationBarViewMac* location_bar_; // Weak, owns us.
// The platform-independent interface.
std::unique_ptr<ManagePasswordsIconCocoa> icon_;
DISALLOW_COPY_AND_ASSIGN(ManagePasswordsDecoration);
};
#endif // CHROME_BROWSER_UI_COCOA_LOCATION_BAR_MANAGE_PASSWORDS_DECORATION_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/location_bar/manage_passwords_decoration.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/app/vector_icons/vector_icons.h"
#include "chrome/browser/command_updater.h"
#include "chrome/browser/ui/cocoa/browser_dialogs_views_mac.h"
#include "chrome/browser/ui/cocoa/location_bar/location_bar_view_mac.h"
#include "chrome/browser/ui/cocoa/omnibox/omnibox_view_mac.h"
#import "chrome/browser/ui/cocoa/themed_window.h"
#include "chrome/browser/ui/views/passwords/password_bubble_view_base.h"
#include "ui/base/l10n/l10n_util_mac.h"
#include "ui/base/material_design/material_design_controller.h"
#include "ui/gfx/color_palette.h"
#include "ui/gfx/image/image_skia_util_mac.h"
#include "ui/gfx/paint_vector_icon.h"
// ManagePasswordsIconCocoa
ManagePasswordsIconCocoa::ManagePasswordsIconCocoa(
ManagePasswordsDecoration* decoration)
: decoration_(decoration) {
}
ManagePasswordsIconCocoa::~ManagePasswordsIconCocoa() {
}
void ManagePasswordsIconCocoa::UpdateVisibleUI() {
decoration_->UpdateVisibleUI();
}
void ManagePasswordsIconCocoa::OnChangingState() {
decoration_->HideBubble();
}
// ManagePasswordsDecoration
ManagePasswordsDecoration::ManagePasswordsDecoration(
CommandUpdater* command_updater,
LocationBarViewMac* location_bar)
: command_updater_(command_updater),
location_bar_(location_bar),
icon_(new ManagePasswordsIconCocoa(this)) {
UpdateUIState();
}
ManagePasswordsDecoration::~ManagePasswordsDecoration() = default;
NSPoint ManagePasswordsDecoration::GetBubblePointInFrame(NSRect frame) {
const NSRect draw_frame = GetDrawRectInFrame(frame);
return NSMakePoint(NSMidX(draw_frame), NSMaxY(draw_frame));
}
AcceptsPress ManagePasswordsDecoration::AcceptsMousePress() {
return AcceptsPress::ALWAYS;
}
bool ManagePasswordsDecoration::OnMousePressed(NSRect frame, NSPoint location) {
if (IsBubbleShowing())
HideBubble();
else
command_updater_->ExecuteCommand(IDC_MANAGE_PASSWORDS_FOR_PAGE);
return true;
}
NSString* ManagePasswordsDecoration::GetToolTip() {
return icon_->tooltip_text_id()
? l10n_util::GetNSStringWithFixup(icon_->tooltip_text_id())
: nil;
}
void ManagePasswordsDecoration::OnChange() {
// |location_bar_| can be NULL in tests.
if (location_bar_)
location_bar_->OnDecorationsChanged();
}
void ManagePasswordsDecoration::UpdateUIState() {
if (icon_->state() == password_manager::ui::INACTIVE_STATE) {
SetVisible(false);
SetImage(nil);
return;
}
SetVisible(true);
// |location_bar_| can be NULL in tests.
bool location_bar_is_dark = location_bar_ &&
[[location_bar_->GetAutocompleteTextField() window]
inIncognitoModeWithSystemTheme];
SetImage(GetMaterialIcon(location_bar_is_dark));
}
void ManagePasswordsDecoration::UpdateVisibleUI() {
UpdateUIState();
OnChange();
}
void ManagePasswordsDecoration::HideBubble() {
if (IsBubbleShowing())
PasswordBubbleViewBase::CloseCurrentBubble();
}
const gfx::VectorIcon* ManagePasswordsDecoration::GetMaterialVectorIcon()
const {
// Note: update unit tests if this vector icon ever changes (it's hard-coded
// there).
return &kKeyIcon;
}
bool ManagePasswordsDecoration::IsBubbleShowing() {
return PasswordBubbleViewBase::manage_password_bubble() != nullptr;
}
// 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 <Cocoa/Cocoa.h>
#include "base/strings/utf_string_conversions.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/app/vector_icons/vector_icons.h"
#include "chrome/browser/command_updater_delegate.h"
#include "chrome/browser/command_updater_impl.h"
#include "chrome/browser/ui/cocoa/location_bar/manage_passwords_decoration.h"
#include "chrome/browser/ui/cocoa/omnibox/omnibox_view_mac.h"
#include "chrome/browser/ui/cocoa/test/cocoa_test_helper.h"
#include "chrome/grit/generated_resources.h"
#include "chrome/grit/theme_resources.h"
#include "components/password_manager/core/common/password_manager_ui.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/gtest_mac.h"
#include "ui/base/l10n/l10n_util_mac.h"
#include "ui/base/material_design/material_design_controller.h"
#include "ui/gfx/color_palette.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/image/image_skia_util_mac.h"
#include "ui/gfx/paint_vector_icon.h"
namespace {
// A simple CommandUpdaterDelegate for testing whether the correct command
// was sent.
class TestCommandUpdaterDelegate : public CommandUpdaterDelegate {
public:
TestCommandUpdaterDelegate() : id_(0) {}
void ExecuteCommandWithDisposition(int id, WindowOpenDisposition disposition)
override {
id_ = id;
}
int id() { return id_; }
private:
int id_;
};
bool ImagesEqual(NSImage* left, NSImage* right) {
if (!left || !right)
return left == right;
gfx::Image leftImage([left copy]);
gfx::Image rightImage([right copy]);
return leftImage.As1xPNGBytes()->Equals(rightImage.As1xPNGBytes());
}
} // namespace
// Tests isolated functionality of the ManagedPasswordsDecoration.
class ManagePasswordsDecorationTest : public CocoaTest {
public:
ManagePasswordsDecorationTest()
: commandUpdater_(&commandDelegate_),
decoration_(&commandUpdater_, NULL) {
commandUpdater_.UpdateCommandEnabled(IDC_MANAGE_PASSWORDS_FOR_PAGE, true);
}
protected:
TestCommandUpdaterDelegate* commandDelegate() { return &commandDelegate_; }
ManagePasswordsDecoration* decoration() { return &decoration_; }
private:
TestCommandUpdaterDelegate commandDelegate_;
CommandUpdaterImpl commandUpdater_;
ManagePasswordsDecoration decoration_;
};
TEST_F(ManagePasswordsDecorationTest, ExecutesManagePasswordsCommandOnClick) {
EXPECT_EQ(AcceptsPress::ALWAYS, decoration()->AcceptsMousePress());
EXPECT_TRUE(decoration()->OnMousePressed(NSRect(), NSPoint()));
EXPECT_EQ(IDC_MANAGE_PASSWORDS_FOR_PAGE, commandDelegate()->id());
}
// Parameter object for ManagePasswordsDecorationStateTests.
struct ManagePasswordsTestCase {
// Inputs
password_manager::ui::State state;
// Outputs
bool visible;
int toolTip;
};
// Tests that setting different combinations of password_manager::ui::State
// and the Active property of the decoration result in the correct visibility,
// decoration icon, and tooltip.
class ManagePasswordsDecorationStateTest
: public ManagePasswordsDecorationTest,
public ::testing::WithParamInterface<ManagePasswordsTestCase> {};
TEST_P(ManagePasswordsDecorationStateTest, TestState) {
decoration()->icon()->SetState(GetParam().state);
EXPECT_EQ(GetParam().visible, decoration()->IsVisible());
const int kIconSize = 16;
NSImage* expected_image =
GetParam().state == password_manager::ui::INACTIVE_STATE
? nil
: NSImageFromImageSkia(gfx::CreateVectorIcon(kKeyIcon, kIconSize,
gfx::kChromeIconGrey));
EXPECT_TRUE(ImagesEqual(expected_image, decoration()->GetImage()));
EXPECT_NSEQ(GetParam().toolTip
? l10n_util::GetNSStringWithFixup(GetParam().toolTip)
: nil,
decoration()->GetToolTip());
}
ManagePasswordsTestCase testCases[] = {
{.state = password_manager::ui::INACTIVE_STATE,
.visible = false,
.toolTip = 0},
{.state = password_manager::ui::MANAGE_STATE,
.visible = true,
.toolTip = IDS_PASSWORD_MANAGER_TOOLTIP_MANAGE},
{.state = password_manager::ui::PENDING_PASSWORD_STATE,
.visible = true,
.toolTip = IDS_PASSWORD_MANAGER_TOOLTIP_SAVE}};
INSTANTIATE_TEST_CASE_P(,
ManagePasswordsDecorationStateTest,
::testing::ValuesIn(testCases));
// Copyright (c) 2011 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_LOCATION_BAR_SELECTED_KEYWORD_DECORATION_H_
#define CHROME_BROWSER_UI_COCOA_LOCATION_BAR_SELECTED_KEYWORD_DECORATION_H_
#include <string>
#import <Cocoa/Cocoa.h>
#include "base/gtest_prod_util.h"
#include "base/macros.h"
#include "base/strings/string16.h"
#include "chrome/browser/ui/cocoa/location_bar/bubble_decoration.h"
class SelectedKeywordDecoration : public BubbleDecoration {
public:
SelectedKeywordDecoration();
~SelectedKeywordDecoration() override;
// Return the color used to draw the SelectedKeywordDecoration in MD.
NSColor* GetBackgroundBorderColor() override;
// Calculates appropriate full and partial label strings based on
// inputs.
void SetKeyword(const base::string16& keyword, bool is_extension_keyword);
// Determines what combination of labels and image will best fit
// within |width|, makes those current for |BubbleDecoration|, and
// return the resulting width.
CGFloat GetWidthForSpace(CGFloat width) override;
void SetImage(NSImage* image);
NSString* GetAccessibilityLabel() override;
bool IsAccessibilityIgnored() override;
private:
friend class SelectedKeywordDecorationTest;
FRIEND_TEST_ALL_PREFIXES(SelectedKeywordDecorationTest,
UsesPartialKeywordIfNarrow);
base::scoped_nsobject<NSImage> search_image_;
base::scoped_nsobject<NSString> full_string_;
base::scoped_nsobject<NSString> partial_string_;
DISALLOW_COPY_AND_ASSIGN(SelectedKeywordDecoration);
};
#endif // CHROME_BROWSER_UI_COCOA_LOCATION_BAR_SELECTED_KEYWORD_DECORATION_H_
// 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.
#import "chrome/browser/ui/cocoa/location_bar/selected_keyword_decoration.h"
#include <stddef.h>
#include "base/i18n/rtl.h"
#include "base/strings/string_util.h"
#include "base/strings/sys_string_conversions.h"
#include "base/strings/utf_string_conversions.h"
#import "chrome/browser/ui/cocoa/omnibox/omnibox_view_mac.h"
#include "chrome/grit/generated_resources.h"
#include "skia/ext/skia_utils_mac.h"
#include "ui/base/l10n/l10n_util_mac.h"
#include "ui/base/material_design/material_design_controller.h"
#include "ui/gfx/color_palette.h"
#include "ui/gfx/text_elider.h"
namespace {
// Build a short string to use in keyword-search when the field isn't very big.
base::string16 CalculateMinString(const base::string16& description) {
// Chop at the first '.' or whitespace.
const size_t chop_index = description.find_first_of(base::kWhitespaceUTF16 +
base::ASCIIToUTF16("."));
base::string16 min_string(
(chop_index == base::string16::npos)
? gfx::TruncateString(description, 3, gfx::WORD_BREAK)
: description.substr(0, chop_index));
base::i18n::AdjustStringForLocaleDirection(&min_string);
return min_string;
}
} // namespace
SelectedKeywordDecoration::SelectedKeywordDecoration() {
// Note: the unit test
// SelectedKeywordDecorationTest.UsesPartialKeywordIfNarrow expects to work
// with a fully-initialized SelectedKeywordDecoration (i.e. one that has a
// text color and image). During ordinary operation,
// LocationBarViewMac::Layout() sets the image before the decoration is
// actually used, which the unit test does as well. If
// SelectedKeywordDecoration's initialization process changes, the unit test
// should also be updated.
SetTextColor(GetBackgroundBorderColor());
}
SelectedKeywordDecoration::~SelectedKeywordDecoration() {}
NSColor* SelectedKeywordDecoration::GetBackgroundBorderColor() {
return skia::SkColorToSRGBNSColor(gfx::kGoogleBlue700);
}
CGFloat SelectedKeywordDecoration::GetWidthForSpace(CGFloat width) {
const CGFloat full_width =
GetWidthForImageAndLabel(search_image_, full_string_);
if (full_width <= width) {
BubbleDecoration::SetImage(search_image_);
SetLabel(full_string_);
return full_width;
}
BubbleDecoration::SetImage(nil);
const CGFloat no_image_width = GetWidthForImageAndLabel(nil, full_string_);
if (no_image_width <= width || !partial_string_) {
SetLabel(full_string_);
return no_image_width;
}
SetLabel(partial_string_);
return GetWidthForImageAndLabel(nil, partial_string_);
}
void SelectedKeywordDecoration::SetKeyword(const base::string16& short_name,
bool is_extension_keyword) {
const base::string16 min_name(CalculateMinString(short_name));
const int keyword_text_id = IDS_OMNIBOX_KEYWORD_TEXT_MD;
NSString* full_string =
is_extension_keyword
? base::SysUTF16ToNSString(short_name)
: l10n_util::GetNSStringF(keyword_text_id, short_name);
// The text will be like "Search <name>:". "<name>" is a parameter
// derived from |short_name|. If we're using Material Design, the text will
// be like "Search <name>" instead.
full_string_.reset([full_string copy]);
if (min_name.empty()) {
partial_string_.reset();
} else {
NSString* partial_string =
is_extension_keyword
? base::SysUTF16ToNSString(min_name)
: l10n_util::GetNSStringF(keyword_text_id, min_name);
partial_string_.reset([partial_string copy]);
}
}
void SelectedKeywordDecoration::SetImage(NSImage* image) {
if (image != search_image_)
search_image_.reset([image retain]);
BubbleDecoration::SetImage(image);
}
NSString* SelectedKeywordDecoration::GetAccessibilityLabel() {
return full_string_.get();
}
bool SelectedKeywordDecoration::IsAccessibilityIgnored() {
return true;
}
// Copyright (c) 2010 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 <Cocoa/Cocoa.h>
#include "base/strings/utf_string_conversions.h"
#import "chrome/browser/ui/cocoa/location_bar/selected_keyword_decoration.h"
#import "chrome/browser/ui/cocoa/test/cocoa_test_helper.h"
#include "components/omnibox/browser/vector_icons.h"
#include "components/vector_icons/vector_icons.h"
#include "testing/gtest/include/gtest/gtest.h"
#import "testing/gtest_mac.h"
#include "ui/base/material_design/material_design_controller.h"
#include "ui/gfx/image/image_skia_util_mac.h"
#include "ui/gfx/paint_vector_icon.h"
namespace {
// A wide width which should fit everything.
const CGFloat kWidth(300.0);
// A narrow width for tests which test things that don't fit.
const CGFloat kNarrowWidth(5.0);
} // namespace
class SelectedKeywordDecorationTest : public CocoaTest {
public:
SelectedKeywordDecorationTest() {
// With Material Design vector images the image isn't set at creation time
// but later by the LocationBar. The unit tests fail with a nil image, so
// initialize it now.
const int kDefaultIconSize = 16;
decoration_.SetImage(NSImageFromImageSkia(gfx::CreateVectorIcon(
vector_icons::kSearchIcon, kDefaultIconSize, SK_ColorBLACK)));
}
SelectedKeywordDecoration decoration_;
};
// Test that the cell correctly chooses the partial keyword if there's
// not enough room.
TEST_F(SelectedKeywordDecorationTest, UsesPartialKeywordIfNarrow) {
const base::string16 kKeyword = base::ASCIIToUTF16("Engine");
NSString* const kFullString = @"Search Engine";
NSString* const kPartialString = @"Search En\u2026";
decoration_.SetKeyword(kKeyword, false);
// Wide width chooses the full string and image.
const CGFloat all_width = decoration_.GetWidthForSpace(kWidth);
EXPECT_TRUE(decoration_.image_);
EXPECT_NSEQ(kFullString, decoration_.label_);
// If not enough space to include the image, uses exactly the full
// string.
const CGFloat full_width = decoration_.GetWidthForSpace(all_width - 5.0);
EXPECT_LT(full_width, all_width);
EXPECT_FALSE(decoration_.image_);
EXPECT_NSEQ(kFullString, decoration_.label_);
// Narrow width chooses the partial string.
const CGFloat partial_width = decoration_.GetWidthForSpace(kNarrowWidth);
EXPECT_LT(partial_width, full_width);
EXPECT_FALSE(decoration_.image_);
EXPECT_NSEQ(kPartialString, decoration_.label_);
// Narrow doesn't choose partial string if there is not one.
decoration_.partial_string_.reset();
decoration_.GetWidthForSpace(kNarrowWidth);
EXPECT_FALSE(decoration_.image_);
EXPECT_NSEQ(kFullString, decoration_.label_);
}
......@@ -4162,9 +4162,7 @@ test("unit_tests") {
"../browser/ui/cocoa/location_bar/autocomplete_text_field_unittest_helper.mm",
"../browser/ui/cocoa/location_bar/image_decoration_unittest.mm",
"../browser/ui/cocoa/location_bar/location_bar_view_mac_unittest.mm",
"../browser/ui/cocoa/location_bar/manage_passwords_decoration_unittest.mm",
"../browser/ui/cocoa/location_bar/page_info_bubble_decoration_unittest.mm",
"../browser/ui/cocoa/location_bar/selected_keyword_decoration_unittest.mm",
"../browser/ui/cocoa/location_bar/zoom_decoration_unittest.mm",
"../browser/ui/cocoa/main_menu_builder_unittest.mm",
"../browser/ui/cocoa/md_hover_button_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