1#!/usr/bin/env python
2# Copyright (c) 2012 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6import os
7
8import pyauto_functional  # must be imported before pyauto
9import pyauto
10import test_utils
11
12
13class CroshTest(pyauto.PyUITest):
14  """Tests for crosh."""
15
16  def setUp(self):
17    """Close all windows at startup."""
18    pyauto.PyUITest.setUp(self)
19    for _ in range(self.GetBrowserWindowCount()):
20      self.CloseBrowserWindow(0)
21
22  def testBasic(self):
23    """Verify crosh basic flow."""
24    test_utils.OpenCroshVerification(self)
25
26    # Verify crosh prompt.
27    self.WaitForHtermText(text='crosh> ',
28        msg='Could not find "crosh> " prompt')
29    self.assertTrue(
30        self.GetHtermRowsText(start=0, end=2).endswith('crosh> '),
31        msg='Could not find "crosh> " prompt')
32
33    # Run a crosh command.
34    self.SendKeysToHterm('help\\n')
35    self.WaitForHtermText(text='help_advanced',
36        msg='Could not find "help_advanced" in help output.')
37
38    # Exit crosh and close tab.
39    self.SendKeysToHterm('exit\\n')
40    self.WaitForHtermText(text='command crosh completed with exit code 0',
41        msg='Could not exit crosh.')
42
43  def testAddBookmark(self):
44    """Test crosh URL can be bookmarked"""
45    test_utils.OpenCroshVerification(self)
46
47    # Add bookmark.
48    bookmarks = self.GetBookmarkModel()
49    bar_id = bookmarks.BookmarkBar()['id']
50    name = 'crosh'
51    url = self.GetActiveTabURL()
52    count = bookmarks.NodeCount()
53    self.AddBookmarkURL(bar_id, 0, name, url.spec())
54    bookmarks = self.GetBookmarkModel()
55    node = bookmarks.BookmarkBar()['children'][0]
56    self.assertEqual(count + 1, bookmarks.NodeCount())
57    self.assertEqual(node['type'], 'url')
58    self.assertEqual(node['name'], name)
59    self.assertEqual(url.spec(), node['url'])
60
61  def testMultipleWindowCrosh(self):
62    """Test that crosh can be opened in multiple windows."""
63    test_utils.OpenCroshVerification(self)
64
65    for windex in range (1, 4):  # 3 new windows
66      self.OpenNewBrowserWindow(True)
67      self.OpenCrosh()
68      self.assertEqual('crosh', self.GetActiveTabTitle())
69
70      # Verify crosh prompt.
71      self.WaitForHtermText(text='crosh> ', tab_index=1, windex=windex,
72          msg='Could not find "crosh> " prompt')
73      self.assertTrue(
74        self.GetHtermRowsText(start=0, end=2, tab_index=1,
75                              windex=windex).endswith('crosh> '),
76        msg='Could not find "crosh> " prompt')
77
78      # Exit crosh.
79      self.SendKeysToHterm('exit\\n', tab_index=1, windex=windex)
80      self.WaitForHtermText(text='command crosh completed with exit code 0',
81          tab_index=1, windex=windex,
82          msg='Could not exit crosh.')
83
84  def testShell(self):
85    """Test shell can be opened in crosh."""
86    test_utils.OpenCroshVerification(self)
87
88    # Verify crosh prompt.
89    self.WaitForHtermText(text='crosh> ',
90        msg='Could not find "crosh> " prompt')
91    self.assertTrue(
92        self.GetHtermRowsText(start=0, end=2).endswith('crosh> '),
93        msg='Could not find "crosh> " prompt')
94
95    # Run a shell command.
96    self.SendKeysToHterm(r'shell\n')
97    self.WaitForHtermText(text='chronos@localhost',
98        msg='Could not find "chronos@localhost" in shell output.')
99
100  def testConnectToAnotherhost(self):
101    """Test ssh to another host."""
102    test_utils.OpenCroshVerification(self)
103
104    # Verify crosh prompt.
105    self.WaitForHtermText(text='crosh> ',
106        msg='Could not find "crosh> " prompt')
107    self.assertTrue(
108        self.GetHtermRowsText(start=0, end=2).endswith('crosh> '),
109        msg='Could not find "crosh> " prompt')
110
111    # Ssh to another host: chronos@localhost.
112    self.SendKeysToHterm(r'ssh chronos@localhost\n')
113    self.WaitForHtermText(text='Password',
114        msg='Could not find "Password" in shell output.')
115    self.SendKeysToHterm(r'test0000\n')
116    self.WaitForHtermText(text='chronos@localhost',
117        msg='Could not find "chronos@localhost" in shell output.')
118
119  def testTabSwitching(self):
120    """Test tab can be switched in crosh."""
121    test_utils.OpenCroshVerification(self)
122
123    # Open 6 tabs
124    for x in xrange(3):
125      self.AppendTab(self.GetHttpURLForDataPath('title2.html'))
126      self.assertEqual('Title Of Awesomeness', self.GetActiveTabTitle(),
127                       msg='Unable to navigate to title2.html and '
128                           'verify tab title.')
129      self.OpenCrosh()
130    self.assertEqual(7, len(self.GetBrowserInfo()['windows'][0]['tabs']))
131
132    # Select tab 5
133    self.ApplyAccelerator(pyauto.IDC_SELECT_TAB_4)
134    self.assertEqual('crosh', self.GetActiveTabTitle(),
135                     msg='Unable to naviage to crosh.')
136
137    # Run a crosh command.
138    self.SendKeysToHterm('help\\n', tab_index=4, windex=0)
139    self.WaitForHtermText(text='help_advanced', tab_index=4, windex=0,
140        msg='Could not find "help_advanced" in help output.')
141
142  def testLargefileCrosh(self):
143    """Test large file is displayed in crosh."""
144    test_utils.OpenCroshVerification(self)
145
146    # Verify crosh prompt.
147    self.WaitForHtermText(text='crosh> ',
148        msg='Could not find "crosh> " prompt')
149    self.assertTrue(
150        self.GetHtermRowsText(start=0, end=2).endswith('crosh> '),
151        msg='Could not find "crosh> " prompt')
152
153    # Login to localhost.
154    self.SendKeysToHterm(r'ssh chronos@localhost\n')
155    self.WaitForHtermText(text='Password',
156        msg='Could not find "Password" in shell output.')
157    self.SendKeysToHterm(r'test0000\n')
158    self.WaitForHtermText(text='chronos@localhost',
159        msg='Could not find "chronos@localhost" in shell output.')
160
161    # Create a file with 140 characters per line, 50000 lines.
162    bigfn = '/tmp/bigfile.txt'
163    with open(bigfn, 'w') as file:
164        file.write(('0' * 140 + '\n') * 50000 + 'complete\n')
165
166    # Cat a large file.
167    self.SendKeysToHterm(r'cat %s\n' % bigfn)
168    self.WaitForHtermText(text='complete',
169        msg='Could not find "complete" in shell output.')
170    os.remove(bigfn)
171
172
173if __name__ == '__main__':
174  pyauto_functional.Main()
175