1 // Copyright 2014 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.
9 #include "platform/network/FormData.h"
11 #include <gtest/gtest.h>
17 class FormDataTest : public ::testing::Test {
19 void checkDeepCopied(const String& a, const String& b)
23 EXPECT_NE(a.impl(), b.impl());
26 void checkDeepCopied(const KURL& a, const KURL& b)
29 checkDeepCopied(a.string(), b.string());
30 if (a.innerURL() && b.innerURL())
31 checkDeepCopied(*a.innerURL(), *b.innerURL());
34 void checkDeepCopied(const FormDataElement& a, const FormDataElement& b)
37 checkDeepCopied(a.m_filename, b.m_filename);
38 checkDeepCopied(a.m_blobUUID, b.m_blobUUID);
39 checkDeepCopied(a.m_fileSystemURL, b.m_fileSystemURL);
43 TEST_F(FormDataTest, DeepCopy)
45 RefPtr<FormData> original(FormData::create());
46 original->appendData("Foo", 3);
47 original->appendFileRange("example.txt", 12345, 56789, 9999.0);
48 original->appendBlob("originalUUID", nullptr);
49 original->appendFileSystemURLRange(KURL(KURL(), "ws://localhost/"), 23456, 34567, 1111.0);
51 RefPtr<FormData> copy = original->deepCopy();
53 // Check that contents are copied (compare the copy with expected values).
54 const Vector<FormDataElement>& originalElements = original->elements();
55 const Vector<FormDataElement>& copyElements = copy->elements();
56 ASSERT_EQ(4ul, copyElements.size());
58 Vector<char> fooVector;
59 fooVector.append("Foo", 3);
61 EXPECT_EQ(FormDataElement::data, copyElements[0].m_type);
62 EXPECT_EQ(fooVector, copyElements[0].m_data);
64 EXPECT_EQ(FormDataElement::encodedFile, copyElements[1].m_type);
65 EXPECT_EQ(String("example.txt"), copyElements[1].m_filename);
66 EXPECT_EQ(12345ll, copyElements[1].m_fileStart);
67 EXPECT_EQ(56789ll, copyElements[1].m_fileLength);
68 EXPECT_EQ(9999.0, copyElements[1].m_expectedFileModificationTime);
70 EXPECT_EQ(FormDataElement::encodedBlob, copyElements[2].m_type);
71 EXPECT_EQ(String("originalUUID"), copyElements[2].m_blobUUID);
73 EXPECT_EQ(FormDataElement::encodedFileSystemURL, copyElements[3].m_type);
74 EXPECT_EQ(KURL(KURL(), String("ws://localhost/")), copyElements[3].m_fileSystemURL);
75 EXPECT_EQ(23456ll, copyElements[3].m_fileStart);
76 EXPECT_EQ(34567ll, copyElements[3].m_fileLength);
77 EXPECT_EQ(1111.0, copyElements[3].m_expectedFileModificationTime);
79 // Check that contents are copied (compare the copy with the original).
80 EXPECT_EQ(*original, *copy);
82 // Check pointers are different, i.e. deep-copied.
83 ASSERT_NE(original.get(), copy.get());
85 for (size_t i = 0; i < 4; ++i) {
86 if (copyElements[i].m_filename.impl()) {
87 EXPECT_NE(originalElements[i].m_filename.impl(), copyElements[i].m_filename.impl());
88 EXPECT_TRUE(copyElements[i].m_filename.impl()->hasOneRef());
91 if (copyElements[i].m_blobUUID.impl()) {
92 EXPECT_NE(originalElements[i].m_blobUUID.impl(), copyElements[i].m_blobUUID.impl());
93 EXPECT_TRUE(copyElements[i].m_blobUUID.impl()->hasOneRef());
96 if (copyElements[i].m_fileSystemURL.string().impl()) {
97 EXPECT_NE(originalElements[i].m_fileSystemURL.string().impl(), copyElements[i].m_fileSystemURL.string().impl());
98 EXPECT_TRUE(copyElements[i].m_fileSystemURL.string().impl()->hasOneRef());
101 EXPECT_EQ(nullptr, copyElements[i].m_fileSystemURL.innerURL());
103 // m_optionalBlobDataHandle is not checked, because BlobDataHandle is ThreadSafeRefCounted.