Commit 0323878d authored by Kent Tamura's avatar Kent Tamura Committed by Commit Bot

File: Add more timestamp tests

Add timestamp tests for blob-backing files and filesystem files.
This CL makes File::LastModifiedTime public because many tests access
it.

This CL has no behavior changes.

Bug: 1027959
Change-Id: I86ff3788f44edf830b1372bc3b17c3a951a77803
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1948666
Commit-Queue: Kent Tamura <tkent@chromium.org>
Reviewed-by: default avatarJoshua Bell <jsbell@chromium.org>
Cr-Commit-Position: refs/heads/master@{#721388}
parent ba1aa4f9
......@@ -26,7 +26,6 @@
#ifndef THIRD_PARTY_BLINK_RENDERER_CORE_FILEAPI_FILE_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_FILEAPI_FILE_H_
#include "base/gtest_prod_util.h"
#include "base/memory/scoped_refptr.h"
#include "base/optional.h"
#include "base/time/time.h"
......@@ -202,6 +201,10 @@ class CORE_EXPORT File final : public Blob {
// http://www.w3.org/TR/FileAPI/#dfn-lastModifiedDate
ScriptValue lastModifiedDate(ScriptState* script_state) const;
// Returns File's last modified time.
// If the modification time isn't known, the current time is returned.
base::Time LastModifiedTime() const;
UserVisibility GetUserVisibility() const { return user_visibility_; }
// Returns the relative path of this file in the context of a directory
......@@ -228,10 +231,6 @@ class CORE_EXPORT File final : public Blob {
private:
void InvalidateSnapshotMetadata() { snapshot_size_.reset(); }
// Returns File's last modified time.
// If the modification time isn't known, the current time is returned.
base::Time LastModifiedTime() const;
#if DCHECK_IS_ON()
// Instances backed by a file must have an empty file system URL.
bool HasValidFileSystemURL() const {
......@@ -256,14 +255,6 @@ class CORE_EXPORT File final : public Blob {
const base::Optional<base::Time> snapshot_modification_time_;
String relative_path_;
FRIEND_TEST_ALL_PREFIXES(FileTest, NativeFileWithoutTimestamp);
FRIEND_TEST_ALL_PREFIXES(FileTest, NativeFileWithUnixEpochTimestamp);
FRIEND_TEST_ALL_PREFIXES(FileTest, NativeFileWithApocalypseTimestamp);
FRIEND_TEST_ALL_PREFIXES(V8ScriptValueSerializerTest,
DecodeFileV4WithSnapshot);
FRIEND_TEST_ALL_PREFIXES(V8ScriptValueSerializerTest,
DecodeFileV8WithSnapshot);
};
template <>
......
......@@ -53,6 +53,15 @@ class MockFileUtilitiesHost : public mojom::blink::FileUtilitiesHost {
base::File::Info file_info_;
};
void ExpectTimestampIsNow(const File& file) {
const base::Time now = base::Time::Now();
const base::TimeDelta delta_now = now - base::Time::UnixEpoch();
// Because lastModified() applies floor() internally, we should compare
// integral millisecond values.
EXPECT_GE(file.lastModified(), delta_now.InMilliseconds());
EXPECT_GE(file.LastModifiedTime(), now);
}
} // namespace
TEST(FileTest, NativeFileWithoutTimestamp) {
......@@ -65,13 +74,7 @@ TEST(FileTest, NativeFileWithoutTimestamp) {
EXPECT_TRUE(file->HasBackingFile());
EXPECT_EQ("/native/path", file->GetPath());
EXPECT_TRUE(file->FileSystemURL().IsEmpty());
const base::Time now = base::Time::Now();
const base::TimeDelta delta_now = now - base::Time::UnixEpoch();
// Because lastModified() applies floor() internally, we should compare
// integral millisecond values.
EXPECT_GE(file->lastModified(), delta_now.InMilliseconds());
EXPECT_GE(file->LastModifiedTime(), now);
ExpectTimestampIsNow(*file);
}
TEST(FileTest, NativeFileWithUnixEpochTimestamp) {
......@@ -100,7 +103,27 @@ TEST(FileTest, NativeFileWithApocalypseTimestamp) {
EXPECT_EQ(base::Time::Max(), file->LastModifiedTime());
}
TEST(FileTest, blobBackingFile) {
TEST(FileTest, BlobBackingFileWithoutTimestamp) {
auto* const file = MakeGarbageCollected<File>("name", base::nullopt,
BlobDataHandle::Create());
EXPECT_FALSE(file->HasBackingFile());
EXPECT_TRUE(file->GetPath().IsEmpty());
EXPECT_TRUE(file->FileSystemURL().IsEmpty());
ExpectTimestampIsNow(*file);
}
TEST(FileTest, BlobBackingFileWithWindowsEpochTimestamp) {
auto* const file = MakeGarbageCollected<File>("name", base::Time(),
BlobDataHandle::Create());
EXPECT_FALSE(file->HasBackingFile());
EXPECT_TRUE(file->GetPath().IsEmpty());
EXPECT_TRUE(file->FileSystemURL().IsEmpty());
EXPECT_EQ((base::Time() - base::Time::UnixEpoch()).InMilliseconds(),
file->lastModified());
EXPECT_EQ(base::Time(), file->LastModifiedTime());
}
TEST(FileTest, BlobBackingFileWithUnixEpochTimestamp) {
const scoped_refptr<BlobDataHandle> blob_data_handle =
BlobDataHandle::Create();
auto* const file = MakeGarbageCollected<File>("name", base::Time::UnixEpoch(),
......@@ -108,6 +131,20 @@ TEST(FileTest, blobBackingFile) {
EXPECT_FALSE(file->HasBackingFile());
EXPECT_TRUE(file->GetPath().IsEmpty());
EXPECT_TRUE(file->FileSystemURL().IsEmpty());
EXPECT_EQ(INT64_C(0), file->lastModified());
EXPECT_EQ(base::Time::UnixEpoch(), file->LastModifiedTime());
}
TEST(FileTest, BlobBackingFileWithApocalypseTimestamp) {
constexpr base::Time kMaxTime = base::Time::Max();
auto* const file =
MakeGarbageCollected<File>("name", kMaxTime, BlobDataHandle::Create());
EXPECT_FALSE(file->HasBackingFile());
EXPECT_TRUE(file->GetPath().IsEmpty());
EXPECT_TRUE(file->FileSystemURL().IsEmpty());
EXPECT_EQ((kMaxTime - base::Time::UnixEpoch()).InMilliseconds(),
file->lastModified());
EXPECT_EQ(kMaxTime, file->LastModifiedTime());
}
TEST(FileTest, fileSystemFileWithNativeSnapshot) {
......@@ -131,6 +168,54 @@ TEST(FileTest, fileSystemFileWithNativeSnapshotAndSize) {
EXPECT_TRUE(file->FileSystemURL().IsEmpty());
}
TEST(FileTest, FileSystemFileWithWindowsEpochTimestamp) {
FileMetadata metadata;
metadata.length = INT64_C(1025);
metadata.modification_time = base::Time();
metadata.platform_path = "/native/snapshot";
File* const file =
File::CreateForFileSystemFile("name", metadata, File::kIsUserVisible);
EXPECT_TRUE(file->HasBackingFile());
EXPECT_EQ("/native/snapshot", file->GetPath());
EXPECT_TRUE(file->FileSystemURL().IsEmpty());
EXPECT_EQ(UINT64_C(1025), file->size());
EXPECT_EQ((base::Time() - base::Time::UnixEpoch()).InMilliseconds(),
file->lastModified());
EXPECT_EQ(base::Time(), file->LastModifiedTime());
}
TEST(FileTest, FileSystemFileWithUnixEpochTimestamp) {
FileMetadata metadata;
metadata.length = INT64_C(1025);
metadata.modification_time = base::Time::UnixEpoch();
metadata.platform_path = "/native/snapshot";
File* const file =
File::CreateForFileSystemFile("name", metadata, File::kIsUserVisible);
EXPECT_TRUE(file->HasBackingFile());
EXPECT_EQ("/native/snapshot", file->GetPath());
EXPECT_TRUE(file->FileSystemURL().IsEmpty());
EXPECT_EQ(UINT64_C(1025), file->size());
EXPECT_EQ(INT64_C(0), file->lastModified());
EXPECT_EQ(base::Time::UnixEpoch(), file->LastModifiedTime());
}
TEST(FileTest, FileSystemFileWithApocalypseTimestamp) {
constexpr base::Time kMaxTime = base::Time::Max();
FileMetadata metadata;
metadata.length = INT64_C(1025);
metadata.modification_time = kMaxTime;
metadata.platform_path = "/native/snapshot";
File* const file =
File::CreateForFileSystemFile("name", metadata, File::kIsUserVisible);
EXPECT_TRUE(file->HasBackingFile());
EXPECT_EQ("/native/snapshot", file->GetPath());
EXPECT_TRUE(file->FileSystemURL().IsEmpty());
EXPECT_EQ(UINT64_C(1025), file->size());
EXPECT_EQ((kMaxTime - base::Time::UnixEpoch()).InMilliseconds(),
file->lastModified());
EXPECT_EQ(kMaxTime, file->LastModifiedTime());
}
TEST(FileTest, fileSystemFileWithoutNativeSnapshot) {
KURL url("filesystem:http://example.com/isolated/hash/non-native-file");
FileMetadata metadata;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment