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

Side by Side Diff: ui/views/controls/textfield/textfield_views_model_unittest.cc

Issue 8747001: Reintroduce password support to NativeTextfieldViews (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase, const syntax, password->obscured, dead methods, DCHECK, Ctrl+[CX] tests, fix other tests Created 9 years 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 #include <vector> 5 #include <vector>
6 6
7 #include "base/auto_reset.h" 7 #include "base/auto_reset.h"
8 #include "base/memory/scoped_ptr.h" 8 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop.h" 9 #include "base/message_loop.h"
10 #include "base/string16.h" 10 #include "base/string16.h"
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after
237 #endif 237 #endif
238 EXPECT_TRUE(model.Backspace()); 238 EXPECT_TRUE(model.Backspace());
239 EXPECT_EQ(WideToUTF16(L"\x002C\x0020\x05D1\x05BC\x05B7\x05E9"), 239 EXPECT_EQ(WideToUTF16(L"\x002C\x0020\x05D1\x05BC\x05B7\x05E9"),
240 model.GetText()); 240 model.GetText());
241 } 241 }
242 242
243 TEST_F(TextfieldViewsModelTest, EmptyString) { 243 TEST_F(TextfieldViewsModelTest, EmptyString) {
244 TextfieldViewsModel model(NULL); 244 TextfieldViewsModel model(NULL);
245 EXPECT_EQ(string16(), model.GetText()); 245 EXPECT_EQ(string16(), model.GetText());
246 EXPECT_EQ(string16(), model.GetSelectedText()); 246 EXPECT_EQ(string16(), model.GetSelectedText());
247 EXPECT_EQ(string16(), model.GetVisibleText());
248 247
249 model.MoveCursorLeft(gfx::CHARACTER_BREAK, true); 248 model.MoveCursorLeft(gfx::CHARACTER_BREAK, true);
250 EXPECT_EQ(0U, model.GetCursorPosition()); 249 EXPECT_EQ(0U, model.GetCursorPosition());
251 model.MoveCursorRight(gfx::CHARACTER_BREAK, true); 250 model.MoveCursorRight(gfx::CHARACTER_BREAK, true);
252 EXPECT_EQ(0U, model.GetCursorPosition()); 251 EXPECT_EQ(0U, model.GetCursorPosition());
253 252
254 EXPECT_EQ(string16(), model.GetSelectedText()); 253 EXPECT_EQ(string16(), model.GetSelectedText());
255 254
256 EXPECT_FALSE(model.Delete()); 255 EXPECT_FALSE(model.Delete());
257 EXPECT_FALSE(model.Backspace()); 256 EXPECT_FALSE(model.Backspace());
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after
407 model.MoveCursorRight(gfx::CHARACTER_BREAK, true); // select "H" 406 model.MoveCursorRight(gfx::CHARACTER_BREAK, true); // select "H"
408 model.ReplaceChar('B'); 407 model.ReplaceChar('B');
409 EXPECT_STR_EQ("BELL", model.GetText()); 408 EXPECT_STR_EQ("BELL", model.GetText());
410 model.MoveCursorRight(gfx::LINE_BREAK, false); 409 model.MoveCursorRight(gfx::LINE_BREAK, false);
411 model.MoveCursorLeft(gfx::CHARACTER_BREAK, true); 410 model.MoveCursorLeft(gfx::CHARACTER_BREAK, true);
412 model.MoveCursorLeft(gfx::CHARACTER_BREAK, true); // select "ELL" 411 model.MoveCursorLeft(gfx::CHARACTER_BREAK, true); // select "ELL"
413 model.ReplaceChar('E'); 412 model.ReplaceChar('E');
414 EXPECT_STR_EQ("BEE", model.GetText()); 413 EXPECT_STR_EQ("BEE", model.GetText());
415 } 414 }
416 415
417 TEST_F(TextfieldViewsModelTest, Password) {
418 TextfieldViewsModel model(NULL);
419 model.set_is_password(true);
420 model.Append(ASCIIToUTF16("HELLO"));
421 EXPECT_STR_EQ("*****", model.GetVisibleText());
422 EXPECT_STR_EQ("HELLO", model.GetText());
423 EXPECT_TRUE(model.Delete());
424
425 EXPECT_STR_EQ("****", model.GetVisibleText());
426 EXPECT_STR_EQ("ELLO", model.GetText());
427 EXPECT_EQ(0U, model.GetCursorPosition());
428
429 model.SelectAll();
430 EXPECT_STR_EQ("ELLO", model.GetSelectedText());
431 EXPECT_EQ(4U, model.GetCursorPosition());
432
433 model.InsertChar('X');
434 EXPECT_STR_EQ("*", model.GetVisibleText());
435 EXPECT_STR_EQ("X", model.GetText());
436 }
437
438 TEST_F(TextfieldViewsModelTest, Word) { 416 TEST_F(TextfieldViewsModelTest, Word) {
439 TextfieldViewsModel model(NULL); 417 TextfieldViewsModel model(NULL);
440 model.Append( 418 model.Append(
441 ASCIIToUTF16("The answer to Life, the Universe, and Everything")); 419 ASCIIToUTF16("The answer to Life, the Universe, and Everything"));
442 model.MoveCursorRight(gfx::WORD_BREAK, false); 420 model.MoveCursorRight(gfx::WORD_BREAK, false);
443 EXPECT_EQ(3U, model.GetCursorPosition()); 421 EXPECT_EQ(3U, model.GetCursorPosition());
444 model.MoveCursorRight(gfx::WORD_BREAK, false); 422 model.MoveCursorRight(gfx::WORD_BREAK, false);
445 EXPECT_EQ(10U, model.GetCursorPosition()); 423 EXPECT_EQ(10U, model.GetCursorPosition());
446 model.MoveCursorRight(gfx::WORD_BREAK, false); 424 model.MoveCursorRight(gfx::WORD_BREAK, false);
447 model.MoveCursorRight(gfx::WORD_BREAK, false); 425 model.MoveCursorRight(gfx::WORD_BREAK, false);
(...skipping 24 matching lines...) Expand all
472 EXPECT_STR_EQ("to Life", model.GetSelectedText()); 450 EXPECT_STR_EQ("to Life", model.GetSelectedText());
473 model.MoveCursorLeft(gfx::WORD_BREAK, true); 451 model.MoveCursorLeft(gfx::WORD_BREAK, true);
474 model.MoveCursorLeft(gfx::WORD_BREAK, true); 452 model.MoveCursorLeft(gfx::WORD_BREAK, true);
475 model.MoveCursorLeft(gfx::WORD_BREAK, true); // Select to the begining. 453 model.MoveCursorLeft(gfx::WORD_BREAK, true); // Select to the begining.
476 EXPECT_STR_EQ("The answer to Life", model.GetSelectedText()); 454 EXPECT_STR_EQ("The answer to Life", model.GetSelectedText());
477 // Should be safe to go pervious word at the begining. 455 // Should be safe to go pervious word at the begining.
478 model.MoveCursorLeft(gfx::WORD_BREAK, true); 456 model.MoveCursorLeft(gfx::WORD_BREAK, true);
479 EXPECT_STR_EQ("The answer to Life", model.GetSelectedText()); 457 EXPECT_STR_EQ("The answer to Life", model.GetSelectedText());
480 model.ReplaceChar('4'); 458 model.ReplaceChar('4');
481 EXPECT_EQ(string16(), model.GetSelectedText()); 459 EXPECT_EQ(string16(), model.GetSelectedText());
482 EXPECT_STR_EQ("42", model.GetVisibleText()); 460 EXPECT_STR_EQ("42", model.GetText());
483 } 461 }
484 462
485 TEST_F(TextfieldViewsModelTest, SetText) { 463 TEST_F(TextfieldViewsModelTest, SetText) {
486 TextfieldViewsModel model(NULL); 464 TextfieldViewsModel model(NULL);
487 model.Append(ASCIIToUTF16("HELLO")); 465 model.Append(ASCIIToUTF16("HELLO"));
488 model.MoveCursorRight(gfx::LINE_BREAK, false); 466 model.MoveCursorRight(gfx::LINE_BREAK, false);
489 model.SetText(ASCIIToUTF16("GOODBYE")); 467 model.SetText(ASCIIToUTF16("GOODBYE"));
490 EXPECT_STR_EQ("GOODBYE", model.GetText()); 468 EXPECT_STR_EQ("GOODBYE", model.GetText());
491 // SetText won't reset the cursor posistion. 469 // SetText won't reset the cursor posistion.
492 EXPECT_EQ(5U, model.GetCursorPosition()); 470 EXPECT_EQ(5U, model.GetCursorPosition());
(...skipping 17 matching lines...) Expand all
510 #endif 488 #endif
511 TEST_F(TextfieldViewsModelTest, MAYBE_Clipboard) { 489 TEST_F(TextfieldViewsModelTest, MAYBE_Clipboard) {
512 ui::Clipboard* clipboard 490 ui::Clipboard* clipboard
513 = views::ViewsDelegate::views_delegate->GetClipboard(); 491 = views::ViewsDelegate::views_delegate->GetClipboard();
514 string16 initial_clipboard_text = ASCIIToUTF16("initial text"); 492 string16 initial_clipboard_text = ASCIIToUTF16("initial text");
515 ui::ScopedClipboardWriter(clipboard).WriteText(initial_clipboard_text); 493 ui::ScopedClipboardWriter(clipboard).WriteText(initial_clipboard_text);
516 494
517 string16 clipboard_text; 495 string16 clipboard_text;
518 TextfieldViewsModel model(NULL); 496 TextfieldViewsModel model(NULL);
519 model.Append(ASCIIToUTF16("HELLO WORLD")); 497 model.Append(ASCIIToUTF16("HELLO WORLD"));
498
499 // Cut with an empty selection should do nothing.
520 model.MoveCursorRight(gfx::LINE_BREAK, false); 500 model.MoveCursorRight(gfx::LINE_BREAK, false);
521
522 // Test for cut: Empty selection.
523 EXPECT_FALSE(model.Cut()); 501 EXPECT_FALSE(model.Cut());
524 clipboard->ReadText(ui::Clipboard::BUFFER_STANDARD, &clipboard_text); 502 clipboard->ReadText(ui::Clipboard::BUFFER_STANDARD, &clipboard_text);
525 EXPECT_STR_EQ(UTF16ToUTF8(initial_clipboard_text), clipboard_text); 503 EXPECT_EQ(initial_clipboard_text, clipboard_text);
526 EXPECT_STR_EQ("HELLO WORLD", model.GetText()); 504 EXPECT_STR_EQ("HELLO WORLD", model.GetText());
527 EXPECT_EQ(11U, model.GetCursorPosition()); 505 EXPECT_EQ(11U, model.GetCursorPosition());
528 506
529 // Test for cut: Non-empty selection. 507 // Copy with an empty selection should do nothing.
508 model.Copy();
509 clipboard->ReadText(ui::Clipboard::BUFFER_STANDARD, &clipboard_text);
510 EXPECT_EQ(initial_clipboard_text, clipboard_text);
511 EXPECT_STR_EQ("HELLO WORLD", model.GetText());
512 EXPECT_EQ(11U, model.GetCursorPosition());
513
514 // Cut on obscured (password) text should do nothing.
515 model.render_text()->SetIsObscured(true);
516 model.SelectAll();
517 EXPECT_FALSE(model.Cut());
518 clipboard->ReadText(ui::Clipboard::BUFFER_STANDARD, &clipboard_text);
519 EXPECT_EQ(initial_clipboard_text, clipboard_text);
520 EXPECT_STR_EQ("HELLO WORLD", model.GetText());
521 EXPECT_STR_EQ("HELLO WORLD", model.GetSelectedText());
522
523 // Copy on obscured text should do nothing.
524 model.SelectAll();
525 EXPECT_FALSE(model.Cut());
526 clipboard->ReadText(ui::Clipboard::BUFFER_STANDARD, &clipboard_text);
527 EXPECT_EQ(initial_clipboard_text, clipboard_text);
528 EXPECT_STR_EQ("HELLO WORLD", model.GetText());
529 EXPECT_STR_EQ("HELLO WORLD", model.GetSelectedText());
530
531 // Cut with non-empty selection.
532 model.render_text()->SetIsObscured(false);
533 model.MoveCursorRight(gfx::LINE_BREAK, false);
530 model.MoveCursorLeft(gfx::WORD_BREAK, true); 534 model.MoveCursorLeft(gfx::WORD_BREAK, true);
531 EXPECT_TRUE(model.Cut()); 535 EXPECT_TRUE(model.Cut());
532 clipboard->ReadText(ui::Clipboard::BUFFER_STANDARD, &clipboard_text); 536 clipboard->ReadText(ui::Clipboard::BUFFER_STANDARD, &clipboard_text);
533 EXPECT_STR_EQ("WORLD", clipboard_text); 537 EXPECT_STR_EQ("WORLD", clipboard_text);
534 EXPECT_STR_EQ("HELLO ", model.GetText()); 538 EXPECT_STR_EQ("HELLO ", model.GetText());
535 EXPECT_EQ(6U, model.GetCursorPosition()); 539 EXPECT_EQ(6U, model.GetCursorPosition());
536 540
537 // Test for copy: Empty selection. 541 // Copy with non-empty selection.
538 model.Copy();
539 clipboard->ReadText(ui::Clipboard::BUFFER_STANDARD, &clipboard_text);
540 EXPECT_STR_EQ("WORLD", clipboard_text);
541 EXPECT_STR_EQ("HELLO ", model.GetText());
542 EXPECT_EQ(6U, model.GetCursorPosition());
543
544 // Test for copy: Non-empty selection.
545 model.Append(ASCIIToUTF16("HELLO WORLD")); 542 model.Append(ASCIIToUTF16("HELLO WORLD"));
546 model.SelectAll(); 543 model.SelectAll();
547 model.Copy(); 544 model.Copy();
548 clipboard->ReadText(ui::Clipboard::BUFFER_STANDARD, &clipboard_text); 545 clipboard->ReadText(ui::Clipboard::BUFFER_STANDARD, &clipboard_text);
549 EXPECT_STR_EQ("HELLO HELLO WORLD", clipboard_text); 546 EXPECT_STR_EQ("HELLO HELLO WORLD", clipboard_text);
550 EXPECT_STR_EQ("HELLO HELLO WORLD", model.GetText()); 547 EXPECT_STR_EQ("HELLO HELLO WORLD", model.GetText());
551 EXPECT_EQ(17U, model.GetCursorPosition()); 548 EXPECT_EQ(17U, model.GetCursorPosition());
552 549
553 // Test for paste. 550 // Paste (with obscured bit set; should be ignored).
554 model.ClearSelection(); 551 model.ClearSelection();
555 model.MoveCursorRight(gfx::LINE_BREAK, false); 552 model.MoveCursorRight(gfx::LINE_BREAK, false);
556 model.MoveCursorLeft(gfx::WORD_BREAK, true); 553 model.MoveCursorLeft(gfx::WORD_BREAK, true);
554 model.render_text()->SetIsObscured(true);
557 EXPECT_TRUE(model.Paste()); 555 EXPECT_TRUE(model.Paste());
558 clipboard->ReadText(ui::Clipboard::BUFFER_STANDARD, &clipboard_text); 556 clipboard->ReadText(ui::Clipboard::BUFFER_STANDARD, &clipboard_text);
559 EXPECT_STR_EQ("HELLO HELLO WORLD", clipboard_text); 557 EXPECT_STR_EQ("HELLO HELLO WORLD", clipboard_text);
560 EXPECT_STR_EQ("HELLO HELLO HELLO HELLO WORLD", model.GetText()); 558 EXPECT_STR_EQ("HELLO HELLO HELLO HELLO WORLD", model.GetText());
561 EXPECT_EQ(29U, model.GetCursorPosition()); 559 EXPECT_EQ(29U, model.GetCursorPosition());
562 } 560 }
563 561
564 void SelectWordTestVerifier(TextfieldViewsModel &model, 562 void SelectWordTestVerifier(TextfieldViewsModel &model,
565 const string16 &expected_selected_string, size_t expected_cursor_pos) { 563 const string16 &expected_selected_string, size_t expected_cursor_pos) {
566 EXPECT_EQ(expected_selected_string, model.GetSelectedText()); 564 EXPECT_EQ(expected_selected_string, model.GetSelectedText());
(...skipping 960 matching lines...) Expand 10 before | Expand all | Expand 10 after
1527 EXPECT_TRUE(model.Undo()); 1525 EXPECT_TRUE(model.Undo());
1528 EXPECT_STR_EQ("ABCDE", model.GetText()); 1526 EXPECT_STR_EQ("ABCDE", model.GetText());
1529 EXPECT_TRUE(model.Redo()); 1527 EXPECT_TRUE(model.Redo());
1530 EXPECT_STR_EQ("1234", model.GetText()); 1528 EXPECT_STR_EQ("1234", model.GetText());
1531 EXPECT_FALSE(model.Redo()); 1529 EXPECT_FALSE(model.Redo());
1532 1530
1533 // TODO(oshima): We need MockInputMethod to test the behavior with IME. 1531 // TODO(oshima): We need MockInputMethod to test the behavior with IME.
1534 } 1532 }
1535 1533
1536 } // namespace views 1534 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698