File: xr_browser_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 (540 lines) | stat: -rw-r--r-- 20,708 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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
// Copyright 2018 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/vr/test/xr_browser_test.h"

#include <cstring>

#include "base/base_paths.h"
#include "base/command_line.h"
#include "base/debug/debugger.h"
#include "base/environment.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/path_service.h"
#include "base/strings/string_split.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/single_thread_task_runner.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/test/base/chrome_test_utils.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/content_features.h"
#include "content/public/common/content_switches.h"
#include "content/public/test/browser_test_utils.h"
#include "url/gurl.h"

#if BUILDFLAG(IS_ANDROID)
#include "base/android/path_utils.h"
#include "chrome/browser/android/tab_android.h"
#include "chrome/browser/ui/android/tab_model/tab_model.h"
#include "chrome/browser/ui/android/tab_model/tab_model_list.h"
#else
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_tabstrip.h"
#endif
namespace vr {

constexpr base::TimeDelta XrBrowserTestBase::kPollCheckIntervalShort;
constexpr base::TimeDelta XrBrowserTestBase::kPollCheckIntervalLong;
constexpr base::TimeDelta XrBrowserTestBase::kPollTimeoutShort;
constexpr base::TimeDelta XrBrowserTestBase::kPollTimeoutMedium;
constexpr base::TimeDelta XrBrowserTestBase::kPollTimeoutLong;
constexpr char XrBrowserTestBase::kOpenXrConfigPathEnvVar[];
constexpr char XrBrowserTestBase::kOpenXrConfigPathVal[];
constexpr char XrBrowserTestBase::kTestFileDir[];
constexpr char XrBrowserTestBase::kSwitchIgnoreRuntimeRequirements[];
const std::vector<std::string> XrBrowserTestBase::kRequiredTestSwitches{
#if BUILDFLAG(IS_WIN)
    "enable-gpu", "enable-pixel-output-in-tests",
    "run-through-xr-wrapper-script", "enable-unsafe-swiftshader"
#endif
};
const std::vector<std::pair<std::string, std::string>>
    XrBrowserTestBase::kRequiredTestSwitchesWithValues{
#if BUILDFLAG(IS_WIN)
        std::pair<std::string, std::string>("test-launcher-jobs", "1")
#endif
    };

XrBrowserTestBase::XrBrowserTestBase() : env_(base::Environment::Create()) {
  enable_features_.push_back(features::kLogJsConsoleMessages);
}

XrBrowserTestBase::~XrBrowserTestBase() = default;

base::FilePath::StringType UTF8ToWideIfNecessary(std::string input) {
#if BUILDFLAG(IS_WIN)
  return base::UTF8ToWide(input);
#else
  return input;
#endif  // BUILDFLAG(IS_WIN)
}

std::string WideToUTF8IfNecessary(base::FilePath::StringType input) {
#if BUILDFLAG(IS_WIN)
  return base::WideToUTF8(input);
#else
  return input;
#endif  // BUILDFLAG(IS_WIN)
}

// Returns an std::string consisting of the given path relative to the test
// executable's path, e.g. if the executable is in out/Debug and the given path
// is "test", the returned string should be out/Debug/test.
std::string MakeExecutableRelative(const char* path) {
  base::FilePath executable_path;
#if BUILDFLAG(IS_ANDROID)
  NOTREACHED();
#else
  EXPECT_TRUE(
      base::PathService::Get(base::BasePathKey::FILE_EXE, &executable_path));
  executable_path = executable_path.DirName();
  // We need an std::string that is an absolute file path, which requires
  // platform-specific logic since Windows uses std::wstring instead of
  // std::string for FilePaths, but SetVar only accepts std::string.
  return WideToUTF8IfNecessary(
      base::MakeAbsoluteFilePath(
          executable_path.Append(base::FilePath(UTF8ToWideIfNecessary(path))))
          .value());
#endif
}

void XrBrowserTestBase::SetUp() {
  // Check whether the required flags were passed to the test - without these,
  // we can fail in ways that are non-obvious, so fail more explicitly here if
  // they aren't present.
  auto* cmd_line = base::CommandLine::ForCurrentProcess();
  for (auto req_switch : kRequiredTestSwitches) {
    ASSERT_TRUE(cmd_line->HasSwitch(req_switch))
        << "Missing switch " << req_switch << " required to run tests properly";
  }
  for (auto req_switch_pair : kRequiredTestSwitchesWithValues) {
    ASSERT_TRUE(cmd_line->HasSwitch(req_switch_pair.first))
        << "Missing switch " << req_switch_pair.first
        << " required to run tests properly";
    ASSERT_TRUE(cmd_line->GetSwitchValueASCII(req_switch_pair.first) ==
                req_switch_pair.second)
        << "Have required switch " << req_switch_pair.first
        << ", but not required value " << req_switch_pair.second;
  }

  // Get the set of runtime requirements to ignore.
  if (cmd_line->HasSwitch(kSwitchIgnoreRuntimeRequirements)) {
    auto reqs = cmd_line->GetSwitchValueASCII(kSwitchIgnoreRuntimeRequirements);
    if (reqs != "") {
      for (auto req : base::SplitString(
               reqs, ",", base::WhitespaceHandling::TRIM_WHITESPACE,
               base::SplitResult::SPLIT_WANT_NONEMPTY)) {
        ignored_requirements_.insert(req);
      }
    }
  }

  // Check whether we meet all runtime requirements for this test.
  XR_CONDITIONAL_SKIP_PRETEST(runtime_requirements_, ignored_requirements_,
                              &test_skipped_at_startup_)

  // OpenXr on Android cannot use the environment variable as the loader cannot
  // read it on Android at present.
#if !BUILDFLAG(IS_ANDROID)
  // Set the environment variable to use the mock OpenXR client.
  // If the kOpenXrConfigPathEnvVar environment variable is set, the OpenXR
  // loader will look for the OpenXR runtime specified in that json file. The
  // json file contains the path to the runtime, relative to the json file
  // itself. Otherwise, the OpenXR loader loads the active OpenXR runtime
  // installed on the system, which is specified by a registry key.
  ASSERT_TRUE(env_->SetVar(kOpenXrConfigPathEnvVar,
                           MakeExecutableRelative(kOpenXrConfigPathVal)))
      << "Failed to set OpenXR JSON location environment variable";
#endif

  // Set any command line flags that subclasses have set, e.g. enabling features
  // or specific runtimes.
  if (forced_runtime_.has_value() && !forced_runtime_->empty()) {
    cmd_line->AppendSwitchASCII(switches::kWebXrForceRuntime,
                                forced_runtime_.value());
  }

#if defined(MEMORY_SANITIZER)
  // Surprisingly enough, there is no constant for this.
  // TODO(crbug.com/40564748): Once generic wrapper scripts for tests are
  // supported, move the logic to avoid passing --enable-gpu to GN.
  if (cmd_line->HasSwitch("enable-gpu")) {
    LOG(WARNING) << "Ignoring --enable-gpu switch, which is incompatible with "
                    "MSan builds.";
    cmd_line->RemoveSwitch("enable-gpu");
  }
#endif

  scoped_feature_list_.InitWithFeatures(enable_features_, disable_features_);

  PlatformBrowserTest::SetUp();
}

void XrBrowserTestBase::TearDown() {
  if (test_skipped_at_startup_) {
    // Since we didn't complete startup, no need to do teardown, either. Doing
    // so can result in hitting a DCHECK.
    return;
  }
  PlatformBrowserTest::TearDown();
}

XrBrowserTestBase::RuntimeType XrBrowserTestBase::GetRuntimeType() const {
  return XrBrowserTestBase::RuntimeType::RUNTIME_NONE;
}

GURL XrBrowserTestBase::GetUrlForFile(const std::string& test_name) {
  // GetURL requires that the path start with /.
  return GetEmbeddedServer()->GetURL(std::string("/") + kTestFileDir +
                                     test_name + ".html");
}

net::EmbeddedTestServer* XrBrowserTestBase::GetEmbeddedServer() {
  if (server_ == nullptr) {
    server_ = std::make_unique<net::EmbeddedTestServer>(
        net::EmbeddedTestServer::Type::TYPE_HTTPS);
    // We need to serve from the root in order for the inclusion of the
    // test harness from //third_party to work.
    server_->ServeFilesFromSourceDirectory(".");
    EXPECT_TRUE(server_->Start()) << "Failed to start embedded test server";
  }
  return server_.get();
}

content::WebContents* XrBrowserTestBase::GetCurrentWebContents() {
#if !BUILDFLAG(IS_ANDROID)
  // `chrome_test_utils::GetActiveWebContents()` doesn't properly account for
  // the presence of an incognito browser, and only looks in the browser
  // returned by the base class, which doesn't get overridden by the incognito
  // browser.
  if (incognito_) {
    Browser* incognito_browser = chrome::FindTabbedBrowser(
        browser()->profile()->GetPrimaryOTRProfile(/*create_if_needed=*/false),
        /*match_original_profiles=*/false);
    return incognito_browser->tab_strip_model()->GetActiveWebContents();
  }
#endif
  return chrome_test_utils::GetActiveWebContents(this);
}

void XrBrowserTestBase::SetIncognito() {
  incognito_ = true;
  OpenNewTab(url::kAboutBlankURL);
}

void XrBrowserTestBase::OpenNewTab(const std::string& url) {
  OpenNewTab(url, incognito_);
}

void XrBrowserTestBase::OpenNewTab(const std::string& url, bool incognito) {
#if BUILDFLAG(IS_ANDROID)
  auto* profile = chrome_test_utils::GetProfile(this);
  if (incognito) {
    profile = profile->GetPrimaryOTRProfile(/*create_if_needed=*/true);
  }

  TabModel* tab_model =
      TabModelList::GetTabModelForWebContents(GetCurrentWebContents());
  TabAndroid* first_tab = TabAndroid::FromWebContents(GetCurrentWebContents());
  std::unique_ptr<content::WebContents> contents =
      content::WebContents::Create(content::WebContents::CreateParams(profile));
  EXPECT_TRUE(content::NavigateToURL(contents.get(), GURL(url)));
  // TabModel takes ownership of the WebContents, so we release it here.
  tab_model->CreateTab(first_tab, contents.release(), true);
#else
  if (incognito) {
    OpenURLOffTheRecord(browser()->profile(), GURL(url));
  } else {
    // -1 is a special index value used to append to the end of the tab list.
    chrome::AddTabAt(browser(), GURL(url), /*index=*/-1, /*foreground=*/true);
  }
#endif
}

void XrBrowserTestBase::LoadFileAndAwaitInitialization(
    const std::string& test_name) {
  OnBeforeLoadFile();
  GURL url = GetUrlForFile(test_name);
  ASSERT_TRUE(content::NavigateToURL(GetCurrentWebContents(), url));
  ASSERT_TRUE(PollJavaScriptBoolean("isInitializationComplete()",
                                    kPollTimeoutMedium,
                                    GetCurrentWebContents()))
      << "Timed out waiting for JavaScript test initialization.";

#if BUILDFLAG(IS_WIN)
  // Now that the browser is opened and has focus, keep track of this window so
  // that we can restore the proper focus after entering each session. This is
  // required for tests that create multiple sessions to work properly.
  hwnd_ = GetForegroundWindow();
#endif
}

void XrBrowserTestBase::RunJavaScriptOrFail(
    const std::string& js_expression,
    content::WebContents* web_contents) {
  if (javascript_failed_) {
    LogJavaScriptFailure();
    return;
  }

  ASSERT_TRUE(content::ExecJs(web_contents, js_expression))
      << "Failed to run given JavaScript: " << js_expression;
}

bool XrBrowserTestBase::RunJavaScriptAndExtractBoolOrFail(
    const std::string& js_expression,
    content::WebContents* web_contents) {
  if (javascript_failed_) {
    LogJavaScriptFailure();
    return false;
  }

  DLOG(INFO) << "Run JavaScript: " << js_expression;
  return content::EvalJs(web_contents, js_expression).ExtractBool();
}

std::string XrBrowserTestBase::RunJavaScriptAndExtractStringOrFail(
    const std::string& js_expression,
    content::WebContents* web_contents) {
  if (javascript_failed_) {
    LogJavaScriptFailure();
    return "";
  }

  return content::EvalJs(web_contents, js_expression).ExtractString();
}

bool XrBrowserTestBase::PollJavaScriptBoolean(
    const std::string& bool_expression,
    const base::TimeDelta& timeout,
    content::WebContents* web_contents) {
  bool result = false;
  base::RunLoop wait_loop(base::RunLoop::Type::kNestableTasksAllowed);
  // Lambda used because otherwise BindRepeating gets confused about which
  // version of RunJavaScriptAndExtractBoolOrFail to use.
  BlockOnCondition(base::BindRepeating(
                       [](XrBrowserTestBase* base, std::string expression,
                          content::WebContents* contents) {
                         return base->RunJavaScriptAndExtractBoolOrFail(
                             expression, contents);
                       },
                       this, bool_expression, web_contents),
                   &result, &wait_loop, base::Time::Now(), timeout);
  wait_loop.Run();
  return result;
}

void XrBrowserTestBase::PollJavaScriptBooleanOrFail(
    const std::string& bool_expression,
    const base::TimeDelta& timeout,
    content::WebContents* web_contents) {
  ASSERT_TRUE(PollJavaScriptBoolean(bool_expression, timeout, web_contents))
      << "Timed out polling JavaScript boolean expression: " << bool_expression;
}

void XrBrowserTestBase::BlockOnCondition(
    base::RepeatingCallback<bool()> condition,
    bool* result,
    base::RunLoop* wait_loop,
    const base::Time& start_time,
    const base::TimeDelta& timeout,
    const base::TimeDelta& period) {
  if (!*result) {
    *result = condition.Run();
  }

  if (*result) {
    if (wait_loop->running()) {
      wait_loop->Quit();
      return;
    }
    // In the case where the condition is met fast enough that the given
    // RunLoop hasn't started yet, spin until it's available.
    base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
        FROM_HERE,
        base::BindOnce(&XrBrowserTestBase::BlockOnCondition,
                       base::Unretained(this), std::move(condition),
                       base::Unretained(result), base::Unretained(wait_loop),
                       start_time, timeout, period));
    return;
  }

