require File.join(File.dirname(__FILE__), ".." , "spec_helper" )

module WinGuiTest
  include WinGui

  describe Window do
    before(:each) { @app = launch_test_app }
    after(:each){ close_test_app }
    
    context 'initializing' do
      it 'can be wrapped around any existing window' do
        any_handle = find_window(nil, nil)
        use{ Window.new any_handle }
      end
    end
    
    context 'manipulating' do
      
      it 'has handle property equal to underlying window handle' do
        any_handle = find_window(nil, nil)
        any = Window.new any_handle
        any.handle.should == any_handle
      end
      
      it 'has text property equal to underlying window text(title)' do
        @app.text.should == WIN_TITLE
      end

      it 'closes when asked nicely' do
        @app.close
        sleep SLEEP_DELAY # needed to ensure window had enough time to close down
        find_window(nil, WIN_TITLE).should == nil
      end
      
      it 'waits f0r window to disappear (NB: this happens before handle is released!)' do 
        start = Time.now 
        @app.close
        @app.wait_for_close
         (Time.now - start).should be <= WG_CLOSE_TIMEOUT
        window_visible?(@app.handle).should be false
      end
    end
    
    context '.top_level class method' do
      it 'finds any top-level window (title = nil) and wraps it in a Window object' do
        use { @win = Window.top_level(title = nil, timeout_sec = 3) }
        Window.should === @win
      end
      
      it 'finds top-level window by title and wraps it in a Window object' do
        win = Window.top_level( WIN_TITLE, 1)
        win.handle.should == @app.handle
      end
    end
    
    context '#child' do
      spec { use { @control = @app.child(title_class_id = nil)  }} 

      it 'finds any child window(control) if given nil' do
        @app.child(nil).should_not == nil
      end
      
      it 'finds child window(control) by class' do
        @app.child(TEXTAREA_CLASS).should_not == nil
      end
      
      it 'finds child window(control) by name' do
        pending 'Need to find control with short name'
        @app.child(TEXTAREA_TEXT).should_not == nil
      end

      it 'finds child window(control) by control ID' do
        pending 'Need to find some control ID'
        @app.child(TEXTAREA_ID).should_not == nil
      end
      
      it 'raises error if wrong control is given' do
        expect { @app.child('Impossible Control')}.to raise_error "Control 'Impossible Control' not found"
      end
      it 'substitutes & for _ when searching by title ("&Yes" type controls)'
      
    end
    
    context '#children' do
      spec { use { children = @app.children  }}

      it 'returns an array' do
        @app.children.should be_a_kind_of(Array)
      end

      it 'returns an array of all child windows to the given window ' do
        children = @app.children
        children.should be_a_kind_of Array
        children.should_not be_empty
        children.should have(2).elements
        children.each{|child| child?(@app.handle, child.handle).should == true }
        get_class_name(children.last.handle).should == TEXTAREA_CLASS
      end

#      it 'finds child window(control) by name' do
#        pending 'Need to find control with short name'
#        @app.child(TEXTAREA_TEXT).should_not == nil
#      end
#
#      it 'finds child window(control) by control ID' do
#        pending 'Need to find some control ID'
#        @app.child(TEXTAREA_ID).should_not == nil
#      end
#
#      it 'raises error if wrong control is given' do
#        expect { @app.child('Impossible Control')}.to raise_error "Control 'Impossible Control' not found"
#      end
#      it 'substitutes & for _ when searching by title ("&Yes" type controls)'
      
    end

    context '#click' do
      it 'emulates clicking of the control identified by id'
    end
  end
end