File: chrome_scanning_app_delegate_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 (293 lines) | stat: -rw-r--r-- 11,838 bytes parent folder | download | duplicates (6)
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
// Copyright 2020 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/scanning/chrome_scanning_app_delegate.h"

#include <memory>
#include <utility>

#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.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 "content/public/browser/web_contents.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/test_web_ui.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace ash {

namespace {

constexpr char kExpectedScanSettings[] = R"({
    "lastUsedScannerName": "Brother MFC-J497DW",
    "scanToPath": "path/to/file",
    "scanners": [
      {
        "name": "Brother MFC-J497DW",
        "lastScanDate": "2021-04-16T02:45:26.768Z",
        "sourceName": "ADF",
        "fileType": 2,
        "colorMode": 1,
        "pageSize": 2,
        "resolutionDpi": 100
      }
    ]
  })";

// "root" is appended to the user's Google Drive directory to form the
// complete path.
constexpr char kRoot[] = "root";

// The name of the sticky settings pref.
constexpr char kScanningStickySettingsPref[] =
    "scanning.scanning_sticky_settings";

}  // namespace

class ChromeScanningAppDelegateTest : public testing::Test {
 public:
  ChromeScanningAppDelegateTest()
      : task_environment_(content::BrowserTaskEnvironment::REAL_IO_THREAD),
        profile_manager_(TestingBrowserProcess::GetGlobal()) {}
  ~ChromeScanningAppDelegateTest() override = default;

  void SetUp() override {
    ASSERT_TRUE(profile_manager_.SetUp());
    profile_ = profile_manager_.CreateTestingProfile("profile1@gmail.com");

    web_contents_ = content::WebContents::Create(
        content::WebContents::CreateParams(profile_));
    web_ui_ = std::make_unique<content::TestWebUI>();
    web_ui_->set_web_contents(web_contents_.get());

    EXPECT_TRUE(temp_dir_.CreateUniqueTempDir());
    drive_path_ = temp_dir_.GetPath().Append("drive/root");
    EXPECT_TRUE(base::CreateDirectory(drive_path_));
    my_files_path_ = temp_dir_.GetPath().Append("MyFiles");
    EXPECT_TRUE(base::CreateDirectory(my_files_path_));
    removable_media_path_ = temp_dir_.GetPath().Append("removable/media");
    EXPECT_TRUE(base::CreateDirectory(removable_media_path_));

    chrome_scanning_app_delegate_ =
        std::make_unique<ChromeScanningAppDelegate>(web_ui_.get());
    chrome_scanning_app_delegate_->SetValidPaths(drive_path_, my_files_path_);
    chrome_scanning_app_delegate_->SetRemoveableMediaPathForTesting(
        removable_media_path_);
  }

  void TearDown() override { web_contents_.reset(); }

  void DidFilesAppOpen(bool expected_value, bool files_app_opened) {
    EXPECT_EQ(expected_value, files_app_opened);
    std::move(run_loop_closure_).Run();
  }

 protected:
  raw_ptr<TestingProfile, DanglingUntriaged> profile_;
  std::unique_ptr<ChromeScanningAppDelegate> chrome_scanning_app_delegate_;
  std::unique_ptr<content::TestWebUI> web_ui_;
  base::ScopedTempDir temp_dir_;
  base::FilePath my_files_path_;
  base::FilePath drive_path_;
  base::FilePath removable_media_path_;
  base::OnceClosure run_loop_closure_;

 private:
  content::BrowserTaskEnvironment task_environment_;
  TestingProfileManager profile_manager_;
  std::unique_ptr<content::WebContents> web_contents_;
};

// Validates that the correct MyFiles path is returned.
TEST_F(ChromeScanningAppDelegateTest, GetMyFilesPath) {
  EXPECT_EQ(my_files_path_, chrome_scanning_app_delegate_->GetMyFilesPath());
}

// Validates that the correct base name is returned from a generic filepath.
TEST_F(ChromeScanningAppDelegateTest, BaseNameFromGenericPath) {
  EXPECT_EQ("directory", chrome_scanning_app_delegate_->GetBaseNameFromPath(
                             base::FilePath("/test/directory")));
}