  if (base::Time::Now() - start_time > timeout &&
      !base::debug::BeingDebugged()) {
    wait_loop->Quit();
    return;
  }

  base::SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
      FROM_HERE,
      base::BindOnce(&XrBrowserTestBase::BlockOnCondition,
                     base::Unretained(this), std::move(condition),
                     base::Unretained(result), base::Unretained(wait_loop),
                     start_time, timeout, period),
      period);
}

void XrBrowserTestBase::WaitOnJavaScriptStep(
    content::WebContents* web_contents) {
  // Make sure we aren't trying to wait on a JavaScript test step without the
  // code to do so.
  bool code_available = RunJavaScriptAndExtractBoolOrFail(
      "typeof javascriptDone !== 'undefined'", web_contents);
  ASSERT_TRUE(code_available) << "Attempted to wait on a JavaScript test step "
                              << "without the code to do so. You either forgot "
                              << "to import webxr_e2e.js or "
                              << "are incorrectly using a C++ function.";

  // Actually wait for the step to finish.
  bool success =
      PollJavaScriptBoolean("javascriptDone", kPollTimeoutLong, web_contents);

  // Check what state we're in to make sure javascriptDone wasn't called
  // because the test failed.
  XrBrowserTestBase::TestStatus test_status = CheckTestStatus(web_contents);
  if (!success || test_status == XrBrowserTestBase::TestStatus::STATUS_FAILED) {
    // Failure states: Either polling failed or polling succeeded, but because
    // the test failed.
    std::string reason;
    if (!success) {
      reason = "Timed out waiting for JavaScript step to finish.";
    } else {
      reason =
          "JavaScript testharness reported failure while waiting for "
          "JavaScript step to finish";
    }

    std::string result_string =
        RunJavaScriptAndExtractStringOrFail("resultString", web_contents);
    if (result_string.empty()) {
      reason +=
          " Did not obtain specific failure reason from JavaScript "
          "testharness.";
    } else {
      reason +=
          " JavaScript testharness reported failure reason: " + result_string;
    }
    // Store that we've failed waiting for a JavaScript step so we can abort
    // further attempts to run JavaScript, which has the potential to do weird
    // things and produce non-useful output due to JavaScript code continuing
    // to run when it's in a known bad state.
    // This is a workaround for the fact that FAIL() and other gtest macros that
    // cause test failures only abort the current function. Thus, a failure here
    // will show up as a test failure, but there's nothing that actually stops
    // the test from continuing to run since FAIL() is not being called in the
    // main test body.
    javascript_failed_ = true;
    // Newlines to help the failure reason stick out.
    LOG(ERROR) << "\n\n\nvvvvvvvvvvvvvvvvv Useful Stack vvvvvvvvvvvvvvvvv\n\n";
    FAIL() << reason;
  }

  // Reset the synchronization boolean.
  RunJavaScriptOrFail("javascriptDone = false", web_contents);
}

