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
3 // found in the LICENSE file.
8 #include "base/command_line.h"
9 #include "base/prefs/pref_service.h"
10 #include "chrome/browser/browser_process.h"
11 #include "chrome/browser/chrome_notification_types.h"
12 #include "chrome/browser/defaults.h"
13 #include "chrome/browser/ui/browser.h"
14 #include "chrome/browser/ui/browser_iterator.h"
15 #include "chrome/browser/ui/browser_window.h"
16 #include "chrome/browser/ui/host_desktop.h"
17 #include "chrome/common/chrome_paths.h"
18 #include "chrome/common/chrome_switches.h"
19 #include "chrome/test/base/in_process_browser_test.h"
20 #include "components/sessions/serialized_navigation_entry_test_helper.h"
21 #include "content/public/browser/notification_service.h"
22 #include "content/public/test/test_utils.h"
25 const char* test_app_popup_name1
= "TestApp1";
26 const char* test_app_popup_name2
= "TestApp2";
29 class SessionRestoreTestChromeOS
: public InProcessBrowserTest
{
31 ~SessionRestoreTestChromeOS() override
{}
34 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
35 InProcessBrowserTest::SetUpCommandLine(command_line
);
38 Browser
* CreateBrowserWithParams(Browser::CreateParams params
) {
39 Browser
* browser
= new Browser(params
);
40 AddBlankTabAndShow(browser
);
41 browser_list_
.push_back(browser
);
45 bool CloseBrowser(Browser
* browser
) {
46 for (std::list
<Browser
*>::iterator iter
= browser_list_
.begin();
47 iter
!= browser_list_
.end(); ++iter
) {
48 if (*iter
== browser
) {
49 CloseBrowserSynchronously(*iter
);
50 browser_list_
.erase(iter
);
57 void CloseBrowserSynchronously(Browser
* browser
) {
58 content::WindowedNotificationObserver
observer(
59 chrome::NOTIFICATION_BROWSER_CLOSED
,
60 content::NotificationService::AllSources());
61 browser
->window()->Close();
65 Browser::CreateParams
CreateParamsForApp(const std::string name
,
67 return Browser::CreateParams::CreateForApp(
68 name
, trusted
, gfx::Rect(), profile(), chrome::GetActiveDesktop());
71 // Turn on session restore before we restart.
72 void TurnOnSessionRestore() {
73 SessionStartupPref::SetStartupPref(
74 browser()->profile(), SessionStartupPref(SessionStartupPref::LAST
));
77 Profile
* profile() { return browser()->profile(); }
79 std::list
<Browser
*> browser_list_
;
82 // Thse tests are in pairs. The PRE_ test creates some browser windows and
83 // the following test confirms that the correct windows are restored after a
86 IN_PROC_BROWSER_TEST_F(SessionRestoreTestChromeOS
, PRE_RestoreBrowserWindows
) {
87 // One browser window is always created by default.
88 EXPECT_TRUE(browser());
89 // Create a second normal browser window.
90 CreateBrowserWithParams(
91 Browser::CreateParams(profile(), chrome::GetActiveDesktop()));
92 // Create a third incognito browser window which should not get restored.
93 CreateBrowserWithParams(Browser::CreateParams(
94 profile()->GetOffTheRecordProfile(), chrome::GetActiveDesktop()));
95 TurnOnSessionRestore();
98 IN_PROC_BROWSER_TEST_F(SessionRestoreTestChromeOS
, RestoreBrowserWindows
) {
99 size_t total_count
= 0;
100 size_t incognito_count
= 0;
101 for (chrome::BrowserIterator it
; !it
.done(); it
.Next()) {
103 if (it
->profile()->IsOffTheRecord())
106 EXPECT_EQ(2u, total_count
);
107 EXPECT_EQ(0u, incognito_count
);
110 IN_PROC_BROWSER_TEST_F(SessionRestoreTestChromeOS
, PRE_RestoreAppsV1
) {
111 // Create a trusted app popup.
112 CreateBrowserWithParams(CreateParamsForApp(test_app_popup_name1
, true));
113 // Create a second trusted app with two popup windows.
114 CreateBrowserWithParams(CreateParamsForApp(test_app_popup_name2
, true));
115 CreateBrowserWithParams(CreateParamsForApp(test_app_popup_name2
, true));
116 // Create a third untrusted (child) app3 popup. This should not get restored.
117 CreateBrowserWithParams(CreateParamsForApp(test_app_popup_name2
, false));
119 TurnOnSessionRestore();
122 IN_PROC_BROWSER_TEST_F(SessionRestoreTestChromeOS
, RestoreAppsV1
) {
123 size_t total_count
= 0;
124 size_t app1_count
= 0;
125 size_t app2_count
= 0;
126 for (chrome::BrowserIterator it
; !it
.done(); it
.Next()) {
128 if (it
->app_name() == test_app_popup_name1
)
130 if (it
->app_name() == test_app_popup_name2
)
133 EXPECT_EQ(1u, app1_count
);
134 EXPECT_EQ(2u, app2_count
); // Only the trusted app windows are restored.
135 EXPECT_EQ(4u, total_count
); // Default browser() + 3 app windows
138 IN_PROC_BROWSER_TEST_F(SessionRestoreTestChromeOS
, PRE_RestoreMaximized
) {
139 // One browser window is always created by default.
140 ASSERT_TRUE(browser());
141 // Create a second browser window and maximize it.
142 Browser
* browser2
= CreateBrowserWithParams(
143 Browser::CreateParams(profile(), chrome::GetActiveDesktop()));
144 browser2
->window()->Maximize();
146 // Create two app popup windows and maximize the second one.
147 Browser
* app_browser1
=
148 CreateBrowserWithParams(CreateParamsForApp(test_app_popup_name1
, true));
149 Browser
* app_browser2
=
150 CreateBrowserWithParams(CreateParamsForApp(test_app_popup_name1
, true));
151 app_browser2
->window()->Maximize();
153 EXPECT_FALSE(browser()->window()->IsMaximized());
154 EXPECT_TRUE(browser2
->window()->IsMaximized());
155 EXPECT_FALSE(app_browser1
->window()->IsMaximized());
156 EXPECT_TRUE(app_browser2
->window()->IsMaximized());
158 TurnOnSessionRestore();
161 IN_PROC_BROWSER_TEST_F(SessionRestoreTestChromeOS
, RestoreMaximized
) {
162 size_t total_count
= 0;
163 size_t maximized_count
= 0;
164 for (chrome::BrowserIterator it
; !it
.done(); it
.Next()) {
166 if (it
->window()->IsMaximized())
169 EXPECT_EQ(4u, total_count
);
170 EXPECT_EQ(2u, maximized_count
);