File: file_info_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (114 lines) | stat: -rw-r--r-- 3,754 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chromeos/ash/components/file_manager/indexing/file_info.h"

#include <sstream>
#include <string>

#include "base/logging.h"
#include "base/time/time.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
#include "url/url_canon.h"
#include "url/url_util.h"

namespace ash::file_manager {
namespace {

class FileInfoTest : public testing::Test {
 public:
  void SetUp() override {
    scheme_registry_ = std::make_unique<url::ScopedSchemeRegistryForTests>();
    url::AddStandardScheme("chrome", url::SCHEME_WITH_HOST);
  }

 private:
  // Allows registering the "chrome://" scheme, without depending on //content.
  std::unique_ptr<url::ScopedSchemeRegistryForTests> scheme_registry_;
};

TEST_F(FileInfoTest, RemoteId) {
  GURL url("filesystem:chrome://file-manager/external/Downloads-user123/a.txt");
  FileInfo file_info(url, 100, base::Time::Now());

  ASSERT_FALSE(file_info.remote_id.has_value());
  const std::string remote_id("46bd8697");
  file_info.remote_id = remote_id;
  EXPECT_TRUE(file_info.remote_id.has_value());
  EXPECT_EQ(file_info.remote_id.value(), remote_id);
}

TEST_F(FileInfoTest, ToString) {
  GURL url("filesystem:chrome://file-manager/external/Downloads-u123/a.txt");
  base::Time last_modified;
  ASSERT_TRUE(
      base::Time::FromUTCString("19 Jan 2023 12:00 UTC", &last_modified));

  FileInfo file_info(url, 100, last_modified);

  {
    std::stringstream ss;
    ss << file_info;
    EXPECT_EQ(ss.str(),
              "FileInfo(file_url=filesystem:chrome://file-manager/"
              "external/Downloads-u123/a.txt, size=100, last_modified"
              "=2023-01-19 12:00:00.000000 UTC, remote_id=null)");
  }

  file_info.remote_id = "remote-id";
  {
    std::stringstream ss;
    ss << file_info;
    EXPECT_EQ(ss.str(),
              "FileInfo(file_url=filesystem:chrome://file-manager/"
              "external/Downloads-u123/a.txt, size=100, last_modified"
              "=2023-01-19 12:00:00.000000 UTC, remote_id=remote-id)");
  }
}

TEST_F(FileInfoTest, Equality) {
  GURL url_a("filesystem:chrome://file-manager/external/Downloads-u123/a.txt");
  GURL url_b("filesystem:chrome://file-manager/external/Downloads-u123/b.txt");
  base::Time last_modified_a;
  ASSERT_TRUE(
      base::Time::FromUTCString("19 Jan 2023 12:00 UTC", &last_modified_a));
  base::Time last_modified_b;
  ASSERT_TRUE(
      base::Time::FromUTCString("19 Feb 2023 12:00 UTC", &last_modified_b));
  int64_t size_a = 100;
  int64_t size_b = 200;

  const std::string remote_id_a("remote_id_a");
  const std::string remote_id_b("remote_id_b");

  EXPECT_EQ(FileInfo(url_a, size_a, last_modified_a),
            FileInfo(url_a, size_a, last_modified_a));
  EXPECT_NE(FileInfo(url_a, size_a, last_modified_a),
            FileInfo(url_b, size_a, last_modified_a));
  EXPECT_NE(FileInfo(url_a, size_a, last_modified_a),
            FileInfo(url_a, size_b, last_modified_a));
  EXPECT_NE(FileInfo(url_a, size_a, last_modified_a),
            FileInfo(url_a, size_a, last_modified_b));

  FileInfo info_a(url_a, size_a, last_modified_a);
  FileInfo info_b(url_a, size_a, last_modified_a);
  info_a.remote_id = remote_id_a;
  EXPECT_NE(info_a, info_b);
  info_a.remote_id.reset();
  info_b.remote_id = remote_id_b;
  EXPECT_NE(info_a, info_b);
  info_a.remote_id = remote_id_a;
  EXPECT_NE(info_a, info_b);
  info_b.remote_id = remote_id_a;
  EXPECT_EQ(info_a, info_b);
}

TEST_F(FileInfoTest, UrlValidity) {
  GURL url("filesystem:chrome://file-manager/external/Downloads-u123/a.txt");
  EXPECT_TRUE(url.is_valid());
}

}  // namespace
}  // namespace ash::file_manager