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

Side by Side Diff: views/controls/textfield/native_textfield_views_unittest.cc

Issue 6245003: Views-implementation of AutocompleteEditView (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: " Created 9 years, 11 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 #include "base/auto_reset.h" 5 #include "base/auto_reset.h"
6 #include "base/message_loop.h" 6 #include "base/message_loop.h"
7 #include "base/utf_string_conversions.h" 7 #include "base/utf_string_conversions.h"
8 #include "testing/gtest/include/gtest/gtest.h" 8 #include "testing/gtest/include/gtest/gtest.h"
9 #include "ui/base/clipboard/clipboard.h" 9 #include "ui/base/clipboard/clipboard.h"
10 #include "ui/base/clipboard/scoped_clipboard_writer.h"
10 #include "ui/base/keycodes/keyboard_codes.h" 11 #include "ui/base/keycodes/keyboard_codes.h"
11 #include "views/controls/menu/menu_2.h" 12 #include "views/controls/menu/menu_2.h"
12 #include "views/controls/textfield/native_textfield_views.h" 13 #include "views/controls/textfield/native_textfield_views.h"
13 #include "views/controls/textfield/textfield.h" 14 #include "views/controls/textfield/textfield.h"
14 #include "views/controls/textfield/textfield_views_model.h" 15 #include "views/controls/textfield/textfield_views_model.h"
15 #include "views/event.h" 16 #include "views/event.h"
16 #include "views/focus/focus_manager.h" 17 #include "views/focus/focus_manager.h"
17 #include "views/test/test_views_delegate.h" 18 #include "views/test/test_views_delegate.h"
18 #include "views/test/views_test_base.h" 19 #include "views/test/views_test_base.h"
19 #include "views/widget/widget.h" 20 #include "views/widget/widget.h"
20 #include "views/views_delegate.h" 21 #include "views/views_delegate.h"
21 22
22 namespace views { 23 namespace views {
23 24
24 #define EXPECT_STR_EQ(ascii, utf16) \ 25 #define EXPECT_STR_EQ(ascii, utf16) \
25 EXPECT_EQ(ASCIIToWide(ascii), UTF16ToWide(utf16)) 26 EXPECT_EQ(ASCIIToWide(ascii), UTF16ToWide(utf16))
Peter Kasting 2011/01/20 19:44:37 Nit: Why not just ASCIIToUTF16() instead of conver
oshima 2011/01/20 21:35:48 Scott suggested to use this in order to be able to
Peter Kasting 2011/01/20 21:41:15 Then we should write an appropriate operator<<() f
27 #define EXPECT_STR_NE(ascii, utf16) \
28 EXPECT_NE(ASCIIToWide(ascii), UTF16ToWide(utf16))
26 29
27 // TODO(oshima): Move tests that are independent of TextfieldViews to 30 // TODO(oshima): Move tests that are independent of TextfieldViews to
28 // textfield_unittests.cc once we move the test utility functions 31 // textfield_unittests.cc once we move the test utility functions
29 // from chrome/browser/automation/ to app/test/. 32 // from chrome/browser/automation/ to app/test/.
30 class NativeTextfieldViewsTest : public ViewsTestBase, 33 class NativeTextfieldViewsTest : public ViewsTestBase,
31 public Textfield::Controller { 34 public Textfield::Controller {
32 public: 35 public:
33 NativeTextfieldViewsTest() 36 NativeTextfieldViewsTest()
34 : widget_(NULL), 37 : widget_(NULL),
35 textfield_(NULL), 38 textfield_(NULL),
(...skipping 380 matching lines...) Expand 10 before | Expand all | Expand 10 after
416 views::ViewsDelegate::views_delegate = test_views_delegate.get(); 419 views::ViewsDelegate::views_delegate = test_views_delegate.get();
417 InitTextfield(Textfield::STYLE_DEFAULT); 420 InitTextfield(Textfield::STYLE_DEFAULT);
418 textfield_->SetText(ASCIIToUTF16("hello world")); 421 textfield_->SetText(ASCIIToUTF16("hello world"));
419 EXPECT_TRUE(GetContextMenu()); 422 EXPECT_TRUE(GetContextMenu());
420 VerifyTextfieldContextMenuContents(false, GetContextMenu()->model()); 423 VerifyTextfieldContextMenuContents(false, GetContextMenu()->model());
421 424
422 textfield_->SelectAll(); 425 textfield_->SelectAll();
423 VerifyTextfieldContextMenuContents(true, GetContextMenu()->model()); 426 VerifyTextfieldContextMenuContents(true, GetContextMenu()->model());
424 } 427 }
425 428
429 TEST_F(NativeTextfieldViewsTest, ReadOnlyTest) {
430 scoped_ptr<TestViewsDelegate> test_views_delegate(new TestViewsDelegate());
431 AutoReset<views::ViewsDelegate*> auto_reset(
432 &views::ViewsDelegate::views_delegate, test_views_delegate.get());
433
434 InitTextfield(Textfield::STYLE_DEFAULT);
435 textfield_->SetText(ASCIIToUTF16(" one two three "));
436 textfield_->SetReadOnly(true);
437 SendKeyEventToTextfieldViews(ui::VKEY_HOME);
438 EXPECT_EQ(0U, textfield_->GetCursorPosition());
439
440 SendKeyEventToTextfieldViews(ui::VKEY_END);
441 EXPECT_EQ(15U, textfield_->GetCursorPosition());
442
443 SendKeyEventToTextfieldViews(ui::VKEY_LEFT, false, false);
444 EXPECT_EQ(14U, textfield_->GetCursorPosition());
445
446 SendKeyEventToTextfieldViews(ui::VKEY_LEFT, false, true);
447 EXPECT_EQ(9U, textfield_->GetCursorPosition());
448
449 SendKeyEventToTextfieldViews(ui::VKEY_LEFT, true, true);
450 EXPECT_EQ(5U, textfield_->GetCursorPosition());
451 EXPECT_STR_EQ("two ", textfield_->GetSelectedText());
452
453 textfield_->SelectAll();
454 EXPECT_STR_EQ(" one two three ", textfield_->GetSelectedText());
455
456 // CUT&PASTE does not work, but COPY works
457 SendKeyEventToTextfieldViews(ui::VKEY_X, false, true);
458 EXPECT_STR_EQ(" one two three ", textfield_->GetSelectedText());
459 string16 str;
460 views::ViewsDelegate::views_delegate->GetClipboard()
461 ->ReadText(ui::Clipboard::BUFFER_STANDARD, &str);
462 EXPECT_STR_NE(" one two three ", str);
463
464 SendKeyEventToTextfieldViews(ui::VKEY_C, false, true);
465 views::ViewsDelegate::views_delegate->GetClipboard()
466 ->ReadText(ui::Clipboard::BUFFER_STANDARD, &str);
467 EXPECT_STR_EQ(" one two three ", str);
468
469 // SetText should work even in read only mode.
470 textfield_->SetText(ASCIIToUTF16(" four five six "));
471 EXPECT_STR_EQ(" four five six ", textfield_->text());
472
473 // Paste shouldn't work.
474 SendKeyEventToTextfieldViews(ui::VKEY_V, false, true);
475 EXPECT_STR_EQ(" four five six ", textfield_->text());
476 EXPECT_TRUE(textfield_->GetSelectedText().empty());
477
478 textfield_->SelectAll();
479 EXPECT_STR_EQ(" four five six ", textfield_->GetSelectedText());
480
481 // Text field is unmodifiable and selection shouldn't change.
482 SendKeyEventToTextfieldViews(ui::VKEY_DELETE);
483 EXPECT_STR_EQ(" four five six ", textfield_->GetSelectedText());
484 SendKeyEventToTextfieldViews(ui::VKEY_BACK);
485 EXPECT_STR_EQ(" four five six ", textfield_->GetSelectedText());
486 SendKeyEventToTextfieldViews(ui::VKEY_T);
487 EXPECT_STR_EQ(" four five six ", textfield_->GetSelectedText());
488 }
489
426 } // namespace views 490 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698