| OLD | NEW |
| 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/memory/tab_manager.h" | 5 #include "chrome/browser/memory/tab_manager.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 EXPECT_TRUE( | 216 EXPECT_TRUE( |
| 217 TabManager::IsInternalPage(GURL("chrome://settings/fakeSetting"))); | 217 TabManager::IsInternalPage(GURL("chrome://settings/fakeSetting"))); |
| 218 } | 218 } |
| 219 | 219 |
| 220 // Ensures discarding tabs leaves TabStripModel in a good state. | 220 // Ensures discarding tabs leaves TabStripModel in a good state. |
| 221 TEST_F(TabManagerTest, DiscardWebContentsAt) { | 221 TEST_F(TabManagerTest, DiscardWebContentsAt) { |
| 222 TabManager tab_manager; | 222 TabManager tab_manager; |
| 223 | 223 |
| 224 TabStripDummyDelegate delegate; | 224 TabStripDummyDelegate delegate; |
| 225 TabStripModel tabstrip(&delegate, profile()); | 225 TabStripModel tabstrip(&delegate, profile()); |
| 226 tabstrip.AddObserver(&tab_manager); |
| 226 | 227 |
| 227 // Fill it with some tabs. | 228 // Fill it with some tabs. |
| 228 WebContents* contents1 = CreateWebContents(); | 229 WebContents* contents1 = CreateWebContents(); |
| 229 tabstrip.AppendWebContents(contents1, true); | 230 tabstrip.AppendWebContents(contents1, true); |
| 230 WebContents* contents2 = CreateWebContents(); | 231 WebContents* contents2 = CreateWebContents(); |
| 231 tabstrip.AppendWebContents(contents2, true); | 232 tabstrip.AppendWebContents(contents2, true); |
| 232 | 233 |
| 233 // Start watching for events after the appends to avoid observing state | 234 // Start watching for events after the appends to avoid observing state |
| 234 // transitions that aren't relevant to this test. | 235 // transitions that aren't relevant to this test. |
| 235 MockTabStripModelObserver tabstrip_observer; | 236 MockTabStripModelObserver tabstrip_observer; |
| 236 tabstrip.AddObserver(&tabstrip_observer); | 237 tabstrip.AddObserver(&tabstrip_observer); |
| 237 | 238 |
| 238 // Discard one of the tabs. | 239 // Discard one of the tabs. |
| 239 WebContents* null_contents1 = tab_manager.DiscardWebContentsAt(0, &tabstrip); | 240 WebContents* null_contents1 = tab_manager.DiscardWebContentsAt(0, &tabstrip); |
| 240 ASSERT_EQ(2, tabstrip.count()); | 241 ASSERT_EQ(2, tabstrip.count()); |
| 241 EXPECT_TRUE( | 242 EXPECT_TRUE(tab_manager.IsTabDiscarded(tabstrip.GetWebContentsAt(0))); |
| 242 TabManager::WebContentsData::IsDiscarded(tabstrip.GetWebContentsAt(0))); | 243 EXPECT_FALSE(tab_manager.IsTabDiscarded(tabstrip.GetWebContentsAt(1))); |
| 243 EXPECT_FALSE( | |
| 244 TabManager::WebContentsData::IsDiscarded(tabstrip.GetWebContentsAt(1))); | |
| 245 ASSERT_EQ(null_contents1, tabstrip.GetWebContentsAt(0)); | 244 ASSERT_EQ(null_contents1, tabstrip.GetWebContentsAt(0)); |
| 246 ASSERT_EQ(contents2, tabstrip.GetWebContentsAt(1)); | 245 ASSERT_EQ(contents2, tabstrip.GetWebContentsAt(1)); |
| 247 ASSERT_EQ(1, tabstrip_observer.NbEvents()); | 246 ASSERT_EQ(1, tabstrip_observer.NbEvents()); |
| 248 EXPECT_EQ(contents1, tabstrip_observer.OldContents()); | 247 EXPECT_EQ(contents1, tabstrip_observer.OldContents()); |
| 249 EXPECT_EQ(null_contents1, tabstrip_observer.NewContents()); | 248 EXPECT_EQ(null_contents1, tabstrip_observer.NewContents()); |
| 250 tabstrip_observer.Reset(); | 249 tabstrip_observer.Reset(); |
| 251 | 250 |
| 252 // Discard the same tab again, after resetting its discard state. | 251 // Discard the same tab again, after resetting its discard state. |
| 253 TabManager::WebContentsData::SetDiscardState(tabstrip.GetWebContentsAt(0), | 252 tab_manager.GetWebContentsData(tabstrip.GetWebContentsAt(0)) |
| 254 false); | 253 ->SetDiscardState(false); |
| 255 WebContents* null_contents2 = tab_manager.DiscardWebContentsAt(0, &tabstrip); | 254 WebContents* null_contents2 = tab_manager.DiscardWebContentsAt(0, &tabstrip); |
| 256 ASSERT_EQ(2, tabstrip.count()); | 255 ASSERT_EQ(2, tabstrip.count()); |
| 257 EXPECT_TRUE( | 256 EXPECT_TRUE(tab_manager.IsTabDiscarded(tabstrip.GetWebContentsAt(0))); |
| 258 TabManager::WebContentsData::IsDiscarded(tabstrip.GetWebContentsAt(0))); | 257 EXPECT_FALSE(tab_manager.IsTabDiscarded(tabstrip.GetWebContentsAt(1))); |
| 259 EXPECT_FALSE( | |
| 260 TabManager::WebContentsData::IsDiscarded(tabstrip.GetWebContentsAt(1))); | |
| 261 ASSERT_EQ(null_contents2, tabstrip.GetWebContentsAt(0)); | 258 ASSERT_EQ(null_contents2, tabstrip.GetWebContentsAt(0)); |
| 262 ASSERT_EQ(contents2, tabstrip.GetWebContentsAt(1)); | 259 ASSERT_EQ(contents2, tabstrip.GetWebContentsAt(1)); |
| 263 ASSERT_EQ(1, tabstrip_observer.NbEvents()); | 260 ASSERT_EQ(1, tabstrip_observer.NbEvents()); |
| 264 EXPECT_EQ(null_contents1, tabstrip_observer.OldContents()); | 261 EXPECT_EQ(null_contents1, tabstrip_observer.OldContents()); |
| 265 EXPECT_EQ(null_contents2, tabstrip_observer.NewContents()); | 262 EXPECT_EQ(null_contents2, tabstrip_observer.NewContents()); |
| 266 | 263 |
| 267 // Activating the tab should clear its discard state. | 264 // Activating the tab should clear its discard state. |
| 268 tabstrip.ActivateTabAt(0, true /* user_gesture */); | 265 tabstrip.ActivateTabAt(0, true /* user_gesture */); |
| 269 ASSERT_EQ(2, tabstrip.count()); | 266 ASSERT_EQ(2, tabstrip.count()); |
| 270 EXPECT_FALSE( | 267 EXPECT_FALSE(tab_manager.IsTabDiscarded(tabstrip.GetWebContentsAt(0))); |
| 271 TabManager::WebContentsData::IsDiscarded(tabstrip.GetWebContentsAt(0))); | 268 EXPECT_FALSE(tab_manager.IsTabDiscarded(tabstrip.GetWebContentsAt(1))); |
| 272 EXPECT_FALSE( | |
| 273 TabManager::WebContentsData::IsDiscarded(tabstrip.GetWebContentsAt(1))); | |
| 274 | 269 |
| 275 // Don't discard active tab. | 270 // Don't discard active tab. |
| 276 tab_manager.DiscardWebContentsAt(0, &tabstrip); | 271 tab_manager.DiscardWebContentsAt(0, &tabstrip); |
| 277 ASSERT_EQ(2, tabstrip.count()); | 272 ASSERT_EQ(2, tabstrip.count()); |
| 278 EXPECT_FALSE( | 273 EXPECT_FALSE(tab_manager.IsTabDiscarded(tabstrip.GetWebContentsAt(0))); |
| 279 TabManager::WebContentsData::IsDiscarded(tabstrip.GetWebContentsAt(0))); | 274 EXPECT_FALSE(tab_manager.IsTabDiscarded(tabstrip.GetWebContentsAt(1))); |
| 280 EXPECT_FALSE( | |
| 281 TabManager::WebContentsData::IsDiscarded(tabstrip.GetWebContentsAt(1))); | |
| 282 | 275 |
| 283 tabstrip.CloseAllTabs(); | 276 tabstrip.CloseAllTabs(); |
| 284 EXPECT_TRUE(tabstrip.empty()); | 277 EXPECT_TRUE(tabstrip.empty()); |
| 285 } | 278 } |
| 286 | 279 |
| 287 // Makes sure that reloading a discarded tab without activating it unmarks the | 280 // Makes sure that reloading a discarded tab without activating it unmarks the |
| 288 // tab as discarded so it won't reload on activation. | 281 // tab as discarded so it won't reload on activation. |
| 289 TEST_F(TabManagerTest, ReloadDiscardedTabContextMenu) { | 282 TEST_F(TabManagerTest, ReloadDiscardedTabContextMenu) { |
| 283 // Note that we do not add |tab_manager| as an observer to |tabstrip| here as |
| 284 // the event we are trying to test for is not related to the tab strip, but |
| 285 // the web content instead and therefore should be handled by WebContentsData |
| 286 // (which observes the web content). |
| 290 TabManager tab_manager; | 287 TabManager tab_manager; |
| 291 TabStripDummyDelegate delegate; | 288 TabStripDummyDelegate delegate; |
| 292 TabStripModel tabstrip(&delegate, profile()); | 289 TabStripModel tabstrip(&delegate, profile()); |
| 293 | 290 |
| 294 // Create 2 tabs because the active tab cannot be discarded. | 291 // Create 2 tabs because the active tab cannot be discarded. |
| 295 tabstrip.AppendWebContents(CreateWebContents(), true); | 292 tabstrip.AppendWebContents(CreateWebContents(), true); |
| 296 content::WebContents* test_contents = | 293 content::WebContents* test_contents = |
| 297 WebContentsTester::CreateTestWebContents(browser_context(), nullptr); | 294 WebContentsTester::CreateTestWebContents(browser_context(), nullptr); |
| 298 tabstrip.AppendWebContents(test_contents, false); // Opened in background. | 295 tabstrip.AppendWebContents(test_contents, false); // Opened in background. |
| 299 | 296 |
| 300 // Navigate to a web page. This is necessary to set a current entry in memory | 297 // Navigate to a web page. This is necessary to set a current entry in memory |
| 301 // so the reload can happen. | 298 // so the reload can happen. |
| 302 WebContentsTester::For(test_contents) | 299 WebContentsTester::For(test_contents) |
| 303 ->NavigateAndCommit(GURL("chrome://newtab")); | 300 ->NavigateAndCommit(GURL("chrome://newtab")); |
| 304 EXPECT_FALSE( | 301 EXPECT_FALSE(tab_manager.IsTabDiscarded(tabstrip.GetWebContentsAt(1))); |
| 305 TabManager::WebContentsData::IsDiscarded(tabstrip.GetWebContentsAt(1))); | |
| 306 | 302 |
| 307 tab_manager.DiscardWebContentsAt(1, &tabstrip); | 303 tab_manager.DiscardWebContentsAt(1, &tabstrip); |
| 308 EXPECT_TRUE( | 304 EXPECT_TRUE(tab_manager.IsTabDiscarded(tabstrip.GetWebContentsAt(1))); |
| 309 TabManager::WebContentsData::IsDiscarded(tabstrip.GetWebContentsAt(1))); | |
| 310 | 305 |
| 311 tabstrip.GetWebContentsAt(1)->GetController().Reload(false); | 306 tabstrip.GetWebContentsAt(1)->GetController().Reload(false); |
| 312 EXPECT_FALSE( | 307 EXPECT_FALSE(tab_manager.IsTabDiscarded(tabstrip.GetWebContentsAt(1))); |
| 313 TabManager::WebContentsData::IsDiscarded(tabstrip.GetWebContentsAt(1))); | |
| 314 | |
| 315 tabstrip.CloseAllTabs(); | 308 tabstrip.CloseAllTabs(); |
| 316 EXPECT_TRUE(tabstrip.empty()); | 309 EXPECT_TRUE(tabstrip.empty()); |
| 317 } | 310 } |
| 318 | 311 |
| 319 // Makes sure that the last active time property is saved even though the tab is | 312 // Makes sure that the last active time property is saved even though the tab is |
| 320 // discarded. | 313 // discarded. |
| 321 TEST_F(TabManagerTest, DiscardedTabKeepsLastActiveTime) { | 314 TEST_F(TabManagerTest, DiscardedTabKeepsLastActiveTime) { |
| 322 TabManager tab_manager; | 315 TabManager tab_manager; |
| 323 TabStripDummyDelegate delegate; | 316 TabStripDummyDelegate delegate; |
| 324 TabStripModel tabstrip(&delegate, profile()); | 317 TabStripModel tabstrip(&delegate, profile()); |
| 318 tabstrip.AddObserver(&tab_manager); |
| 325 | 319 |
| 326 tabstrip.AppendWebContents(CreateWebContents(), true); | 320 tabstrip.AppendWebContents(CreateWebContents(), true); |
| 327 WebContents* test_contents = CreateWebContents(); | 321 WebContents* test_contents = CreateWebContents(); |
| 328 tabstrip.AppendWebContents(test_contents, false); | 322 tabstrip.AppendWebContents(test_contents, false); |
| 329 | 323 |
| 330 // Simulate an old inactive tab about to get discarded. | 324 // Simulate an old inactive tab about to get discarded. |
| 331 base::TimeTicks new_last_active_time = | 325 base::TimeTicks new_last_active_time = |
| 332 base::TimeTicks::Now() - base::TimeDelta::FromMinutes(35); | 326 base::TimeTicks::Now() - base::TimeDelta::FromMinutes(35); |
| 333 test_contents->SetLastActiveTime(new_last_active_time); | 327 test_contents->SetLastActiveTime(new_last_active_time); |
| 334 EXPECT_EQ(new_last_active_time, test_contents->GetLastActiveTime()); | 328 EXPECT_EQ(new_last_active_time, test_contents->GetLastActiveTime()); |
| 335 | 329 |
| 336 WebContents* null_contents = tab_manager.DiscardWebContentsAt(1, &tabstrip); | 330 WebContents* null_contents = tab_manager.DiscardWebContentsAt(1, &tabstrip); |
| 337 EXPECT_EQ(new_last_active_time, null_contents->GetLastActiveTime()); | 331 EXPECT_EQ(new_last_active_time, null_contents->GetLastActiveTime()); |
| 338 | 332 |
| 339 tabstrip.CloseAllTabs(); | 333 tabstrip.CloseAllTabs(); |
| 340 EXPECT_TRUE(tabstrip.empty()); | 334 EXPECT_TRUE(tabstrip.empty()); |
| 341 } | 335 } |
| 342 | 336 |
| 343 } // namespace memory | 337 } // namespace memory |
| OLD | NEW |