1 // Copyright 2014 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/passwords/manage_passwords_view_test.h" 6 7 #include "base/test/statistics_delta_reader.h" 8 #include "chrome/app/chrome_command_ids.h" 9 #include "chrome/browser/ui/browser.h" 10 #include "chrome/browser/ui/browser_command_controller.h" 11 #include "chrome/browser/ui/passwords/manage_passwords_icon.h" 12 #include "chrome/browser/ui/passwords/manage_passwords_ui_controller_mock.h" 13 #include "chrome/browser/ui/tabs/tab_strip_model.h" 14 #include "chrome/browser/ui/views/frame/browser_view.h" 15 #include "chrome/browser/ui/views/passwords/manage_passwords_bubble_view.h" 16 #include "chrome/browser/ui/views/passwords/manage_passwords_icon_view.h" 17 #include "chrome/browser/ui/views/toolbar/toolbar_view.h" 18 #include "chrome/test/base/in_process_browser_test.h" 19 #include "chrome/test/base/interactive_test_utils.h" 20 #include "components/autofill/core/common/password_form.h" 21 #include "components/password_manager/core/browser/password_form_manager.h" 22 #include "components/password_manager/core/browser/password_manager_metrics_util.h" 23 #include "components/password_manager/core/browser/stub_password_manager_client.h" 24 #include "components/password_manager/core/browser/stub_password_manager_driver.h" 25 #include "testing/gtest/include/gtest/gtest.h" 26 27 void ManagePasswordsViewTest::SetUpOnMainThread() { 28 AddTabAtIndex(0, GURL("http://example.com/"), content::PAGE_TRANSITION_TYPED); 29 // Create the test UIController here so that it's bound to the currently 30 // active WebContents. 31 new ManagePasswordsUIControllerMock( 32 browser()->tab_strip_model()->GetActiveWebContents()); 33 } 34 35 ManagePasswordsUIControllerMock* ManagePasswordsViewTest::controller() { 36 return static_cast<ManagePasswordsUIControllerMock*>( 37 ManagePasswordsUIController::FromWebContents( 38 browser()->tab_strip_model()->GetActiveWebContents())); 39 } 40 41 ManagePasswordsIconView* ManagePasswordsViewTest::view() { 42 BrowserView* browser_view = static_cast<BrowserView*>(browser()->window()); 43 return browser_view->GetToolbarView()->location_bar()-> 44 manage_passwords_icon_view(); 45 } 46 47 void ManagePasswordsViewTest::ExecuteManagePasswordsCommand() { 48 // Show the window to ensure that it's active. 49 browser()->window()->Show(); 50 51 CommandUpdater* updater = browser()->command_controller()->command_updater(); 52 EXPECT_TRUE(updater->IsCommandEnabled(IDC_MANAGE_PASSWORDS_FOR_PAGE)); 53 EXPECT_TRUE(updater->ExecuteCommand(IDC_MANAGE_PASSWORDS_FOR_PAGE)); 54 55 // Wait for the command execution to pop up the bubble. 56 content::RunAllPendingInMessageLoop(); 57 } 58 59 void ManagePasswordsViewTest::SetupManagingPasswords() { 60 base::string16 kTestUsername = base::ASCIIToUTF16("test_username"); 61 autofill::PasswordFormMap map; 62 map[kTestUsername] = test_form(); 63 controller()->OnPasswordAutofilled(map); 64 controller()->UpdateIconAndBubbleState(view()); 65 } 66 67 void ManagePasswordsViewTest::SetupPendingPassword() { 68 password_manager::StubPasswordManagerClient client; 69 password_manager::StubPasswordManagerDriver driver; 70 scoped_ptr<password_manager::PasswordFormManager> test_form_manager( 71 new password_manager::PasswordFormManager( 72 NULL, &client, &driver, *test_form(), false)); 73 controller()->OnPasswordSubmitted(test_form_manager.release()); 74 75 // Wait for the command execution triggered by the automatic popup to pop up 76 // the bubble. 77 content::RunAllPendingInMessageLoop(); 78 controller()->UpdateIconAndBubbleState(view()); 79 } 80 81 void ManagePasswordsViewTest::SetupBlackistedPassword() { 82 base::string16 kTestUsername = base::ASCIIToUTF16("test_username"); 83 autofill::PasswordFormMap map; 84 map[kTestUsername] = test_form(); 85 controller()->OnBlacklistBlockedAutofill(map); 86 controller()->UpdateIconAndBubbleState(view()); 87 } 88 89 base::HistogramSamples* ManagePasswordsViewTest::GetSamples( 90 const char* histogram) { 91 // Ensure that everything has been properly recorded before pulling samples. 92 content::RunAllPendingInMessageLoop(); 93 return statistics_reader_.GetHistogramSamplesSinceCreation( 94 histogram).release(); 95 } 96