Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / chrome / test / base / interactive_test_utils_win.cc
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/test/base/interactive_test_utils.h"
6
7 #include <Psapi.h>
8
9 #include "base/files/file_path.h"
10 #include "base/logging.h"
11 #include "base/strings/stringprintf.h"
12 #include "base/time/time.h"
13 #include "chrome/browser/ui/host_desktop.h"
14 #include "chrome/test/base/interactive_test_utils_aura.h"
15 #include "ui/aura/window_tree_host.h"
16 #include "ui/base/test/ui_controls.h"
17 #include "ui/base/win/foreground_helper.h"
18 #include "ui/views/focus/focus_manager.h"
19
20 namespace ui_test_utils {
21
22 void HideNativeWindow(gfx::NativeWindow window) {
23   if (chrome::GetHostDesktopTypeForNativeWindow(window) ==
24       chrome::HOST_DESKTOP_TYPE_ASH) {
25     HideNativeWindowAura(window);
26     return;
27   }
28   HWND hwnd = window->GetHost()->GetAcceleratedWidget();
29   ::ShowWindow(hwnd, SW_HIDE);
30 }
31
32 bool ShowAndFocusNativeWindow(gfx::NativeWindow window) {
33   if (chrome::GetHostDesktopTypeForNativeWindow(window) ==
34       chrome::HOST_DESKTOP_TYPE_ASH)
35     ShowAndFocusNativeWindowAura(window);
36   window->Show();
37   // Always make sure the window hosting ash is visible and focused.
38   HWND hwnd = window->GetHost()->GetAcceleratedWidget();
39
40   ::ShowWindow(hwnd, SW_SHOW);
41
42   if (GetForegroundWindow() != hwnd) {
43     VLOG(1) << "Forcefully refocusing front window";
44     ui::ForegroundHelper::SetForeground(hwnd);
45   }
46
47   // ShowWindow does not necessarily activate the window. In particular if a
48   // window from another app is the foreground window then the request to
49   // activate the window fails. See SetForegroundWindow for details.
50   HWND foreground_window = GetForegroundWindow();
51   if (foreground_window == hwnd)
52     return true;
53
54   wchar_t window_title[256];
55   std::wstring path_str;
56   if (foreground_window) {
57     DWORD process_id = 0;
58     GetWindowThreadProcessId(foreground_window, &process_id);
59     HANDLE process_handle = OpenProcess(
60         PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, process_id);
61     if (process_handle) {
62       wchar_t path[MAX_PATH];
63       if (GetModuleFileNameEx(process_handle, NULL, path, MAX_PATH))
64         path_str = path;
65       CloseHandle(process_handle);
66     }
67   }
68   GetWindowText(foreground_window, window_title, arraysize(window_title));
69   LOG(ERROR) << "ShowAndFocusNativeWindow failed. foreground window: "
70              << foreground_window << ", title: " << window_title << ", path: "
71              << path_str;
72   return false;
73 }
74
75 }  // namespace ui_test_utils