void XrBrowserTestBase::ExecuteStepAndWait(const std::string& step_function,
                                           content::WebContents* web_contents) {
  RunJavaScriptOrFail(step_function, web_contents);
  WaitOnJavaScriptStep(web_contents);
}

XrBrowserTestBase::TestStatus XrBrowserTestBase::CheckTestStatus(
    content::WebContents* web_contents) {
  std::string result_string =
      RunJavaScriptAndExtractStringOrFail("resultString", web_contents);
  bool test_passed =
      RunJavaScriptAndExtractBoolOrFail("testPassed", web_contents);
  if (test_passed) {
    return XrBrowserTestBase::TestStatus::STATUS_PASSED;
  } else if (!test_passed && result_string.empty()) {
    return XrBrowserTestBase::TestStatus::STATUS_RUNNING;
  }
  // !test_passed && result_string != ""
  return XrBrowserTestBase::TestStatus::STATUS_FAILED;
}

void XrBrowserTestBase::EndTest(content::WebContents* web_contents) {
  switch (CheckTestStatus(web_contents)) {
    case XrBrowserTestBase::TestStatus::STATUS_PASSED:
      break;
    case XrBrowserTestBase::TestStatus::STATUS_FAILED:
      FAIL() << "JavaScript testharness failed with reason: "
             << RunJavaScriptAndExtractStringOrFail("resultString",
                                                    web_contents);
    case XrBrowserTestBase::TestStatus::STATUS_RUNNING:
      FAIL() << "Attempted to end test in C++ without finishing in JavaScript.";
    default:
      FAIL() << "Received unknown test status.";
  }
}

