File: file_suggest_keyed_service_unittest.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 (400 lines) | stat: -rw-r--r-- 18,101 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
// 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 <memory>

#include "ash/constants/ash_features.h"
#include "ash/constants/ash_pref_names.h"
#include "base/files/scoped_temp_dir.h"
#include "base/memory/raw_ptr.h"
#include "base/scoped_observation.h"
#include "base/test/metrics/histogram_tester.h"
#include "chrome/browser/ash/drive/drive_integration_service.h"
#include "chrome/browser/ash/drive/drive_integration_service_factory.h"
#include "chrome/browser/ash/file_suggest/file_suggest_keyed_service_factory.h"
#include "chrome/browser/ash/file_suggest/file_suggest_test_util.h"
#include "chrome/browser/ash/file_suggest/file_suggest_util.h"
#include "chrome/browser/ash/file_suggest/mock_file_suggest_keyed_service.h"
#include "chrome/browser/ash/file_suggest/mock_file_suggest_keyed_service_observer.h"
#include "chrome/browser/global_features.h"
#include "chrome/browser/ui/ash/holding_space/scoped_test_mount_point.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "components/prefs/pref_service.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace ash::test {
// TODO(https://crbug.com/1370774): move `ScopedTestMountPoint` out of holding
// space to remove the dependency on holding space code.
using ash::holding_space::ScopedTestMountPoint;

class FileSuggestKeyedServiceTest : public testing::Test {
 protected:
  // testing::Test:
  void SetUp() override {
    testing_profile_manager_ = std::make_unique<TestingProfileManager>(
        TestingBrowserProcess::GetGlobal());
    EXPECT_TRUE(testing_profile_manager_->SetUp());
    profile_ = testing_profile_manager_->CreateTestingProfile(
        "primary_profile@test", GetTestingFactories());
    WaitUntilFileSuggestServiceReady(
        FileSuggestKeyedServiceFactory::GetInstance()->GetService(profile_));
  }

  virtual TestingProfile::TestingFactories GetTestingFactories() { return {}; }

  PrefService* GetPrefService() { return profile_->GetTestingPrefService(); }

  content::BrowserTaskEnvironment task_environment_{
      base::test::TaskEnvironment::TimeSource::MOCK_TIME};
  std::unique_ptr<TestingProfileManager> testing_profile_manager_;
  raw_ptr<TestingProfile> profile_ = nullptr;
};

TEST_F(FileSuggestKeyedServiceTest, GetSuggestData) {
  base::HistogramTester tester;
  if (features::IsForestFeatureEnabled()) {
    drive::DriveIntegrationServiceFactory::GetInstance()
        ->GetForProfile(profile_)
        ->SetEnabled(true);
  }
  FileSuggestKeyedServiceFactory::GetInstance()
      ->GetService(profile_)
      ->GetSuggestFileData(
          FileSuggestionType::kDriveFile,
          base::BindOnce([](const std::optional<std::vector<FileSuggestData>>&
                                suggest_data) {
            EXPECT_FALSE(suggest_data.has_value());
          }));
  tester.ExpectBucketCount(
      "Ash.Search.DriveFileSuggestDataValidation.Status",
      /*sample=*/DriveSuggestValidationStatus::kDriveFSNotMounted,
      /*expected_count=*/
      (features::IsLauncherContinueSectionWithRecentsEnabled() ||
       features::IsForestFeatureEnabled())
          ? 0
          : 1);
}

TEST_F(FileSuggestKeyedServiceTest, DisabledByPolicy) {
  base::HistogramTester tester;
  if (features::IsForestFeatureEnabled()) {
    drive::DriveIntegrationServiceFactory::GetInstance()
        ->GetForProfile(profile_)
        ->SetEnabled(true);
  }
  FileSuggestKeyedServiceFactory::GetInstance()
      ->GetService(profile_)
      ->GetSuggestFileData(
          FileSuggestionType::kDriveFile,
          base::BindOnce([](const std::optional<std::vector<FileSuggestData>>&
                                suggest_data) {
            EXPECT_FALSE(suggest_data.has_value());
          }));

  // Disable file suggestion integration by policy.
  GetPrefService()->SetList(prefs::kContextualGoogleIntegrationsConfiguration,
                            {});

  FileSuggestKeyedServiceFactory::GetInstance()
      ->GetService(profile_)
      ->GetSuggestFileData(
          FileSuggestionType::kDriveFile,
          base::BindOnce([](const std::optional<std::vector<FileSuggestData>>&
                                suggest_data) {
            // Disabling by policy should ensure a list with zero items is
            // returned.
            EXPECT_TRUE(suggest_data.has_value());
            EXPECT_EQ(suggest_data->size(), 0u);
          }));
}