// Validates that the correct base name is returned from the file paths under
// the Google Drive root.
TEST_F(ChromeScanningAppDelegateTest, BaseNameFromGoogleDrivePath) {
  EXPECT_EQ("Sub Folder", chrome_scanning_app_delegate_->GetBaseNameFromPath(
                              drive_path_.Append(kRoot).Append("Sub Folder")));
  EXPECT_EQ("My Drive", chrome_scanning_app_delegate_->GetBaseNameFromPath(
                            drive_path_.Append(kRoot)));
}

// Validates that the correct base name is returned from file paths under the
// 'My Files' path.
TEST_F(ChromeScanningAppDelegateTest, BaseNameFromMyFilesPath) {
  EXPECT_EQ("Sub Folder", chrome_scanning_app_delegate_->GetBaseNameFromPath(
                              my_files_path_.Append("Sub Folder")));
  EXPECT_EQ("My files",
            chrome_scanning_app_delegate_->GetBaseNameFromPath(my_files_path_));
}

// Validates that passing a file path that exists and is a child of the MyFiles
// path returns true for showing the Files app.
TEST_F(ChromeScanningAppDelegateTest, ShowFilesAppMyFilesChild) {
  base::RunLoop run_loop;
  run_loop_closure_ = run_loop.QuitWhenIdleClosure();

  const base::FilePath test_file = my_files_path_.Append("test_file.png");
  base::File(test_file, base::File::FLAG_CREATE | base::File::FLAG_READ);
  chrome_scanning_app_delegate_->ShowFileInFilesApp(
      test_file,
      base::BindOnce(&ChromeScanningAppDelegateTest::DidFilesAppOpen,
                     base::Unretained(this), /*expected_value=*/true));
  run_loop.Run();
}

// Validates that passing a file path that exists and is a child of the Drive
// path returns true for showing the Files app.
TEST_F(ChromeScanningAppDelegateTest, ShowFilesAppGoogleDrivePathChild) {
  base::RunLoop run_loop;
  run_loop_closure_ = run_loop.QuitWhenIdleClosure();

  const base::FilePath test_file = drive_path_.Append("test_file.png");
  base::File(test_file, base::File::FLAG_CREATE | base::File::FLAG_READ);
  chrome_scanning_app_delegate_->ShowFileInFilesApp(
      test_file,
      base::BindOnce(&ChromeScanningAppDelegateTest::DidFilesAppOpen,
                     base::Unretained(this), /*expected_value=*/true));
  run_loop.Run();
}

// Validates that passing a file path that exists and is a child of a removable
// media returns true for showing the Files app.
TEST_F(ChromeScanningAppDelegateTest, ShowFilesAppRemovableMediaPathChild) {
  base::RunLoop run_loop;
  run_loop_closure_ = run_loop.QuitWhenIdleClosure();

  const base::FilePath test_file =
      removable_media_path_.Append("test_file.png");
  base::File(test_file, base::File::FLAG_CREATE | base::File::FLAG_READ);
  chrome_scanning_app_delegate_->ShowFileInFilesApp(
      test_file,
      base::BindOnce(&ChromeScanningAppDelegateTest::DidFilesAppOpen,
                     base::Unretained(this), /*expected_value=*/true));
  run_loop.Run();
}

// Validates that passing a non-existent file in the MyFiles path returns false
// for showing the Files app.
TEST_F(ChromeScanningAppDelegateTest, ShowFilesAppMyFilesFileNotFound) {
  base::RunLoop run_loop;
  run_loop_closure_ = run_loop.QuitWhenIdleClosure();

  const base::FilePath missing_my_file =
      my_files_path_.Append("missing_my_file.png");
  chrome_scanning_app_delegate_->ShowFileInFilesApp(
      missing_my_file,
      base::BindOnce(&ChromeScanningAppDelegateTest::DidFilesAppOpen,
                     base::Unretained(this), /*expected_value=*/false));
  run_loop.Run();
}

// Validates that passing a non-existent file in the Drive path returns false
// for showing the Files app.
TEST_F(ChromeScanningAppDelegateTest, ShowFilesAppGoogleDriveFileNotFound) {
  base::RunLoop run_loop;
  run_loop_closure_ = run_loop.QuitWhenIdleClosure();

  const base::FilePath missing_drive_file =
      drive_path_.Append("missing_drive_file.png");
  chrome_scanning_app_delegate_->ShowFileInFilesApp(
      missing_drive_file,
      base::BindOnce(&ChromeScanningAppDelegateTest::DidFilesAppOpen,
                     base::Unretained(this), /*expected_value=*/false));
  run_loop.Run();
}

