Commit 465a0eb0 authored by Mark Cogan's avatar Mark Cogan Committed by Commit Bot

[iOS] Add TabModel unit tests.

Per comments on the CL to change the ownership of WebStateLists
(crrev.com/c/1796358), there are a couple of nuances to behavior
in TabModel that aren't currently covered by unit tests.

First, multiple calls to -browserStateDestroyed must be safe. Since
possible failure modes are DCHECKS or outright segfaults, the test just
creates and populates a tab model, then calls -browserStateDestroyed
twice.

Second, empty sessions should be saved (meaning that when the user has
no open tabs, restoring the session should not show any of the previously
open tabs). The test here is also straightforward: force-save a non-empty
session, close all of the tabs, force-save again, restore, and verify
that there are no open tabs.

Change-Id: I3b40d8fc6f60ca51bfaebeb704324a04d60bd7d7
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1819239Reviewed-by: default avatarMohammad Refaat <mrefaat@chromium.org>
Commit-Queue: Mark Cogan <marq@chromium.org>
Cr-Commit-Position: refs/heads/master@{#699238}
parent 7ef4fdc6
...@@ -175,6 +175,18 @@ TEST_P(TabModelTest, InsertUrlSingle) { ...@@ -175,6 +175,18 @@ TEST_P(TabModelTest, InsertUrlSingle) {
EXPECT_EQ(web_state, tab_model_.webStateList->GetWebStateAt(0)); EXPECT_EQ(web_state, tab_model_.webStateList->GetWebStateAt(0));
} }
TEST_P(TabModelTest, BrowserStateDestroyedMultiple) {
[tab_model_ insertWebStateWithURL:GURL(kURL1)
referrer:web::Referrer()
transition:ui::PAGE_TRANSITION_TYPED
opener:nil
openedByDOM:NO
atIndex:0
inBackground:NO];
[tab_model_ browserStateDestroyed];
[tab_model_ browserStateDestroyed];
}
TEST_P(TabModelTest, InsertUrlMultiple) { TEST_P(TabModelTest, InsertUrlMultiple) {
web::WebState* web_state0 = web::WebState* web_state0 =
[tab_model_ insertWebStateWithURL:GURL(kURL1) [tab_model_ insertWebStateWithURL:GURL(kURL1)
...@@ -610,6 +622,45 @@ TEST_P(TabModelTest, AddWithOrderController) { ...@@ -610,6 +622,45 @@ TEST_P(TabModelTest, AddWithOrderController) {
tab_model_.webStateList->GetIndexOfWebState(web_state3) + 1); tab_model_.webStateList->GetIndexOfWebState(web_state3) + 1);
} }
// Test that saving a non-empty session, then saving an empty session, then
// restoring, restores zero tabs, and not the non-empty session.
TEST_P(TabModelTest, RestorePersistedSessionAfterEmpty) {
// Reset the TabModel with a custom SessionServiceIOS (to control whether
// data is saved to disk).
TestSessionService* test_session_service = [[TestSessionService alloc] init];
SetTabModel(CreateTabModel(test_session_service, nil));
[tab_model_ insertWebStateWithURL:GURL(kURL1)
referrer:web::Referrer()
transition:ui::PAGE_TRANSITION_TYPED
opener:nil
openedByDOM:NO
atIndex:0
inBackground:NO];
[test_session_service setPerformIO:YES];
[tab_model_ saveSessionImmediately:YES];
[test_session_service setPerformIO:NO];
// Session should be saved, now remove the tab.
[tab_model_ closeTabAtIndex:0];
[test_session_service setPerformIO:YES];
[tab_model_ saveSessionImmediately:YES];
[test_session_service setPerformIO:NO];
// Restore, expect that there are no sessions.
NSString* state_path = base::SysUTF8ToNSString(
chrome_browser_state_->GetStatePath().AsUTF8Unsafe());
SessionIOS* session =
[test_session_service loadSessionFromDirectory:state_path];
ASSERT_EQ(1u, session.sessionWindows.count);
SessionWindowIOS* session_window = session.sessionWindows[0];
[tab_model_ restoreSessionWindow:session_window forInitialRestore:NO];
EXPECT_EQ(0U, [tab_model_ count]);
EXPECT_TRUE([[NSFileManager defaultManager] removeItemAtPath:state_path
error:nullptr]);
}
TEST_P(TabModelTest, DISABLED_PersistSelectionChange) { TEST_P(TabModelTest, DISABLED_PersistSelectionChange) {
NSString* stashPath = NSString* stashPath =
base::SysUTF8ToNSString(chrome_browser_state_->GetStatePath().value()); base::SysUTF8ToNSString(chrome_browser_state_->GetStatePath().value());
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment