| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/ui/views/payments/validating_textfield.h" |
| 6 |
| 7 #include <memory> |
| 8 |
| 9 #include "base/macros.h" |
| 10 #include "base/strings/string16.h" |
| 11 #include "base/strings/utf_string_conversions.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 #include "ui/views/controls/textfield/textfield.h" |
| 14 |
| 15 namespace payments { |
| 16 |
| 17 class ValidatingTextfieldTest : public testing::Test { |
| 18 public: |
| 19 ValidatingTextfieldTest() {} |
| 20 ~ValidatingTextfieldTest() override {} |
| 21 |
| 22 protected: |
| 23 class ValidationDelegate : public ValidatingTextfield::Delegate { |
| 24 public: |
| 25 ValidationDelegate() {} |
| 26 ~ValidationDelegate() override {} |
| 27 |
| 28 // ValidatingTextfield::Delegate |
| 29 bool ValidateTextfield(views::Textfield* textfield) override { |
| 30 // We really don't like textfields with more than 5 characters in them. |
| 31 return textfield->text().size() <= 5u; |
| 32 } |
| 33 |
| 34 private: |
| 35 DISALLOW_COPY_AND_ASSIGN(ValidationDelegate); |
| 36 }; |
| 37 |
| 38 private: |
| 39 DISALLOW_COPY_AND_ASSIGN(ValidatingTextfieldTest); |
| 40 }; |
| 41 |
| 42 TEST_F(ValidatingTextfieldTest, Validation) { |
| 43 std::unique_ptr<ValidationDelegate> delegate(new ValidationDelegate()); |
| 44 std::unique_ptr<ValidatingTextfield> textfield( |
| 45 new ValidatingTextfield(std::move(delegate))); |
| 46 |
| 47 // Set an invalid string (>5 characters). |
| 48 textfield->SetText(base::ASCIIToUTF16("evilstring")); |
| 49 // This should be called on new contents by the textfield controller. |
| 50 textfield->OnContentsChanged(); |
| 51 |
| 52 // Not marked as invalid. |
| 53 EXPECT_FALSE(textfield->invalid()); |
| 54 |
| 55 // On blur though, there is a first validation. |
| 56 textfield->OnBlur(); |
| 57 EXPECT_TRUE(textfield->invalid()); |
| 58 |
| 59 // On further text adjustements, the validation runs now. Set a valid string |
| 60 // (<=5 characters). |
| 61 textfield->SetText(base::ASCIIToUTF16("good")); |
| 62 textfield->OnContentsChanged(); |
| 63 |
| 64 // No longer invalid. |
| 65 EXPECT_FALSE(textfield->invalid()); |
| 66 } |
| 67 |
| 68 } // namespace payments |
| OLD | NEW |