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.
5 #include "chrome/browser/ui/cocoa/passwords/manage_passwords_bubble_cocoa.h"
7 #import <Cocoa/Cocoa.h>
9 #include "base/compiler_specific.h"
10 #include "base/mac/foundation_util.h"
11 #include "chrome/browser/ui/browser.h"
12 #include "chrome/browser/ui/browser_window.h"
13 #import "chrome/browser/ui/cocoa/browser_window_controller.h"
14 #include "chrome/browser/ui/cocoa/cocoa_profile_test.h"
15 #include "chrome/browser/ui/cocoa/cocoa_test_helper.h"
16 #import "chrome/browser/ui/cocoa/info_bubble_window.h"
17 #include "chrome/browser/ui/cocoa/location_bar/location_bar_view_mac.h"
18 #include "chrome/browser/ui/cocoa/location_bar/manage_passwords_decoration.h"
19 #import "chrome/browser/ui/cocoa/passwords/manage_passwords_bubble_controller.h"
20 #include "chrome/browser/ui/passwords/manage_passwords_bubble.h"
21 #include "chrome/browser/ui/passwords/manage_passwords_ui_controller_mock.h"
22 #include "chrome/browser/ui/tabs/tab_strip_model.h"
23 #include "content/public/browser/site_instance.h"
24 #include "content/public/test/test_browser_thread_bundle.h"
25 #include "content/public/test/web_contents_tester.h"
26 #include "testing/gtest/include/gtest/gtest.h"
27 #include "testing/gtest_mac.h"
28 #include "testing/platform_test.h"
30 class ManagePasswordsBubbleCocoaTest : public CocoaProfileTest {
32 virtual void SetUp() OVERRIDE {
33 CocoaProfileTest::SetUp();
35 // Create the WebContents.
36 siteInstance_ = content::SiteInstance::Create(profile());
37 webContents_ = CreateWebContents();
38 browser()->tab_strip_model()->AppendWebContents(
39 webContents_, /*foreground=*/true);
41 // Create the test UIController here so that it's bound to
42 // |test_web_contents_| and therefore accessible to the model.
43 ManagePasswordsUIControllerMock* ui_controller =
44 new ManagePasswordsUIControllerMock(webContents_);
45 // Set the initial state.
46 ui_controller->SetState(password_manager::ui::PENDING_PASSWORD_STATE);
49 content::WebContents* webContents() { return webContents_; }
51 content::WebContents* CreateWebContents() {
52 return content::WebContents::Create(
53 content::WebContents::CreateParams(profile(), siteInstance_.get()));
57 chrome::ShowManagePasswordsBubble(webContents());
58 if (ManagePasswordsBubbleCocoa::instance()) {
59 // Disable animations so that closing happens immediately.
60 InfoBubbleWindow* bubbleWindow = base::mac::ObjCCast<InfoBubbleWindow>(
61 [ManagePasswordsBubbleCocoa::instance()->controller_ window]);
62 [bubbleWindow setAllowedAnimations:info_bubble::kAnimateNone];
67 ManagePasswordsBubbleCocoa::instance()->Close();
70 NSWindow* bubbleWindow() {
71 ManagePasswordsBubbleCocoa* bubble = ManagePasswordsBubbleCocoa::instance();
72 return bubble ? [bubble->controller_ window] : nil;
76 scoped_refptr<content::SiteInstance> siteInstance_;
77 content::WebContents* webContents_; // weak
80 TEST_F(ManagePasswordsBubbleCocoaTest, ShowShouldCreateAndShowBubble) {
81 EXPECT_FALSE(ManagePasswordsBubbleCocoa::instance());
82 EXPECT_FALSE([bubbleWindow() isVisible]);
84 EXPECT_TRUE(ManagePasswordsBubbleCocoa::instance());
85 EXPECT_TRUE([bubbleWindow() isVisible]);
88 TEST_F(ManagePasswordsBubbleCocoaTest, CloseShouldCloseAndDeleteBubble) {
90 EXPECT_TRUE(ManagePasswordsBubbleCocoa::instance());
91 EXPECT_TRUE([bubbleWindow() isVisible]);
93 EXPECT_FALSE(ManagePasswordsBubbleCocoa::instance());
94 EXPECT_FALSE([bubbleWindow() isVisible]);
97 TEST_F(ManagePasswordsBubbleCocoaTest, BackgroundCloseShouldDeleteBubble) {
99 EXPECT_TRUE(ManagePasswordsBubbleCocoa::instance());
100 EXPECT_TRUE([bubbleWindow() isVisible]);
101 // Close the window directly instead of using the bubble interface.
102 [bubbleWindow() close];
103 EXPECT_FALSE(ManagePasswordsBubbleCocoa::instance());
104 EXPECT_FALSE([bubbleWindow() isVisible]);
107 TEST_F(ManagePasswordsBubbleCocoaTest, ShowBubbleOnInactiveTabShouldDoNothing) {
108 // Start in the tab that we'll try to show the bubble on.
109 TabStripModel* tabStripModel = browser()->tab_strip_model();
110 EXPECT_EQ(0, tabStripModel->active_index());
112 // Open a second tab and make it active.
113 content::WebContents* webContents2 = CreateWebContents();
114 tabStripModel->AppendWebContents(webContents2, /*foreground=*/true);
115 EXPECT_EQ(1, tabStripModel->active_index());
116 EXPECT_EQ(2, tabStripModel->count());
118 // Try to show the bubble on the inactive tab. Nothing should happen.
120 EXPECT_FALSE(ManagePasswordsBubbleCocoa::instance());