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

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

Issue 6314012: Make uneditable when readonly (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
25 // Convert to Wide so that the printed string will be readable when
26 // check fails.
24 #define EXPECT_STR_EQ(ascii, utf16) \ 27 #define EXPECT_STR_EQ(ascii, utf16) \
25 EXPECT_EQ(ASCIIToWide(ascii), UTF16ToWide(utf16)) 28 EXPECT_EQ(ASCIIToWide(ascii), UTF16ToWide(utf16))
29 #define EXPECT_STR_NE(ascii, utf16) \
30 EXPECT_NE(ASCIIToWide(ascii), UTF16ToWide(utf16))
26 31
27 // TODO(oshima): Move tests that are independent of TextfieldViews to 32 // TODO(oshima): Move tests that are independent of TextfieldViews to
28 // textfield_unittests.cc once we move the test utility functions 33 // textfield_unittests.cc once we move the test utility functions
29 // from chrome/browser/automation/ to app/test/. 34 // from chrome/browser/automation/ to app/test/.
30 class NativeTextfieldViewsTest : public ViewsTestBase, 35 class NativeTextfieldViewsTest : public ViewsTestBase,
31 public Textfield::Controller { 36 public Textfield::Controller {
32 public: 37 public:
33 NativeTextfieldViewsTest() 38 NativeTextfieldViewsTest()
34 : widget_(NULL), 39 : widget_(NULL),
35 textfield_(NULL), 40 textfield_(NULL),
(...skipping 361 matching lines...) Expand 10 before | Expand all | Expand 10 after
397 } 402 }
398 403
399 void VerifyTextfieldContextMenuContents(bool textfield_has_selection, 404 void VerifyTextfieldContextMenuContents(bool textfield_has_selection,
400 ui::MenuModel* menu_model) { 405 ui::MenuModel* menu_model) {
401 EXPECT_TRUE(menu_model->IsEnabledAt(4 /* Separator */)); 406 EXPECT_TRUE(menu_model->IsEnabledAt(4 /* Separator */));
402 EXPECT_TRUE(menu_model->IsEnabledAt(5 /* SELECT ALL */)); 407 EXPECT_TRUE(menu_model->IsEnabledAt(5 /* SELECT ALL */));
403 EXPECT_EQ(textfield_has_selection, menu_model->IsEnabledAt(0 /* CUT */)); 408 EXPECT_EQ(textfield_has_selection, menu_model->IsEnabledAt(0 /* CUT */));
404 EXPECT_EQ(textfield_has_selection, menu_model->IsEnabledAt(1 /* COPY */)); 409 EXPECT_EQ(textfield_has_selection, menu_model->IsEnabledAt(1 /* COPY */));
405 EXPECT_EQ(textfield_has_selection, menu_model->IsEnabledAt(3 /* DELETE */)); 410 EXPECT_EQ(textfield_has_selection, menu_model->IsEnabledAt(3 /* DELETE */));
406 string16 str; 411 string16 str;
407 views::ViewsDelegate::views_delegate->GetClipboard() 412 views::ViewsDelegate::views_delegate->GetClipboard()->
408 ->ReadText(ui::Clipboard::BUFFER_STANDARD, &str); 413 ReadText(ui::Clipboard::BUFFER_STANDARD, &str);
409 EXPECT_NE(str.empty(), menu_model->IsEnabledAt(2 /* PASTE */)); 414 EXPECT_NE(str.empty(), menu_model->IsEnabledAt(2 /* PASTE */));
410 } 415 }
411 416
412 TEST_F(NativeTextfieldViewsTest, ContextMenuDisplayTest) { 417 TEST_F(NativeTextfieldViewsTest, ContextMenuDisplayTest) {
413 scoped_ptr<TestViewsDelegate> test_views_delegate(new TestViewsDelegate()); 418 scoped_ptr<TestViewsDelegate> test_views_delegate(new TestViewsDelegate());
414 AutoReset<views::ViewsDelegate*> auto_reset( 419 AutoReset<views::ViewsDelegate*> auto_reset(
415 &views::ViewsDelegate::views_delegate, test_views_delegate.get()); 420 &views::ViewsDelegate::views_delegate, test_views_delegate.get());
416 views::ViewsDelegate::views_delegate = test_views_delegate.get(); 421 views::ViewsDelegate::views_delegate = test_views_delegate.get();
417 InitTextfield(Textfield::STYLE_DEFAULT); 422 InitTextfield(Textfield::STYLE_DEFAULT);
418 textfield_->SetText(ASCIIToUTF16("hello world")); 423 textfield_->SetText(ASCIIToUTF16("hello world"));
419 EXPECT_TRUE(GetContextMenu()); 424 EXPECT_TRUE(GetContextMenu());
420 VerifyTextfieldContextMenuContents(false, GetContextMenu()->model()); 425 VerifyTextfieldContextMenuContents(false, GetContextMenu()->model());
421 426
422 textfield_->SelectAll(); 427 textfield_->SelectAll();
423 VerifyTextfieldContextMenuContents(true, GetContextMenu()->model()); 428 VerifyTextfieldContextMenuContents(true, GetContextMenu()->model());
424 } 429 }
425 430
431 TEST_F(NativeTextfieldViewsTest, ReadOnlyTest) {
432 scoped_ptr<TestViewsDelegate> test_views_delegate(new TestViewsDelegate());
433 AutoReset<views::ViewsDelegate*> auto_reset(
434 &views::ViewsDelegate::views_delegate, test_views_delegate.get());
435
436 InitTextfield(Textfield::STYLE_DEFAULT);
437 textfield_->SetText(ASCIIToUTF16(" one two three "));
438 textfield_->SetReadOnly(true);
439 SendKeyEventToTextfieldViews(ui::VKEY_HOME);
440 EXPECT_EQ(0U, textfield_->GetCursorPosition());
441
442 SendKeyEventToTextfieldViews(ui::VKEY_END);
443 EXPECT_EQ(15U, textfield_->GetCursorPosition());
444
445 SendKeyEventToTextfieldViews(ui::VKEY_LEFT, false, false);
446 EXPECT_EQ(14U, textfield_->GetCursorPosition());
447
448 SendKeyEventToTextfieldViews(ui::VKEY_LEFT, false, true);
449 EXPECT_EQ(9U, textfield_->GetCursorPosition());
450
451 SendKeyEventToTextfieldViews(ui::VKEY_LEFT, true, true);
452 EXPECT_EQ(5U, textfield_->GetCursorPosition());
453 EXPECT_STR_EQ("two ", textfield_->GetSelectedText());
454
455 textfield_->SelectAll();
456 EXPECT_STR_EQ(" one two three ", textfield_->GetSelectedText());
457
458 // CUT&PASTE does not work, but COPY works
459 SendKeyEventToTextfieldViews(ui::VKEY_X, false, true);
460 EXPECT_STR_EQ(" one two three ", textfield_->GetSelectedText());
461 string16 str;
462 views::ViewsDelegate::views_delegate->GetClipboard()->
463 ReadText(ui::Clipboard::BUFFER_STANDARD, &str);
464 EXPECT_STR_NE(" one two three ", str);
465
466 SendKeyEventToTextfieldViews(ui::VKEY_C, false, true);
467 views::ViewsDelegate::views_delegate->GetClipboard()->
468 ReadText(ui::Clipboard::BUFFER_STANDARD, &str);
469 EXPECT_STR_EQ(" one two three ", str);
470
471 // SetText should work even in read only mode.
472 textfield_->SetText(ASCIIToUTF16(" four five six "));
473 EXPECT_STR_EQ(" four five six ", textfield_->text());
474
475 // Paste shouldn't work.
476 SendKeyEventToTextfieldViews(ui::VKEY_V, false, true);
477 EXPECT_STR_EQ(" four five six ", textfield_->text());
478 EXPECT_TRUE(textfield_->GetSelectedText().empty());
479
480 textfield_->SelectAll();
481 EXPECT_STR_EQ(" four five six ", textfield_->GetSelectedText());
482
483 // Text field is unmodifiable and selection shouldn't change.
484 SendKeyEventToTextfieldViews(ui::VKEY_DELETE);
485 EXPECT_STR_EQ(" four five six ", textfield_->GetSelectedText());
486 SendKeyEventToTextfieldViews(ui::VKEY_BACK);
487 EXPECT_STR_EQ(" four five six ", textfield_->GetSelectedText());
488 SendKeyEventToTextfieldViews(ui::VKEY_T);
489 EXPECT_STR_EQ(" four five six ", textfield_->GetSelectedText());
490 }
491
426 } // namespace views 492 } // namespace views
OLDNEW
« no previous file with comments | « views/controls/textfield/native_textfield_views.cc ('k') | views/controls/textfield/textfield.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698