<commit_before>#!/usr/bin/python
# Copyright (c) 2011 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import os
import re

import pyauto_functional
import pyauto


class ShortcutsTest(pyauto.PyUITest):
  """Test for browser shortcuts.

  No tests for print, save page as... shortcuts as they involve interaction
  with OS native dialogs.
  """

  def testNewTabShortcut(self):
    """Verify new tab shortcut."""
    self.RunCommand(pyauto.IDC_NEW_TAB)
    self.assertEqual(2, self.GetTabCount(), msg='Can not open a new tab.')

  def testCloseTabShortcut(self):
    """Verify close tab shortcut."""
    self.RunCommand(pyauto.IDC_NEW_TAB)
    self.assertEqual(2, self.GetTabCount(), msg='Can not open a new tab.')
    self.RunCommand(pyauto.IDC_CLOSE_TAB)
    self.assertEqual(1, self.GetTabCount(), msg='Can not close a tab.')

  def testReopenClosedTabShortcut(self):
    """Verify reopen closed tab shortcut opens recently closed tab."""
    self.RunCommand(pyauto.IDC_NEW_TAB)
    url = self.GetFileURLForPath(os.path.join(self.DataDir(), 'title2.html'))
    self.NavigateToURL(url)
    title = self.GetActiveTabTitle()
    self.GetBrowserWindow(0).GetTab(1).Close()
    self.assertEqual(1, self.GetTabCount(), msg='Can not close a tab.')
    # Verify shortcut reopens the correct tab.
    self.RunCommand(pyauto.IDC_RESTORE_TAB)
    self.assertEqual(2, self.GetTabCount(), msg='Can not restore a tab.')
    self.assertEqual(title, self.GetActiveTabTitle())

  def testNewWindowShortcut(self):
    """Verify new window shortcut."""
    self.RunCommand(pyauto.IDC_NEW_WINDOW)
    self.assertEquals(2, self.GetBrowserWindowCount())

  def testNewIncognitoWindowShortcut(self):
    """Verify new incognito window shortcut launches incognito window."""
    self.RunCommand(pyauto.IDC_NEW_INCOGNITO_WINDOW)
    self.assertEqual(2, self.GetBrowserWindowCount())
    # Check if it is incognito by checking history.
    assert not self.GetHistoryInfo().History(), 'Expecting clean history.'
    url = self.GetFileURLForPath(os.path.join(self.DataDir(), 'title2.html'))
    self.NavigateToURL(url, 1, 0)
    self.assertEqual(0, len(self.GetHistoryInfo().History()))

  def testCloseWindowShortcut(self):
    """Verify close window shortcut."""
    self.RunCommand(pyauto.IDC_NEW_WINDOW)
    self.assertEquals(2, self.GetBrowserWindowCount())
    self.RunCommand(pyauto.IDC_CLOSE_WINDOW)
    self.assertEquals(1, self.GetBrowserWindowCount())

  def testFindShortcut(self):
    """Verify find in box shortcut."""
    self.ApplyAccelerator(pyauto.IDC_FIND)
    self.assertTrue(self.WaitUntil(lambda: self.IsFindInPageVisible()),
                    msg='Find in box is not visible.')

  def testAlwaysShowBookmarksBarShortcut(self):
    """Verify always show bookmarks bar shortcut."""
    # Show bookmark bar.
    self.ApplyAccelerator(pyauto.IDC_SHOW_BOOKMARK_BAR)
    self.assertTrue(self.WaitUntil(lambda: self.GetBookmarkBarVisibility()),
                    msg='Bookmarks bar is not visible.')
    # Hide bookmark bar.
    self.ApplyAccelerator(pyauto.IDC_SHOW_BOOKMARK_BAR)
    self.assertTrue(self.WaitUntil(lambda:
        self.GetBookmarkBarVisibility() is False),
        msg='Bookmarks bar is visible.')

  # TODO: Task Manager Shortcut. crbug.com/73454

  def testClearBrowsingDataShortcut(self):
    """Verify clear browsing data shortcut."""
    self.ApplyAccelerator(pyauto.IDC_CLEAR_BROWSING_DATA)
    self.assertEquals(2, self.GetTabCount())
    self.assertTrue(re.search('clearBrowserData',
        self.GetActiveTabURL().spec()), 'Clear browsing data url is wrong.')
    # Wait until the clear browsing data DOM UI window opens.
    self.assertTrue(self.WaitUntil(lambda:
        self.ExecuteJavascript(
        'var element = document.getElementById("clearBrowserDataOverlay");'
        'if(element) window.domAutomationController.send(element.nodeName);'
        'else window.domAutomationController.send(0)', 0, 1),
        expect_retval='DIV'), msg='Could not find the DOM UI window element.')

  def testViewSourceShortcut(self):
    """Verify view source shortcut."""
    self.ApplyAccelerator(pyauto.IDC_VIEW_SOURCE)
    self.assertEqual(2, self.GetTabCount(), msg='Cannot View Source.')
    self.assertEqual('view-source:about:blank', self.GetActiveTabURL().spec(),
                      msg='View Source URL is not correct.')

  def testDeveloperToolsShortcut(self):
    """Verify developer tools shortcut opens developer tools window.."""
    # Setting the pref to undock devtools so that it can be seen
    # as a separate window.
    self.SetPrefs(pyauto.kDevToolsOpenDocked, False)
    self.ApplyAccelerator(pyauto.IDC_DEV_TOOLS)
    self.assertEqual(2, self.GetBrowserWindowCount(),
                     msg='DevTools window is not open.')

  def testJavaScriptConsoleShortcut(self):
    """Verify javascript console shortcut opens developer tools window.
    We can not check if console is open or not.<commit_msg>Update expectation in a pyauto test: testClearBrowsingDataShortcut<commit_after>#!/usr/bin/python
# Copyright (c) 2011 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import os
import re

import pyauto_functional
import pyauto


class ShortcutsTest(pyauto.PyUITest):
  """Test for browser shortcuts.

  No tests for print, save page as... shortcuts as they involve interaction
  with OS native dialogs.
  """

  def testNewTabShortcut(self):
    """Verify new tab shortcut."""
    self.RunCommand(pyauto.IDC_NEW_TAB)
    self.assertEqual(2, self.GetTabCount(), msg='Can not open a new tab.')

  def testCloseTabShortcut(self):
    """Verify close tab shortcut."""
    self.RunCommand(pyauto.IDC_NEW_TAB)
    self.assertEqual(2, self.GetTabCount(), msg='Can not open a new tab.')
    self.RunCommand(pyauto.IDC_CLOSE_TAB)
    self.assertEqual(1, self.GetTabCount(), msg='Can not close a tab.')

  def testReopenClosedTabShortcut(self):
    """Verify reopen closed tab shortcut opens recently closed tab."""
    self.RunCommand(pyauto.IDC_NEW_TAB)
    url = self.GetFileURLForPath(os.path.join(self.DataDir(), 'title2.html'))
    self.NavigateToURL(url)
    title = self.GetActiveTabTitle()
    self.GetBrowserWindow(0).GetTab(1).Close()
    self.assertEqual(1, self.GetTabCount(), msg='Can not close a tab.')
    # Verify shortcut reopens the correct tab.
    self.RunCommand(pyauto.IDC_RESTORE_TAB)
    self.assertEqual(2, self.GetTabCount(), msg='Can not restore a tab.')
    self.assertEqual(title, self.GetActiveTabTitle())

  def testNewWindowShortcut(self):
    """Verify new window shortcut."""
    self.RunCommand(pyauto.IDC_NEW_WINDOW)
    self.assertEquals(2, self.GetBrowserWindowCount())

  def testNewIncognitoWindowShortcut(self):
    """Verify new incognito window shortcut launches incognito window."""
    self.RunCommand(pyauto.IDC_NEW_INCOGNITO_WINDOW)
    self.assertEqual(2, self.GetBrowserWindowCount())
    # Check if it is incognito by checking history.
    assert not self.GetHistoryInfo().History(), 'Expecting clean history.'
    url = self.GetFileURLForPath(os.path.join(self.DataDir(), 'title2.html'))
    self.NavigateToURL(url, 1, 0)
    self.assertEqual(0, len(self.GetHistoryInfo().History()))

  def testCloseWindowShortcut(self):
    """Verify close window shortcut."""
    self.RunCommand(pyauto.IDC_NEW_WINDOW)
    self.assertEquals(2, self.GetBrowserWindowCount())
    self.RunCommand(pyauto.IDC_CLOSE_WINDOW)
    self.assertEquals(1, self.GetBrowserWindowCount())

  def testFindShortcut(self):
    """Verify find in box shortcut."""
    self.ApplyAccelerator(pyauto.IDC_FIND)
    self.assertTrue(self.WaitUntil(lambda: self.IsFindInPageVisible()),
                    msg='Find in box is not visible.')

  def testAlwaysShowBookmarksBarShortcut(self):
    """Verify always show bookmarks bar shortcut."""
    # Show bookmark bar.
    self.ApplyAccelerator(pyauto.IDC_SHOW_BOOKMARK_BAR)
    self.assertTrue(self.WaitUntil(lambda: self.GetBookmarkBarVisibility()),
                    msg='Bookmarks bar is not visible.')
    # Hide bookmark bar.
    self.ApplyAccelerator(pyauto.IDC_SHOW_BOOKMARK_BAR)
    self.assertTrue(self.WaitUntil(lambda:
        self.GetBookmarkBarVisibility() is False),
        msg='Bookmarks bar is visible.')

  # TODO: Task Manager Shortcut. crbug.com/73454

  def testClearBrowsingDataShortcut(self):
    """Verify clear browsing data shortcut."""
    self.NavigateToURL(self.GetFileURLForDataPath('title2.html'))
    self.ApplyAccelerator(pyauto.IDC_CLEAR_BROWSING_DATA)
    self.assertEquals(2, self.GetTabCount())
    self.assertTrue(re.search('clearBrowserData',
        self.GetActiveTabURL().spec()), 'Clear browsing data url is wrong.')
    # Wait until the clear browsing data DOM UI window opens.
    self.assertTrue(self.WaitUntil(lambda:
        self.ExecuteJavascript(
        'var element = document.getElementById("clearBrowserDataOverlay");'
        'if(element) window.domAutomationController.send(element.nodeName);'
        'else window.domAutomationController.send("")', 0, 1),
        expect_retval='DIV'), msg='Could not find the DOM UI window element.')

  def testViewSourceShortcut(self):
    """Verify view source shortcut."""
    self.ApplyAccelerator(pyauto.IDC_VIEW_SOURCE)
    self.assertEqual(2, self.GetTabCount(), msg='Cannot View Source.')
    self.assertEqual('view-source:about:blank', self.GetActiveTabURL().spec(),
                      msg='View Source URL is not correct.')

  def testDeveloperToolsShortcut(self):
    """Verify developer tools shortcut opens developer tools window.."""
    # Setting the pref to undock devtools so that it can be seen
    # as a separate window.
    self.SetPrefs(pyauto.kDevToolsOpenDocked, False)
    self.ApplyAccelerator(pyauto.IDC_DEV_TOOLS)
    self.assertEqual(2, self.GetBrowserWindowCount(),
                     msg='DevTools window is not open.')

  def testJavaScriptConsoleShortcut(self):
    """Verify javascript console shortcut opens developer tools window.
    We can not check if console is open or not.
{'fraction_non_alphanumeric': 0.11174279553028818, 'fraction_numerical': 0.00607723975691041, 'mean_word_length': 4.104052026013006, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4210028', 'n_tokens_mistral': 3166, 'n_tokens_neox': 3057, 'n_words': 748}