1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
|
// 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.
#import <Cocoa/Cocoa.h>
#include "base/mac/scoped_nsobject.h"
#include "base/strings/utf_string_conversions.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/keyword_hint_decoration.h"
#import "chrome/browser/ui/cocoa/location_bar/location_bar_decoration.h"
#import "chrome/browser/ui/cocoa/location_bar/location_icon_decoration.h"
#import "chrome/browser/ui/cocoa/location_bar/security_state_bubble_decoration.h"
#import "chrome/browser/ui/cocoa/location_bar/selected_keyword_decoration.h"
#import "chrome/browser/ui/cocoa/location_bar/star_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"
#include "testing/gtest/include/gtest/gtest.h"
#import "testing/gtest_mac.h"
#include "testing/platform_test.h"
#import "third_party/ocmock/OCMock/OCMock.h"
#include "third_party/ocmock/gtest_support.h"
#include "ui/base/material_design/material_design_controller.h"
using ::testing::Return;
using ::testing::StrictMock;
using ::testing::_;
namespace {
// Width of the field so that we don't have to ask |field_| for it all
// the time.
const CGFloat kWidth(300.0);
class MockDecoration : public LocationBarDecoration {
public:
virtual CGFloat GetWidthForSpace(CGFloat width) { return 20.0; }
MOCK_METHOD2(DrawInFrame, void(NSRect frame, NSView* control_view));
MOCK_METHOD0(GetToolTip, NSString*());
};
class AutocompleteTextFieldCellTest : public CocoaTest {
public:
AutocompleteTextFieldCellTest() {
// Make sure this is wide enough to play games with the cell
// decorations.
const NSRect frame = NSMakeRect(0, 0, kWidth, 30);
base::scoped_nsobject<NSTextField> view(
[[NSTextField alloc] initWithFrame:frame]);
view_ = view.get();
base::scoped_nsobject<AutocompleteTextFieldCell> cell(
[[AutocompleteTextFieldCell alloc] initTextCell:@"Testing"]);
[cell setEditable:YES];
[cell setBordered:YES];
[cell clearDecorations];
mock_leading_decoration_.SetVisible(false);
[cell addLeadingDecoration:&mock_leading_decoration_];
mock_trailing_decoration0_.SetVisible(false);
mock_trailing_decoration1_.SetVisible(false);
[cell addTrailingDecoration:&mock_trailing_decoration0_];
[cell addTrailingDecoration:&mock_trailing_decoration1_];
[view_ setCell:cell.get()];
[[test_window() contentView] addSubview:view_];
}
NSTextField* view_;
MockDecoration mock_leading_decoration_;
MockDecoration mock_trailing_decoration0_;
MockDecoration mock_trailing_decoration1_;
};
// Basic view tests (AddRemove, Display).
TEST_VIEW(AutocompleteTextFieldCellTest, view_);
// Test drawing, mostly to ensure nothing leaks or crashes.
// Flaky, disabled. Bug http://crbug.com/49522
TEST_F(AutocompleteTextFieldCellTest, DISABLED_FocusedDisplay) {
[view_ display];
// Test focused drawing.
[test_window() makePretendKeyWindowAndSetFirstResponder:view_];
[view_ display];
[test_window() clearPretendKeyWindowAndFirstResponder];
// Test display of various cell configurations.
AutocompleteTextFieldCell* cell =
static_cast<AutocompleteTextFieldCell*>([view_ cell]);
// Load available decorations and try drawing. To make sure that
// they are actually drawn, check that |GetWidthForSpace()| doesn't
// 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.
LocationIconDecoration location_icon_decoration(NULL);
location_icon_decoration.SetVisible(true);
location_icon_decoration.SetImage([NSImage imageNamed:@"NSApplicationIcon"]);
[cell addLeadingDecoration:&location_icon_decoration];
EXPECT_NE(location_icon_decoration.GetWidthForSpace(kVeryWide),
LocationBarDecoration::kOmittedWidth);
SecurityStateBubbleDecoration security_state_bubble_decoration(
&location_icon_decoration, nullptr);
security_state_bubble_decoration.SetVisible(true);
security_state_bubble_decoration.SetImage(
[NSImage imageNamed:@"NSApplicationIcon"]);
security_state_bubble_decoration.SetLabel(@"Application");
[cell addLeadingDecoration:&security_state_bubble_decoration];
EXPECT_NE(security_state_bubble_decoration.GetWidthForSpace(kVeryWide),
LocationBarDecoration::kOmittedWidth);
StarDecoration star_decoration(NULL);
star_decoration.SetVisible(true);
[cell addTrailingDecoration:&star_decoration];
EXPECT_NE(star_decoration.GetWidthForSpace(kVeryWide),
LocationBarDecoration::kOmittedWidth);
KeywordHintDecoration keyword_hint_decoration;
keyword_hint_decoration.SetVisible(true);
keyword_hint_decoration.SetKeyword(base::ASCIIToUTF16("google"), false);
[cell addTrailingDecoration:&keyword_hint_decoration];
EXPECT_NE(keyword_hint_decoration.GetWidthForSpace(kVeryWide),
LocationBarDecoration::kOmittedWidth);
// Make sure we're actually calling |DrawInFrame()|.
StrictMock<MockDecoration> mock_decoration;
mock_decoration.SetVisible(true);
[cell addLeadingDecoration:&mock_decoration];
EXPECT_CALL(mock_decoration, DrawInFrame(_, _));
EXPECT_NE(mock_decoration.GetWidthForSpace(kVeryWide),
LocationBarDecoration::kOmittedWidth);
[view_ display];
[cell clearDecorations];
}
TEST_F(AutocompleteTextFieldCellTest, TextFrame) {
AutocompleteTextFieldCell* cell =
static_cast<AutocompleteTextFieldCell*>([view_ cell]);
const NSRect bounds([view_ bounds]);
NSRect textFrame;
// The cursor frame should stay the same throughout.
const NSRect cursorFrame([cell textCursorFrameForFrame:bounds]);
EXPECT_NSEQ(cursorFrame, bounds);
// At default settings, everything goes to the text area.
textFrame = [cell textFrameForFrame:bounds];
EXPECT_FALSE(NSIsEmptyRect(textFrame));
EXPECT_TRUE(NSContainsRect(bounds, textFrame));
EXPECT_EQ(1, NSMinX(textFrame));
EXPECT_EQ(NSMaxX(bounds), NSMaxX(textFrame));
EXPECT_TRUE(NSContainsRect(cursorFrame, textFrame));
// Leading decoration takes up space.
mock_leading_decoration_.SetVisible(true);
textFrame = [cell textFrameForFrame:bounds];
EXPECT_FALSE(NSIsEmptyRect(textFrame));
EXPECT_TRUE(NSContainsRect(bounds, textFrame));
EXPECT_GT(NSMinX(textFrame), NSMinX(bounds));
EXPECT_TRUE(NSContainsRect(cursorFrame, textFrame));
}
// The editor frame should be slightly inset from the text frame.
TEST_F(AutocompleteTextFieldCellTest, DrawingRectForBounds) {
AutocompleteTextFieldCell* cell =
static_cast<AutocompleteTextFieldCell*>([view_ cell]);
const NSRect bounds([view_ bounds]);
NSRect textFrame, drawingRect;
textFrame = [cell textFrameForFrame:bounds];
drawingRect = [cell drawingRectForBounds:bounds];
EXPECT_FALSE(NSIsEmptyRect(drawingRect));
EXPECT_TRUE(NSContainsRect(textFrame, NSInsetRect(drawingRect, 1, 1)));
// Save the starting frame for after clear.
const NSRect originalDrawingRect = drawingRect;
mock_leading_decoration_.SetVisible(true);
textFrame = [cell textFrameForFrame:bounds];
drawingRect = [cell drawingRectForBounds:bounds];
EXPECT_FALSE(NSIsEmptyRect(drawingRect));
EXPECT_TRUE(NSContainsRect(NSInsetRect(textFrame, 1, 1), drawingRect));
mock_trailing_decoration0_.SetVisible(true);
textFrame = [cell textFrameForFrame:bounds];
drawingRect = [cell drawingRectForBounds:bounds];
EXPECT_FALSE(NSIsEmptyRect(drawingRect));
EXPECT_TRUE(NSContainsRect(NSInsetRect(textFrame, 1, 1), drawingRect));
mock_leading_decoration_.SetVisible(false);
mock_trailing_decoration0_.SetVisible(false);
drawingRect = [cell drawingRectForBounds:bounds];
EXPECT_FALSE(NSIsEmptyRect(drawingRect));
EXPECT_NSEQ(drawingRect, originalDrawingRect);
}
// Test that leading decorations are at the correct edge of the cell.
TEST_F(AutocompleteTextFieldCellTest, LeadingDecorationFrame) {
AutocompleteTextFieldCell* cell =
static_cast<AutocompleteTextFieldCell*>([view_ cell]);
const NSRect bounds = [view_ bounds];
mock_leading_decoration_.SetVisible(true);
const NSRect decorationRect =
[cell frameForDecoration:&mock_leading_decoration_ inFrame:bounds];
EXPECT_FALSE(NSIsEmptyRect(decorationRect));
EXPECT_TRUE(NSContainsRect(bounds, decorationRect));
// Decoration should be left of |drawingRect|.
const NSRect drawingRect = [cell drawingRectForBounds:bounds];
EXPECT_GT(NSMinX(drawingRect), NSMinX(decorationRect));
// Decoration should be left of |textFrame|.
const NSRect textFrame = [cell textFrameForFrame:bounds];
EXPECT_GT(NSMinX(textFrame), NSMinX(decorationRect));
}
// Test that trailing decorations are at the correct edge of the cell.
TEST_F(AutocompleteTextFieldCellTest, TrailingDecorationFrame) {
AutocompleteTextFieldCell* cell =
static_cast<AutocompleteTextFieldCell*>([view_ cell]);
const NSRect bounds = [view_ bounds];
mock_trailing_decoration0_.SetVisible(true);
mock_trailing_decoration1_.SetVisible(true);
const NSRect decoration0Rect =
[cell frameForDecoration:&mock_trailing_decoration0_ inFrame:bounds];
EXPECT_FALSE(NSIsEmptyRect(decoration0Rect));
EXPECT_TRUE(NSContainsRect(bounds, decoration0Rect));
// Trailing decorations are ordered from innermost to outermost.
// Outer decoration (0) to right of inner decoration (1).
const NSRect decoration1Rect =
[cell frameForDecoration:&mock_trailing_decoration1_ inFrame:bounds];
EXPECT_FALSE(NSIsEmptyRect(decoration1Rect));
EXPECT_TRUE(NSContainsRect(bounds, decoration1Rect));
EXPECT_LT(NSMinX(decoration1Rect), NSMinX(decoration0Rect));
// Decoration should be right of |drawingRect|.
const NSRect drawingRect = [cell drawingRectForBounds:bounds];
EXPECT_LT(NSMinX(drawingRect), NSMinX(decoration1Rect));
// Decoration should be right of |textFrame|.
const NSRect textFrame = [cell textFrameForFrame:bounds];
EXPECT_LT(NSMinX(textFrame), NSMinX(decoration1Rect));
}
// Verify -[AutocompleteTextFieldCell
// updateMouseTrackingAndToolTipsInRect:ofView:].
TEST_F(AutocompleteTextFieldCellTest, UpdateToolTips) {
NSString* tooltip = @"tooltip";
// Leading decoration returns a tooltip, make sure it is called at
// least once.
mock_leading_decoration_.SetVisible(true);
EXPECT_CALL(mock_leading_decoration_, GetToolTip())
.WillOnce(Return(tooltip))
.WillRepeatedly(Return(tooltip));
// Right decoration returns no tooltip, make sure it is called at
// least once.
mock_trailing_decoration0_.SetVisible(true);
EXPECT_CALL(mock_trailing_decoration0_, GetToolTip())
.WillOnce(Return((NSString*)nil))
.WillRepeatedly(Return((NSString*)nil));
AutocompleteTextFieldCell* cell =
static_cast<AutocompleteTextFieldCell*>([view_ cell]);
const NSRect bounds = [view_ bounds];
const NSRect leadingDecorationRect =
[cell frameForDecoration:&mock_leading_decoration_ inFrame:bounds];
// |controlView| gets the tooltip for the leading decoration.
id controlView = [OCMockObject mockForClass:[AutocompleteTextField class]];
[[controlView expect] addToolTip:tooltip forRect:leadingDecorationRect];
[cell updateMouseTrackingAndToolTipsInRect:bounds ofView:controlView];
EXPECT_OCMOCK_VERIFY(controlView);
}
class AutocompleteTextFieldCellTestRTL : public AutocompleteTextFieldCellTest {
private:
cocoa_l10n_util::ScopedForceRTLMac rtl_;
};
// Test that leading decorations are at the correct edge of the cell.
TEST_F(AutocompleteTextFieldCellTestRTL, LeadingDecorationFrame) {
AutocompleteTextFieldCell* cell =
static_cast<AutocompleteTextFieldCell*>([view_ cell]);
const NSRect bounds = [view_ bounds];
mock_leading_decoration_.SetVisible(true);
const NSRect decorationRect =
[cell frameForDecoration:&mock_leading_decoration_ inFrame:bounds];
EXPECT_FALSE(NSIsEmptyRect(decorationRect));
EXPECT_TRUE(NSContainsRect(bounds, decorationRect));
// Decoration should be right of |drawingRect|.
const NSRect drawingRect = [cell drawingRectForBounds:bounds];
EXPECT_LT(NSMinX(drawingRect), NSMinX(decorationRect));
// Decoration should be right of |textFrame|.
const NSRect textFrame = [cell textFrameForFrame:bounds];
EXPECT_LT(NSMinX(textFrame), NSMinX(decorationRect));
}
// Test that trailing decorations are at the correct edge of the cell.
TEST_F(AutocompleteTextFieldCellTestRTL, TrailingDecorationFrame) {
AutocompleteTextFieldCell* cell =
static_cast<AutocompleteTextFieldCell*>([view_ cell]);
const NSRect bounds = [view_ bounds];
mock_trailing_decoration0_.SetVisible(true);
mock_trailing_decoration1_.SetVisible(true);
const NSRect decoration0Rect =
[cell frameForDecoration:&mock_trailing_decoration0_ inFrame:bounds];
EXPECT_FALSE(NSIsEmptyRect(decoration0Rect));
EXPECT_TRUE(NSContainsRect(bounds, decoration0Rect));
// Trailing decorations are ordered from front to back..
// Outer decoration (0) to the left of inner decoration (1).
const NSRect decoration1Rect =
[cell frameForDecoration:&mock_trailing_decoration1_ inFrame:bounds];
EXPECT_FALSE(NSIsEmptyRect(decoration1Rect));
EXPECT_TRUE(NSContainsRect(bounds, decoration1Rect));
EXPECT_GT(NSMinX(decoration1Rect), NSMinX(decoration0Rect));
// Decoration should be left of |drawingRect|.
const NSRect drawingRect = [cell drawingRectForBounds:bounds];
EXPECT_GT(NSMinX(drawingRect), NSMinX(decoration1Rect));
// Decoration should be left of |textFrame|.
const NSRect textFrame = [cell textFrameForFrame:bounds];
EXPECT_GT(NSMinX(textFrame), NSMinX(decoration1Rect));
}
} // namespace
|