'Test the fileview interface.'

def testVersionDropDownChangesFileContentAndDownloads(self):

    self.Open('/')
    self.Type('client_query', 'C.0000000000000001')
    self.Click('client_query_submit')
    self.WaitUntilEqual(u'C.0000000000000001', self.GetText, 'css=span[type=subject]')
    self.Click("css=td:contains('0001')")
    self.Click("css=a[grrtarget='client.vfs']")
    self.Click('css=#_fs    i.jstree-icon')
    self.Click('css=#_fs-os    i.jstree-icon')
    self.Click('css=#_fs-os-c    i.jstree-icon')
    self.WaitUntil(self.IsElementPresent, 'css=#_fs-os-c-Downloads')
    self.Click('link=Downloads')
    self.assertTrue((gui_test_lib.DateString(gui_test_lib.TIME_2) in self.GetText('css=tr:contains("a.txt")')))
    self.Click('css=tr:contains("a.txt")')
    self.WaitUntilContains('a.txt', self.GetText, 'css=div#main_bottomPane    h1')
    self.WaitUntilContains('HEAD', self.GetText, 'css=.version-dropdown    >    option[selected]')
    self.WaitUntilContains(gui_test_lib.DateString(gui_test_lib.TIME_2), self.GetText, 'css=.version-dropdown    >    option:nth(1)')
    self.Click('css=li[heading=TextView]')
    self.WaitUntilContains('Goodbye    World', self.GetText, 'css=div.monospace    pre')
    downloaded_files = []
    def FakeDownloadHandle(unused_self, args, token=None):
        _ = token
        aff4_path = args.client_id.ToClientURN().Add(args.file_path)
        age = (args.timestamp or aff4.NEWEST_TIME)
        downloaded_files.append((aff4_path, age))
        return api_call_handler_base.ApiBinaryStream(filename=aff4_path.Basename(), content_generator=xrange(42))
    with utils.Stubber(api_vfs.ApiGetFileBlobHandler, 'Handle', FakeDownloadHandle):
        self.Click('css=li[heading=Download]')
        self.WaitUntilContains(gui_test_lib.DateTimeString(gui_test_lib.TIME_2), self.GetText, 'css=grr-file-download-view')
        self.Click('css=button:contains("Download")')
        self.Click(('css=select.version-dropdown    >    option:contains("%s")' % gui_test_lib.DateString(gui_test_lib.TIME_1)))
        self.WaitUntilContains(gui_test_lib.DateTimeString(gui_test_lib.TIME_1), self.GetText, 'css=grr-file-download-view')
        self.Click('css=button:contains("Download")')
        self.WaitUntil(self.IsElementPresent, 'css=li[heading=TextView]')
        self.WaitUntil((lambda : (len(downloaded_files) == 4)))
    self.assertEqual(downloaded_files[0][0], u'aff4:/C.0000000000000001/fs/os/c/Downloads/a.txt')
    self.assertEqual(downloaded_files[2][0], u'aff4:/C.0000000000000001/fs/os/c/Downloads/a.txt')
    self.assertEqual(downloaded_files[0][1], aff4.NEWEST_TIME)
    self.assertAlmostEqual(downloaded_files[2][1], gui_test_lib.TIME_1, delta=rdfvalue.Duration('1s'))
    self.Click('css=li[heading=TextView]')
    self.WaitUntilContains('Hello    World', self.GetText, 'css=div.monospace    pre')
