OLD | NEW |
---|---|
(Empty) | |
1 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
2 # Use of this source code is governed by a BSD-style license that can be | |
3 # found in the LICENSE file. | |
4 | |
5 import uuid | |
6 | |
7 | |
8 class TabTracker: | |
9 """Uniquely track tabs within a window. | |
10 | |
11 This allows the creation of tabs whose indices can be | |
12 determined even after lower indexed tabs have been closed, therefore changing | |
13 that tab's index. | |
14 | |
15 This is accomplished via a containing window which is created and tracked via | |
16 the window's index. As a result of this, all calls to open and close tabs in | |
17 this TabTracker's window must go through the appropriate instance of the | |
18 TabTracker. Also note that if a lower indexed window is closed after this | |
19 TabTracker is instantiated, this TabTracker will lose track of it's window""" | |
Nirnimesh
2012/07/31 20:05:57
it's -> its
| |
20 | |
21 def __init__(self, browser, visible=False): | |
22 """ | |
23 Args: | |
24 browser: an instance of a PyUITest | |
Nirnimesh
2012/07/31 20:05:57
remove 'a'
| |
25 visible: whether or not this TabTracker's window will be visible | |
26 """ | |
27 # A binary search tree would be faster, but this is easier to write. | |
28 # If this needs to become faster, understand that the important operations | |
29 # here are append, arbitrary deletion and searching. | |
30 self._uuids = [None] | |
31 self._window_idx = browser.GetBrowserWindowCount() | |
32 self._browser = browser | |
33 browser.OpenNewBrowserWindow(visible) | |
34 # We leave the 0'th tab empty to have something to close on __del__ | |
35 | |
36 def __del__(self): | |
37 self._browser.CloseBrowserWindow(self._window_idx) | |
38 | |
39 def CreateTab(self, url='about:blank'): | |
40 """Create a tracked tab and return it's uuid. | |
Nirnimesh
2012/07/31 20:05:57
its
| |
41 | |
42 Args: | |
43 url: a URL to navigate to | |
44 | |
45 Returns: | |
46 a uuid uniquely identifying that tab within this TabTracker | |
47 """ | |
48 self._browser.AppendTab(url, self._window_idx) | |
49 # We use uuids here rather than a monotonic integer to prevent confusion | |
50 # with the tab index. | |
51 tab_uuid = uuid.uuid4() | |
52 self._uuids.append(tab_uuid) | |
53 return tab_uuid | |
54 | |
55 def ReleaseTab(self, tab_uuid): | |
56 """Release and close a tab tracked by this TabTracker. | |
57 | |
58 Args: | |
59 tab_uuid: the uuid of the tab to close | |
60 """ | |
61 idx = self.GetTabIndex(tab_uuid) | |
62 self._browser.GetBrowserWindow(self._window_idx).GetTab(idx).Close() | |
63 del self._uuids[idx] | |
64 | |
65 def GetTabIndex(self, tab_uuid): | |
66 """Get the index of a tracked tab within this TabTracker's window. | |
67 | |
68 Args: | |
69 tab_uuid: the uuid of the tab to close | |
70 | |
71 Returns: | |
72 the index of the tab within this TabTracker's window | |
73 """ | |
74 return self._uuids.index(tab_uuid) | |
75 | |
76 def GetWindowIndex(self): | |
77 """Get the index of this TabTracker's window. | |
78 | |
79 Returns: | |
80 the index of this TabTracker's window | |
81 """ | |
82 return self._window_idx | |
OLD | NEW |