1// Copyright (c) 2013 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 <vector> 6 7#include "base/command_line.h" 8#include "base/files/file_path.h" 9#include "base/message_loop/message_loop.h" 10#include "chrome/browser/browser_process.h" 11#include "chrome/browser/prefs/session_startup_pref.h" 12#include "chrome/browser/profiles/profile_manager.h" 13#include "chrome/browser/sessions/session_restore.h" 14#include "chrome/browser/ui/browser.h" 15#include "chrome/browser/ui/browser_finder.h" 16#include "chrome/browser/ui/browser_window.h" 17#include "chrome/browser/ui/startup/startup_browser_creator.h" 18#include "chrome/test/base/in_process_browser_test.h" 19#include "chrome/test/base/ui_test_utils.h" 20#include "testing/gtest/include/gtest/gtest.h" 21#include "url/gurl.h" 22 23typedef InProcessBrowserTest StartupBrowserCreatorTest; 24 25// Chrome OS doesn't support multiprofile. 26// And BrowserWindow::IsActive() always returns false in tests on MAC. 27// And this test is useless without that functionality. 28#if !defined(OS_CHROMEOS) && !defined(OS_MACOSX) 29IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, LastUsedProfileActivated) { 30 ProfileManager* profile_manager = g_browser_process->profile_manager(); 31 32 // Create 4 profiles, they will be scheduled for destruction when the last 33 // browser window they are associated to will be closed. 34 Profile* profile_1 = profile_manager->GetProfile( 35 profile_manager->user_data_dir().Append(FILE_PATH_LITERAL( 36 "New Profile 1"))); 37 ASSERT_TRUE(profile_1); 38 Profile* profile_2 = profile_manager->GetProfile( 39 profile_manager->user_data_dir().Append(FILE_PATH_LITERAL( 40 "New Profile 2"))); 41 ASSERT_TRUE(profile_2); 42 Profile* profile_3 = profile_manager->GetProfile( 43 profile_manager->user_data_dir().Append(FILE_PATH_LITERAL( 44 "New Profile 3"))); 45 ASSERT_TRUE(profile_3); 46 Profile* profile_4 = profile_manager->GetProfile( 47 profile_manager->user_data_dir().Append(FILE_PATH_LITERAL( 48 "New Profile 4"))); 49 ASSERT_TRUE(profile_4); 50 51 SessionStartupPref pref_urls(SessionStartupPref::URLS); 52 pref_urls.urls.push_back(ui_test_utils::GetTestUrl( 53 base::FilePath(base::FilePath::kCurrentDirectory), 54 base::FilePath(FILE_PATH_LITERAL("title1.html")))); 55 SessionStartupPref::SetStartupPref(profile_1, pref_urls); 56 SessionStartupPref::SetStartupPref(profile_2, pref_urls); 57 SessionStartupPref::SetStartupPref(profile_3, pref_urls); 58 SessionStartupPref::SetStartupPref(profile_4, pref_urls); 59 60 // Do a simple non-process-startup browser launch. 61 CommandLine dummy(CommandLine::NO_PROGRAM); 62 63 int return_code; 64 StartupBrowserCreator browser_creator; 65 std::vector<Profile*> last_opened_profiles; 66 last_opened_profiles.push_back(profile_1); 67 last_opened_profiles.push_back(profile_2); 68 last_opened_profiles.push_back(profile_3); 69 last_opened_profiles.push_back(profile_4); 70 browser_creator.Start(dummy, profile_manager->user_data_dir(), profile_2, 71 last_opened_profiles, &return_code); 72 73 while (!browser_creator.ActivatedProfile()) 74 base::MessageLoop::current()->RunUntilIdle(); 75 76 Browser* new_browser = NULL; 77 78 // The last used profile (the profile_2 in this case) must be active. 79 ASSERT_EQ(1u, chrome::GetBrowserCount(profile_2, 80 browser()->host_desktop_type())); 81 new_browser = FindBrowserWithProfile(profile_2, 82 browser()->host_desktop_type()); 83 ASSERT_TRUE(new_browser); 84 EXPECT_TRUE(new_browser->window()->IsActive()); 85 86 // All other profiles browser should not be active. 87 ASSERT_EQ(1u, chrome::GetBrowserCount(profile_1, 88 browser()->host_desktop_type())); 89 new_browser = FindBrowserWithProfile(profile_1, 90 browser()->host_desktop_type()); 91 ASSERT_TRUE(new_browser); 92 EXPECT_FALSE(new_browser->window()->IsActive()); 93 94 ASSERT_EQ(1u, chrome::GetBrowserCount(profile_3, 95 browser()->host_desktop_type())); 96 new_browser = FindBrowserWithProfile(profile_3, 97 browser()->host_desktop_type()); 98 ASSERT_TRUE(new_browser); 99 EXPECT_FALSE(new_browser->window()->IsActive()); 100 101 ASSERT_EQ(1u, chrome::GetBrowserCount(profile_4, 102 browser()->host_desktop_type())); 103 new_browser = FindBrowserWithProfile(profile_4, 104 browser()->host_desktop_type()); 105 ASSERT_TRUE(new_browser); 106 EXPECT_FALSE(new_browser->window()->IsActive()); 107 108} 109#endif // !OS_MACOSX && !OS_CHROMEOS 110