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

Unified Diff: third_party/WebKit/Source/modules/credentialmanager/PasswordCredentialTest.cpp

Issue 1828213002: CREDENTIAL: Implement the 'PasswordCredential(HTMLFormElement)' constructor. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: EXPORT2 Created 4 years, 9 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 side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/modules/credentialmanager/PasswordCredentialTest.cpp
diff --git a/third_party/WebKit/Source/modules/credentialmanager/PasswordCredentialTest.cpp b/third_party/WebKit/Source/modules/credentialmanager/PasswordCredentialTest.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..d5af14a3847421ad8819e7ede3e45399469c0b59
--- /dev/null
+++ b/third_party/WebKit/Source/modules/credentialmanager/PasswordCredentialTest.cpp
@@ -0,0 +1,108 @@
+// 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 "modules/credentialmanager/PasswordCredential.h"
+
+#include "bindings/core/v8/ExceptionState.h"
+#include "bindings/core/v8/ExceptionStatePlaceholder.h"
+#include "core/dom/ExceptionCode.h"
+#include "core/frame/FrameView.h"
+#include "core/html/FormData.h"
+#include "core/html/HTMLDocument.h"
+#include "core/html/HTMLFormElement.h"
+#include "core/html/forms/FormController.h"
+#include "core/testing/DummyPageHolder.h"
+#include "testing/gtest/include/gtest/gtest.h"
+#include "wtf/text/StringBuilder.h"
+
+namespace blink {
+
+class PasswordCredentialTest : public ::testing::Test {
+protected:
+ void SetUp() override
+ {
+ m_dummyPageHolder = DummyPageHolder::create();
+ m_document = toHTMLDocument(&m_dummyPageHolder->document());
+ }
+
+ HTMLDocument& document() const { return *m_document; }
+
+ HTMLFormElement* populateForm(const char* html)
+ {
+ StringBuilder b;
+ b.appendLiteral("<!DOCTYPE html><html><body><form id='theForm'>");
+ b.append(html);
+ b.appendLiteral("</form></body></html>");
+ document().documentElement()->setInnerHTML(b.toString(), ASSERT_NO_EXCEPTION);
+ document().view()->updateAllLifecyclePhases();
+ HTMLFormElement* form = toHTMLFormElement(document().getElementById("theForm"));
+ EXPECT_NE(nullptr, form);
+ return form;
+ }
+
+private:
+ OwnPtr<DummyPageHolder> m_dummyPageHolder;
+ RefPtrWillBePersistent<HTMLDocument> m_document;
+};
+
+TEST_F(PasswordCredentialTest, CreateFromForm)
+{
+ HTMLFormElement* form = populateForm(
+ "<input type='text' name='theId' value='musterman' autocomplete='username'>"
+ "<input type='text' name='thePassword' value='sekrit' autocomplete='current-password'>"
+ "<input type='text' name='theIcon' value='https://example.com/photo' autocomplete='photo'>"
+ "<input type='text' name='theExtraField' value='extra'>"
+ "<input type='text' name='theName' value='friendly name' autocomplete='name'>");
+ PasswordCredential* credential = PasswordCredential::create(form, ASSERT_NO_EXCEPTION);
+ ASSERT_NE(nullptr, credential);
+ EXPECT_EQ("theId", credential->idName());
+ EXPECT_EQ("thePassword", credential->passwordName());
+
+ EXPECT_EQ("musterman", credential->id());
+ EXPECT_EQ("sekrit", credential->password());
+ EXPECT_EQ(KURL(ParsedURLString, "https://example.com/photo"), credential->iconURL());
+ EXPECT_EQ("friendly name", credential->name());
+ EXPECT_EQ("password", credential->type());
+
+ FormDataOrURLSearchParams additionalData;
+ credential->additionalData(additionalData);
+ ASSERT_TRUE(additionalData.isFormData());
+ EXPECT_TRUE(additionalData.getAsFormData()->has("theId"));
+ EXPECT_TRUE(additionalData.getAsFormData()->has("thePassword"));
+ EXPECT_TRUE(additionalData.getAsFormData()->has("theIcon"));
+ EXPECT_TRUE(additionalData.getAsFormData()->has("theName"));
+ EXPECT_TRUE(additionalData.getAsFormData()->has("theExtraField"));
+}
+
+TEST_F(PasswordCredentialTest, CreateFromFormNoPassword)
+{
+ HTMLFormElement* form = populateForm(
+ "<input type='text' name='theId' value='musterman' autocomplete='username'>"
+ "<!-- No password field -->"
+ "<input type='text' name='theIcon' value='https://example.com/photo' autocomplete='photo'>"
+ "<input type='text' name='theName' value='friendly name' autocomplete='name'>");
+ TrackExceptionState exceptionState;
+ PasswordCredential* credential = PasswordCredential::create(form, exceptionState);
+ EXPECT_EQ(nullptr, credential);
+ EXPECT_TRUE(exceptionState.hadException());
+ EXPECT_EQ(V8TypeError, exceptionState.code());
+ EXPECT_EQ("'password' must not be empty.", exceptionState.message());
+}
+
+TEST_F(PasswordCredentialTest, CreateFromFormNoId)
+{
+ HTMLFormElement* form = populateForm(
+ "<!-- No username field. -->"
+ "<input type='text' name='thePassword' value='sekrit' autocomplete='current-password'>"
+ "<input type='text' name='theIcon' value='https://example.com/photo' autocomplete='photo'>"
+ "<input type='text' name='theName' value='friendly name' autocomplete='name'>");
+ TrackExceptionState exceptionState;
+ PasswordCredential* credential = PasswordCredential::create(form, exceptionState);
+ EXPECT_EQ(nullptr, credential);
+ EXPECT_TRUE(exceptionState.hadException());
+ EXPECT_EQ(V8TypeError, exceptionState.code());
+ EXPECT_EQ("'id' must not be empty.", exceptionState.message());
+}
+
+} // namespace blink

Powered by Google App Engine
This is Rietveld 408576698