File: drive_integration_service_browser_test_base.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (122 lines) | stat: -rw-r--r-- 4,790 bytes parent folder | download | duplicates (3)
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
115
116
117
118
119
120
121
122
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ash/drive/drive_integration_service_browser_test_base.h"

#include "base/files/file_util.h"
#include "chrome/browser/ash/drive/drive_integration_service.h"
#include "chrome/browser/ash/drive/drive_integration_service_factory.h"
#include "chrome/browser/ash/drive/drivefs_test_support.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/profile.h"

namespace drive {

DriveIntegrationServiceBrowserTestBase::
    DriveIntegrationServiceBrowserTestBase() = default;

DriveIntegrationServiceBrowserTestBase::
    ~DriveIntegrationServiceBrowserTestBase() = default;

drivefs::FakeDriveFs*
DriveIntegrationServiceBrowserTestBase::GetFakeDriveFsForProfile(
    Profile* profile) {
  return &fake_drivefs_helpers_[profile]->fake_drivefs();
}

drive::DriveIntegrationService*
DriveIntegrationServiceBrowserTestBase::CreateDriveIntegrationService(
    Profile* profile) {
  base::ScopedAllowBlockingForTesting allow_blocking;
  base::FilePath mount_path = profile->GetPath().Append("drivefs");
  fake_drivefs_helpers_[profile] =
      std::make_unique<drive::FakeDriveFsHelper>(profile, mount_path);
  auto* integration_service = new drive::DriveIntegrationService(
      g_browser_process->local_state(), profile, std::string(), mount_path,
      fake_drivefs_helpers_[profile]->CreateFakeDriveFsListenerFactory());
  return integration_service;
}

void DriveIntegrationServiceBrowserTestBase::InitTestFileMountRoot(
    Profile* profile) {
  base::ScopedAllowBlockingForTesting allow_blocking;
  EXPECT_EQ(test_file_mount_root_mappings_.end(),
            test_file_mount_root_mappings_.find(profile));

  const base::FilePath drive_fs_mount_path =
      DriveIntegrationServiceFactory::FindForProfile(profile)
          ->GetMountPointPath();
  base::ScopedTempDir temp_dir;
  EXPECT_TRUE(temp_dir.CreateUniqueTempDirUnderPath(drive_fs_mount_path));
  test_file_mount_root_mappings_.emplace(profile, std::move(temp_dir));
}

void DriveIntegrationServiceBrowserTestBase::AddDriveFileWithRelativePath(
    Profile* profile,
    const std::string& drive_file_id,
    const base::FilePath& directory_path,
    base::FilePath* new_file_relative_path,
    base::FilePath* new_file_absolute_path) {
  base::ScopedAllowBlockingForTesting allow_blocking;

  // `directory_path` should be a relative path.
  EXPECT_FALSE(directory_path.IsAbsolute());

  auto iter = test_file_mount_root_mappings_.find(profile);
  if (test_file_mount_root_mappings_.end() == iter) {
    ADD_FAILURE() << "the test file mount root has not been initialized yet. "
                     "Have you called InitTestFileMountRoot()?";
  }

  const std::vector<base::FilePath::StringType> components =
      directory_path.GetComponents();
  base::FilePath absolute_directory_path = iter->second.GetPath();

  // Create the missing directories.
  for (const auto& directory : components) {
    absolute_directory_path = absolute_directory_path.Append(directory);
    if (!base::DirectoryExists(absolute_directory_path))
      base::CreateDirectory(absolute_directory_path);
  }

  base::FilePath absolute_file_path;
  base::CreateTemporaryFileInDir(absolute_directory_path, &absolute_file_path);

  // Calculate the relative path from the drive file system root.
  const base::FilePath fs_mount_path =
      DriveIntegrationServiceFactory::FindForProfile(profile)
          ->GetMountPointPath();
  base::FilePath relative_to_drive_fs_mount("/");
  fs_mount_path.AppendRelativePath(absolute_file_path,
                                   &relative_to_drive_fs_mount);

  // Update the drive file metadata.
  drivefs::FakeMetadata metadata;
  metadata.path = relative_to_drive_fs_mount;
  metadata.mime_type = "text/plain";
  metadata.doc_id = drive_file_id;
  GetFakeDriveFsForProfile(profile)->SetMetadata(std::move(metadata));

  // Update the relative/absolute paths to the generated file.
  if (new_file_relative_path)
    *new_file_relative_path = relative_to_drive_fs_mount;
  if (new_file_absolute_path)
    *new_file_absolute_path = absolute_file_path;
}

bool DriveIntegrationServiceBrowserTestBase::SetUpUserDataDirectory() {
  return drive::SetUpUserDataDirectoryForDriveFsTest();
}

void DriveIntegrationServiceBrowserTestBase::
    SetUpInProcessBrowserTestFixture() {
  create_drive_integration_service_ = base::BindRepeating(
      &DriveIntegrationServiceBrowserTestBase::CreateDriveIntegrationService,
      base::Unretained(this));
  service_factory_for_test_ = std::make_unique<
      drive::DriveIntegrationServiceFactory::ScopedFactoryForTest>(
      &create_drive_integration_service_);
}

}  // namespace drive