Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / passwords / manage_passwords_bubble_cocoa_unittest.mm
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/cocoa/passwords/manage_passwords_bubble_cocoa.h"
6
7 #import <Cocoa/Cocoa.h>
8
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"
29
30 class ManagePasswordsBubbleCocoaTest : public CocoaProfileTest {
31  public:
32   virtual void SetUp() OVERRIDE {
33     CocoaProfileTest::SetUp();
34
35     // Create the WebContents.
36     siteInstance_ = content::SiteInstance::Create(profile());
37     webContents_ = CreateWebContents();
38     browser()->tab_strip_model()->AppendWebContents(
39         webContents_, /*foreground=*/true);
40
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);
47   }
48
49   content::WebContents* webContents() { return webContents_; }
50
51   content::WebContents* CreateWebContents() {
52     return content::WebContents::Create(
53         content::WebContents::CreateParams(profile(), siteInstance_.get()));
54   }
55
56   void ShowBubble() {
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];
63     }
64   }
65
66   void CloseBubble() {
67     ManagePasswordsBubbleCocoa::instance()->Close();
68   }
69
70   NSWindow* bubbleWindow() {
71     ManagePasswordsBubbleCocoa* bubble = ManagePasswordsBubbleCocoa::instance();
72     return bubble ? [bubble->controller_ window] : nil;
73   }
74
75  private:
76   scoped_refptr<content::SiteInstance> siteInstance_;
77   content::WebContents* webContents_;  // weak
78 };
79
80 TEST_F(ManagePasswordsBubbleCocoaTest, ShowShouldCreateAndShowBubble) {
81   EXPECT_FALSE(ManagePasswordsBubbleCocoa::instance());
82   EXPECT_FALSE([bubbleWindow() isVisible]);
83   ShowBubble();
84   EXPECT_TRUE(ManagePasswordsBubbleCocoa::instance());
85   EXPECT_TRUE([bubbleWindow() isVisible]);
86 }
87
88 TEST_F(ManagePasswordsBubbleCocoaTest, CloseShouldCloseAndDeleteBubble) {
89   ShowBubble();
90   EXPECT_TRUE(ManagePasswordsBubbleCocoa::instance());
91   EXPECT_TRUE([bubbleWindow() isVisible]);
92   CloseBubble();
93   EXPECT_FALSE(ManagePasswordsBubbleCocoa::instance());
94   EXPECT_FALSE([bubbleWindow() isVisible]);
95 }
96
97 TEST_F(ManagePasswordsBubbleCocoaTest, BackgroundCloseShouldDeleteBubble) {
98   ShowBubble();
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]);
105 }
106
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());
111
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());
117
118   // Try to show the bubble on the inactive tab. Nothing should happen.
119   ShowBubble();
120   EXPECT_FALSE(ManagePasswordsBubbleCocoa::instance());
121 }