Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / platform / network / FormDataTest.cpp
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.
4
5 #ifndef FormDataTest_h
6 #define FormDataTest_h
7
8 #include "config.h"
9 #include "platform/network/FormData.h"
10
11 #include <gtest/gtest.h>
12
13 namespace blink {
14
15 namespace {
16
17 class FormDataTest : public ::testing::Test {
18 public:
19     void checkDeepCopied(const String& a, const String& b)
20     {
21         EXPECT_EQ(a, b);
22         if (b.impl())
23             EXPECT_NE(a.impl(), b.impl());
24     }
25
26     void checkDeepCopied(const KURL& a, const KURL& b)
27     {
28         EXPECT_EQ(a, b);
29         checkDeepCopied(a.string(), b.string());
30         if (a.innerURL() && b.innerURL())
31             checkDeepCopied(*a.innerURL(), *b.innerURL());
32     }
33
34     void checkDeepCopied(const FormDataElement& a, const FormDataElement& b)
35     {
36         EXPECT_EQ(a, 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);
40     }
41 };
42
43 TEST_F(FormDataTest, DeepCopy)
44 {
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);
50
51     RefPtr<FormData> copy = original->deepCopy();
52
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());
57
58     Vector<char> fooVector;
59     fooVector.append("Foo", 3);
60
61     EXPECT_EQ(FormDataElement::data, copyElements[0].m_type);
62     EXPECT_EQ(fooVector, copyElements[0].m_data);
63
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);
69
70     EXPECT_EQ(FormDataElement::encodedBlob, copyElements[2].m_type);
71     EXPECT_EQ(String("originalUUID"), copyElements[2].m_blobUUID);
72
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);
78
79     // Check that contents are copied (compare the copy with the original).
80     EXPECT_EQ(*original, *copy);
81
82     // Check pointers are different, i.e. deep-copied.
83     ASSERT_NE(original.get(), copy.get());
84
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());
89         }
90
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());
94         }
95
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());
99         }
100
101         EXPECT_EQ(nullptr, copyElements[i].m_fileSystemURL.innerURL());
102
103         // m_optionalBlobDataHandle is not checked, because BlobDataHandle is ThreadSafeRefCounted.
104     }
105 }
106
107 } // namespace
108
109 } // namespace blink
110
111 #endif