1 # Copyright (c) 2012 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.
8 class TabTracker(object):
9 """Uniquely track tabs within a window.
11 This allows the creation of tabs whose indices can be
12 determined even after lower indexed tabs have been closed, therefore changing
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 its window
22 def __init__(self, browser, visible=False):
25 browser: an instance of PyUITest
26 visible: whether or not this TabTracker's window will be visible
28 # A binary search tree would be faster, but this is easier to write.
29 # If this needs to become faster, understand that the important operations
30 # here are append, arbitrary deletion and searching.
32 self._window_idx = browser.GetBrowserWindowCount()
33 self._browser = browser
34 browser.OpenNewBrowserWindow(visible)
35 # We leave the 0'th tab empty to have something to close on __del__
38 self._browser.CloseBrowserWindow(self._window_idx)
40 def CreateTab(self, url='about:blank'):
41 """Create a tracked tab and return its uuid.
44 url: a URL to navigate to
47 a uuid uniquely identifying that tab within this TabTracker
49 self._browser.AppendTab(url, self._window_idx)
50 # We use uuids here rather than a monotonic integer to prevent confusion
52 tab_uuid = uuid.uuid4()
53 self._uuids.append(tab_uuid)
56 def ReleaseTab(self, tab_uuid):
57 """Release and close a tab tracked by this TabTracker.
60 tab_uuid: the uuid of the tab to close
62 idx = self.GetTabIndex(tab_uuid)
63 self._browser.CloseTab(tab_index=idx, windex=self._window_idx)
66 def GetTabIndex(self, tab_uuid):
67 """Get the index of a tracked tab within this TabTracker's window.
70 tab_uuid: the uuid of the tab to close
73 the index of the tab within this TabTracker's window
75 return self._uuids.index(tab_uuid)
77 def GetWindowIndex(self):
78 """Get the index of this TabTracker's window.
81 the index of this TabTracker's window
83 return self._window_idx