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