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.
5 #include "chrome/browser/browser_process.h"
6 #include "chrome/browser/chromeos/oom_priority_manager.h"
7 #include "chrome/browser/ui/browser.h"
8 #include "chrome/browser/ui/browser_commands.h"
9 #include "chrome/browser/ui/browser_tabstrip.h"
10 #include "chrome/browser/ui/find_bar/find_bar_controller.h"
11 #include "chrome/browser/ui/tabs/tab_strip_model.h"
12 #include "chrome/common/url_constants.h"
13 #include "chrome/test/base/in_process_browser_test.h"
14 #include "chrome/test/base/ui_test_utils.h"
15 #include "content/public/browser/notification_service.h"
16 #include "content/public/browser/notification_types.h"
17 #include "googleurl/src/gurl.h"
19 using content::OpenURLParams
;
23 typedef InProcessBrowserTest OomPriorityManagerTest
;
25 IN_PROC_BROWSER_TEST_F(OomPriorityManagerTest
, OomPriorityManagerBasics
) {
26 using ui_test_utils::WindowedNotificationObserver
;
28 // Get three tabs open.
29 WindowedNotificationObserver
load1(
30 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
31 content::NotificationService::AllSources());
32 OpenURLParams
open1(GURL(chrome::kChromeUIAboutURL
), content::Referrer(),
33 CURRENT_TAB
, content::PAGE_TRANSITION_TYPED
, false);
34 browser()->OpenURL(open1
);
37 WindowedNotificationObserver
load2(
38 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
39 content::NotificationService::AllSources());
40 OpenURLParams
open2(GURL(chrome::kChromeUICreditsURL
), content::Referrer(),
41 NEW_FOREGROUND_TAB
, content::PAGE_TRANSITION_TYPED
,
43 browser()->OpenURL(open2
);
46 WindowedNotificationObserver
load3(
47 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
48 content::NotificationService::AllSources());
49 OpenURLParams
open3(GURL(chrome::kChromeUITermsURL
), content::Referrer(),
50 NEW_FOREGROUND_TAB
, content::PAGE_TRANSITION_TYPED
,
52 browser()->OpenURL(open3
);
55 EXPECT_EQ(3, browser()->tab_count());
57 // Navigate the current (third) tab to a different URL, so we can test
58 // back/forward later.
59 WindowedNotificationObserver
load4(
60 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
61 content::NotificationService::AllSources());
62 OpenURLParams
open4(GURL(chrome::kChromeUIVersionURL
), content::Referrer(),
63 CURRENT_TAB
, content::PAGE_TRANSITION_TYPED
,
65 browser()->OpenURL(open4
);
68 // Navigate the third tab again, such that we have three navigation entries.
69 WindowedNotificationObserver
load5(
70 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
71 content::NotificationService::AllSources());
72 OpenURLParams
open5(GURL("chrome://dns"), content::Referrer(),
73 CURRENT_TAB
, content::PAGE_TRANSITION_TYPED
,
75 browser()->OpenURL(open5
);
78 EXPECT_EQ(3, browser()->tab_count());
80 // Discard a tab. It should kill the first tab, since it was the oldest
81 // and was not selected.
82 EXPECT_TRUE(g_browser_process
->oom_priority_manager()->DiscardTab());
83 EXPECT_EQ(3, browser()->tab_count());
84 EXPECT_TRUE(browser()->tab_strip_model()->IsTabDiscarded(0));
85 EXPECT_FALSE(browser()->tab_strip_model()->IsTabDiscarded(1));
86 EXPECT_FALSE(browser()->tab_strip_model()->IsTabDiscarded(2));
88 // Run discard again, make sure it kills the second tab.
89 g_browser_process
->oom_priority_manager()->DiscardTab();
90 EXPECT_EQ(3, browser()->tab_count());
91 EXPECT_TRUE(browser()->tab_strip_model()->IsTabDiscarded(0));
92 EXPECT_TRUE(browser()->tab_strip_model()->IsTabDiscarded(1));
93 EXPECT_FALSE(browser()->tab_strip_model()->IsTabDiscarded(2));
96 EXPECT_TRUE(g_browser_process
->oom_priority_manager()->DiscardTab());
97 EXPECT_TRUE(browser()->tab_strip_model()->IsTabDiscarded(0));
98 EXPECT_TRUE(browser()->tab_strip_model()->IsTabDiscarded(1));
99 EXPECT_TRUE(browser()->tab_strip_model()->IsTabDiscarded(2));
101 // Running when all tabs are discarded should do nothing.
102 EXPECT_FALSE(g_browser_process
->oom_priority_manager()->DiscardTab());
104 // Force creation of the FindBarController.
105 browser()->GetFindBarController();
107 // Select the first tab. It should reload.
108 WindowedNotificationObserver
reload1(
109 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
110 content::NotificationService::AllSources());
111 chrome::SelectNumberedTab(browser(), 0);
113 // Make sure the FindBarController gets the right TabContents.
114 EXPECT_EQ(browser()->GetFindBarController()->tab_contents(),
115 chrome::GetActiveTabContents(browser()));
116 EXPECT_EQ(0, browser()->active_index());
117 EXPECT_FALSE(browser()->tab_strip_model()->IsTabDiscarded(0));
118 EXPECT_TRUE(browser()->tab_strip_model()->IsTabDiscarded(1));
119 EXPECT_TRUE(browser()->tab_strip_model()->IsTabDiscarded(2));
121 // Select the third tab. It should reload.
122 WindowedNotificationObserver
reload2(
123 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
124 content::NotificationService::AllSources());
125 chrome::SelectNumberedTab(browser(), 2);
127 EXPECT_EQ(2, browser()->active_index());
128 EXPECT_FALSE(browser()->tab_strip_model()->IsTabDiscarded(0));
129 EXPECT_TRUE(browser()->tab_strip_model()->IsTabDiscarded(1));
130 EXPECT_FALSE(browser()->tab_strip_model()->IsTabDiscarded(2));
132 // Navigate the third tab back twice. We used to crash here due to
134 EXPECT_TRUE(chrome::CanGoBack(browser()));
135 EXPECT_FALSE(chrome::CanGoForward(browser()));
136 WindowedNotificationObserver
back1(
137 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
138 content::NotificationService::AllSources());
139 chrome::GoBack(browser(), CURRENT_TAB
);
141 EXPECT_TRUE(chrome::CanGoBack(browser()));
142 EXPECT_TRUE(chrome::CanGoForward(browser()));
143 WindowedNotificationObserver
back2(
144 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
145 content::NotificationService::AllSources());
146 chrome::GoBack(browser(), CURRENT_TAB
);
148 EXPECT_FALSE(chrome::CanGoBack(browser()));
149 EXPECT_TRUE(chrome::CanGoForward(browser()));