Home | History | Annotate | Download | only in profiles
      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 "chrome/browser/profiles/avatar_menu_model.h"
      6 
      7 #include "base/command_line.h"
      8 #include "base/path_service.h"
      9 #include "chrome/browser/chrome_notification_types.h"
     10 #include "chrome/browser/profiles/profile_manager.h"
     11 #include "chrome/browser/profiles/profiles_state.h"
     12 #include "chrome/browser/ui/browser.h"
     13 #include "chrome/browser/ui/browser_list.h"
     14 #include "chrome/common/chrome_paths.h"
     15 #include "chrome/test/base/in_process_browser_test.h"
     16 #include "chrome/test/base/test_switches.h"
     17 #include "chrome/test/base/testing_browser_process.h"
     18 #include "content/public/test/test_utils.h"
     19 
     20 namespace {
     21 
     22 // An observer that returns back to test code after a new profile is
     23 // initialized.
     24 void OnUnblockOnProfileCreation(Profile* profile,
     25                                 Profile::CreateStatus status) {
     26   if (status == Profile::CREATE_STATUS_INITIALIZED)
     27     base::MessageLoop::current()->Quit();
     28 }
     29 
     30 typedef InProcessBrowserTest AvatarMenuModelTest;
     31 
     32 IN_PROC_BROWSER_TEST_F(AvatarMenuModelTest, SignOut) {
     33   if (!profiles::IsMultipleProfilesEnabled())
     34     return;
     35 
     36   ProfileManager* profile_manager = g_browser_process->profile_manager();
     37   Profile* current_profile = browser()->profile();
     38   ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
     39   size_t index = cache.GetIndexOfProfileWithPath(current_profile->GetPath());
     40 
     41   AvatarMenuModel model(&cache, NULL, browser());
     42 
     43   BrowserList* browser_list =
     44       BrowserList::GetInstance(chrome::GetActiveDesktop());
     45   EXPECT_EQ(1U, browser_list->size());
     46   content::WindowedNotificationObserver window_close_observer(
     47       chrome::NOTIFICATION_BROWSER_CLOSED,
     48       content::Source<Browser>(browser()));
     49 
     50   EXPECT_FALSE(cache.ProfileIsSigninRequiredAtIndex(index));
     51   model.SetLogoutURL("about:blank");
     52   model.BeginSignOut();
     53   EXPECT_TRUE(cache.ProfileIsSigninRequiredAtIndex(index));
     54 
     55   window_close_observer.Wait();  // rely on test time-out for failure indication
     56   EXPECT_EQ(0U, browser_list->size());
     57 }
     58 
     59 IN_PROC_BROWSER_TEST_F(AvatarMenuModelTest, SwitchToProfile) {
     60 #if defined(OS_WIN) && defined(USE_ASH)
     61   // Disable this test in Metro+Ash for now (http://crbug.com/262796).
     62   if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
     63     return;
     64 #endif
     65 
     66   if (!profiles::IsMultipleProfilesEnabled())
     67     return;
     68 
     69   ProfileManager* profile_manager = g_browser_process->profile_manager();
     70   Profile* current_profile = browser()->profile();
     71   ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
     72   base::FilePath path_profile1 = current_profile->GetPath();
     73   base::FilePath user_dir = cache.GetUserDataDir();
     74 
     75   // Create an additional profile.
     76   base::FilePath path_profile2 = user_dir.Append(
     77       FILE_PATH_LITERAL("New Profile 2"));
     78   profile_manager->CreateProfileAsync(path_profile2,
     79                                       base::Bind(&OnUnblockOnProfileCreation),
     80                                       string16(), string16(), std::string());
     81 
     82   // Spin to allow profile creation to take place, loop is terminated
     83   // by OnUnblockOnProfileCreation when the profile is created.
     84   content::RunMessageLoop();
     85   ASSERT_EQ(cache.GetNumberOfProfiles(), 2U);
     86 
     87   AvatarMenuModel model(&cache, NULL, browser());
     88   BrowserList* browser_list =
     89       BrowserList::GetInstance(chrome::GetActiveDesktop());
     90   EXPECT_EQ(1U, browser_list->size());
     91   EXPECT_EQ(path_profile1, browser_list->get(0)->profile()->GetPath());
     92 
     93   // Open a browser window for the first profile.
     94   model.SwitchToProfile(cache.GetIndexOfProfileWithPath(path_profile1), false);
     95   EXPECT_EQ(1U, browser_list->size());
     96   EXPECT_EQ(path_profile1, browser_list->get(0)->profile()->GetPath());
     97 
     98   // Open a browser window for the second profile.
     99   model.SwitchToProfile(cache.GetIndexOfProfileWithPath(path_profile2), false);
    100   EXPECT_EQ(2U, browser_list->size());
    101 
    102   // Switch to the first profile without opening a new window.
    103   model.SwitchToProfile(cache.GetIndexOfProfileWithPath(path_profile1), false);
    104   EXPECT_EQ(2U, browser_list->size());
    105   EXPECT_EQ(path_profile1, browser_list->get(0)->profile()->GetPath());
    106   EXPECT_EQ(path_profile2, browser_list->get(1)->profile()->GetPath());
    107 
    108 }
    109 
    110 }  // namespace
    111