void XrBrowserTestBase::AssertNoJavaScriptErrors(
    content::WebContents* web_contents) {
  if (CheckTestStatus(web_contents) ==
      XrBrowserTestBase::TestStatus::STATUS_FAILED) {
    FAIL() << "JavaScript testharness failed with reason: "
           << RunJavaScriptAndExtractStringOrFail("resultString", web_contents);
  }
}

void XrBrowserTestBase::RunJavaScriptOrFail(const std::string& js_expression) {
  RunJavaScriptOrFail(js_expression, GetCurrentWebContents());
}

bool XrBrowserTestBase::RunJavaScriptAndExtractBoolOrFail(
    const std::string& js_expression) {
  return RunJavaScriptAndExtractBoolOrFail(js_expression,
                                           GetCurrentWebContents());
}

std::string XrBrowserTestBase::RunJavaScriptAndExtractStringOrFail(
    const std::string& js_expression) {
  return RunJavaScriptAndExtractStringOrFail(js_expression,
                                             GetCurrentWebContents());
}

bool XrBrowserTestBase::PollJavaScriptBoolean(
    const std::string& bool_expression,
    const base::TimeDelta& timeout) {
  return PollJavaScriptBoolean(bool_expression, timeout,
                               GetCurrentWebContents());
}

void XrBrowserTestBase::PollJavaScriptBooleanOrFail(
    const std::string& bool_expression,
    const base::TimeDelta& timeout) {
  PollJavaScriptBooleanOrFail(bool_expression, timeout,
                              GetCurrentWebContents());
}

void XrBrowserTestBase::WaitOnJavaScriptStep() {
  WaitOnJavaScriptStep(GetCurrentWebContents());
}

void XrBrowserTestBase::ExecuteStepAndWait(const std::string& step_function) {
  ExecuteStepAndWait(step_function, GetCurrentWebContents());
}

void XrBrowserTestBase::EndTest() {
  EndTest(GetCurrentWebContents());
}

void XrBrowserTestBase::AssertNoJavaScriptErrors() {
  AssertNoJavaScriptErrors(GetCurrentWebContents());
}

void XrBrowserTestBase::LogJavaScriptFailure() {
  LOG(ERROR) << "HEY! LISTEN! Not running requested JavaScript due to previous "
                "failure. Failures below this are likely garbage. Look for the "
                "useful stack above.";
}

}  // namespace vr