Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / html / forms / FileInputTypeTest.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 #include "config.h"
6 #include "FileInputType.h"
7
8 #include "core/clipboard/DataObject.h"
9 #include "core/dom/Document.h"
10 #include "core/fileapi/FileList.h"
11 #include "core/html/HTMLInputElement.h"
12 #include "core/page/DragData.h"
13 #include <gtest/gtest.h>
14
15 namespace blink {
16
17 TEST(FileInputTypeTest, createFileList)
18 {
19     Vector<FileChooserFileInfo> files;
20
21     // Natvie file.
22     files.append(FileChooserFileInfo(
23         "/native/path/native-file",
24         "display-name"));
25
26     // Non-native file.
27     KURL url(ParsedURLStringTag(), "filesystem:http://example.com/isolated/hash/non-native-file");
28     FileMetadata metadata;
29     metadata.length = 64;
30     metadata.modificationTime = 24 * 60 * 60 /* sec */;
31     files.append(FileChooserFileInfo(url, metadata));
32
33
34     FileList* list = FileInputType::createFileList(files, false);
35     ASSERT_TRUE(list);
36     ASSERT_EQ(2u, list->length());
37
38     EXPECT_EQ("/native/path/native-file", list->item(0)->path());
39     EXPECT_EQ("display-name", list->item(0)->name());
40     EXPECT_TRUE(list->item(0)->fileSystemURL().isEmpty());
41
42     EXPECT_TRUE(list->item(1)->path().isEmpty());
43     EXPECT_EQ("non-native-file", list->item(1)->name());
44     EXPECT_EQ(url, list->item(1)->fileSystemURL());
45     EXPECT_EQ(64u, list->item(1)->size());
46     EXPECT_EQ(24 * 60 * 60 * 1000 /* ms */, list->item(1)->lastModified());
47 }
48
49 TEST(FileInputTypeTest, ignoreDroppedNonNativeFiles)
50 {
51     const RefPtrWillBeRawPtr<Document> document = Document::create();
52     const RefPtrWillBeRawPtr<HTMLInputElement> input =
53         HTMLInputElement::create(*document, nullptr, /* createdByParser */true);
54     const RefPtrWillBeRawPtr<InputType> fileInput = FileInputType::create(*input);
55
56     const RefPtrWillBeRawPtr<DataObject> nativeFileRawDragData = DataObject::create();
57     const DragData nativeFileDragData(nativeFileRawDragData.get(), IntPoint(), IntPoint(), DragOperationCopy);
58     nativeFileDragData.platformData()->add(File::create("/native/path"));
59     nativeFileDragData.platformData()->setFilesystemId("fileSystemId");
60     fileInput->receiveDroppedFiles(&nativeFileDragData);
61     EXPECT_EQ("fileSystemId", fileInput->droppedFileSystemId());
62     ASSERT_EQ(1u, fileInput->files()->length());
63     EXPECT_EQ(String("/native/path"), fileInput->files()->item(0)->path());
64
65     const RefPtrWillBeRawPtr<DataObject> nonNativeFileRawDragData = DataObject::create();
66     const DragData nonNativeFileDragData(nonNativeFileRawDragData.get(), IntPoint(), IntPoint(), DragOperationCopy);
67     FileMetadata metadata;
68     metadata.length = 1234;
69     const KURL url(ParsedURLStringTag(), "filesystem:http://example.com/isolated/hash/non-native-file");
70     nonNativeFileDragData.platformData()->add(File::createForFileSystemFile(url, metadata, File::IsUserVisible));
71     nonNativeFileDragData.platformData()->setFilesystemId("fileSystemId");
72     fileInput->receiveDroppedFiles(&nonNativeFileDragData);
73     // Dropping non-native files should not change the existing files.
74     EXPECT_EQ("fileSystemId", fileInput->droppedFileSystemId());
75     ASSERT_EQ(1u, fileInput->files()->length());
76     EXPECT_EQ(String("/native/path"), fileInput->files()->item(0)->path());
77 }
78
79 } // namespace blink