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 |
| 14 namespace payments { |
| 15 |
| 16 class ValidatingTextfieldTest : public testing::Test, |
| 17 public ValidatingTextfield::Delegate { |
| 18 public: |
| 19 ValidatingTextfieldTest() {} |
| 20 ~ValidatingTextfieldTest() override {} |
| 21 |
| 22 // ValidatingTextfield::Delegate |
| 23 bool ValidateTextfield(ValidatingTextfield* textfield) override { |
| 24 // We really don't like textfields with more than 5 characters in them. |
| 25 return textfield->text().size() <= 5u; |
| 26 } |
| 27 |
| 28 private: |
| 29 DISALLOW_COPY_AND_ASSIGN(ValidatingTextfieldTest); |
| 30 }; |
| 31 |
| 32 TEST_F(ValidatingTextfieldTest, Validation) { |
| 33 std::unique_ptr<ValidatingTextfield> textfield(new ValidatingTextfield(this)); |
| 34 |
| 35 // Set an invalid string (>5 characters). |
| 36 textfield->SetText(base::ASCIIToUTF16("evilstring")); |
| 37 // This should be called on new contents by the textfield controller. |
| 38 textfield->OnContentsChanged(); |
| 39 |
| 40 // Not marked as invalid. |
| 41 EXPECT_FALSE(textfield->invalid()); |
| 42 |
| 43 // On blur though, there is a first validation. |
| 44 textfield->OnBlur(); |
| 45 EXPECT_TRUE(textfield->invalid()); |
| 46 |
| 47 // On further text adjustements, the validation runs now. Set a valid string |
| 48 // (<=5 characters). |
| 49 textfield->SetText(base::ASCIIToUTF16("good")); |
| 50 textfield->OnContentsChanged(); |
| 51 |
| 52 // No longer invalid. |
| 53 EXPECT_FALSE(textfield->invalid()); |
| 54 } |
| 55 |
| 56 } // namespace payments |
OLD | NEW |