Adding instrumentation to locate the source of jankiness
[chromium-blink-merge.git] / chrome / browser / sessions / session_restore_browsertest_chromeos.cc
blobb26f53ca0b535fecc97a54e98bfa1f29a685843f
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.
5 #include <list>
6 #include <vector>
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/common/pref_names.h"
20 #include "chrome/test/base/in_process_browser_test.h"
21 #include "components/sessions/serialized_navigation_entry_test_helper.h"
22 #include "content/public/browser/notification_service.h"
23 #include "content/public/test/test_utils.h"
25 namespace {
26 const char* test_app_popup_name1 = "TestApp1";
27 const char* test_app_popup_name2 = "TestApp2";
30 class SessionRestoreTestChromeOS : public InProcessBrowserTest {
31 public:
32 virtual ~SessionRestoreTestChromeOS() {}
34 protected:
35 virtual void SetUpCommandLine(base::CommandLine* command_line) override {
36 InProcessBrowserTest::SetUpCommandLine(command_line);
39 Browser* CreateBrowserWithParams(Browser::CreateParams params) {
40 Browser* browser = new Browser(params);
41 AddBlankTabAndShow(browser);
42 browser_list_.push_back(browser);
43 return browser;
46 bool CloseBrowser(Browser* browser) {
47 for (std::list<Browser*>::iterator iter = browser_list_.begin();
48 iter != browser_list_.end(); ++iter) {
49 if (*iter == browser) {
50 CloseBrowserSynchronously(*iter);
51 browser_list_.erase(iter);
52 return true;
55 return false;
58 void CloseBrowserSynchronously(Browser* browser) {
59 content::WindowedNotificationObserver observer(
60 chrome::NOTIFICATION_BROWSER_CLOSED,
61 content::NotificationService::AllSources());
62 browser->window()->Close();
63 observer.Wait();
66 Browser::CreateParams CreateParamsForApp(const std::string name,
67 bool trusted) {
68 return Browser::CreateParams::CreateForApp(
69 name, trusted, gfx::Rect(), profile(), chrome::GetActiveDesktop());
72 // Turn on session restore before we restart.
73 void TurnOnSessionRestore() {
74 SessionStartupPref::SetStartupPref(
75 browser()->profile(), SessionStartupPref(SessionStartupPref::LAST));
78 Profile* profile() { return browser()->profile(); }
80 std::list<Browser*> browser_list_;
83 // Thse tests are in pairs. The PRE_ test creates some browser windows and
84 // the following test confirms that the correct windows are restored after a
85 // restart.
87 IN_PROC_BROWSER_TEST_F(SessionRestoreTestChromeOS, PRE_RestoreBrowserWindows) {
88 // One browser window is always created by default.
89 EXPECT_TRUE(browser());
90 // Create a second normal browser window.
91 CreateBrowserWithParams(
92 Browser::CreateParams(profile(), chrome::GetActiveDesktop()));
93 // Create a third incognito browser window which should not get restored.
94 CreateBrowserWithParams(Browser::CreateParams(
95 profile()->GetOffTheRecordProfile(), chrome::GetActiveDesktop()));
96 TurnOnSessionRestore();
99 IN_PROC_BROWSER_TEST_F(SessionRestoreTestChromeOS, RestoreBrowserWindows) {
100 size_t total_count = 0;
101 size_t incognito_count = 0;
102 for (chrome::BrowserIterator it; !it.done(); it.Next()) {
103 ++total_count;
104 if (it->profile()->IsOffTheRecord())
105 ++incognito_count;
107 EXPECT_EQ(2u, total_count);
108 EXPECT_EQ(0u, incognito_count);
111 IN_PROC_BROWSER_TEST_F(SessionRestoreTestChromeOS, PRE_RestoreAppsV1) {
112 // Create a trusted app popup.
113 CreateBrowserWithParams(CreateParamsForApp(test_app_popup_name1, true));
114 // Create a second trusted app with two popup windows.
115 CreateBrowserWithParams(CreateParamsForApp(test_app_popup_name2, true));
116 CreateBrowserWithParams(CreateParamsForApp(test_app_popup_name2, true));
117 // Create a third untrusted (child) app3 popup. This should not get restored.
118 CreateBrowserWithParams(CreateParamsForApp(test_app_popup_name2, false));
120 TurnOnSessionRestore();
123 IN_PROC_BROWSER_TEST_F(SessionRestoreTestChromeOS, RestoreAppsV1) {
124 size_t total_count = 0;
125 size_t app1_count = 0;
126 size_t app2_count = 0;
127 for (chrome::BrowserIterator it; !it.done(); it.Next()) {
128 ++total_count;
129 if (it->app_name() == test_app_popup_name1)
130 ++app1_count;
131 if (it->app_name() == test_app_popup_name2)
132 ++app2_count;
134 EXPECT_EQ(1u, app1_count);
135 EXPECT_EQ(2u, app2_count); // Only the trusted app windows are restored.
136 EXPECT_EQ(4u, total_count); // Default browser() + 3 app windows
139 IN_PROC_BROWSER_TEST_F(SessionRestoreTestChromeOS, PRE_RestoreMaximized) {
140 // One browser window is always created by default.
141 ASSERT_TRUE(browser());
142 // Create a second browser window and maximize it.
143 Browser* browser2 = CreateBrowserWithParams(
144 Browser::CreateParams(profile(), chrome::GetActiveDesktop()));
145 browser2->window()->Maximize();
147 // Create two app popup windows and maximize the second one.
148 Browser* app_browser1 =
149 CreateBrowserWithParams(CreateParamsForApp(test_app_popup_name1, true));
150 Browser* app_browser2 =
151 CreateBrowserWithParams(CreateParamsForApp(test_app_popup_name1, true));
152 app_browser2->window()->Maximize();
154 EXPECT_FALSE(browser()->window()->IsMaximized());
155 EXPECT_TRUE(browser2->window()->IsMaximized());
156 EXPECT_FALSE(app_browser1->window()->IsMaximized());
157 EXPECT_TRUE(app_browser2->window()->IsMaximized());
159 TurnOnSessionRestore();
162 IN_PROC_BROWSER_TEST_F(SessionRestoreTestChromeOS, RestoreMaximized) {
163 size_t total_count = 0;
164 size_t maximized_count = 0;
165 for (chrome::BrowserIterator it; !it.done(); it.Next()) {
166 ++total_count;
167 if (it->window()->IsMaximized())
168 ++maximized_count;
170 EXPECT_EQ(4u, total_count);
171 EXPECT_EQ(2u, maximized_count);