// Validates that passing a unsupported path returns false for showing the Files
// app.
TEST_F(ChromeScanningAppDelegateTest, ShowFilesAppPathNotSupported) {
  base::RunLoop run_loop;
  run_loop_closure_ = run_loop.QuitWhenIdleClosure();

  chrome_scanning_app_delegate_->ShowFileInFilesApp(
      base::FilePath("/wrong/file/path/file.png"),
      base::BindOnce(&ChromeScanningAppDelegateTest::DidFilesAppOpen,
                     base::Unretained(this), /*expected_value=*/false));
  run_loop.Run();
}

// Validates that passing file paths with references returns false for showing
// the Files app.
TEST_F(ChromeScanningAppDelegateTest, ShowFilesAppReferencesNotSupported) {
  base::RunLoop run_loop;
  run_loop_closure_ = run_loop.QuitWhenIdleClosure();

  const base::FilePath test_file = my_files_path_.Append("test_file.png");
  base::File(test_file, base::File::FLAG_CREATE | base::File::FLAG_READ);
  chrome_scanning_app_delegate_->ShowFileInFilesApp(
      my_files_path_.Append("../MyFiles/test_file.png"),
      base::BindOnce(&ChromeScanningAppDelegateTest::DidFilesAppOpen,
                     base::Unretained(this), /*expected_value=*/false));
  run_loop.Run();
}

// Validates that scan settings are saved to the Pref service.
TEST_F(ChromeScanningAppDelegateTest, SaveScanSettings) {
  chrome_scanning_app_delegate_->SaveScanSettingsToPrefs(kExpectedScanSettings);
  EXPECT_EQ(kExpectedScanSettings,
            profile_->GetPrefs()->GetString(kScanningStickySettingsPref));
}

// Validates that scan settings can be retrieved from the Pref service.
TEST_F(ChromeScanningAppDelegateTest, GetScanSettings) {
  // No pref should be found for |kScanningStickySettingsPref| yet.
  EXPECT_EQ(std::string(),
            chrome_scanning_app_delegate_->GetScanSettingsFromPrefs());

  profile_->GetPrefs()->SetString(kScanningStickySettingsPref,
                                  kExpectedScanSettings);
  EXPECT_EQ(kExpectedScanSettings,
            chrome_scanning_app_delegate_->GetScanSettingsFromPrefs());
}

// Validates that passing a file path that is a child of the MyFiles path
// returns true for IsFilePathSupported().
TEST_F(ChromeScanningAppDelegateTest, FilePathSupportedMyFilesChild) {
  const base::FilePath test_file = my_files_path_.Append("test_file.png");
  base::File(test_file, base::File::FLAG_CREATE | base::File::FLAG_READ);
  EXPECT_TRUE(chrome_scanning_app_delegate_->IsFilePathSupported(test_file));
}

// Validates that passing a file path that is a child of the Drive path returns
// true for IsFilePathSupported().
TEST_F(ChromeScanningAppDelegateTest, FilePathSupportedGoogleDrivePathChild) {
  const base::FilePath test_file = drive_path_.Append("test_file.png");
  base::File(test_file, base::File::FLAG_CREATE | base::File::FLAG_READ);
  EXPECT_TRUE(chrome_scanning_app_delegate_->IsFilePathSupported(test_file));
}

// Validates that passing an unsupported path returns false for
// IsFilePathSupported().
TEST_F(ChromeScanningAppDelegateTest, FilePathNotSupported) {
  ASSERT_FALSE(chrome_scanning_app_delegate_->IsFilePathSupported(
      base::FilePath("/wrong/file/path/file.png")));
}

// Validates that passing a file path with a reference returns false for
// IsFilePathSupported().
TEST_F(ChromeScanningAppDelegateTest, FilePathReferencesNotSupported) {
  const base::FilePath test_file = my_files_path_.Append("test_file.png");
  base::File(test_file, base::File::FLAG_CREATE | base::File::FLAG_READ);
  ASSERT_FALSE(chrome_scanning_app_delegate_->IsFilePathSupported(
      my_files_path_.Append("../MyFiles/test_file.png")));
}

}  // namespace ash