Home | History | Annotate | Download | only in multi_user
      1 // Copyright 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 "ash/shell.h"
      6 #include "ash/test/ash_test_base.h"
      7 #include "ash/test/test_session_state_delegate.h"
      8 #include "ash/test/test_shell_delegate.h"
      9 #include "base/command_line.h"
     10 #include "base/compiler_specific.h"
     11 #include "base/logging.h"
     12 #include "chrome/browser/ui/ash/multi_user/multi_user_context_menu.h"
     13 #include "chrome/browser/ui/ash/multi_user/multi_user_window_manager.h"
     14 #include "chrome/browser/ui/ash/multi_user/multi_user_window_manager_chromeos.h"
     15 #include "chrome/common/chrome_switches.h"
     16 #include "ui/base/models/menu_model.h"
     17 #include "ui/base/ui_base_types.h"
     18 
     19 namespace ash {
     20 namespace test {
     21 
     22 // A test class for preparing the chrome::MultiUserContextMenu.
     23 class MultiUserContextMenuChromeOSTest : public AshTestBase {
     24  public:
     25   MultiUserContextMenuChromeOSTest() : multi_user_window_manager_(NULL) {}
     26 
     27   virtual void SetUp() OVERRIDE;
     28   virtual void TearDown() OVERRIDE;
     29 
     30  protected:
     31   // Set up the test environment for this many windows.
     32   void SetUpForThisManyWindows(int windows);
     33 
     34   aura::Window* window() { return window_; }
     35   chrome::MultiUserWindowManagerChromeOS* multi_user_window_manager() {
     36     return multi_user_window_manager_;
     37   }
     38 
     39   void SetNumberOfUsers(int users) {
     40     ash::test::TestSessionStateDelegate* delegate =
     41         static_cast<ash::test::TestSessionStateDelegate*>(
     42             ash::Shell::GetInstance()->session_state_delegate());
     43     delegate->set_logged_in_users(users);
     44   }
     45 
     46  private:
     47   // A window which can be used for testing.
     48   aura::Window* window_;
     49 
     50   // The instance of the MultiUserWindowManager.
     51   chrome::MultiUserWindowManagerChromeOS* multi_user_window_manager_;
     52 
     53   DISALLOW_COPY_AND_ASSIGN(MultiUserContextMenuChromeOSTest);
     54 };
     55 
     56 void MultiUserContextMenuChromeOSTest::SetUp() {
     57   AshTestBase::SetUp();
     58 
     59   window_ = CreateTestWindowInShellWithId(0);
     60   window_->Show();
     61 
     62   multi_user_window_manager_ = new chrome::MultiUserWindowManagerChromeOS("A");
     63   chrome::MultiUserWindowManager::SetInstanceForTest(multi_user_window_manager_,
     64         chrome::MultiUserWindowManager::MULTI_PROFILE_MODE_SEPARATED);
     65   EXPECT_TRUE(multi_user_window_manager_);
     66 }
     67 
     68 void MultiUserContextMenuChromeOSTest::TearDown() {
     69   delete window_;
     70 
     71   chrome::MultiUserWindowManager::DeleteInstance();
     72   AshTestBase::TearDown();
     73 }
     74 
     75 // Check that an unowned window will never create a menu.
     76 TEST_F(MultiUserContextMenuChromeOSTest, UnownedWindow) {
     77   EXPECT_EQ(NULL, CreateMultiUserContextMenu(window()).get());
     78 
     79   // Add more users.
     80   SetNumberOfUsers(2);
     81   EXPECT_EQ(NULL, CreateMultiUserContextMenu(window()).get());
     82 }
     83 
     84 // Check that an owned window will never create a menu.
     85 TEST_F(MultiUserContextMenuChromeOSTest, OwnedWindow) {
     86   // Make the window owned and check that there is no menu (since only a single
     87   // user exists).
     88   multi_user_window_manager()->SetWindowOwner(window(), "A");
     89   EXPECT_EQ(NULL, CreateMultiUserContextMenu(window()).get());
     90 
     91   // After adding another user a menu should get created.
     92   {
     93     SetNumberOfUsers(2);
     94     scoped_ptr<ui::MenuModel> menu = CreateMultiUserContextMenu(window());
     95     ASSERT_TRUE(menu.get());
     96     EXPECT_EQ(1, menu.get()->GetItemCount());
     97   }
     98   {
     99     SetNumberOfUsers(3);
    100     scoped_ptr<ui::MenuModel> menu = CreateMultiUserContextMenu(window());
    101     ASSERT_TRUE(menu.get());
    102     EXPECT_EQ(2, menu.get()->GetItemCount());
    103   }
    104 }
    105 
    106 }  // namespace test
    107 }  // namespace ash
    108