class FileSuggestKeyedServiceRemoveTest : public FileSuggestKeyedServiceTest {
 protected:
  // FileSuggestKeyedServiceTest:
  void SetUp() override {
    ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
    FileSuggestKeyedServiceTest::SetUp();
    file_suggest_service_ = static_cast<MockFileSuggestKeyedService*>(
        FileSuggestKeyedServiceFactory::GetInstance()->GetService(profile_));

    // Initialize the local file mount point.
    local_fs_mount_point_ = std::make_unique<ScopedTestMountPoint>(
        /*name=*/"test_mount", storage::kFileSystemTypeLocal,
        file_manager::VOLUME_TYPE_TESTING);
    local_fs_mount_point_->Mount(profile_);

    // Initialize the drive file mount point.
    drive_fs_mount_point_ = std::make_unique<ScopedTestMountPoint>(
        /*name=*/"drivefs-delayed_mount", storage::kFileSystemTypeDriveFs,
        file_manager::VOLUME_TYPE_GOOGLE_DRIVE);
    drive_fs_mount_point_->Mount(profile_);
  }

  void TearDown() override {
    drive_fs_mount_point_.reset();
    local_fs_mount_point_.reset();
    FileSuggestKeyedServiceTest::TearDown();
  }

  TestingProfile::TestingFactories GetTestingFactories() override {
    return {TestingProfile::TestingFactory{
        FileSuggestKeyedServiceFactory::GetInstance(),
        base::BindRepeating(
            &MockFileSuggestKeyedService::BuildMockFileSuggestKeyedService,
            TestingBrowserProcess::GetGlobal()
                ->GetFeatures()
                ->application_locale_storage(),
            temp_dir_.GetPath().Append("proto"))}};
  }

  std::optional<std::vector<FileSuggestData>> GetSuggestionsForType(
      FileSuggestionType type) {
    std::optional<std::vector<FileSuggestData>> suggestions;
    base::RunLoop run_loop;
    file_suggest_service_->GetSuggestFileData(
        type, base::BindOnce(
                  [](base::RunLoop* run_loop,
                     std::optional<std::vector<FileSuggestData>>* suggestions,
                     const std::optional<std::vector<FileSuggestData>>&
                         fetched_suggestions) {
                    *suggestions = fetched_suggestions;
                    run_loop->Quit();
                  },
                  &run_loop, &suggestions));
    run_loop.Run();
    return suggestions;
  }

  // Creates files and suggests these files through the file suggest keyed
  // service. Returns paths to these files.
  std::vector<base::FilePath> PrepareFileSuggestionsForType(
      FileSuggestionType type,
      size_t count) {
    ScopedTestMountPoint* mount_point = nullptr;
    switch (type) {
      case FileSuggestionType::kDriveFile:
        mount_point = drive_fs_mount_point_.get();
        break;
      case FileSuggestionType::kLocalFile:
        mount_point = local_fs_mount_point_.get();
        break;
    }

    std::vector<base::FilePath> suggested_file_paths;
    std::vector<FileSuggestData> suggestions;
    for (size_t index = 0; index < count; ++index) {
      suggested_file_paths.push_back(mount_point->CreateArbitraryFile());
      suggestions.emplace_back(type, suggested_file_paths.back(),
                               /*title=*/std::nullopt,
                               /*new_prediction_reason=*/std::nullopt,
                               /*modified_time=*/std::nullopt,
                               /*viewed_time=*/std::nullopt,
                               /*shared_time=*/std::nullopt,
                               /*new_score=*/std::nullopt,
                               /*drive_file_id=*/std::nullopt,
                               /*icon_url=*/std::nullopt);
    }
    file_suggest_service_->SetSuggestionsForType(type, suggestions);
    return suggested_file_paths;
  }

  MockFileSuggestKeyedService* file_suggest_service() {
    return file_suggest_service_;
  }
  ScopedTestMountPoint* drive_mount_point() {
    return drive_fs_mount_point_.get();
  }
  ScopedTestMountPoint* local_mount_point() {
    return local_fs_mount_point_.get();
  }
  // Hosts the proto file.
  base::ScopedTempDir temp_dir_;

  // This test verifies the suggestion removal only. Therefore, a mock file
  // suggest keyed service is sufficient.
  raw_ptr<MockFileSuggestKeyedService> file_suggest_service_ = nullptr;

  // The mount point for local files.
  std::unique_ptr<ScopedTestMountPoint> local_fs_mount_point_;

  // The mount point for drive files.
  std::unique_ptr<ScopedTestMountPoint> drive_fs_mount_point_;
};

