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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <stddef.h>
#include <list>
#include <vector>
#include "base/command_line.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/defaults.h"
#include "chrome/browser/prefs/session_startup_pref.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/test_launcher_utils.h"
#include "components/prefs/pref_service.h"
#include "components/sessions/core/serialized_navigation_entry_test_helper.h"
#include "content/public/browser/notification_service.h"
#include "content/public/test/test_utils.h"
#include "ui/wm/core/wm_core_switches.h"
namespace {
const char* test_app_popup_name1 = "TestApp1";
const char* test_app_popup_name2 = "TestApp2";
}
class SessionRestoreTestChromeOS : public InProcessBrowserTest {
public:
~SessionRestoreTestChromeOS() override {}
protected:
void SetUpDefaultCommandLine(base::CommandLine* command_line) override {
base::CommandLine default_command_line(base::CommandLine::NO_PROGRAM);
InProcessBrowserTest::SetUpDefaultCommandLine(&default_command_line);
// Animations have caused crashes in session restore in the past but are
// usually disabled in tests. Remove --wm-window-animations-disabled to
// re-enable animations.
test_launcher_utils::RemoveCommandLineSwitch(
default_command_line, wm::switches::kWindowAnimationsDisabled,
command_line);
}
Browser* CreateBrowserWithParams(Browser::CreateParams params) {
Browser* browser = new Browser(params);
AddBlankTabAndShow(browser);
browser_list_.push_back(browser);
return browser;
}
bool CloseBrowser(Browser* browser) {
for (std::list<Browser*>::iterator iter = browser_list_.begin();
iter != browser_list_.end(); ++iter) {
if (*iter == browser) {
CloseBrowserSynchronously(*iter);
browser_list_.erase(iter);
return true;
}
}
return false;
}
Browser::CreateParams CreateParamsForApp(const std::string name,
bool trusted) {
return Browser::CreateParams::CreateForApp(name, trusted, gfx::Rect(),
profile());
}
// Turn on session restore before we restart.
void TurnOnSessionRestore() {
SessionStartupPref::SetStartupPref(
browser()->profile(), SessionStartupPref(SessionStartupPref::LAST));
}
Profile* profile() { return browser()->profile(); }
std::list<Browser*> browser_list_;
};
// Thse tests are in pairs. The PRE_ test creates some browser windows and
// the following test confirms that the correct windows are restored after a
// restart.
IN_PROC_BROWSER_TEST_F(SessionRestoreTestChromeOS, PRE_RestoreBrowserWindows) {
// One browser window is always created by default.
EXPECT_TRUE(browser());
// Create a second normal browser window.
CreateBrowserWithParams(Browser::CreateParams(profile()));
// Create a third incognito browser window which should not get restored.
CreateBrowserWithParams(
Browser::CreateParams(profile()->GetOffTheRecordProfile()));
TurnOnSessionRestore();
}
IN_PROC_BROWSER_TEST_F(SessionRestoreTestChromeOS, RestoreBrowserWindows) {
size_t total_count = 0;
size_t incognito_count = 0;
for (auto* browser : *BrowserList::GetInstance()) {
++total_count;
if (browser->profile()->IsOffTheRecord())
++incognito_count;
}
EXPECT_EQ(2u, total_count);
EXPECT_EQ(0u, incognito_count);
}
IN_PROC_BROWSER_TEST_F(SessionRestoreTestChromeOS, PRE_RestoreAppsV1) {
// Create a trusted app popup.
CreateBrowserWithParams(CreateParamsForApp(test_app_popup_name1, true));
// Create a second trusted app with two popup windows.
CreateBrowserWithParams(CreateParamsForApp(test_app_popup_name2, true));
CreateBrowserWithParams(CreateParamsForApp(test_app_popup_name2, true));
// Create a third untrusted (child) app3 popup. This should not get restored.
CreateBrowserWithParams(CreateParamsForApp(test_app_popup_name2, false));
TurnOnSessionRestore();
}
IN_PROC_BROWSER_TEST_F(SessionRestoreTestChromeOS, RestoreAppsV1) {
size_t total_count = 0;
size_t app1_count = 0;
size_t app2_count = 0;
for (auto* browser : *BrowserList::GetInstance()) {
++total_count;
if (browser->app_name() == test_app_popup_name1)
++app1_count;
if (browser->app_name() == test_app_popup_name2)
++app2_count;
}
EXPECT_EQ(1u, app1_count);
EXPECT_EQ(2u, app2_count); // Only the trusted app windows are restored.
EXPECT_EQ(4u, total_count); // Default browser() + 3 app windows
}
IN_PROC_BROWSER_TEST_F(SessionRestoreTestChromeOS, PRE_RestoreMaximized) {
// One browser window is always created by default.
ASSERT_TRUE(browser());
// Create a second browser window and maximize it.
Browser* browser2 = CreateBrowserWithParams(Browser::CreateParams(profile()));
browser2->window()->Maximize();
// Create two app popup windows and maximize the second one.
Browser* app_browser1 =
CreateBrowserWithParams(CreateParamsForApp(test_app_popup_name1, true));
Browser* app_browser2 =
CreateBrowserWithParams(CreateParamsForApp(test_app_popup_name1, true));
app_browser2->window()->Maximize();
EXPECT_FALSE(browser()->window()->IsMaximized());
EXPECT_TRUE(browser2->window()->IsMaximized());
EXPECT_FALSE(app_browser1->window()->IsMaximized());
EXPECT_TRUE(app_browser2->window()->IsMaximized());
TurnOnSessionRestore();
}
IN_PROC_BROWSER_TEST_F(SessionRestoreTestChromeOS, RestoreMaximized) {
size_t total_count = 0;
size_t maximized_count = 0;
for (auto* browser : *BrowserList::GetInstance()) {
++total_count;
if (browser->window()->IsMaximized())
++maximized_count;
}
EXPECT_EQ(4u, total_count);
EXPECT_EQ(2u, maximized_count);
}
// Test for crash when restoring minimized windows. http://crbug.com/679513.
IN_PROC_BROWSER_TEST_F(SessionRestoreTestChromeOS, PRE_RestoreMinimized) {
// One browser window is always created by default.
ASSERT_TRUE(browser());
browser()->window()->Minimize();
Browser* browser2 = CreateBrowserWithParams(Browser::CreateParams(profile()));
browser2->window()->Minimize();
EXPECT_TRUE(browser()->window()->IsMinimized());
EXPECT_TRUE(browser2->window()->IsMinimized());
TurnOnSessionRestore();
}
IN_PROC_BROWSER_TEST_F(SessionRestoreTestChromeOS, RestoreMinimized) {
size_t total_count = 0;
size_t minimized_count = 0;
for (auto* browser : *BrowserList::GetInstance()) {
++total_count;
if (browser->window()->IsMinimized())
++minimized_count;
}
EXPECT_EQ(2u, total_count);
// Chrome OS always activates the last browser window on login, which results
// in one window being restored. This seems reasonable as it reminds users
// they have a browser running instead of just showing them an empty desktop.
EXPECT_EQ(1u, minimized_count);
}
|