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