Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(26)

Side by Side Diff: chrome/browser/ui/tabs/tab_strip_model_unittest.cc

Issue 1332003002: Add option to disallow the discarding of a tab that was previously discarded. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: sky@ comments + fix tests. Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/ui/tabs/tab_strip_model.h" 5 #include "chrome/browser/ui/tabs/tab_strip_model.h"
6 6
7 #include <map> 7 #include <map>
8 #include <string> 8 #include <string>
9 9
10 #include "base/files/file_path.h" 10 #include "base/files/file_path.h"
11 #include "base/memory/scoped_ptr.h" 11 #include "base/memory/scoped_ptr.h"
12 #include "base/stl_util.h" 12 #include "base/stl_util.h"
13 #include "base/strings/string_number_conversions.h" 13 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/string_split.h" 14 #include "base/strings/string_split.h"
15 #include "base/strings/string_util.h" 15 #include "base/strings/string_util.h"
16 #include "base/strings/utf_string_conversions.h" 16 #include "base/strings/utf_string_conversions.h"
17 #include "chrome/browser/defaults.h" 17 #include "chrome/browser/defaults.h"
18 #include "chrome/browser/extensions/tab_helper.h" 18 #include "chrome/browser/extensions/tab_helper.h"
19 #include "chrome/browser/profiles/profile.h" 19 #include "chrome/browser/profiles/profile.h"
20 #include "chrome/browser/ui/browser.h" 20 #include "chrome/browser/ui/browser.h"
21 #include "chrome/browser/ui/browser_tabstrip.h" 21 #include "chrome/browser/ui/browser_tabstrip.h"
22 #include "chrome/browser/ui/tabs/tab_discard_state.h"
22 #include "chrome/browser/ui/tabs/tab_strip_model_delegate.h" 23 #include "chrome/browser/ui/tabs/tab_strip_model_delegate.h"
23 #include "chrome/browser/ui/tabs/tab_strip_model_order_controller.h" 24 #include "chrome/browser/ui/tabs/tab_strip_model_order_controller.h"
24 #include "chrome/browser/ui/tabs/test_tab_strip_model_delegate.h" 25 #include "chrome/browser/ui/tabs/test_tab_strip_model_delegate.h"
25 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h" 26 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h"
26 #include "chrome/common/url_constants.h" 27 #include "chrome/common/url_constants.h"
27 #include "chrome/test/base/chrome_render_view_host_test_harness.h" 28 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
28 #include "chrome/test/base/testing_profile.h" 29 #include "chrome/test/base/testing_profile.h"
29 #include "components/web_modal/web_contents_modal_dialog_manager.h" 30 #include "components/web_modal/web_contents_modal_dialog_manager.h"
30 #include "content/public/browser/navigation_controller.h" 31 #include "content/public/browser/navigation_controller.h"
31 #include "content/public/browser/navigation_entry.h" 32 #include "content/public/browser/navigation_entry.h"
(...skipping 2102 matching lines...) Expand 10 before | Expand all | Expand 10 after
2134 tabstrip.AppendWebContents(contents2, true); 2135 tabstrip.AppendWebContents(contents2, true);
2135 2136
2136 // Start watching for events after the appends to avoid observing state 2137 // Start watching for events after the appends to avoid observing state
2137 // transitions that aren't relevant to this test. 2138 // transitions that aren't relevant to this test.
2138 MockTabStripModelObserver tabstrip_observer(&tabstrip); 2139 MockTabStripModelObserver tabstrip_observer(&tabstrip);
2139 tabstrip.AddObserver(&tabstrip_observer); 2140 tabstrip.AddObserver(&tabstrip_observer);
2140 2141
2141 // Discard one of the tabs. 2142 // Discard one of the tabs.
2142 WebContents* null_contents1 = tabstrip.DiscardWebContentsAt(0); 2143 WebContents* null_contents1 = tabstrip.DiscardWebContentsAt(0);
2143 ASSERT_EQ(2, tabstrip.count()); 2144 ASSERT_EQ(2, tabstrip.count());
2144 EXPECT_TRUE(tabstrip.IsTabDiscarded(0)); 2145 EXPECT_TRUE(TabDiscardState::IsDiscarded(tabstrip.GetWebContentsAt(0)));
2145 EXPECT_FALSE(tabstrip.IsTabDiscarded(1)); 2146 EXPECT_FALSE(TabDiscardState::IsDiscarded(tabstrip.GetWebContentsAt(1)));
2146 ASSERT_EQ(null_contents1, tabstrip.GetWebContentsAt(0)); 2147 ASSERT_EQ(null_contents1, tabstrip.GetWebContentsAt(0));
2147 ASSERT_EQ(contents2, tabstrip.GetWebContentsAt(1)); 2148 ASSERT_EQ(contents2, tabstrip.GetWebContentsAt(1));
2148 ASSERT_EQ(1, tabstrip_observer.GetStateCount()); 2149 ASSERT_EQ(1, tabstrip_observer.GetStateCount());
2149 State state1(null_contents1, 0, MockTabStripModelObserver::REPLACED); 2150 State state1(null_contents1, 0, MockTabStripModelObserver::REPLACED);
2150 state1.src_contents = contents1; 2151 state1.src_contents = contents1;
2151 EXPECT_TRUE(tabstrip_observer.StateEquals(0, state1)); 2152 EXPECT_TRUE(tabstrip_observer.StateEquals(0, state1));
2152 tabstrip_observer.ClearStates(); 2153 tabstrip_observer.ClearStates();
2153 2154
2154 // Discard the same tab again. 2155 // Discard the same tab again.
2155 WebContents* null_contents2 = tabstrip.DiscardWebContentsAt(0); 2156 WebContents* null_contents2 = tabstrip.DiscardWebContentsAt(0);
2156 ASSERT_EQ(2, tabstrip.count()); 2157 ASSERT_EQ(2, tabstrip.count());
2157 EXPECT_TRUE(tabstrip.IsTabDiscarded(0)); 2158 EXPECT_TRUE(TabDiscardState::IsDiscarded(tabstrip.GetWebContentsAt(0)));
2158 EXPECT_FALSE(tabstrip.IsTabDiscarded(1)); 2159 EXPECT_FALSE(TabDiscardState::IsDiscarded(tabstrip.GetWebContentsAt(1)));
2159 ASSERT_EQ(null_contents2, tabstrip.GetWebContentsAt(0)); 2160 ASSERT_EQ(null_contents2, tabstrip.GetWebContentsAt(0));
2160 ASSERT_EQ(contents2, tabstrip.GetWebContentsAt(1)); 2161 ASSERT_EQ(contents2, tabstrip.GetWebContentsAt(1));
2161 ASSERT_EQ(1, tabstrip_observer.GetStateCount()); 2162 ASSERT_EQ(1, tabstrip_observer.GetStateCount());
2162 State state2(null_contents2, 0, MockTabStripModelObserver::REPLACED); 2163 State state2(null_contents2, 0, MockTabStripModelObserver::REPLACED);
2163 state2.src_contents = null_contents1; 2164 state2.src_contents = null_contents1;
2164 EXPECT_TRUE(tabstrip_observer.StateEquals(0, state2)); 2165 EXPECT_TRUE(tabstrip_observer.StateEquals(0, state2));
2165 tabstrip_observer.ClearStates(); 2166 tabstrip_observer.ClearStates();
2166 2167
2167 // Activating the tab should clear its discard state. 2168 // Activating the tab should clear its discard state.
2168 tabstrip.ActivateTabAt(0, true /* user_gesture */); 2169 tabstrip.ActivateTabAt(0, true /* user_gesture */);
2169 ASSERT_EQ(2, tabstrip.count()); 2170 ASSERT_EQ(2, tabstrip.count());
2170 EXPECT_FALSE(tabstrip.IsTabDiscarded(0)); 2171 EXPECT_FALSE(TabDiscardState::IsDiscarded(tabstrip.GetWebContentsAt(0)));
2171 EXPECT_FALSE(tabstrip.IsTabDiscarded(1)); 2172 EXPECT_FALSE(TabDiscardState::IsDiscarded(tabstrip.GetWebContentsAt(1)));
2172 2173
2173 // Don't discard active tab. 2174 // Don't discard active tab.
2174 tabstrip.DiscardWebContentsAt(0); 2175 tabstrip.DiscardWebContentsAt(0);
2175 ASSERT_EQ(2, tabstrip.count()); 2176 ASSERT_EQ(2, tabstrip.count());
2176 EXPECT_FALSE(tabstrip.IsTabDiscarded(0)); 2177 EXPECT_FALSE(TabDiscardState::IsDiscarded(tabstrip.GetWebContentsAt(0)));
2177 EXPECT_FALSE(tabstrip.IsTabDiscarded(1)); 2178 EXPECT_FALSE(TabDiscardState::IsDiscarded(tabstrip.GetWebContentsAt(1)));
2178 2179
2179 tabstrip.CloseAllTabs(); 2180 tabstrip.CloseAllTabs();
2180 } 2181 }
2181 2182
2182 // Makes sure that reloading a discarded tab without activating it unmarks the 2183 // Makes sure that reloading a discarded tab without activating it unmarks the
2183 // tab as discarded so it won't reload on activation. 2184 // tab as discarded so it won't reload on activation.
2184 TEST_F(TabStripModelTest, ReloadDiscardedTabContextMenu) { 2185 TEST_F(TabStripModelTest, ReloadDiscardedTabContextMenu) {
2185 TabStripDummyDelegate delegate; 2186 TabStripDummyDelegate delegate;
2186 TabStripModel tabstrip(&delegate, profile()); 2187 TabStripModel tabstrip(&delegate, profile());
2187 2188
2188 // Create 2 tabs because the active tab cannot be discarded. 2189 // Create 2 tabs because the active tab cannot be discarded.
2189 tabstrip.AppendWebContents(CreateWebContents(), true); 2190 tabstrip.AppendWebContents(CreateWebContents(), true);
2190 content::WebContents* test_contents = 2191 content::WebContents* test_contents =
2191 WebContentsTester::CreateTestWebContents(browser_context(), nullptr); 2192 WebContentsTester::CreateTestWebContents(browser_context(), nullptr);
2192 tabstrip.AppendWebContents(test_contents, false); // Opened in background. 2193 tabstrip.AppendWebContents(test_contents, false); // Opened in background.
2193 2194
2194 // Navigate to a web page. This is necessary to set a current entry in memory 2195 // Navigate to a web page. This is necessary to set a current entry in memory
2195 // so the reload can happen. 2196 // so the reload can happen.
2196 WebContentsTester::For(test_contents) 2197 WebContentsTester::For(test_contents)
2197 ->NavigateAndCommit(GURL("chrome://newtab")); 2198 ->NavigateAndCommit(GURL("chrome://newtab"));
2198 EXPECT_FALSE(tabstrip.IsTabDiscarded(1)); 2199 EXPECT_FALSE(TabDiscardState::IsDiscarded(tabstrip.GetWebContentsAt(1)));
2199 2200
2200 tabstrip.DiscardWebContentsAt(1); 2201 tabstrip.DiscardWebContentsAt(1);
2201 EXPECT_TRUE(tabstrip.IsTabDiscarded(1)); 2202 EXPECT_TRUE(TabDiscardState::IsDiscarded(tabstrip.GetWebContentsAt(1)));
2202 2203
2203 tabstrip.GetWebContentsAt(1)->GetController().Reload(false); 2204 tabstrip.GetWebContentsAt(1)->GetController().Reload(false);
2204 EXPECT_FALSE(tabstrip.IsTabDiscarded(1)); 2205 EXPECT_FALSE(TabDiscardState::IsDiscarded(tabstrip.GetWebContentsAt(1)));
2205 2206
2206 tabstrip.CloseAllTabs(); 2207 tabstrip.CloseAllTabs();
2207 } 2208 }
2208 2209
2209 // Makes sure that the last active time property is saved even though the tab is 2210 // Makes sure that the last active time property is saved even though the tab is
2210 // discarded. 2211 // discarded.
2211 TEST_F(TabStripModelTest, DiscardedTabKeepsLastActiveTime) { 2212 TEST_F(TabStripModelTest, DiscardedTabKeepsLastActiveTime) {
2212 TabStripDummyDelegate delegate; 2213 TabStripDummyDelegate delegate;
2213 TabStripModel tabstrip(&delegate, profile()); 2214 TabStripModel tabstrip(&delegate, profile());
2214 2215
(...skipping 412 matching lines...) Expand 10 before | Expand all | Expand 10 after
2627 WebContents* page_d_contents = CreateWebContents(); 2628 WebContents* page_d_contents = CreateWebContents();
2628 strip.AddWebContents(page_c_contents, -1, ui::PAGE_TRANSITION_LINK, 2629 strip.AddWebContents(page_c_contents, -1, ui::PAGE_TRANSITION_LINK,
2629 TabStripModel::ADD_NONE); 2630 TabStripModel::ADD_NONE);
2630 strip.AddWebContents(page_d_contents, -1, ui::PAGE_TRANSITION_LINK, 2631 strip.AddWebContents(page_d_contents, -1, ui::PAGE_TRANSITION_LINK,
2631 TabStripModel::ADD_NONE); 2632 TabStripModel::ADD_NONE);
2632 2633
2633 EXPECT_EQ(2, strip.GetIndexOfWebContents(page_c_contents)); 2634 EXPECT_EQ(2, strip.GetIndexOfWebContents(page_c_contents));
2634 EXPECT_EQ(3, strip.GetIndexOfWebContents(page_d_contents)); 2635 EXPECT_EQ(3, strip.GetIndexOfWebContents(page_d_contents));
2635 strip.CloseAllTabs(); 2636 strip.CloseAllTabs();
2636 } 2637 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698