OLD | NEW |
| (Empty) |
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 | |
6 import os | |
7 import logging | |
8 | |
9 import pyauto_functional # Must be imported before pyauto | |
10 import pyauto | |
11 import test_utils | |
12 | |
13 class PopupsTest(pyauto.PyUITest): | |
14 """TestCase for Popup blocking.""" | |
15 | |
16 def Debug(self): | |
17 """Test method for experimentation. | |
18 | |
19 This method will not run automatically. | |
20 """ | |
21 while True: | |
22 raw_input('Interact with the browser and hit <enter>') | |
23 self.pprint(self.GetBlockedPopupsInfo()) | |
24 | |
25 def testPopupBlockerEnabled(self): | |
26 """Verify popup blocking is enabled.""" | |
27 self.assertFalse(self.GetBlockedPopupsInfo(), | |
28 msg='Should have no blocked popups on startup') | |
29 file_url = self.GetFileURLForPath(os.path.join( | |
30 self.DataDir(), 'popup_blocker', 'popup-window-open.html')) | |
31 self.NavigateToURL(file_url) | |
32 blocked_popups = self.GetBlockedPopupsInfo() | |
33 self.assertEqual(1, len(blocked_popups), msg='Popup not blocked') | |
34 # It might take a while for the title to get set. Don't need to check it. | |
35 # self.assertEqual('Popup Success!', blocked_popups[0]['title']) | |
36 | |
37 def testLaunchBlockedPopup(self): | |
38 """Verify that a blocked popup can be unblocked.""" | |
39 file_url = self.GetFileURLForPath(os.path.join( | |
40 self.DataDir(), 'popup_blocker', 'popup-window-open.html')) | |
41 self.NavigateToURL(file_url) | |
42 self.assertEqual(1, len(self.GetBlockedPopupsInfo())) | |
43 self.UnblockAndLaunchBlockedPopup(0) | |
44 # Verify that no more popups are blocked | |
45 self.assertFalse(self.GetBlockedPopupsInfo()) | |
46 # Verify that popup window was created | |
47 self.assertEqual(2, self.GetBrowserWindowCount(), | |
48 msg='Popup could not be launched'); | |
49 # Wait until popup title is read correctly. | |
50 self.assertTrue(self.WaitUntil(lambda: | |
51 self.GetActiveTabTitle(1), expect_retval='Popup Success!'), | |
52 msg='Popup title is wrong.') | |
53 | |
54 def testPopupBlockerInIncognito(self): | |
55 """Verify popup blocking is enabled in incognito windows.""" | |
56 self.RunCommand(pyauto.IDC_NEW_INCOGNITO_WINDOW) | |
57 file_url = self.GetFileURLForPath(os.path.join( | |
58 self.DataDir(), 'popup_blocker', 'popup-window-open.html')) | |
59 self.NavigateToURL(file_url, 1, 0) | |
60 blocked_popups = self.GetBlockedPopupsInfo(tab_index=0, windex=1) | |
61 self.assertEqual(1, len(blocked_popups), msg='Popup not blocked') | |
62 | |
63 def testLaunchBlockedPopupInIncognito(self): | |
64 """Verify that a blocked popup can be unblocked in incognito.""" | |
65 self.RunCommand(pyauto.IDC_NEW_INCOGNITO_WINDOW) | |
66 self.assertTrue(2, self.GetBrowserWindowCount()) | |
67 file_url = self.GetFileURLForPath(os.path.join( | |
68 self.DataDir(), 'popup_blocker', 'popup-window-open.html')) | |
69 self.NavigateToURL(file_url, 1, 0) | |
70 self.assertEquals('Popup created using window.open', | |
71 self.GetActiveTabTitle(1)) | |
72 # Wait until the popup is blocked | |
73 self.assertTrue(self.WaitUntil(lambda: | |
74 len(self.GetBlockedPopupsInfo(tab_index=0, windex=1)) is 1), | |
75 msg='Popup not blocked') | |
76 self.UnblockAndLaunchBlockedPopup(0, tab_index=0, windex=1) | |
77 # Verify that no more popups are blocked | |
78 self.assertFalse(self.GetBlockedPopupsInfo(tab_index=0, windex=1)) | |
79 # Verify that popup window was created | |
80 self.assertEqual(3, self.GetBrowserWindowCount(), | |
81 msg='Popup could not be launched'); | |
82 # Wait until popup title is read correctly. | |
83 self.assertTrue(self.WaitUntil(lambda: | |
84 self.GetActiveTabTitle(2), expect_retval='Popup Success!'), | |
85 msg='Popup title is wrong.') | |
86 | |
87 def testMultiplePopups(self): | |
88 """Verify multiple popups are blocked.""" | |
89 url = self.GetHttpURLForDataPath( | |
90 os.path.join('pyauto_private', 'popup_blocker', | |
91 'PopupTest1.html')) | |
92 self.NavigateToURL(url) | |
93 self.assertEqual(6, len(self.GetBlockedPopupsInfo()), | |
94 msg='Did not block 6 popups.') | |
95 | |
96 def testPopupBlockedEverySec(self): | |
97 """Verify that a popup is blocked every second.""" | |
98 url = self.GetHttpURLForDataPath( | |
99 os.path.join('pyauto_private', 'popup_blocker', | |
100 'PopupTest4.html')) | |
101 self.NavigateToURL(url) | |
102 self.assertTrue(self.WaitUntil(lambda: len(self.GetBlockedPopupsInfo()), | |
103 expect_retval=2)) | |
104 | |
105 def _SetPopupsException(self): | |
106 """Set an exception to allow popups from www.popuptest.com.""" | |
107 value = {'[*.]www.popuptest.com,*': {'popups': 1}} | |
108 return self.SetPrefs(pyauto.kContentSettingsPatternPairs, value) | |
109 | |
110 def testAllowPopupsFromExternalSite(self): | |
111 """Verify that popups are allowed from an external website.""" | |
112 self._SetPopupsException() | |
113 self.NavigateToURL('http://www.popuptest.com/popuptest1.html') | |
114 self.assertEqual(7, self.GetBrowserWindowCount(), | |
115 msg='Popups did not launch from the external site.') | |
116 | |
117 def testPopupsLaunchUponBrowserBackButton(self): | |
118 """Verify that popups are launched on browser back button.""" | |
119 self._SetPopupsException() | |
120 url = self.GetHttpURLForDataPath( | |
121 os.path.join('popup_blocker', 'popup-blocked-to-post-blank.html')) | |
122 self.NavigateToURL(url) | |
123 self.NavigateToURL('http://www.popuptest.com/popuptest1.html') | |
124 self.assertEqual(7, self.GetBrowserWindowCount(), | |
125 msg='Popups did not launch from the external site.') | |
126 self.TabGoBack() | |
127 # Check if two additional popups launch when navigating away from the page. | |
128 self.assertEqual(9, self.GetBrowserWindowCount(), | |
129 msg='Additional popups did not launch.') | |
130 | |
131 def testPopupsLaunchWhenTabIsClosed(self): | |
132 """Verify popups are launched when closing a tab.""" | |
133 self._SetPopupsException() | |
134 self.AppendTab(pyauto.GURL('http://www.popuptest.com/popuptest12.html')) | |
135 self.assertEqual(4, self.GetBrowserWindowCount(), | |
136 msg='Popups did not launch from the external site.') | |
137 self.CloseTab(tab_index=1) | |
138 # Check if last popup is launched when the tab is closed. | |
139 self.assertEqual(5, self.GetBrowserWindowCount(), | |
140 msg='Last popup did not launch when the tab is closed.') | |
141 | |
142 def testUnblockedPopupShowsInHistory(self): | |
143 """Verify that when you unblock popup, the popup shows in history.""" | |
144 file_url = self.GetFileURLForDataPath('popup_blocker', | |
145 'popup-window-open.html') | |
146 self.NavigateToURL(file_url) | |
147 self.assertEqual(1, len(self.GetBlockedPopupsInfo())) | |
148 self.UnblockAndLaunchBlockedPopup(0) | |
149 self.assertTrue(self.WaitUntil( | |
150 lambda: len(self.GetHistoryInfo().History()) == 2)) | |
151 self.assertEqual('Popup Success!', | |
152 self.GetHistoryInfo().History()[0]['title']) | |
153 | |
154 def testBlockedPopupNotShowInHistory(self): | |
155 """Verify that a blocked popup does not show up in history.""" | |
156 file_url = self.GetFileURLForDataPath('popup_blocker', | |
157 'popup-window-open.html') | |
158 self.NavigateToURL(file_url) | |
159 self.assertEqual(1, len(self.GetBlockedPopupsInfo())) | |
160 self.assertEqual(1, len(self.GetHistoryInfo().History())) | |
161 | |
162 def testUnblockedPopupAddedToOmnibox(self): | |
163 """Verify that an unblocked popup shows up in omnibox suggestions.""" | |
164 file_url = self.GetFileURLForDataPath('popup_blocker', | |
165 'popup-window-open.html') | |
166 self.NavigateToURL(file_url) | |
167 self.assertEqual(1, len(self.GetBlockedPopupsInfo())) | |
168 self.UnblockAndLaunchBlockedPopup(0) | |
169 search_string = 'data:text/html,<title>Popup Success!</title> \ | |
170 you should not see this message if popup blocker is enabled' | |
171 matches = test_utils.GetOmniboxMatchesFor(self, search_string) | |
172 self.assertEqual(search_string, matches[0]['destination_url']) | |
173 self.assertEqual(search_string, matches[0]['contents']) | |
174 | |
175 | |
176 if __name__ == '__main__': | |
177 pyauto_functional.Main() | |
OLD | NEW |