Home | History | Annotate | Download | only in sessions
      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/prefs/pref_service.h"
      9 #include "chrome/browser/browser_process.h"
     10 #include "chrome/browser/chrome_notification_types.h"
     11 #include "chrome/browser/defaults.h"
     12 #include "chrome/browser/ui/browser.h"
     13 #include "chrome/browser/ui/browser_iterator.h"
     14 #include "chrome/browser/ui/browser_window.h"
     15 #include "chrome/browser/ui/host_desktop.h"
     16 #include "chrome/common/chrome_paths.h"
     17 #include "chrome/common/pref_names.h"
     18 #include "chrome/test/base/in_process_browser_test.h"
     19 #include "components/sessions/serialized_navigation_entry_test_helper.h"
     20 #include "content/public/browser/notification_service.h"
     21 #include "content/public/test/test_utils.h"
     22 
     23 namespace {
     24 const char* test_app_popup_name1 = "TestApp1";
     25 const char* test_app_popup_name2 = "TestApp2";
     26 }
     27 
     28 class SessionRestoreTestChromeOS : public InProcessBrowserTest {
     29  public:
     30   virtual ~SessionRestoreTestChromeOS() {}
     31 
     32  protected:
     33   virtual void SetUpCommandLine(base::CommandLine* command_line) OVERRIDE {
     34     InProcessBrowserTest::SetUpCommandLine(command_line);
     35   }
     36 
     37   virtual void SetUpOnMainThread() OVERRIDE {
     38     InProcessBrowserTest::SetUpOnMainThread();
     39   }
     40 
     41   virtual void TearDownOnMainThread() OVERRIDE {
     42     InProcessBrowserTest::TearDownOnMainThread();
     43     for (std::list<Browser*>::iterator iter = browser_list_.begin();
     44          iter != browser_list_.end(); ++iter) {
     45       CloseBrowserSynchronously(*iter);
     46     }
     47     browser_list_.clear();
     48   }
     49 
     50   Browser* CreateBrowserWithParams(Browser::CreateParams params) {
     51     Browser* browser = new Browser(params);
     52     AddBlankTabAndShow(browser);
     53     browser_list_.push_back(browser);
     54     return browser;
     55   }
     56 
     57   bool CloseBrowser(Browser* browser) {
     58     for (std::list<Browser*>::iterator iter = browser_list_.begin();
     59          iter != browser_list_.end(); ++iter) {
     60       if (*iter == browser) {
     61         CloseBrowserSynchronously(*iter);
     62         browser_list_.erase(iter);
     63         return true;
     64       }
     65     }
     66     return false;
     67   }
     68 
     69   void CloseBrowserSynchronously(Browser* browser) {
     70     content::WindowedNotificationObserver observer(
     71         chrome::NOTIFICATION_BROWSER_CLOSED,
     72         content::NotificationService::AllSources());
     73     browser->window()->Close();
     74     observer.Wait();
     75   }
     76 
     77   Browser::CreateParams CreateParamsForApp(const std::string name,
     78                                            bool trusted) {
     79     return Browser::CreateParams::CreateForApp(
     80         name, trusted, gfx::Rect(), profile(), chrome::GetActiveDesktop());
     81   }
     82 
     83   // Simluate restarting the browser
     84   void SetRestart() {
     85     PrefService* pref_service = g_browser_process->local_state();
     86     pref_service->SetBoolean(prefs::kWasRestarted, true);
     87   }
     88 
     89   Profile* profile() { return browser()->profile(); }
     90 
     91   std::list<Browser*> browser_list_;
     92 };
     93 
     94 // Thse tests are in pairs. The PRE_ test creates some browser windows and
     95 // the following test confirms that the correct windows are restored after a
     96 // restart.
     97 
     98 IN_PROC_BROWSER_TEST_F(SessionRestoreTestChromeOS, PRE_RestoreBrowserWindows) {
     99   // One browser window is always created by default.
    100   EXPECT_TRUE(browser());
    101   // Create a second normal browser window.
    102   CreateBrowserWithParams(
    103       Browser::CreateParams(profile(), chrome::GetActiveDesktop()));
    104   // Create a third incognito browser window which should not get restored.
    105   CreateBrowserWithParams(Browser::CreateParams(
    106       profile()->GetOffTheRecordProfile(), chrome::GetActiveDesktop()));
    107   SetRestart();
    108 }
    109 
    110 IN_PROC_BROWSER_TEST_F(SessionRestoreTestChromeOS, RestoreBrowserWindows) {
    111   size_t total_count = 0;
    112   size_t incognito_count = 0;
    113   for (chrome::BrowserIterator it; !it.done(); it.Next()) {
    114     ++total_count;
    115     if (it->profile()->IsOffTheRecord())
    116       ++incognito_count;
    117   }
    118   EXPECT_EQ(2u, total_count);
    119   EXPECT_EQ(0u, incognito_count);
    120 }
    121 
    122 IN_PROC_BROWSER_TEST_F(SessionRestoreTestChromeOS, PRE_RestoreAppsV1) {
    123   // Create a trusted app popup.
    124   CreateBrowserWithParams(CreateParamsForApp(test_app_popup_name1, true));
    125   // Create a second trusted app with two popup windows.
    126   CreateBrowserWithParams(CreateParamsForApp(test_app_popup_name2, true));
    127   CreateBrowserWithParams(CreateParamsForApp(test_app_popup_name2, true));
    128   // Create a third untrusted (child) app3 popup. This should not get restored.
    129   CreateBrowserWithParams(CreateParamsForApp(test_app_popup_name2, false));
    130 
    131   SetRestart();
    132 }
    133 
    134 IN_PROC_BROWSER_TEST_F(SessionRestoreTestChromeOS, RestoreAppsV1) {
    135   size_t total_count = 0;
    136   size_t app1_count = 0;
    137   size_t app2_count = 0;
    138   for (chrome::BrowserIterator it; !it.done(); it.Next()) {
    139     ++total_count;
    140     if (it->app_name() == test_app_popup_name1)
    141       ++app1_count;
    142     if (it->app_name() == test_app_popup_name2)
    143       ++app2_count;
    144   }
    145   EXPECT_EQ(1u, app1_count);
    146   EXPECT_EQ(2u, app2_count);  // Only the trusted app windows are restored.
    147   EXPECT_EQ(4u, total_count);  // Default browser() + 3 app windows
    148 }
    149 
    150 IN_PROC_BROWSER_TEST_F(SessionRestoreTestChromeOS, PRE_RestoreMaximized) {
    151   // One browser window is always created by default.
    152   ASSERT_TRUE(browser());
    153   // Create a second browser window and maximize it.
    154   Browser* browser2 = CreateBrowserWithParams(
    155       Browser::CreateParams(profile(), chrome::GetActiveDesktop()));
    156   browser2->window()->Maximize();
    157 
    158   // Create two app popup windows and maximize the second one.
    159   Browser* app_browser1 =
    160       CreateBrowserWithParams(CreateParamsForApp(test_app_popup_name1, true));
    161   Browser* app_browser2 =
    162       CreateBrowserWithParams(CreateParamsForApp(test_app_popup_name1, true));
    163   app_browser2->window()->Maximize();
    164 
    165   EXPECT_FALSE(browser()->window()->IsMaximized());
    166   EXPECT_TRUE(browser2->window()->IsMaximized());
    167   EXPECT_FALSE(app_browser1->window()->IsMaximized());
    168   EXPECT_TRUE(app_browser2->window()->IsMaximized());
    169 
    170   SetRestart();
    171 }
    172 
    173 IN_PROC_BROWSER_TEST_F(SessionRestoreTestChromeOS, RestoreMaximized) {
    174   size_t total_count = 0;
    175   size_t maximized_count = 0;
    176   for (chrome::BrowserIterator it; !it.done(); it.Next()) {
    177     ++total_count;
    178     if (it->window()->IsMaximized())
    179       ++maximized_count;
    180   }
    181   EXPECT_EQ(4u, total_count);
    182   EXPECT_EQ(2u, maximized_count);
    183 }
    184