require File.join(__FILE__.gsub(/(.*)?\/spec\/.*$/, '\1'), 'spec/spec_helper') describe Rtml::Widgets::EventListener do before(:each) do @doc = Rtml::Document.new @doc.screen :on_cancel @doc.screen :on_menu @doc.screen :on_key end context "at document level" do it "should raise an error given an invalid key" do proc { @doc.on :invalid_key => 'invalid_destination' }.should raise_error(Rtml::Errors::RulesViolationError) end it "should assign a cancel button" do @doc.on :cancel => 'on_cancel' @doc.to_tml.should have_selector(:defaults, :cancel => '#on_cancel') end it "should assign a menu button" do @doc.on :menu => 'on_menu' @doc.to_tml.should have_selector(:defaults, :menu => '#on_menu') end end context "at screen level" do before(:each) { @scr = @doc.screen :main } it "should merge multiple calls" do @scr.on 1 => :on_cancel @scr.on 2 => :on_menu @scr.on 3 => :on_key @scr.to_tml.should have_selector(:variant, :uri => "#on_cancel", :key => "1") @scr.to_tml.should have_selector(:variant, :uri => "#on_menu", :key => "2") @scr.to_tml.should have_selector(:variant, :uri => "#on_key", :key => "3") end it "should raise an error given an invalid key" do proc { @scr.on :invalid_key => 'invalid_destination' }.should raise_error(Rtml::Errors::RulesViolationError) end it "should assign a cancel button" do @scr.on :cancel => 'on_cancel' @doc.to_tml.should have_selector(:variant, :uri => '#on_cancel', :key => "cancel") end it "should assign a menu button" do @scr.on :menu => 'on_menu' @doc.to_tml.should have_selector(:variant, :uri => '#on_menu', :key => "menu") end %w(0 1 2 3 4 5 6 7 8 9 00 f1 f2 f3 f4 f5 f6 f7 f8 f9 down up stop enter).each do |key| it "should assign a #{key} button" do @scr.on key => "on_key" @doc.to_tml.should have_selector(:variant, :uri => "#on_key", :key => key) end end end end