require "spec_helper" require "tempfile" describe Dotremap do let!(:config) { Tempfile.new(".remap") } let(:xml_dir) { "/tmp" } let(:xml_path) { File.join(xml_dir, Dotremap::XML_FILE_NAME) } let(:result) { File.read(xml_path) } before do stub_const("Dotremap::XML_DIR", xml_dir) allow(Dotremap::Karabiner).to receive(:reload_xml) # Silence stdout allow_any_instance_of(Kernel).to receive(:puts) end after do config.close! end def prepare_dotremap(dotremap) config.write(dotremap) config.rewind end def expect_result(expected_result) dotremap = Dotremap.new(config.path) dotremap.apply_configuration expect(result).to eq(expected_result) end it "accepts blank config" do prepare_dotremap("") expect_result(<<-EOS.unindent) EOS end it "accepts cmd combination" do prepare_dotremap(<<-EOS) item "Command+A to Command+B" do remap "Cmd-A", to: "Cmd-B" end EOS expect_result(<<-EOS.unindent) Command+A to Command+B remap.command_a_to_command_b __KeyToKey__ KeyCode::A, VK_COMMAND, KeyCode::B, VK_COMMAND EOS end it "accepts multiple remaps" do prepare_dotremap(<<-EOS) item "multiple remaps" do remap "Cmd-A", to: "Cmd-B" remap "Shift-A", to: "Shift-B" end EOS expect_result(<<-EOS.unindent) multiple remaps remap.multiple_remaps __KeyToKey__ KeyCode::A, VK_COMMAND, KeyCode::B, VK_COMMAND __KeyToKey__ KeyCode::A, VK_SHIFT, KeyCode::B, VK_SHIFT EOS end it "accepts multiple items" do prepare_dotremap(<<-EOS) item "first item" do remap "Cmd-C-A", to: "Cmd-M-B" end item "second item" do remap "Shift-Opt-A", to: "Shift-Cmd-B" end EOS expect_result(<<-EOS.unindent) first item remap.first_item __KeyToKey__ KeyCode::A, VK_COMMAND | VK_CONTROL, KeyCode::B, VK_COMMAND | VK_OPTION second item remap.second_item __KeyToKey__ KeyCode::A, VK_SHIFT | VK_OPTION, KeyCode::B, VK_SHIFT | VK_COMMAND EOS end it "accepts appdef and app option" do prepare_dotremap(<<-EOS) appdef "CHROME", equal: "com.google.Chrome" item "Command+K to Command+L", only: "CHROME" do remap "Cmd-K", to: "Cmd-L" end EOS expect_result(<<-EOS.unindent) CHROME com.google.Chrome Command+K to Command+L remap.command_k_to_command_l CHROME __KeyToKey__ KeyCode::K, VK_COMMAND, KeyCode::L, VK_COMMAND EOS end it "accepts config and show_message" do prepare_dotremap(<<-EOS) item "CapsLock ON", config_not: "notsave.private_capslock_on" do remap "Cmd-L", to: ["capslock", "VK_CONFIG_FORCE_ON_notsave_private_capslock_on"] end item "CapsLock OFF", config_only: "notsave.private_capslock_on" do remap "Cmd-L", to: ["capslock", "VK_CONFIG_FORCE_OFF_notsave_private_capslock_on"] end item "CapsLock Mode" do identifier "notsave.private_capslock_on", vk_config: "true" show_message "CapsLock" end EOS expect_result(<<-EOS.unindent) CapsLock ON remap.capslock_on notsave.private_capslock_on __KeyToKey__ KeyCode::L, VK_COMMAND, KeyCode::CAPSLOCK, KeyCode::VK_CONFIG_FORCE_ON_notsave_private_capslock_on CapsLock OFF remap.capslock_off notsave.private_capslock_on __KeyToKey__ KeyCode::L, VK_COMMAND, KeyCode::CAPSLOCK, KeyCode::VK_CONFIG_FORCE_OFF_notsave_private_capslock_on CapsLock Mode notsave.private_capslock_on __ShowStatusMessage__ CapsLock EOS end it "accepts implicit autogen selection" do prepare_dotremap(<<-EOS) item "Control+LeftClick to Command+LeftClick" do autogen "__PointingButtonToPointingButton__ PointingButton::LEFT, MODIFIERFLAG_EITHER_LEFT_OR_RIGHT_CONTROL, PointingButton::LEFT, MODIFIERFLAG_EITHER_LEFT_OR_RIGHT_COMMAND" end EOS expect_result(<<-EOS.unindent) Control+LeftClick to Command+LeftClick remap.control_leftclick_to_command_leftclick __PointingButtonToPointingButton__ PointingButton::LEFT, MODIFIERFLAG_EITHER_LEFT_OR_RIGHT_CONTROL, PointingButton::LEFT, MODIFIERFLAG_EITHER_LEFT_OR_RIGHT_COMMAND EOS end it "application invoking" do prepare_dotremap(<<-EOS) item "Application shortcuts" do remap "C-o", to: invoke("YoruFukurou") remap "C-u", to: invoke("Google Chrome") remap "C-h", to: invoke("iTerm") end item "duplicate app" do remap "C-a", to: invoke("YoruFukurou") end EOS expect_result(<<-EOS.unindent) Application shortcuts remap.application_shortcuts __KeyToKey__ KeyCode::O, VK_CONTROL, KeyCode::VK_OPEN_URL_APP_YoruFukurou __KeyToKey__ KeyCode::U, VK_CONTROL, KeyCode::VK_OPEN_URL_APP_Google_Chrome __KeyToKey__ KeyCode::H, VK_CONTROL, KeyCode::VK_OPEN_URL_APP_iTerm duplicate app remap.duplicate_app __KeyToKey__ KeyCode::A, VK_CONTROL, KeyCode::VK_OPEN_URL_APP_YoruFukurou KeyCode::VK_OPEN_URL_APP_YoruFukurou /Applications/YoruFukurou.app KeyCode::VK_OPEN_URL_APP_Google_Chrome /Applications/Google Chrome.app KeyCode::VK_OPEN_URL_APP_iTerm /Applications/iTerm.app EOS end end