// Verifies removing drive file suggestions.
TEST_F(FileSuggestKeyedServiceRemoveTest, RemoveDriveFileSuggestions) {
  const std::vector<base::FilePath> drive_file_suggestions =
      PrepareFileSuggestionsForType(FileSuggestionType::kDriveFile,
                                    /*count=*/2);
  const base::FilePath& file_path_1 = drive_file_suggestions[0];
  const base::FilePath& file_path_2 = drive_file_suggestions[1];

  std::optional<std::vector<FileSuggestData>> suggestions =
      GetSuggestionsForType(FileSuggestionType::kDriveFile);
  EXPECT_EQ(suggestions->size(), 2u);
  EXPECT_EQ(suggestions->at(0).file_path.value(), file_path_1.value());
  EXPECT_EQ(suggestions->at(0).id, "zero_state_drive://" + file_path_1.value());
  EXPECT_EQ(suggestions->at(1).file_path.value(), file_path_2.value());
  EXPECT_EQ(suggestions->at(1).id, "zero_state_drive://" + file_path_2.value());

  MockFileSuggestKeyedServiceObserver observer_mocker;
  base::ScopedObservation<FileSuggestKeyedService,
                          FileSuggestKeyedService::Observer>
      scoped_observation(&observer_mocker);
  scoped_observation.Observe(file_suggest_service_.get());

  // The observer should be notified of the drive file suggestion update.
  EXPECT_CALL(observer_mocker,
              OnFileSuggestionUpdated(FileSuggestionType::kDriveFile));

  file_suggest_service_->RemoveSuggestionsAndNotify(
      /*absolute_file_paths=*/{{file_path_1}});

  // Check the suggested files after suggestion removal.
  suggestions = GetSuggestionsForType(FileSuggestionType::kDriveFile);
  EXPECT_EQ(suggestions->size(), 1u);
  EXPECT_EQ(suggestions->at(0).file_path.value(), file_path_2.value());
  EXPECT_EQ(suggestions->at(0).id, "zero_state_drive://" + file_path_2.value());
}

// Verifies removing local file suggestions.
TEST_F(FileSuggestKeyedServiceRemoveTest, RemoveLocalFileSuggestions) {
  const std::vector<base::FilePath> local_file_suggestions =
      PrepareFileSuggestionsForType(FileSuggestionType::kLocalFile,
                                    /*count=*/2);
  const base::FilePath& file_path_1 = local_file_suggestions[0];
  const base::FilePath& file_path_2 = local_file_suggestions[1];

  std::optional<std::vector<FileSuggestData>> suggestions =
      GetSuggestionsForType(FileSuggestionType::kLocalFile);
  EXPECT_EQ(suggestions->size(), 2u);
  EXPECT_EQ(suggestions->at(0).file_path.value(), file_path_1.value());
  EXPECT_EQ(suggestions->at(0).id, "zero_state_file://" + file_path_1.value());
  EXPECT_EQ(suggestions->at(1).file_path.value(), file_path_2.value());
  EXPECT_EQ(suggestions->at(1).id, "zero_state_file://" + file_path_2.value());

  MockFileSuggestKeyedServiceObserver observer_mocker;
  base::ScopedObservation<FileSuggestKeyedService,
                          FileSuggestKeyedService::Observer>
      scoped_observation(&observer_mocker);
  scoped_observation.Observe(file_suggest_service_.get());

  // The observer should be notified of the local file suggestion update.
  EXPECT_CALL(observer_mocker,
              OnFileSuggestionUpdated(FileSuggestionType::kLocalFile));

  file_suggest_service_->RemoveSuggestionsAndNotify(
      /*absolute_file_paths=*/{file_path_2});

  // Check the suggested files after suggestion removal.
  suggestions = GetSuggestionsForType(FileSuggestionType::kLocalFile);
  EXPECT_EQ(suggestions->size(), 1u);
  EXPECT_EQ(suggestions->at(0).file_path.value(), file_path_1.value());
  EXPECT_EQ(suggestions->at(0).id, "zero_state_file://" + file_path_1.value());
}

