File: web_handwriting_integration_test.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (103 lines) | stat: -rw-r--r-- 3,780 bytes parent folder | download | duplicates (5)
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
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <vector>

#include "base/files/file_path.h"
#include "base/strings/string_split.h"
#include "base/system/sys_info.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/test/base/chromeos/crosier/annotations.h"
#include "chrome/test/base/chromeos/crosier/chromeos_integration_test_mixin.h"
#include "chrome/test/base/mixin_based_in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "chromeos/services/machine_learning/public/cpp/ml_switches.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "url/gurl.h"

namespace {

class WebHandwritingIntegrationTest : public MixinBasedInProcessBrowserTest {
 public:
  WebHandwritingIntegrationTest()
      : supports_ondevice_handwriting_(crosier::HasRequirement(
            crosier::Requirement::kOndeviceHandwriting)) {}

  void SetUpCommandLine(base::CommandLine* command_line) override {
    MixinBasedInProcessBrowserTest::SetUpCommandLine(command_line);
    if (supports_ondevice_handwriting_) {
      command_line->AppendSwitchASCII(::switches::kOndeviceHandwritingSwitch,
                                      "use_rootfs");
    }
  }

  void TearDownOnMainThread() override {
    // Close the browser otherwise the test may hang on shutdown.
    browser()->window()->Close();
    MixinBasedInProcessBrowserTest::TearDownOnMainThread();
  }

  ChromeOSIntegrationTestMixin chromeos_mixin_{&mixin_host_};

  // Whether this board supports on-device handwriting.
  bool supports_ondevice_handwriting_ = false;
};

IN_PROC_BROWSER_TEST_F(WebHandwritingIntegrationTest, Recognition) {
  // The full board name may have the form "glimmer-signed-mp-v4keys" and we
  // just want "glimmer".
  std::vector<std::string> board =
      base::SplitString(base::SysInfo::GetLsbReleaseBoard(), "-",
                        base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
  if (board.empty()) {
    LOG(ERROR) << "Unable to determine LSB release board";
    GTEST_SKIP();
  }
  // TODO(b/342174514): Fails on jacuzzi.
  if (board[0] == "jacuzzi") {
    GTEST_SKIP();
  }

  // Navigate to the appropriate test page, based on whether handwriting
  // recognition is supported or not.
  const char* test_file =
      supports_ondevice_handwriting_
          ? "web_handwriting_recognition.html"
          : "web_handwriting_recognition_not_supported.html";
  GURL url = ui_test_utils::GetTestUrl(
      base::FilePath("chromeos/web_handwriting/"), base::FilePath(test_file));
  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));

  content::WebContents* web_contents =
      browser()->tab_strip_model()->GetActiveWebContents();

  // Wait for JS object window.resultPromise to exist in the test page.
  bool object_exists = false;
  int iterations = 0;
  while (iterations < 10 && !object_exists) {
    object_exists = content::EvalJs(web_contents, "'resultPromise' in window")
                        .ExtractBool();
    if (object_exists) {
      break;
    }
    iterations++;
    // Sleep for a short time.
    base::RunLoop run_loop;
    base::SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
        FROM_HERE, run_loop.QuitClosure(), base::Milliseconds(100));
    run_loop.Run();
  }
  EXPECT_TRUE(object_exists);

  // The promise should have evaluated without errors. See the HTML files for
  // details.
  std::string result_error =
      content::EvalJs(web_contents, "window.resultPromise").error;
  EXPECT_TRUE(result_error.empty()) << result_error;
}

}  // namespace