Home | History | Annotate | Download | only in views
      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 "chrome/browser/ui/views/avatar_menu_button.h"
      6 
      7 #include "base/command_line.h"
      8 #include "base/path_service.h"
      9 #include "base/strings/utf_string_conversions.h"
     10 #include "chrome/browser/chrome_notification_types.h"
     11 #include "chrome/browser/profiles/avatar_menu_model.h"
     12 #include "chrome/browser/profiles/profile_manager.h"
     13 #include "chrome/browser/profiles/profiles_state.h"
     14 #include "chrome/browser/ui/browser_list.h"
     15 #include "chrome/browser/ui/tabs/tab_strip_model.h"
     16 #include "chrome/browser/ui/views/avatar_menu_bubble_view.h"
     17 #include "chrome/browser/ui/views/frame/browser_view.h"
     18 #include "chrome/browser/ui/views/profile_chooser_view.h"
     19 #include "chrome/common/chrome_paths.h"
     20 #include "chrome/common/chrome_switches.h"
     21 #include "chrome/common/url_constants.h"
     22 #include "chrome/test/base/in_process_browser_test.h"
     23 #include "chrome/test/base/test_switches.h"
     24 #include "chrome/test/base/testing_browser_process.h"
     25 #include "content/public/test/test_utils.h"
     26 #include "grit/generated_resources.h"
     27 #include "ui/base/l10n/l10n_util.h"
     28 #include "ui/views/controls/button/label_button.h"
     29 
     30 class AvatarMenuButtonTest : public InProcessBrowserTest,
     31                              public testing::WithParamInterface<bool> {
     32  public:
     33   AvatarMenuButtonTest();
     34   virtual ~AvatarMenuButtonTest();
     35 
     36  protected:
     37   virtual void SetUp() OVERRIDE;
     38 
     39   bool UsingNewProfileChooser();
     40   void CreateTestingProfile();
     41   AvatarMenuButton* GetAvatarMenuButton();
     42   void StartAvatarMenu();
     43 
     44  private:
     45   DISALLOW_COPY_AND_ASSIGN(AvatarMenuButtonTest);
     46 };
     47 
     48 AvatarMenuButtonTest::AvatarMenuButtonTest() {
     49 }
     50 
     51 AvatarMenuButtonTest::~AvatarMenuButtonTest() {
     52 }
     53 
     54 void AvatarMenuButtonTest::SetUp() {
     55   if (GetParam()) {
     56     if (!UsingNewProfileChooser()) {
     57       CommandLine::ForCurrentProcess()->AppendSwitch(
     58         switches::kNewProfileManagement);
     59     }
     60     DCHECK(UsingNewProfileChooser());
     61   } else {
     62     DCHECK(!UsingNewProfileChooser());
     63   }
     64 
     65   InProcessBrowserTest::SetUp();
     66 }
     67 
     68 bool AvatarMenuButtonTest::UsingNewProfileChooser() {
     69   return CommandLine::ForCurrentProcess()->HasSwitch(
     70     switches::kNewProfileManagement);
     71 }
     72 
     73 void AvatarMenuButtonTest::CreateTestingProfile() {
     74   ProfileManager* profile_manager = g_browser_process->profile_manager();
     75   EXPECT_EQ(1u, profile_manager->GetNumberOfProfiles());
     76 
     77   base::FilePath path;
     78   PathService::Get(chrome::DIR_USER_DATA, &path);
     79   path = path.AppendASCII("test_profile");
     80   if (!base::PathExists(path))
     81     ASSERT_TRUE(file_util::CreateDirectory(path));
     82   Profile* profile =
     83       Profile::CreateProfile(path, NULL, Profile::CREATE_MODE_SYNCHRONOUS);
     84   profile_manager->RegisterTestingProfile(profile, true, false);
     85 
     86   EXPECT_EQ(2u, profile_manager->GetNumberOfProfiles());
     87 }
     88 
     89 AvatarMenuButton* AvatarMenuButtonTest::GetAvatarMenuButton() {
     90   BrowserView* browser_view = reinterpret_cast<BrowserView*>(
     91       browser()->window());
     92   return browser_view->frame()->GetAvatarMenuButton();
     93 }
     94 
     95 void AvatarMenuButtonTest::StartAvatarMenu() {
     96   AvatarMenuButton* button = GetAvatarMenuButton();
     97   ASSERT_TRUE(button);
     98 
     99   AvatarMenuBubbleView::set_close_on_deactivate(false);
    100   ProfileChooserView::set_close_on_deactivate(false);
    101   static_cast<views::MenuButtonListener*>(button)->OnMenuButtonClicked(
    102       NULL, gfx::Point());
    103   base::MessageLoop::current()->RunUntilIdle();
    104   EXPECT_NE(AvatarMenuBubbleView::IsShowing(),
    105             ProfileChooserView::IsShowing());
    106 }
    107 
    108 IN_PROC_BROWSER_TEST_P(AvatarMenuButtonTest, HideOnSecondClick) {
    109 #if defined(OS_WIN) && defined(USE_ASH)
    110   // Disable this test in Metro+Ash for now (http://crbug.com/262796).
    111   if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
    112     return;
    113 #endif
    114 
    115   if (!profiles::IsMultipleProfilesEnabled() ||
    116       UsingNewProfileChooser()) {
    117     return;
    118   }
    119 
    120   CreateTestingProfile();
    121   StartAvatarMenu();
    122 
    123   // Verify that clicking again doesn't reshow it.
    124   AvatarMenuButton* button = GetAvatarMenuButton();
    125   static_cast<views::MenuButtonListener*>(button)->OnMenuButtonClicked(
    126       NULL, gfx::Point());
    127   // Hide the bubble manually. In the browser this would normally happen during
    128   // the event processing.
    129   AvatarMenuBubbleView::Hide();
    130   base::MessageLoop::current()->RunUntilIdle();
    131   EXPECT_FALSE(AvatarMenuBubbleView::IsShowing());
    132   EXPECT_FALSE(ProfileChooserView::IsShowing());
    133 }
    134 
    135 IN_PROC_BROWSER_TEST_P(AvatarMenuButtonTest, NewSignOut) {
    136   if (!profiles::IsMultipleProfilesEnabled() ||
    137       !UsingNewProfileChooser()) {
    138     return;
    139   }
    140 
    141   CreateTestingProfile();
    142   StartAvatarMenu();
    143 
    144   BrowserList* browser_list =
    145       BrowserList::GetInstance(chrome::GetActiveDesktop());
    146   EXPECT_EQ(1U, browser_list->size());
    147   content::WindowedNotificationObserver window_close_observer(
    148       chrome::NOTIFICATION_BROWSER_CLOSED,
    149       content::Source<Browser>(browser()));
    150 
    151   AvatarMenuModel* model =
    152       ProfileChooserView::profile_bubble_->avatar_menu_model_.get();
    153   const AvatarMenuModel::Item& model_item_before =
    154       model->GetItemAt(model->GetActiveProfileIndex());
    155   EXPECT_FALSE(model_item_before.signin_required);
    156 
    157   ui::MouseEvent mouse_ev(ui::ET_MOUSE_RELEASED, gfx::Point(), gfx::Point(), 0);
    158   model->SetLogoutURL("about:blank");
    159   ProfileChooserView::profile_bubble_->ButtonPressed(
    160       static_cast<views::Button*>(
    161           ProfileChooserView::profile_bubble_->signout_current_profile_view_),
    162       mouse_ev);
    163 
    164   EXPECT_TRUE(model->GetItemAt(model->GetActiveProfileIndex()).signin_required);
    165 
    166   window_close_observer.Wait();  // Rely on test timeout for failure indication.
    167   EXPECT_TRUE(browser_list->empty());
    168 }
    169 
    170 IN_PROC_BROWSER_TEST_P(AvatarMenuButtonTest, LaunchUserManagerScreen) {
    171   if (!profiles::IsMultipleProfilesEnabled() ||
    172       !UsingNewProfileChooser()) {
    173     return;
    174   }
    175 
    176   CreateTestingProfile();
    177   StartAvatarMenu();
    178 
    179   int starting_tab_count = browser()->tab_strip_model()->count();
    180   BrowserList* browser_list =
    181       BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_NATIVE);
    182   EXPECT_EQ(1U, browser_list->size());
    183 
    184   ui::MouseEvent mouse_ev(ui::ET_MOUSE_RELEASED, gfx::Point(), gfx::Point(), 0);
    185   ProfileChooserView::profile_bubble_->ButtonPressed(
    186       static_cast<views::Button*>(
    187           ProfileChooserView::profile_bubble_->users_button_view_),
    188       mouse_ev);
    189 
    190   // The user manager screen should go into a new, active tab.
    191   int final_tab_count = browser()->tab_strip_model()->count();
    192   EXPECT_EQ(1U, browser_list->size());
    193   EXPECT_EQ(starting_tab_count + 1, final_tab_count);
    194 
    195   GURL tab_url = browser()->tab_strip_model()->GetActiveWebContents()->GetURL();
    196   EXPECT_EQ(std::string(chrome::kChromeUIUserManagerURL), tab_url.spec());
    197 }
    198 
    199 INSTANTIATE_TEST_CASE_P(Old, AvatarMenuButtonTest, testing::Values(false));
    200 INSTANTIATE_TEST_CASE_P(New, AvatarMenuButtonTest, testing::Values(true));
    201