1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
|
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <memory>
#include <unordered_map>
#include "base/run_loop.h"
#include "build/build_config.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/prefs/session_startup_pref.h"
#include "chrome/browser/profiles/keep_alive/profile_keep_alive_types.h"
#include "chrome/browser/profiles/keep_alive/scoped_profile_keep_alive.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/resource_coordinator/tab_load_tracker_test_support.h"
#include "chrome/browser/resource_coordinator/tab_manager.h"
#include "chrome/browser/sessions/session_restore.h"
#include "chrome/browser/sessions/session_restore_observer.h"
#include "chrome/browser/sessions/session_restore_test_helper.h"
#include "chrome/browser/sessions/session_service_factory.h"
#include "chrome/browser/sessions/session_service_test_helper.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_tabstrip.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/keep_alive_registry/keep_alive_types.h"
#include "components/keep_alive_registry/scoped_keep_alive.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "testing/gtest/include/gtest/gtest.h"
using content::WebContents;
using content::NavigationHandle;
class MockSessionRestoreObserver : public SessionRestoreObserver {
public:
MockSessionRestoreObserver() { SessionRestore::AddObserver(this); }
MockSessionRestoreObserver(const MockSessionRestoreObserver&) = delete;
MockSessionRestoreObserver& operator=(const MockSessionRestoreObserver&) =
delete;
~MockSessionRestoreObserver() { SessionRestore::RemoveObserver(this); }
enum class SessionRestoreEvent { kStartedLoadingTabs, kFinishedLoadingTabs };
const std::vector<SessionRestoreEvent>& session_restore_events() const {
return session_restore_events_;
}
// SessionRestoreObserver implementation:
void OnSessionRestoreStartedLoadingTabs() override {
session_restore_events_.emplace_back(
SessionRestoreEvent::kStartedLoadingTabs);
}
void OnSessionRestoreFinishedLoadingTabs() override {
session_restore_events_.emplace_back(
SessionRestoreEvent::kFinishedLoadingTabs);
}
private:
std::vector<SessionRestoreEvent> session_restore_events_;
};
class SessionRestoreObserverTest : public InProcessBrowserTest {
protected:
SessionRestoreObserverTest() = default;
SessionRestoreObserverTest(const SessionRestoreObserverTest&) = delete;
SessionRestoreObserverTest& operator=(const SessionRestoreObserverTest&) =
delete;
void SetUpOnMainThread() override {
SessionStartupPref pref(SessionStartupPref::LAST);
SessionStartupPref::SetStartupPref(browser()->profile(), pref);
#if BUILDFLAG(IS_CHROMEOS)
SessionServiceTestHelper helper(
SessionServiceFactory::GetForProfile(browser()->profile()));
helper.SetForceBrowserNotAliveWithNoWindows(true);
#endif
ASSERT_TRUE(embedded_test_server()->Start());
}
Browser* QuitBrowserAndRestore(Browser* browser) {
Profile* profile = browser->profile();
auto keep_alive = std::make_unique<ScopedKeepAlive>(
KeepAliveOrigin::SESSION_RESTORE, KeepAliveRestartOption::DISABLED);
auto profile_keep_alive = std::make_unique<ScopedProfileKeepAlive>(
profile, ProfileKeepAliveOrigin::kBrowserWindow);
CloseBrowserSynchronously(browser);
// Create a new window, which should trigger session restore.
chrome::NewEmptyWindow(profile);
SessionRestoreTestHelper().Wait();
return BrowserList::GetInstance()->GetLastActive();
}
void WaitForTabsToLoad(Browser* browser) {
for (int i = 0; i < browser->tab_strip_model()->count(); ++i) {
WebContents* contents = browser->tab_strip_model()->GetWebContentsAt(i);
contents->GetController().LoadIfNecessary();
resource_coordinator::WaitForTransitionToLoaded(contents);
}
}
GURL GetTestURL() const {
return embedded_test_server()->GetURL("/title1.html");
}
const std::vector<MockSessionRestoreObserver::SessionRestoreEvent>&
session_restore_events() const {
return mock_observer_.session_restore_events();
}
size_t number_of_session_restore_events() const {
return session_restore_events().size();
}
MockSessionRestoreObserver& session_restore_observer() {
return mock_observer_;
}
private:
MockSessionRestoreObserver mock_observer_;
};
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
#define MAYBE_SingleTabSessionRestore DISABLED_SingleTabSessionRestore
#else
#define MAYBE_SingleTabSessionRestore SingleTabSessionRestore
#endif
IN_PROC_BROWSER_TEST_F(SessionRestoreObserverTest,
MAYBE_SingleTabSessionRestore) {
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), GetTestURL()));
Browser* new_browser = QuitBrowserAndRestore(browser());
// The restored browser should have 1 tab.
TabStripModel* tab_strip = new_browser->tab_strip_model();
ASSERT_TRUE(tab_strip);
ASSERT_EQ(1, tab_strip->count());
ASSERT_EQ(1u, number_of_session_restore_events());
EXPECT_EQ(
MockSessionRestoreObserver::SessionRestoreEvent::kStartedLoadingTabs,
session_restore_events()[0]);
ASSERT_NO_FATAL_FAILURE(WaitForTabsToLoad(new_browser));
ASSERT_EQ(2u, number_of_session_restore_events());
EXPECT_EQ(
MockSessionRestoreObserver::SessionRestoreEvent::kFinishedLoadingTabs,
session_restore_events()[1]);
}
IN_PROC_BROWSER_TEST_F(SessionRestoreObserverTest, MultipleTabSessionRestore) {
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), GetTestURL()));
ui_test_utils::NavigateToURLWithDisposition(
browser(), GetTestURL(), WindowOpenDisposition::NEW_BACKGROUND_TAB,
ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP);
Browser* new_browser = QuitBrowserAndRestore(browser());
// The restored browser should have 2 tabs.
TabStripModel* tab_strip = new_browser->tab_strip_model();
ASSERT_TRUE(tab_strip);
ASSERT_EQ(2, tab_strip->count());
ASSERT_EQ(1u, number_of_session_restore_events());
EXPECT_EQ(
MockSessionRestoreObserver::SessionRestoreEvent::kStartedLoadingTabs,
session_restore_events()[0]);
ASSERT_NO_FATAL_FAILURE(WaitForTabsToLoad(new_browser));
ASSERT_EQ(2u, number_of_session_restore_events());
EXPECT_EQ(
MockSessionRestoreObserver::SessionRestoreEvent::kFinishedLoadingTabs,
session_restore_events()[1]);
}
|