def open_backup(path, flex_is_open):
    # Opening: Get to the 'Restore a Project' screen
    ################
    helper = TestHelper("open_flex_from_backup")

    if flex_is_open:
        helper.Click(Pattern("File.png").similar(0.90), "Couldn't find 'File'")
        helper.Click("ProjectManag.png",
            "Couldn't find 'Project Management' in File menu")
        helper.Click("RestoreaProj.png", "Couldn't find 'Restore a Project...' option")
    else:
        # Not sure if this line is needed...
        #subprocess.Popen("fieldworks-flex &")
        helper.Click(Pattern("Restoreaproj.png").similar(0.89), 
            "Couldn't find 'Restore a project from a backup file...'")
        
    # Goal: Find the backup
    #################
    if not helper.Click(Pattern("Anotherlocat.png").similar(0.80).targetOffset(-48,1), "Couldn't find 'Another location' option", give_up=False):
        close_box()
    if not helper.Click(Pattern("Browse.png").similar(0.90), "Couldn't find 'Browse...' button", give_up=False):
        close_box()
    helper.Click("1436284725146.png", "Couldn't find button to type a file name", restart=True)
    paste(path)
    helper.Type(Key.ENTER)
   
    helper.Click(Pattern("OK.png").similar(0.91), "Couldn't click 'OK'", restart=True)
    
    if exists(Pattern("7ReplaceExls.png").similar(0.88)):
        helper.Click(Pattern("Yes.png").similar(0.95), "Couldn't click 'Yes'", time=60, restart=True)
        helper.write("Successfully opened project: " + path.split("/")[-1])
    else:
        helper.write("Successfully opened project: " + path.split("/")[-1])
        wait(40)
        
def check_word(word, compare_path):
    helper = TestHelper("check_lexicon_entry_change")
    set_flex_helper(helper)
    
    # Opening
    ###############
    helper.Click("Lexicon.png", "Couldn't find 'Lexicon' button")
    LEFT_SIDEBAR.Click("LexiconEdit.png", "Couldn't find 'Lexicon Edit' button", time=10)
    helper.Click(Pattern("LexemeForm.png").targetOffset(-52,23), "'Lexeme Form' column not found")
    helper.Click("lterfor.png", "'Filter for' button not found")
    paste(word)
    
    # Now the 'Filter for' box is up, so if we can't click something
    # we should restart Flex
    helper.Click(Pattern("W_holeitem.png").targetOffset(-42,0), "'Whole item' option not found", restart=True)
    helper.Click(Pattern("OK-1.png").similar(0.85), "OK button not found", time=10, restart=True)
    # 'Filter for' box is gone now, word should be selected automatically
    
    
    # Create a region inside the 'Entry' panel
    entry_header = helper.Find("9EntryHShowH.png",
        "'Entry' field not found in right panel")
    entry_region = entry_header.below()
    
    # Goal
    ###############
    if entry_region.exists(Pattern(compare_path).similar(0.9)):
        helper.write("'" + word + "' field looks the same as in provided image")
    else:
        helper.write("'" + word + "' field has changed")
def check_text(text, compare_path):
    # Opening
    ###############
    helper = TestHelper("check_text_change")
    set_flex_helper(helper)
    
    helper.Click("TUTextsWord.png", "Couldn't find 'Texts & Words' button")
    LEFT_SIDEBAR.Click("InterlinearT.png", "Couldn't find 'Interlinear Texts' button")
    
    # Find the menu to search for text
    column_menu = helper.Find("Show.png", "Couldn't find 'Show All'")
    column_menu = Regionplus(helper, column_menu.right())
    if not column_menu.Click(Pattern("1436391324903.png").similar(0.90),
        "Couldn't find arrow for drop-down menu, trying to expand column", give_up=False):
        
        # Click on hopefully a different test, so the view of our text will reset
        helper.Click(Pattern("Show.png").targetOffset(0, 30),
            "Couldn't find 'Show All'", time=3)
        
        # Drag out the column so the arrow is visible
        while not column_menu.exists(Pattern("1436391324903.png").similar(0.90)):
            drag_point = helper.Find(Pattern("1436455824256.png").targetOffset(8,0), "Couldn't find point to drag out column", time=2)
            drop_point = helper.Find(Pattern("Title.png").similar(0.90), "Couldn't find 'Title'")
            dragDrop(drag_point, drop_point)
            
        # Click the arrow
        column_menu.Click(Pattern("1436391324903.png").similar(0.90),
            "Couldn't find arrow for drop-down menu even after expanding column")
        
    helper.Click("lterfor.png", "'Filter for' button not found")
    paste(text) 

    # Now the 'Filter for' box is up, so if we can't click something
    # we should restart Flex
    helper.Click(Pattern("W_holeitem.png").targetOffset(-42,0), "'Whole item' option not found", restart=True)
    helper.Click(Pattern("OK-1.png").similar(0.85), "OK button not found", time=10, restart=True)
    # 'Filter for' box is gone now, text should be selected automatically

    # Create a region inside the 'Entry' panel
    text_header = helper.Find("1436391807956.png",
        "'Text' field not found in right panel")
    text_region = Regionplus(helper, text_header.below())
    text_region.Click("PrintView.png", " Couldn't find 'Print View' tab")

    # Goal
    ###############
    if text_region.exists(Pattern(compare_path).similar(0.9)):
        helper.write("'" + text + "' print view looks the same as in provided image")
    else:
        helper.write("'" + text + "' print view has changed")
def restart_flex():
    helper = TestHelper("restart_flex")
    helper.write("restarting flex")
    helper.Type("t", KeyModifier.CTRL | KeyModifier.ALT, time=5)
    helper.Type("sudo /home/vagrant/Integration-Testing-Framework/sikuli/examples/restart_flex.sh && exit" + Key.ENTER)

    # If the 'hello' project shows up as the last opened project,
    # just press enter
    if helper.Exists(Pattern("iii3iuE.png").similar(0.90), "'hello' project not showing as last opened, " +
              "trying to open via 'Open a project'", give_up=False):
        helper.write("Success")
        helper.Type(Key.ENTER, time=20)

    # If not, try to find it in the 'Open project screen'
    else:
        helper.Click("Openaproject.png", "Couldn't find 'Open a project' button")
        hello = helper.Find(Pattern("hello.png").similar(0.90), "'hello' project not found in the 'Open Project'" +
                     "screen, trying to create a new project", give_up=False)
        if hello:
            helper.DoubleClick(hello, "Found 'hello' but didn't manage to click it, wat!?",
                  time=20)
            helper.write("Success")

        # As a last resort, try to create the project (this shouldn't happen
        # and will probably not work: green screen if hello already exists.)
        else:
            helper.write_fail("'hello' project not found, quitting.")
            exit()
     wait(2)
def check_dictionary(compare_path):

    # Opening
    ################
    helper = TestHelper("check_dictionary_change")
    set_flex_helper(helper)
    
    helper.Click("Lexicon.png", "Couldn't find 'Lexicon' button")
    # First make sure all entries are showing
    LEFT_SIDEBAR.Click("LexiconEdit.png", "Couldn't find 'Lexicon Edit' button", time=10)
    if not exists(Pattern("ieadwordShow.png").similar(0.90)):
        helper.Click(Pattern("LexemeForm.png").targetOffset(-52,23), "'Lexeme Form' column not found")
        helper.Click(Pattern("Headword-1.png").similar(0.90).targetOffset(0,50), "Couldn't find 'Headword'", time=15)
    
    LEFT_SIDEBAR.Click("ElDictionary.png", "Couldn't find 'Dictionary' button", time=20)

    # Goal
    #################
    TOOLBARS.Click("1436456695019.png", "Couldn't find 'First' button -- may already be at start", give_up=False)
    if exists(Pattern(compare_path).similar(0.9)):
        helper.write("Dictionary looks the same as in provided image")
    else:
        helper.write("Dictionary print view has changed")