This source file includes following definitions.
- SendNativeKeyEventToXDisplay
- SendNativeCommandShift
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
#include "chrome/browser/extensions/extension_apitest.h"
#include "chrome/browser/extensions/window_controller.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/test/base/interactive_test_utils.h"
#include "content/public/test/browser_test_utils.h"
#include "ui/base/base_window.h"
#include "ui/base/test/ui_controls.h"
#if defined(OS_LINUX)
#include <X11/Xlib.h>
#include <X11/extensions/XTest.h>
#include <X11/keysym.h>
#include "ui/events/keycodes/keyboard_code_conversion_x.h"
#include "ui/gfx/x/x11_types.h"
#endif
#if defined(OS_MACOSX)
#include <Carbon/Carbon.h>
#include "base/mac/scoped_cftyperef.h"
#endif
namespace extensions {
typedef ExtensionApiTest GlobalCommandsApiTest;
#if defined(OS_LINUX)
void SendNativeKeyEventToXDisplay(ui::KeyboardCode key,
                                  bool control,
                                  bool shift,
                                  bool alt) {
  Display* display = gfx::GetXDisplay();
  KeyCode ctrl_key_code = XKeysymToKeycode(display, XK_Control_L);
  KeyCode shift_key_code = XKeysymToKeycode(display, XK_Shift_L);
  KeyCode alt_key_code = XKeysymToKeycode(display, XK_Alt_L);
  
  
  
  XTestFakeKeyEvent(display, ctrl_key_code, False, CurrentTime);
  XTestFakeKeyEvent(display, shift_key_code, False, CurrentTime);
  XTestFakeKeyEvent(display, alt_key_code, False, CurrentTime);
  typedef std::vector<KeyCode> KeyCodes;
  KeyCodes key_codes;
  if (control)
    key_codes.push_back(ctrl_key_code);
  if (shift)
    key_codes.push_back(shift_key_code);
  if (alt)
    key_codes.push_back(alt_key_code);
  key_codes.push_back(XKeysymToKeycode(display,
                                       XKeysymForWindowsKeyCode(key, false)));
  
  for (KeyCodes::iterator it = key_codes.begin(); it != key_codes.end(); it++)
    XTestFakeKeyEvent(display, *it, True, CurrentTime);
  
  for (KeyCodes::iterator it = key_codes.begin(); it != key_codes.end(); it++)
    XTestFakeKeyEvent(display, *it, False, CurrentTime);
  XFlush(display);
}
#endif  
#if defined(OS_MACOSX)
using base::ScopedCFTypeRef;
void SendNativeCommandShift(int key_code) {
  CGEventSourceRef event_source =
      CGEventSourceCreate(kCGEventSourceStateHIDSystemState);
  CGEventTapLocation event_tap_location = kCGHIDEventTap;
  
  ScopedCFTypeRef<CGEventRef> command_down(CGEventCreateKeyboardEvent(
      event_source, kVK_Command, true));
  ScopedCFTypeRef<CGEventRef> shift_down(CGEventCreateKeyboardEvent(
      event_source, kVK_Shift, true));
  ScopedCFTypeRef<CGEventRef> key_down(CGEventCreateKeyboardEvent(
      event_source, key_code, true));
  CGEventSetFlags(key_down, kCGEventFlagMaskCommand | kCGEventFlagMaskShift);
  
  ScopedCFTypeRef<CGEventRef> command_up(CGEventCreateKeyboardEvent(
      event_source, kVK_Command, false));
  ScopedCFTypeRef<CGEventRef> shift_up(CGEventCreateKeyboardEvent(
      event_source, kVK_Shift, false));
  ScopedCFTypeRef<CGEventRef> key_up(CGEventCreateKeyboardEvent(
      event_source, key_code, false));
  CGEventSetFlags(key_up, kCGEventFlagMaskCommand | kCGEventFlagMaskShift);
  
  CGEventPost(event_tap_location, command_down);
  CGEventPost(event_tap_location, shift_down);
  CGEventPost(event_tap_location, key_down);
  CGEventPost(event_tap_location, key_up);
  CGEventPost(event_tap_location, shift_up);
  CGEventPost(event_tap_location, command_up);
  CFRelease(event_source);
}
#endif
#if defined(OS_CHROMEOS)
#define MAYBE_GlobalCommand DISABLED_GlobalCommand
#else
#define MAYBE_GlobalCommand GlobalCommand
#endif
IN_PROC_BROWSER_TEST_F(GlobalCommandsApiTest, MAYBE_GlobalCommand) {
  FeatureSwitch::ScopedOverride enable_global_commands(
      FeatureSwitch::global_commands(), true);
  
  ResultCatcher catcher;
  ASSERT_TRUE(RunExtensionTest("keybinding/global")) << message_;
  ASSERT_TRUE(catcher.GetNextResult());
#if defined(OS_WIN)
  
  
  
  
  
  
  Browser* incognito_browser = CreateIncognitoBrowser();
  
  
  
  
  ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
      incognito_browser, ui::VKEY_1, true, true, false, false));
  ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
      incognito_browser, ui::VKEY_A, true, true, false, false));
  
  ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
      incognito_browser, ui::VKEY_9, true, true, false, false));
#elif defined(OS_LINUX)
  
  CreateIncognitoBrowser();
  
  
  
  
  
  SendNativeKeyEventToXDisplay(ui::VKEY_1, true, true, false);
  SendNativeKeyEventToXDisplay(ui::VKEY_A, true, true, false);
  SendNativeKeyEventToXDisplay(ui::VKEY_9, true, true, false);
#elif defined(OS_MACOSX)
  
  CreateIncognitoBrowser();
  
  SendNativeCommandShift(kVK_ANSI_1);
  SendNativeCommandShift(kVK_ANSI_A);
  SendNativeCommandShift(kVK_ANSI_9);
#endif
  
  
  
  ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
}
#if defined(OS_WIN)
#define MAYBE_GlobalDuplicatedMediaKey GlobalDuplicatedMediaKey
#else
#define MAYBE_GlobalDuplicatedMediaKey DISABLED_GlobalDuplicatedMediaKey
#endif
IN_PROC_BROWSER_TEST_F(GlobalCommandsApiTest, MAYBE_GlobalDuplicatedMediaKey) {
  FeatureSwitch::ScopedOverride enable_global_commands(
      FeatureSwitch::global_commands(), true);
  ResultCatcher catcher;
  ASSERT_TRUE(RunExtensionTest("keybinding/global_media_keys_0")) << message_;
  ASSERT_TRUE(catcher.GetNextResult());
  ASSERT_TRUE(RunExtensionTest("keybinding/global_media_keys_1")) << message_;
  ASSERT_TRUE(catcher.GetNextResult());
  Browser* incognito_browser = CreateIncognitoBrowser();  
  WindowController* controller =
      incognito_browser->extension_window_controller();
  ui_controls::SendKeyPress(controller->window()->GetNativeWindow(),
                            ui::VKEY_MEDIA_NEXT_TRACK,
                            false,
                            false,
                            false,
                            false);
  
  ASSERT_TRUE(catcher.GetNextResult());
  ASSERT_TRUE(catcher.GetNextResult());
}
}