// Verifies removing drive and local file suggestions at the same time.
TEST_F(FileSuggestKeyedServiceRemoveTest, RemoveMixedFileSuggestions) {
  const std::vector<base::FilePath> drive_file_suggestions =
      PrepareFileSuggestionsForType(FileSuggestionType::kDriveFile,
                                    /*count=*/1);
  const std::vector<base::FilePath> local_file_suggestions =
      PrepareFileSuggestionsForType(FileSuggestionType::kLocalFile,
                                    /*count=*/1);

  MockFileSuggestKeyedServiceObserver observer_mocker;
  base::ScopedObservation<FileSuggestKeyedService,
                          FileSuggestKeyedService::Observer>
      scoped_observation(&observer_mocker);
  scoped_observation.Observe(file_suggest_service_.get());

  // The observer should be notified of the updates in drive and local file
  // suggestions.
  EXPECT_CALL(observer_mocker,
              OnFileSuggestionUpdated(FileSuggestionType::kDriveFile));
  EXPECT_CALL(observer_mocker,
              OnFileSuggestionUpdated(FileSuggestionType::kLocalFile));

  file_suggest_service_->RemoveSuggestionsAndNotify(
      /*absolute_file_paths=*/{drive_file_suggestions[0],
                               local_file_suggestions[0]});

  // Check the suggested files after suggestion removal.
  EXPECT_TRUE(GetSuggestionsForType(FileSuggestionType::kDriveFile)->empty());
  EXPECT_TRUE(GetSuggestionsForType(FileSuggestionType::kLocalFile)->empty());
}

// Verifies filtering out duplicate drive file suggestions.
TEST_F(FileSuggestKeyedServiceRemoveTest, FilterDuplicateDriveFileSuggestions) {
  const base::FilePath file_path_1 = drive_mount_point()->CreateArbitraryFile();
  std::optional<std::vector<FileSuggestData>> suggestions;
  suggestions =
      std::vector<FileSuggestData>{{FileSuggestionType::kDriveFile, file_path_1,
                                    /*title=*/std::nullopt,
                                    /*new_prediction_reason=*/std::nullopt,
                                    /*modified_time=*/std::nullopt,
                                    /*viewed_time=*/std::nullopt,
                                    /*shared_time=*/std::nullopt,
                                    /*new_score=*/std::nullopt,
                                    /*drive_file_id=*/std::nullopt,
                                    /*icon_url=*/std::nullopt},
                                   {FileSuggestionType::kDriveFile, file_path_1,
                                    /*title=*/std::nullopt,
                                    /*new_prediction_reason=*/std::nullopt,
                                    /*modified_time=*/std::nullopt,
                                    /*viewed_time=*/std::nullopt,
                                    /*shared_time=*/std::nullopt,
                                    /*new_score=*/std::nullopt,
                                    /*drive_file_id=*/std::nullopt,
                                    /*icon_url=*/std::nullopt}};

  EXPECT_EQ(suggestions->size(), 2u);
  file_suggest_service()->SetSuggestionsForType(FileSuggestionType::kDriveFile,
                                                /*suggestions=*/suggestions);

  // Check the suggested drive files, duplicate files should be removed.
  suggestions = GetSuggestionsForType(FileSuggestionType::kDriveFile);
  EXPECT_EQ(suggestions->size(), 1u);
  EXPECT_EQ(suggestions->at(0).file_path.value(), file_path_1.value());
}

// Verifies filtering out duplicate local file suggestions.
TEST_F(FileSuggestKeyedServiceRemoveTest, FilterDuplicateLocalFileSuggestions) {
  const base::FilePath file_path_1 = local_mount_point()->CreateArbitraryFile();
  std::optional<std::vector<FileSuggestData>> suggestions;
  suggestions =
      std::vector<FileSuggestData>{{FileSuggestionType::kLocalFile, file_path_1,
                                    /*title=*/std::nullopt,
                                    /*new_prediction_reason=*/std::nullopt,
                                    /*modified_time=*/std::nullopt,
                                    /*viewed_time=*/std::nullopt,
                                    /*shared_time=*/std::nullopt,
                                    /*new_score=*/std::nullopt,
                                    /*drive_file_id=*/std::nullopt,
                                    /*icon_url=*/std::nullopt},
                                   {FileSuggestionType::kLocalFile, file_path_1,
                                    /*title=*/std::nullopt,
                                    /*new_prediction_reason=*/std::nullopt,
                                    /*modified_time=*/std::nullopt,
                                    /*viewed_time=*/std::nullopt,
                                    /*shared_time=*/std::nullopt,
                                    /*new_score=*/std::nullopt,
                                    /*drive_file_id=*/std::nullopt,
                                    /*icon_url=*/std::nullopt}};

  EXPECT_EQ(suggestions->size(), 2u);
  file_suggest_service()->SetSuggestionsForType(FileSuggestionType::kLocalFile,
                                                /*suggestions=*/suggestions);

  // Check the suggested local files, duplicate files should be removed.
  suggestions = GetSuggestionsForType(FileSuggestionType::kLocalFile);
  EXPECT_EQ(suggestions->size(), 1u);
  EXPECT_EQ(suggestions->at(0).file_path.value(), file_path_1.value());
}

}  // namespace ash::test