File: multi_page_scan_integration_test.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 (179 lines) | stat: -rw-r--r-- 6,489 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
// 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 <memory>

#include "base/check.h"
#include "base/files/file_enumerator.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/path_service.h"
#include "base/run_loop.h"
#include "chrome/browser/ash/file_manager/path_util.h"
#include "chrome/browser/ash/login/test/session_manager_state_waiter.h"
#include "chrome/browser/ash/scanning/fake_lorgnette_scanner_manager.h"
#include "chrome/browser/ash/scanning/lorgnette_scanner_manager_factory.h"
#include "chrome/browser/ash/scanning/scan_service.h"
#include "chrome/browser/ash/scanning/scan_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/test/base/chromeos/crosier/ash_integration_test.h"
#include "chrome/test/base/chromeos/crosier/chromeos_integration_login_mixin.h"
#include "chrome/test/interaction/interactive_browser_test.h"
#include "chromeos/ash/components/browser_context_helper/browser_context_types.h"
#include "chromeos/ash/components/dbus/lorgnette/lorgnette_service.pb.h"
#include "components/keyed_service/core/keyed_service.h"
#include "content/public/browser/browser_context.h"
#include "content/public/test/browser_test.h"
#include "ui/aura/window.h"

namespace ash {

namespace {

constexpr char kScanningUrl[] = "chrome://scanning";

// Scan settings.
constexpr char kFirstTestScannerName[] = "Test Scanner 1";
constexpr char kDocumentSourceName[] = "Flatbed";
constexpr uint32_t kFirstResolution = 75;
constexpr uint32_t kSecondResolution = 150;

// Kombucha helpers.
constexpr char kClickFn[] = "e => e.click()";

lorgnette::DocumentSource CreateLorgnetteDocumentSource() {
  lorgnette::DocumentSource source;
  source.set_type(lorgnette::SOURCE_PLATEN);
  source.set_name(kDocumentSourceName);
  source.add_color_modes(lorgnette::MODE_GRAYSCALE);
  source.add_resolutions(kFirstResolution);
  source.add_resolutions(kSecondResolution);
  return source;
}

lorgnette::ScannerCapabilities CreateLorgnetteScannerCapabilities() {
  lorgnette::ScannerCapabilities caps;
  *caps.add_sources() = CreateLorgnetteDocumentSource();
  return caps;
}

// Creates a new LorgnetteScannerManager for the given `context`.
std::unique_ptr<KeyedService> BuildLorgnetteScannerManager(
    content::BrowserContext* context) {
  auto manager = std::make_unique<FakeLorgnetteScannerManager>();
  manager->SetGetScannerNamesResponse({kFirstTestScannerName});
  manager->SetGetScannerCapabilitiesResponse(
      CreateLorgnetteScannerCapabilities());
  return manager;
}

class MultiPageScanIntegrationTest : public AshIntegrationTest {
 public:
  MultiPageScanIntegrationTest() {
    set_exit_when_last_browser_closes(false);
    login_mixin().SetMode(ChromeOSIntegrationLoginMixin::Mode::kTestLogin);
  }

  const DeepQuery kScanButtonQuery{
      "scanning-app",
      "cr-button#scanButton",
  };

  const DeepQuery kScanDoneSectionQuery{
      "scanning-app",
      "scan-done-section",
  };

  const DeepQuery kMultiPageCheckboxQuery{
      "scanning-app",
      "multi-page-checkbox",
      "div#checkboxDiv",
  };

  const DeepQuery kSourceSelectQuery{
      "scanning-app",
      "source-select",
      "select",
  };

  const DeepQuery kMultiPageScanningElementQuery{
      "scanning-app",
      "multi-page-scan",
  };

  const DeepQuery kScanNextPageButtonQuery{"scanning-app", "multi-page-scan",
                                           "cr-button#scanButton"};

  const DeepQuery kMultiPageScanSaveButtonQuery{
      "scanning-app", "multi-page-scan", "cr-button#saveButton"};

  auto LaunchScanningApp() {
    return Do([&]() { CreateBrowserWindow(GURL(kScanningUrl)); });
  }

  std::vector<base::FilePath> GetScannedFiles() {
    base::FileEnumerator e(
        file_manager::util::GetMyFilesFolderForProfile(GetActiveUserProfile()),
        /*recursive=*/false, base::FileEnumerator::FILES,
        FILE_PATH_LITERAL("*.pdf"));
    std::vector<base::FilePath> files;
    e.ForEach([&files](const base::FilePath& item) { files.push_back(item); });
    return files;
  }
};

IN_PROC_BROWSER_TEST_F(MultiPageScanIntegrationTest, MultiPageScan) {
  // Set up context for element tracking for InteractiveBrowserTest.
  SetupContextWidget();

  login_mixin().Login();

  // Waits for the primary user session to start.
  ash::test::WaitForPrimaryUserSessionStart();

  // Ensure the Scanning system web app (SWA) is installed.
  InstallSystemApps();
  DEFINE_LOCAL_ELEMENT_IDENTIFIER_VALUE(kScanAppWebContentsId);
  base::ScopedAllowBlockingForTesting allow_io;

  LorgnetteScannerManagerFactory::GetInstance()->SetTestingFactory(
      GetActiveUserProfile(),
      base::BindRepeating(&BuildLorgnetteScannerManager));

  RunTestSequence(
      InstrumentNextTab(kScanAppWebContentsId, AnyBrowser()),
      Log("Launching Scanning app"), LaunchScanningApp(),
      Log("Waiting for Scanning app to load"),
      WaitForWebContentsReady(kScanAppWebContentsId, GURL(kScanningUrl)),
      FocusWebContents(kScanAppWebContentsId),
      Log("Verifying that Flatbed is the selected source"),
      WaitForElementTextContains(kScanAppWebContentsId, kSourceSelectQuery,
                                 "Flatbed"),
      Log("Toggling multi page scanning checkbox"),
      ExecuteJsAt(kScanAppWebContentsId, kMultiPageCheckboxQuery, kClickFn),
      Log("Clicking scan button"),
      ExecuteJsAt(kScanAppWebContentsId, kScanButtonQuery, kClickFn),
      Log("Verifying that multi page scan UI is shown"),
      WaitForElementExists(kScanAppWebContentsId,
                           kMultiPageScanningElementQuery),
      Log("Verifying that no file has been created yet"),
      Check([this]() { return GetScannedFiles().size() == 0u; }),
      Log("Clicking scan next page button"),
      ExecuteJsAt(kScanAppWebContentsId, kScanNextPageButtonQuery, kClickFn),
      Log("Clicking save button"),
      ExecuteJsAt(kScanAppWebContentsId, kMultiPageScanSaveButtonQuery,
                  kClickFn),
      Check([this]() {
        const auto files = GetScannedFiles();
        // PDFs do not have stable contents (e.g. metadata changes), so do not
        // compare to a golden file. If the file was written, we assume its
        // contents are valid.
        return files.size() == 1u && base::PathExists(files.front());
      }));
}

}  // namespace

}  // namespace ash