Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / sessions / session_restore_browsertest_chromeos.cc
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.
4
5 #include <list>
6 #include <vector>
7
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"
23
24 namespace {
25 const char* test_app_popup_name1 = "TestApp1";
26 const char* test_app_popup_name2 = "TestApp2";
27 }
28
29 class SessionRestoreTestChromeOS : public InProcessBrowserTest {
30  public:
31   virtual ~SessionRestoreTestChromeOS() {}
32
33  protected:
34   virtual void SetUpCommandLine(base::CommandLine* command_line) override {
35     InProcessBrowserTest::SetUpCommandLine(command_line);
36   }
37
38   Browser* CreateBrowserWithParams(Browser::CreateParams params) {
39     Browser* browser = new Browser(params);
40     AddBlankTabAndShow(browser);
41     browser_list_.push_back(browser);
42     return browser;
43   }
44
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);
51         return true;
52       }
53     }
54     return false;
55   }
56
57   void CloseBrowserSynchronously(Browser* browser) {
58     content::WindowedNotificationObserver observer(
59         chrome::NOTIFICATION_BROWSER_CLOSED,
60         content::NotificationService::AllSources());
61     browser->window()->Close();
62     observer.Wait();
63   }
64
65   Browser::CreateParams CreateParamsForApp(const std::string name,
66                                            bool trusted) {
67     return Browser::CreateParams::CreateForApp(
68         name, trusted, gfx::Rect(), profile(), chrome::GetActiveDesktop());
69   }
70
71   // Turn on session restore before we restart.
72   void TurnOnSessionRestore() {
73     SessionStartupPref::SetStartupPref(
74         browser()->profile(), SessionStartupPref(SessionStartupPref::LAST));
75   }
76
77   Profile* profile() { return browser()->profile(); }
78
79   std::list<Browser*> browser_list_;
80 };
81
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
84 // restart.
85
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();
96 }
97
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()) {
102     ++total_count;
103     if (it->profile()->IsOffTheRecord())
104       ++incognito_count;
105   }
106   EXPECT_EQ(2u, total_count);
107   EXPECT_EQ(0u, incognito_count);
108 }
109
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));
118
119   TurnOnSessionRestore();
120 }
121
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()) {
127     ++total_count;
128     if (it->app_name() == test_app_popup_name1)
129       ++app1_count;
130     if (it->app_name() == test_app_popup_name2)
131       ++app2_count;
132   }
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
136 }
137
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();
145
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();
152
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());
157
158   TurnOnSessionRestore();
159 }
160
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()) {
165     ++total_count;
166     if (it->window()->IsMaximized())
167       ++maximized_count;
168   }
169   EXPECT_EQ(4u, total_count);
170   EXPECT_EQ(2u, maximized_count);
171 }