Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(293)

Side by Side Diff: chrome/browser/ui/cocoa/location_bar/autocomplete_text_field_editor_unittest.mm

Issue 10915069: Add Copy URL option to Omnibox context menu when URL is replaced by Instant Extended. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #import "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field_editor.h" 5 #import "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field_editor.h"
6 6
7 #include "base/memory/scoped_nsobject.h" 7 #include "base/memory/scoped_nsobject.h"
8 #include "base/memory/scoped_ptr.h" 8 #include "base/memory/scoped_ptr.h"
9 #include "base/string_util.h" 9 #include "base/string_util.h"
10 #include "chrome/app/chrome_command_ids.h" // IDC_* 10 #include "chrome/app/chrome_command_ids.h" // IDC_*
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after
195 195
196 // Test that -copy: is correctly delegated to the observer. 196 // Test that -copy: is correctly delegated to the observer.
197 TEST_F(AutocompleteTextFieldEditorObserverTest, Copy) { 197 TEST_F(AutocompleteTextFieldEditorObserverTest, Copy) {
198 EXPECT_CALL(field_observer_, CanCopy()) 198 EXPECT_CALL(field_observer_, CanCopy())
199 .WillOnce(Return(true)); 199 .WillOnce(Return(true));
200 EXPECT_CALL(field_observer_, CopyToPasteboard(A<NSPasteboard*>())) 200 EXPECT_CALL(field_observer_, CopyToPasteboard(A<NSPasteboard*>()))
201 .Times(1); 201 .Times(1);
202 [editor_ copy:nil]; 202 [editor_ copy:nil];
203 } 203 }
204 204
205 // Test that -copyURL: is correctly delegated to the observer.
206 TEST_F(AutocompleteTextFieldEditorObserverTest, CopyURL) {
207 EXPECT_CALL(field_observer_, CanCopy()).WillOnce(Return(true));
208 EXPECT_CALL(field_observer_,
209 CopyURLToPasteboard(A<NSPasteboard*>())).Times(1);
210 [editor_ copyURL:nil];
211 }
212
205 // Test that -cut: is correctly delegated to the observer and clears 213 // Test that -cut: is correctly delegated to the observer and clears
206 // the text field. 214 // the text field.
207 TEST_F(AutocompleteTextFieldEditorObserverTest, Cut) { 215 TEST_F(AutocompleteTextFieldEditorObserverTest, Cut) {
208 // Sets a string in the field. 216 // Sets a string in the field.
209 NSString* test_string = @"astring"; 217 NSString* test_string = @"astring";
210 EXPECT_CALL(field_observer_, OnDidBeginEditing()); 218 EXPECT_CALL(field_observer_, OnDidBeginEditing());
211 EXPECT_CALL(field_observer_, OnBeforeChange()); 219 EXPECT_CALL(field_observer_, OnBeforeChange());
212 EXPECT_CALL(field_observer_, OnDidChange()); 220 EXPECT_CALL(field_observer_, OnDidChange());
213 EXPECT_CALL(field_observer_, SelectionRangeForProposedRange(A<NSRange>())) 221 EXPECT_CALL(field_observer_, SelectionRangeForProposedRange(A<NSRange>()))
214 .WillRepeatedly(ReturnArg<0>()); 222 .WillRepeatedly(ReturnArg<0>());
(...skipping 12 matching lines...) Expand all
227 } 235 }
228 236
229 // Test that -pasteAndGo: is correctly delegated to the observer. 237 // Test that -pasteAndGo: is correctly delegated to the observer.
230 TEST_F(AutocompleteTextFieldEditorObserverTest, PasteAndGo) { 238 TEST_F(AutocompleteTextFieldEditorObserverTest, PasteAndGo) {
231 EXPECT_CALL(field_observer_, OnPasteAndGo()); 239 EXPECT_CALL(field_observer_, OnPasteAndGo());
232 [editor_ pasteAndGo:nil]; 240 [editor_ pasteAndGo:nil];
233 } 241 }
234 242
235 // Test that the menu is constructed correctly when CanPasteAndGo(). 243 // Test that the menu is constructed correctly when CanPasteAndGo().
236 TEST_F(AutocompleteTextFieldEditorObserverTest, CanPasteAndGoMenu) { 244 TEST_F(AutocompleteTextFieldEditorObserverTest, CanPasteAndGoMenu) {
237 EXPECT_CALL(field_observer_, CanPasteAndGo()) 245 EXPECT_CALL(field_observer_, GetPasteActionStringId()).
238 .WillOnce(Return(true)); 246 WillOnce(Return(IDS_PASTE_AND_GO));
239 EXPECT_CALL(field_observer_, GetPasteActionStringId())
240 .WillOnce(Return(IDS_PASTE_AND_GO));
241 247
242 NSMenu* menu = [editor_ menuForEvent:nil]; 248 NSMenu* menu = [editor_ menuForEvent:nil];
243 NSArray* items = [menu itemArray]; 249 NSArray* items = [menu itemArray];
244 ASSERT_EQ([items count], 6U); 250 ASSERT_EQ([items count], 7U);
245 // TODO(shess): Check the titles, too? 251 // TODO(shess): Check the titles, too?
246 NSUInteger i = 0; // Use an index to make future changes easier. 252 NSUInteger i = 0; // Use an index to make future changes easier.
247 EXPECT_EQ([[items objectAtIndex:i++] action], @selector(cut:)); 253 EXPECT_EQ([[items objectAtIndex:i++] action], @selector(cut:));
248 EXPECT_EQ([[items objectAtIndex:i++] action], @selector(copy:)); 254 EXPECT_EQ([[items objectAtIndex:i++] action], @selector(copy:));
255 EXPECT_EQ([[items objectAtIndex:i++] action], @selector(copyURL:));
249 EXPECT_EQ([[items objectAtIndex:i++] action], @selector(paste:)); 256 EXPECT_EQ([[items objectAtIndex:i++] action], @selector(paste:));
250 EXPECT_EQ([[items objectAtIndex:i++] action], @selector(pasteAndGo:)); 257 EXPECT_EQ([[items objectAtIndex:i++] action], @selector(pasteAndGo:));
251 EXPECT_TRUE([[items objectAtIndex:i++] isSeparatorItem]); 258 EXPECT_TRUE([[items objectAtIndex:i++] isSeparatorItem]);
252 259
253 EXPECT_EQ([[items objectAtIndex:i] action], @selector(commandDispatch:)); 260 EXPECT_EQ([[items objectAtIndex:i] action], @selector(commandDispatch:));
254 EXPECT_EQ([[items objectAtIndex:i] tag], IDC_EDIT_SEARCH_ENGINES); 261 EXPECT_EQ([[items objectAtIndex:i] tag], IDC_EDIT_SEARCH_ENGINES);
255 i++; 262 i++;
256 } 263 }
257 264
258 // Test that the menu is constructed correctly when !CanPasteAndGo(). 265 // Test that the menu is constructed correctly when !CanPasteAndGo().
259 TEST_F(AutocompleteTextFieldEditorObserverTest, CannotPasteAndGoMenu) { 266 TEST_F(AutocompleteTextFieldEditorObserverTest, CannotPasteAndGoMenu) {
260 EXPECT_CALL(field_observer_, CanPasteAndGo()) 267 EXPECT_CALL(field_observer_, GetPasteActionStringId()).
261 .WillOnce(Return(false)); 268 WillOnce(Return(IDS_PASTE_AND_GO));
262 269
263 NSMenu* menu = [editor_ menuForEvent:nil]; 270 NSMenu* menu = [editor_ menuForEvent:nil];
264 NSArray* items = [menu itemArray]; 271 NSArray* items = [menu itemArray];
265 ASSERT_EQ([items count], 5U); 272 ASSERT_EQ([items count], 7U);
266 // TODO(shess): Check the titles, too? 273 // TODO(shess): Check the titles, too?
267 NSUInteger i = 0; // Use an index to make future changes easier. 274 NSUInteger i = 0; // Use an index to make future changes easier.
268 EXPECT_EQ([[items objectAtIndex:i++] action], @selector(cut:)); 275 EXPECT_EQ([[items objectAtIndex:i++] action], @selector(cut:));
269 EXPECT_EQ([[items objectAtIndex:i++] action], @selector(copy:)); 276 EXPECT_EQ([[items objectAtIndex:i++] action], @selector(copy:));
277 EXPECT_EQ([[items objectAtIndex:i++] action], @selector(copyURL:));
270 EXPECT_EQ([[items objectAtIndex:i++] action], @selector(paste:)); 278 EXPECT_EQ([[items objectAtIndex:i++] action], @selector(paste:));
279 EXPECT_EQ([[items objectAtIndex:i++] action], @selector(pasteAndGo:));
271 EXPECT_TRUE([[items objectAtIndex:i++] isSeparatorItem]); 280 EXPECT_TRUE([[items objectAtIndex:i++] isSeparatorItem]);
272 281
273 EXPECT_EQ([[items objectAtIndex:i] action], @selector(commandDispatch:)); 282 EXPECT_EQ([[items objectAtIndex:i] action], @selector(commandDispatch:));
274 EXPECT_EQ([[items objectAtIndex:i] tag], IDC_EDIT_SEARCH_ENGINES); 283 EXPECT_EQ([[items objectAtIndex:i] tag], IDC_EDIT_SEARCH_ENGINES);
275 i++; 284 i++;
276 } 285 }
277 286
278 // Test that the menu is constructed correctly when field isn't 287 // Test that the menu is constructed correctly when field isn't
279 // editable. 288 // editable.
280 TEST_F(AutocompleteTextFieldEditorObserverTest, CanPasteAndGoMenuNotEditable) { 289 TEST_F(AutocompleteTextFieldEditorObserverTest, CanPasteAndGoMenuNotEditable) {
281 [field_ setEditable:NO]; 290 [field_ setEditable:NO];
282 [editor_ setEditable:NO]; 291 [editor_ setEditable:NO];
283 292
284 // Never call these when not editable. 293 // Never call this when not editable.
285 EXPECT_CALL(field_observer_, CanPasteAndGo()) 294 EXPECT_CALL(field_observer_, GetPasteActionStringId()).Times(0);
286 .Times(0);
287 EXPECT_CALL(field_observer_, GetPasteActionStringId())
288 .Times(0);
289 295
290 NSMenu* menu = [editor_ menuForEvent:nil]; 296 NSMenu* menu = [editor_ menuForEvent:nil];
291 NSArray* items = [menu itemArray]; 297 NSArray* items = [menu itemArray];
292 ASSERT_EQ([items count], 3U); 298 ASSERT_EQ([items count], 3U);
293 // TODO(shess): Check the titles, too? 299 // TODO(shess): Check the titles, too?
294 NSUInteger i = 0; // Use an index to make future changes easier. 300 NSUInteger i = 0; // Use an index to make future changes easier.
295 EXPECT_EQ([[items objectAtIndex:i++] action], @selector(cut:)); 301 EXPECT_EQ([[items objectAtIndex:i++] action], @selector(cut:));
296 EXPECT_EQ([[items objectAtIndex:i++] action], @selector(copy:)); 302 EXPECT_EQ([[items objectAtIndex:i++] action], @selector(copy:));
297 EXPECT_EQ([[items objectAtIndex:i++] action], @selector(paste:)); 303 EXPECT_EQ([[items objectAtIndex:i++] action], @selector(paste:));
298 } 304 }
299 305
306 // Test that the menu is constructed correctly when ShouldEnableCopyURL()
307 // returns true.
308 TEST_F(AutocompleteTextFieldEditorObserverTest, ShouldEnableCopyURLMenu) {
309 EXPECT_CALL(field_observer_, GetPasteActionStringId()).
310 WillOnce(Return(IDS_PASTE_AND_GO));
311
312 NSMenu* menu = [editor_ menuForEvent:nil];
313 NSArray* items = [menu itemArray];
314 ASSERT_EQ([items count], 7U);
315 // TODO(shess): Check the titles, too?
316 NSUInteger i = 0; // Use an index to make future changes easier.
317 EXPECT_EQ([[items objectAtIndex:i++] action], @selector(cut:));
318 EXPECT_EQ([[items objectAtIndex:i++] action], @selector(copy:));
319 EXPECT_EQ([[items objectAtIndex:i++] action], @selector(copyURL:));
320 EXPECT_EQ([[items objectAtIndex:i++] action], @selector(paste:));
321 EXPECT_EQ([[items objectAtIndex:i++] action], @selector(pasteAndGo:));
322 EXPECT_TRUE([[items objectAtIndex:i++] isSeparatorItem]);
323
324 EXPECT_EQ([[items objectAtIndex:i] action], @selector(commandDispatch:));
325 EXPECT_EQ([[items objectAtIndex:i] tag], IDC_EDIT_SEARCH_ENGINES);
326 }
327
328 // Test that the menu validation works as expected when CanPasteAndGo().
329 TEST_F(AutocompleteTextFieldEditorObserverTest, CanPasteAndGoValidate) {
330 EXPECT_CALL(field_observer_, GetPasteActionStringId())
331 .WillOnce(Return(IDS_PASTE_AND_GO));
332 EXPECT_CALL(field_observer_, CanPasteAndGo()).WillOnce(Return(true));
333
334 NSMenu* menu = [editor_ menuForEvent:nil];
335 NSArray* items = [menu itemArray];
336 ASSERT_EQ([items count], 7U);
337 for (NSUInteger i = 0; i < [items count]; ++i) {
338 NSMenuItem* item = [items objectAtIndex:i];
339 if ([item action] == @selector(pasteAndGo:)) {
340 EXPECT_TRUE([editor_ validateMenuItem:item]);
341 break;
342 }
343 }
344 }
345
346 // Test that the menu validation works as expected when !CanPasteAndGo().
347 TEST_F(AutocompleteTextFieldEditorObserverTest, CannotPasteAndGoValidate) {
348 EXPECT_CALL(field_observer_, GetPasteActionStringId())
349 .WillOnce(Return(IDS_PASTE_AND_GO));
350 EXPECT_CALL(field_observer_, CanPasteAndGo()).WillOnce(Return(false));
351
352 NSMenu* menu = [editor_ menuForEvent:nil];
353 NSArray* items = [menu itemArray];
354 ASSERT_EQ([items count], 7U);
355 for (NSUInteger i = 0; i < [items count]; ++i) {
356 NSMenuItem* item = [items objectAtIndex:i];
357 if ([item action] == @selector(pasteAndGo:)) {
358 EXPECT_FALSE([editor_ validateMenuItem:item]);
359 break;
360 }
361 }
362 }
363
364 // Test that the menu validation works as expected when ShouldEnableCopyURL().
365 TEST_F(AutocompleteTextFieldEditorObserverTest, ShouldEnableCopyURLValidate) {
366 EXPECT_CALL(field_observer_, GetPasteActionStringId())
367 .WillOnce(Return(IDS_PASTE_AND_GO));
368 EXPECT_CALL(field_observer_, ShouldEnableCopyURL()).WillOnce(Return(true));
369
370 NSMenu* menu = [editor_ menuForEvent:nil];
371 NSArray* items = [menu itemArray];
372 ASSERT_EQ([items count], 7U);
373 for (NSUInteger i = 0; i < [items count]; ++i) {
374 NSMenuItem* item = [items objectAtIndex:i];
375 if ([item action] == @selector(copyURL:)) {
376 EXPECT_TRUE([editor_ validateMenuItem:item]);
377 break;
378 }
379 }
380 }
381
382 // Test that the menu validation works as expected when !ShouldEnableCopyURL().
383 TEST_F(AutocompleteTextFieldEditorObserverTest,
384 ShouldNotEnableCopyURLValidate) {
385 EXPECT_CALL(field_observer_, GetPasteActionStringId())
386 .WillOnce(Return(IDS_PASTE_AND_GO));
387 EXPECT_CALL(field_observer_, ShouldEnableCopyURL()).WillOnce(Return(false));
388
389 NSMenu* menu = [editor_ menuForEvent:nil];
390 NSArray* items = [menu itemArray];
391 ASSERT_EQ([items count], 7U);
392 for (NSUInteger i = 0; i < [items count]; ++i) {
393 NSMenuItem* item = [items objectAtIndex:i];
394 if ([item action] == @selector(copyURL:)) {
395 EXPECT_FALSE([editor_ validateMenuItem:item]);
396 break;
397 }
398 }
399 }
400
300 } // namespace 401 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698