File: interactive_test_utils_win.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 (153 lines) | stat: -rw-r--r-- 5,394 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
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#include "chrome/test/base/interactive_test_utils.h"

#include <windows.h>

#include <Psapi.h>

#include <memory>

#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/process/process_handle.h"
#include "base/test/test_timeouts.h"
#include "base/threading/platform_thread.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "chrome/test/base/interactive_test_utils_aura.h"
#include "chrome/test/base/process_lineage_win.h"
#include "chrome/test/base/save_desktop_snapshot.h"
#include "chrome/test/base/window_contents_as_string_win.h"
#include "ui/aura/window_tree_host.h"
#include "ui/base/test/ui_controls.h"
#include "ui/base/win/foreground_helper.h"
#include "ui/views/focus/focus_manager.h"

namespace ui_test_utils {

void HideNativeWindow(gfx::NativeWindow window) {
  HWND hwnd = window->GetHost()->GetAcceleratedWidget();
  ::ShowWindow(hwnd, SW_HIDE);
}

bool ShowAndFocusNativeWindow(gfx::NativeWindow window) {
  window->Show();
  // Always make sure the window is visible and focused.
  HWND hwnd = window->GetHost()->GetAcceleratedWidget();

  ::ShowWindow(hwnd, SW_SHOW);

  int attempts_left = 5;
  bool have_snapshot = false;
  while (true) {
    if (::GetForegroundWindow() == hwnd)
      return true;

    VLOG(1) << "Forcefully refocusing front window";
    ui::ForegroundHelper::SetForeground(hwnd);

    // ShowWindow does not necessarily activate the window. In particular if a
    // window from another app is the foreground window then the request to
    // activate the window fails. See SetForegroundWindow for details.
    HWND foreground_window = ::GetForegroundWindow();
    if (foreground_window == hwnd)
      return true;

    // Emit some diagnostic information about the foreground window and its
    // owning process.
    wchar_t window_title[256];
    GetWindowText(foreground_window, window_title, std::size(window_title));

    std::wstring lineage_str;
    std::wstring window_contents;
    DWORD foreground_process_id = 0;
    if (foreground_window) {
      GetWindowThreadProcessId(foreground_window, &foreground_process_id);
      ProcessLineage lineage = ProcessLineage::Create(foreground_process_id);
      if (!lineage.IsEmpty()) {
        lineage_str = L", process lineage: ";
        lineage_str.append(lineage.ToString());
      }

      window_contents = WindowContentsAsString(foreground_window);
    }
    LOG(ERROR) << "ShowAndFocusNativeWindow found a foreground window: "
               << foreground_window << ", title: " << window_title
               << lineage_str << ", contents:" << std::endl
               << window_contents;

    // Take a snapshot of the screen.
    const base::FilePath output_dir =
        base::CommandLine::ForCurrentProcess()->GetSwitchValuePath(
            kSnapshotOutputDir);
    if (!have_snapshot && !output_dir.empty()) {
      base::FilePath snapshot_file = SaveDesktopSnapshot(output_dir);
      if (!snapshot_file.empty()) {
        have_snapshot = true;
        LOG(ERROR) << "Screenshot saved to file: \"" << snapshot_file.value()
                   << "\"";
      }
    }

    if (!attempts_left--) {
      LOG(ERROR) << "ShowAndFocusNativeWindow failed after too many attempts.";
      break;
    }

    // Give up if there is no foreground window or if it's mysteriously from
    // this process.
    if (!foreground_window) {
      LOG(ERROR) << "ShowAndFocusNativeWindow failed to focus any window.";
      break;
    }

    if (foreground_process_id == base::GetCurrentProcId()) {
      LOG(ERROR) << "ShowAndFocusNativeWindow failed because another window in"
                    " the test process will not give up focus.";
      break;
    }

    // Attempt to close the offending window.
    ::PostMessageW(foreground_window, WM_CLOSE, 0, 0);
    // Poll to wait for the window to be destroyed. While it is possible to
    // avoid polling via use of UI Automation to observe the closing of the
    // window, the code to do so is non-trivial and requires use of another
    // thread in the MTA.
    base::RunLoop run_loop;
    base::RepeatingTimer timer(
        FROM_HERE, TestTimeouts::tiny_timeout(),
        base::BindRepeating(
            [](HWND foreground_window,
               const base::RepeatingClosure& quit_closure, int* polls) {
              if (!*polls-- || ::GetForegroundWindow() != foreground_window)
                quit_closure.Run();
            },
            foreground_window, run_loop.QuitClosure(),
            base::Owned(std::make_unique<int>(
                TestTimeouts::action_timeout().InMicroseconds() /
                TestTimeouts::tiny_timeout().InMicroseconds()))));
    timer.Reset();
    run_loop.Run();
    if (::GetForegroundWindow() == foreground_window) {
      LOG(ERROR) << "ShowAndFocusNativeWindow timed out closing the "
                    "foreground window.";
      break;
    }
    // Otherwise, loop around and try focusing the desired window again.
  }

  return false;
}

}  // namespace ui_test_utils