Index: third_party/WebKit/Source/core/html/forms/PasswordInputTypeTest.cpp |
diff --git a/third_party/WebKit/Source/core/html/forms/PasswordInputTypeTest.cpp b/third_party/WebKit/Source/core/html/forms/PasswordInputTypeTest.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..457a06931e3e029b02ca4bd8d0de384c475bb75f |
--- /dev/null |
+++ b/third_party/WebKit/Source/core/html/forms/PasswordInputTypeTest.cpp |
@@ -0,0 +1,152 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "core/html/forms/PasswordInputType.h" |
+ |
+#include "core/dom/IntersectionObserverController.h" |
+#include "core/frame/FrameView.h" |
+#include "core/html/HTMLInputElement.h" |
+#include "core/testing/DummyPageHolder.h" |
+#include "mojo/public/cpp/bindings/interface_request.h" |
+#include "mojo/public/cpp/bindings/strong_binding.h" |
+#include "platform/testing/UnitTestHelpers.h" |
+#include "public/platform/InterfaceProvider.h" |
+#include "public/platform/modules/sensitive_input_visibility/sensitive_input_visibility_service.mojom-blink.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace blink { |
+ |
+class MockInterfaceProvider : public blink::InterfaceProvider { |
+ |
+public: |
+ MockInterfaceProvider() |
+ : m_passwordFieldVisibleCalled(false) |
+ { |
+ } |
+ virtual ~MockInterfaceProvider() {} |
+ |
+ void getInterface(const char* name, mojo::ScopedMessagePipeHandle handle) override |
+ { |
+ m_mockSensitiveInputVisibilityService.reset(new MockSensitiveInputVisibilityService(this, mojo::MakeRequest<mojom::blink::SensitiveInputVisibilityService>(std::move(handle)))); |
+ } |
+ |
+ void setPasswordFieldVisibleCalled() { m_passwordFieldVisibleCalled = true; } |
+ |
+ bool passwordFieldVisibleCalled() const { return m_passwordFieldVisibleCalled; } |
+ |
+private: |
+ class MockSensitiveInputVisibilityService : public mojom::blink::SensitiveInputVisibilityService { |
+ public: |
+ MockSensitiveInputVisibilityService(MockInterfaceProvider* registry, mojom::blink::SensitiveInputVisibilityServiceRequest request) |
+ : m_binding(this, std::move(request)) |
+ , m_registry(registry) |
+ { |
+ } |
+ ~MockSensitiveInputVisibilityService() override {} |
+ private: |
+ // mojom::SensitiveInputVisibilityService |
+ void PasswordFieldVisible() override { m_registry->setPasswordFieldVisibleCalled(); } |
+ |
+ mojo::Binding<SensitiveInputVisibilityService> m_binding; |
+ MockInterfaceProvider* const m_registry; |
+ }; |
+ |
+ std::unique_ptr<MockSensitiveInputVisibilityService> m_mockSensitiveInputVisibilityService; |
+ bool m_passwordFieldVisibleCalled; |
+}; |
+ |
+// Tests that a Mojo message is sent when a visible password field |
+// appears on the page. |
+TEST(PasswordInputTypeTest, PasswordVisibilityEvent) |
+{ |
+ MockInterfaceProvider interfaceProvider; |
+ std::unique_ptr<DummyPageHolder> pageHolder = DummyPageHolder::create(IntSize(2000, 2000), nullptr, nullptr, nullptr, &interfaceProvider); |
+ pageHolder->document().body()->setInnerHTML("<input type='password'>", ASSERT_NO_EXCEPTION); |
+ pageHolder->document().view()->updateAllLifecyclePhases(); |
+ |
+ // Force the intersection observer callback to fire synchronously and |
+ // wait for the Mojo message to go through. |
+ ASSERT_TRUE(pageHolder->document().intersectionObserverController()); |
+ pageHolder->document().intersectionObserverController()->deliverIntersectionObservations(); |
+ blink::testing::runPendingTasks(); |
+ |
+ EXPECT_TRUE(interfaceProvider.passwordFieldVisibleCalled()); |
+} |
+ |
+// Tests that a Mojo message is sent when a previously invisible password field becomes visible. |
+TEST(PasswordInputTypeTest, InvisiblePasswordFieldBecomesVisible) |
+{ |
+ MockInterfaceProvider interfaceProvider; |
+ std::unique_ptr<DummyPageHolder> pageHolder = DummyPageHolder::create(IntSize(2000, 2000), nullptr, nullptr, nullptr, &interfaceProvider); |
+ pageHolder->document().body()->setInnerHTML("<input type='password' style='display:none;'>", ASSERT_NO_EXCEPTION); |
+ pageHolder->document().view()->updateAllLifecyclePhases(); |
+ |
+ // Force the intersection observer callback to fire synchronously and |
+ // wait for the Mojo message to go through. |
+ ASSERT_TRUE(pageHolder->document().intersectionObserverController()); |
+ pageHolder->document().intersectionObserverController()->deliverIntersectionObservations(); |
+ blink::testing::runPendingTasks(); |
+ |
+ // The message should not be sent for a hidden password field. |
+ EXPECT_FALSE(interfaceProvider.passwordFieldVisibleCalled()); |
+ |
+ // Now make the input visible. |
+ HTMLInputElement* input = toHTMLInputElement(pageHolder->document().body()->firstChild()); |
+ input->setAttribute("style", "", ASSERT_NO_EXCEPTION); |
+ pageHolder->document().view()->updateAllLifecyclePhases(); |
+ pageHolder->document().intersectionObserverController()->deliverIntersectionObservations(); |
+ blink::testing::runPendingTasks(); |
+ EXPECT_TRUE(interfaceProvider.passwordFieldVisibleCalled()); |
+} |
+ |
+// Tests that a Mojo message is sent when a previously non-password field becomes a password. |
+TEST(PasswordInputTypeTest, NonPasswordFieldBecomesPassword) |
+{ |
+ MockInterfaceProvider interfaceProvider; |
+ std::unique_ptr<DummyPageHolder> pageHolder = DummyPageHolder::create(IntSize(2000, 2000), nullptr, nullptr, nullptr, &interfaceProvider); |
+ pageHolder->document().body()->setInnerHTML("<input type='text'>", ASSERT_NO_EXCEPTION); |
+ pageHolder->document().view()->updateAllLifecyclePhases(); |
+ |
+ // The message should not be sent for a non-password field. |
+ ASSERT_FALSE(pageHolder->document().intersectionObserverController()); |
+ blink::testing::runPendingTasks(); |
+ EXPECT_FALSE(interfaceProvider.passwordFieldVisibleCalled()); |
+ |
+ // Now make the input a password field. |
+ HTMLInputElement* input = toHTMLInputElement(pageHolder->document().body()->firstChild()); |
+ input->setType("password"); |
+ pageHolder->document().view()->updateAllLifecyclePhases(); |
+ pageHolder->document().intersectionObserverController()->deliverIntersectionObservations(); |
+ blink::testing::runPendingTasks(); |
+ EXPECT_TRUE(interfaceProvider.passwordFieldVisibleCalled()); |
+} |
+ |
+// Tests that a Mojo message is *not* sent when a previously invisible password field becomes a visible non-password field. |
+TEST(PasswordInputTypeTest, InvisiblePasswordFieldBecomesVisibleNonPasswordField) |
+{ |
+ MockInterfaceProvider interfaceProvider; |
+ std::unique_ptr<DummyPageHolder> pageHolder = DummyPageHolder::create(IntSize(2000, 2000), nullptr, nullptr, nullptr, &interfaceProvider); |
+ pageHolder->document().body()->setInnerHTML("<input type='password' style='display:none;'>", ASSERT_NO_EXCEPTION); |
+ pageHolder->document().view()->updateAllLifecyclePhases(); |
+ |
+ // Force the intersection observer callback to fire synchronously and |
+ // wait for the Mojo message to go through. |
+ ASSERT_TRUE(pageHolder->document().intersectionObserverController()); |
+ pageHolder->document().intersectionObserverController()->deliverIntersectionObservations(); |
+ blink::testing::runPendingTasks(); |
+ |
+ // The message should not be sent for a hidden password field. |
+ EXPECT_FALSE(interfaceProvider.passwordFieldVisibleCalled()); |
+ |
+ // Now make the input a visible non-password field. |
+ HTMLInputElement* input = toHTMLInputElement(pageHolder->document().body()->firstChild()); |
+ input->setType("text"); |
+ input->setAttribute("style", "", ASSERT_NO_EXCEPTION); |
+ pageHolder->document().view()->updateAllLifecyclePhases(); |
+ pageHolder->document().intersectionObserverController()->deliverIntersectionObservations(); |
+ blink::testing::runPendingTasks(); |
+ EXPECT_TRUE(interfaceProvider.passwordFieldVisibleCalled()); |
+} |
+ |
+} // namespace blink |