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
|
// 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 "chrome/browser/ui/webui/ash/office_fallback/office_fallback_dialog.h"
#include <utility>
#include "base/strings/utf_string_conversions.h"
#include "base/test/bind.h"
#include "base/test/run_until.h"
#include "chrome/browser/ash/file_manager/file_manager_test_util.h"
#include "chrome/browser/ash/file_manager/office_file_tasks.h"
#include "chrome/browser/ash/file_system_provider/service.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/webui/ash/office_fallback/office_fallback_ui.h"
#include "chrome/common/extensions/extension_constants.h"
#include "chrome/grit/generated_resources.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chromeos/constants/chromeos_features.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/test_navigation_observer.h"
#include "ui/base/l10n/l10n_util.h"
namespace ash::office_fallback {
namespace {
static const char kTaskTitle[] = "some app";
} // namespace
content::WebContents* GetWebContentsFromOfficeFallbackDialog() {
ash::SystemWebDialogDelegate* dialog =
ash::SystemWebDialogDelegate::FindInstance(
chrome::kChromeUIOfficeFallbackURL);
EXPECT_TRUE(dialog);
content::WebUI* webui = dialog->GetWebUIForTest();
EXPECT_TRUE(webui);
content::WebContents* web_contents = webui->GetWebContents();
EXPECT_TRUE(web_contents);
return web_contents;
}
// Launch the Office Fallback dialog at chrome://office-fallback by calling
// OfficeFallbackDialog::Show() with the arguments provided. Wait until the
// "office-fallback" DOM element exists at chrome://office-fallback and return
// the web contents.
content::WebContents* LaunchOfficeFallbackDialogAndGetWebContentsForDialog(
const std::vector<storage::FileSystemURL>& file_urls,
FallbackReason fallback_reason,
const std::string& task_title,
DialogChoiceCallback callback) {
// Watch for Office Fallback dialog URL chrome://office-fallback.
content::TestNavigationObserver navigation_observer_dialog(
(GURL(chrome::kChromeUIOfficeFallbackURL)));
navigation_observer_dialog.StartWatchingNewWebContents();
// Launch Office Fallback dialog.
EXPECT_TRUE(OfficeFallbackDialog::Show(file_urls, fallback_reason, task_title,
std::move(callback)));
// Wait for chrome://office-fallback to open.
navigation_observer_dialog.Wait();
EXPECT_TRUE(navigation_observer_dialog.last_navigation_succeeded());
// Get the web contents of the dialog to be able to check that it is exists.
content::WebContents* web_contents = GetWebContentsFromOfficeFallbackDialog();
// Wait until the DOM element actually exists at office-fallback.
EXPECT_TRUE(base::test::RunUntil([&] {
return content::EvalJs(web_contents,
"!!document.querySelector('office-fallback')")
.ExtractBool();
}));
return web_contents;
}
class OfficeFallbackDialogBrowserTest : public InProcessBrowserTest {
public:
OfficeFallbackDialogBrowserTest() {
feature_list_.InitWithFeatures(
{chromeos::features::kUploadOfficeToCloud,
chromeos::features::kMicrosoftOneDriveIntegrationForEnterprise,
chromeos::features::kUploadOfficeToCloudForEnterprise},
{});
}
OfficeFallbackDialogBrowserTest(const OfficeFallbackDialogBrowserTest&) =
delete;
OfficeFallbackDialogBrowserTest& operator=(
const OfficeFallbackDialogBrowserTest&) = delete;
void SetUpOnMainThread() override {
InProcessBrowserTest::SetUpOnMainThread();
// This is needed to simulate the presence of the ODFS extension, which is
// checked in `IsMicrosoftOfficeOneDriveIntegrationAllowedAndOdfsInstalled`.
auto fake_provider =
ash::file_system_provider::FakeExtensionProvider::Create(
extension_misc::kODFSExtensionId);
auto* service =
ash::file_system_provider::Service::Get(browser()->profile());
service->RegisterProvider(std::move(fake_provider));
files_ = file_manager::test::CopyTestFilesIntoMyFiles(browser()->profile(),
{"text.docx"});
}
protected:
std::vector<storage::FileSystemURL> files_;
private:
base::test::ScopedFeatureList feature_list_;
};
// Test which launches an `OfficeFallbackDialog` which in turn creates an
// `OfficeFallbackElement`. Tests that the correct title and reason is displayed
// when the fallback reason is that the system is offline.
IN_PROC_BROWSER_TEST_F(OfficeFallbackDialogBrowserTest,
OfficeFallbackDialogWhenOffline) {
// Launch Office Fallback dialog.
const std::string application_name = "Test app name";
content::WebContents* web_contents =
LaunchOfficeFallbackDialogAndGetWebContentsForDialog(
files_, FallbackReason::kOffline, application_name,
base::DoNothing());
content::EvalJsResult eval_result_title =
content::EvalJs(web_contents,
"document.querySelector('office-fallback')"
".$('#title').innerText");
EXPECT_EQ(eval_result_title.ExtractString(),
l10n_util::GetStringFUTF8(
IDS_OFFICE_FALLBACK_TITLE_OFFLINE,
files_.front().path().BaseName().LossyDisplayName()));
content::EvalJsResult eval_result_reason =
content::EvalJs(web_contents,
"document.querySelector('office-fallback')"
".$('#reason-message').innerText");
EXPECT_EQ(eval_result_reason.ExtractString(),
l10n_util::GetStringFUTF8(IDS_OFFICE_FALLBACK_REASON_OFFLINE,
base::UTF8ToUTF16(application_name)));
}
// Test which launches an `OfficeFallbackDialog` which in turn creates an
// `OfficeFallbackElement`. Tests that the correct title is displayed when the
// fallback reason is that Drive authentication is not ready.
IN_PROC_BROWSER_TEST_F(OfficeFallbackDialogBrowserTest,
OfficeFallbackDialogWhenDriveAuthenticationNotReady) {
// Launch Office Fallback dialog.
content::WebContents* web_contents =
LaunchOfficeFallbackDialogAndGetWebContentsForDialog(
files_, FallbackReason::kDriveAuthenticationNotReady, kTaskTitle,
base::DoNothing());
content::EvalJsResult eval_result =
content::EvalJs(web_contents,
"document.querySelector('office-fallback')"
".$('#title').innerText");
EXPECT_EQ(eval_result.ExtractString(),
l10n_util::GetStringFUTF8(
IDS_OFFICE_FALLBACK_TITLE_OFFLINE,
files_.front().path().BaseName().LossyDisplayName()));
}
// Test which launches an `OfficeFallbackDialog` which in turn creates an
// `OfficeFallbackElement`. Tests that the correct instructions are displayed
// when the fallback reason is that the disable Drive preference is set.
IN_PROC_BROWSER_TEST_F(OfficeFallbackDialogBrowserTest,
OfficeFallbackDialogWhenDisableDrivePreferenceSet) {
// Launch Office Fallback dialog.
content::WebContents* web_contents =
LaunchOfficeFallbackDialogAndGetWebContentsForDialog(
files_, FallbackReason::kDisableDrivePreferenceSet, kTaskTitle,
base::DoNothing());
content::EvalJsResult eval_result =
content::EvalJs(web_contents,
"document.querySelector('office-fallback')"
".$('#instructions-message').innerText");
EXPECT_EQ(eval_result.ExtractString(),
l10n_util::GetStringUTF8(
IDS_OFFICE_FALLBACK_INSTRUCTIONS_DISABLE_DRIVE_PREFERENCE));
}
// Test which launches an `OfficeFallbackDialog` which in turn creates an
// `OfficeFallbackElement`. Tests that the correct reason and instructions are
// displayed when the fallback reason is that Drive is unavailable for the
// account type.
IN_PROC_BROWSER_TEST_F(OfficeFallbackDialogBrowserTest,
OfficeFallbackDialogWhenDriveDisabledForAccountType) {
// Launch Office Fallback dialog.
content::WebContents* web_contents =
LaunchOfficeFallbackDialogAndGetWebContentsForDialog(
files_, FallbackReason::kDriveDisabledForAccountType, kTaskTitle,
base::DoNothing());
content::EvalJsResult eval_result_instructions =
content::EvalJs(web_contents,
"document.querySelector('office-fallback')"
".$('#instructions-message').innerText");
EXPECT_EQ(eval_result_instructions.ExtractString(),
l10n_util::GetStringUTF8(
IDS_OFFICE_FALLBACK_INSTRUCTIONS_DRIVE_DISABLED_FOR_ACCOUNT));
content::EvalJsResult eval_result_reason =
content::EvalJs(web_contents,
"document.querySelector('office-fallback')"
".$('#reason-message').innerText");
EXPECT_EQ(eval_result_reason.ExtractString(),
l10n_util::GetStringUTF8(
IDS_OFFICE_FALLBACK_REASON_DRIVE_DISABLED_FOR_ACCOUNT));
}
// Test which launches an `OfficeFallbackDialog` which in turn creates an
// `OfficeFallbackElement`. Tests that the correct instructions are displayed
// when the fallback reason is that Drive has not service.
IN_PROC_BROWSER_TEST_F(OfficeFallbackDialogBrowserTest,
OfficeFallbackDialogWhenNoDriveService) {
// Launch Office Fallback dialog.
content::WebContents* web_contents =
LaunchOfficeFallbackDialogAndGetWebContentsForDialog(
files_, FallbackReason::kNoDriveService, kTaskTitle,
base::DoNothing());
content::EvalJsResult eval_result =
content::EvalJs(web_contents,
"document.querySelector('office-fallback')"
".$('#instructions-message').innerText");
EXPECT_EQ(eval_result.ExtractString(),
l10n_util::GetStringUTF8(IDS_OFFICE_FALLBACK_INSTRUCTIONS));
}
// Test which launches an `OfficeFallbackDialog` which in turn creates an
// `OfficeFallbackElement`. Tests that the correct instructions are displayed
// when the fallback reason is that the file is still waiting to be uploaded,
// and that the correct user choice is received after clicking the OK button.
IN_PROC_BROWSER_TEST_F(OfficeFallbackDialogBrowserTest,
OfficeFallbackDialogWhenWaitingForUpload) {
base::RunLoop run_loop;
// Launch Office Fallback dialog.
content::WebContents* web_contents =
LaunchOfficeFallbackDialogAndGetWebContentsForDialog(
files_, FallbackReason::kWaitingForUpload, kTaskTitle,
base::BindLambdaForTesting(
[&run_loop](std::optional<const std::string> choice) {
// Expect the dialog is closed with the "cancel" user choice.
if (choice.has_value() &&
choice.value() == ash::office_fallback::kDialogChoiceOk) {
run_loop.Quit();
}
}));
// Check the displayed instruction.
content::EvalJsResult eval_result =
content::EvalJs(web_contents,
"document.querySelector('office-fallback')"
".$('#instructions-message').innerText");
EXPECT_EQ(eval_result.ExtractString(),
l10n_util::GetStringUTF8(
IDS_OFFICE_FALLBACK_INSTRUCTIONS_WAITING_FOR_UPLOAD));
// Click the OK button and wait until the dialog is closed with the correct
// user choice.
EXPECT_TRUE(content::ExecJs(web_contents,
"document.querySelector('office-fallback')"
".$('#ok-button').click()"));
run_loop.Run();
}
// Test which launches an `OfficeFallbackDialog` which in turn creates an
// `OfficeFallbackElement`. Tests that the correct instructions are displayed
// when the fallback reason is that the file cannot be open from its current
// Android OneDrive location, and that the correct user choice is received after
// clicking the OK button.
IN_PROC_BROWSER_TEST_F(
OfficeFallbackDialogBrowserTest,
OfficeFallbackDialogWhenAndroidOneDriveLocationNotSupported) {
base::RunLoop run_loop;
// Launch Office Fallback dialog.
content::WebContents* web_contents =
LaunchOfficeFallbackDialogAndGetWebContentsForDialog(
files_, FallbackReason::kAndroidOneDriveUnsupportedLocation,
"Microsoft 365",
base::BindLambdaForTesting(
[&run_loop](std::optional<const std::string> choice) {
// Expect the dialog is closed with the "OK" user choice.
if (choice.has_value() &&
choice.value() == ash::office_fallback::kDialogChoiceOk) {
run_loop.Quit();
}
}));
// Check the displayed instruction.
content::EvalJsResult eval_result =
content::EvalJs(web_contents,
"document.querySelector('office-fallback')"
".$('#instructions-message').innerText");
EXPECT_EQ(
eval_result.ExtractString(),
l10n_util::GetStringUTF8(
IDS_OFFICE_FALLBACK_INSTRUCTIONS_ANDROID_ONE_DRIVE_LOCATION_NOT_SUPPORTED));
// Click the OK button and wait until the dialog is closed with the correct
// user choice.
EXPECT_TRUE(content::ExecJs(web_contents,
"document.querySelector('office-fallback')"
".$('#ok-button').click()"));
run_loop.Run();
}
// Test which launches an `OfficeFallbackDialog` which in turn creates an
// `OfficeFallbackElement`. Tests that the cancel button works.
IN_PROC_BROWSER_TEST_F(OfficeFallbackDialogBrowserTest, ClickCancel) {
base::RunLoop run_loop;
content::WebContents* web_contents =
LaunchOfficeFallbackDialogAndGetWebContentsForDialog(
files_, FallbackReason::kOffline, kTaskTitle,
base::BindLambdaForTesting(
[&run_loop](std::optional<const std::string> choice) {
// Expect the dialog is closed with the "cancel" user choice.
if (choice.has_value() &&
choice.value() ==
ash::office_fallback::kDialogChoiceCancel) {
run_loop.Quit();
}
}));
// Click the close button and wait until the dialog is closed with the correct
// user choice.
EXPECT_TRUE(content::ExecJs(web_contents,
"document.querySelector('office-fallback')"
".$('#cancel-button').click()"));
run_loop.Run();
}
// Test which launches an `OfficeFallbackDialog` which in turn creates an
// `OfficeFallbackElement`. Tests that the try again button works.
IN_PROC_BROWSER_TEST_F(OfficeFallbackDialogBrowserTest, ClickTryAgain) {
base::RunLoop run_loop;
content::WebContents* web_contents =
LaunchOfficeFallbackDialogAndGetWebContentsForDialog(
files_, FallbackReason::kOffline, kTaskTitle,
base::BindLambdaForTesting(
[&run_loop](std::optional<const std::string> choice) {
// Expect the dialog is closed with the "cancel" user choice.
if (choice.has_value() &&
choice.value() ==
ash::office_fallback::kDialogChoiceTryAgain) {
run_loop.Quit();
}
}));
// Click the try again button and wait until the dialog is closed with the
// correct user choice.
EXPECT_TRUE(content::ExecJs(web_contents,
"document.querySelector('office-fallback')"
".$('#try-again-button').click()"));
run_loop.Run();
}
// Test which launches an `OfficeFallbackDialog` which in turn creates an
// `OfficeFallbackElement`. Tests that the quick office button works.
IN_PROC_BROWSER_TEST_F(OfficeFallbackDialogBrowserTest, ClickQuickOffice) {
base::RunLoop run_loop;
content::WebContents* web_contents =
LaunchOfficeFallbackDialogAndGetWebContentsForDialog(
files_, FallbackReason::kOffline, kTaskTitle,
base::BindLambdaForTesting(
[&run_loop](std::optional<const std::string> choice) {
// Expect the dialog is closed with the "cancel" user choice.
if (choice.has_value() &&
choice.value() ==
ash::office_fallback::kDialogChoiceQuickOffice) {
run_loop.Quit();
}
}));
// Click the try again button and wait until the dialog is closed with the
// correct user choice.
EXPECT_TRUE(content::ExecJs(web_contents,
"document.querySelector('office-fallback')"
".$('#quick-office-button').click()"));
run_loop.Run();
}
} // namespace ash::office_fallback
|