- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / autofill / new_credit_card_bubble_cocoa_unittest.mm
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 "chrome/browser/ui/cocoa/autofill/new_credit_card_bubble_cocoa.h"
6
7 #import <Cocoa/Cocoa.h>
8 #include "base/compiler_specific.h"
9 #include "base/gtest_prod_util.h"
10 #include "base/mac/foundation_util.h"
11 #include "base/mac/scoped_nsautorelease_pool.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/weak_ptr.h"
14 #include "chrome/browser/ui/autofill/new_credit_card_bubble_controller.h"
15 #include "chrome/browser/ui/autofill/new_credit_card_bubble_view.h"
16 #include "chrome/browser/ui/browser.h"
17 #include "chrome/browser/ui/browser_window.h"
18 #include "chrome/browser/ui/cocoa/browser_window_controller.h"
19 #include "chrome/browser/ui/cocoa/cocoa_profile_test.h"
20 #import "chrome/browser/ui/cocoa/info_bubble_window.h"
21 #include "chrome/test/base/testing_profile.h"
22 #include "content/public/browser/site_instance.h"
23 #include "content/public/browser/web_contents.h"
24 #include "testing/gtest_mac.h"
25 using autofill::NewCreditCardBubbleView;
26 using autofill::NewCreditCardBubbleController;
27 using autofill::NewCreditCardBubbleCocoa;
28 using content::SiteInstance;
29 using content::WebContents;
30
31 namespace autofill {
32
33 class NewCreditCardBubbleCocoaUnitTest : public CocoaProfileTest {
34  public:
35   virtual void SetUp() OVERRIDE {
36     CocoaProfileTest::SetUp();
37     ASSERT_TRUE(profile());
38
39     site_instance_ = SiteInstance::Create(profile());
40   }
41
42   NewCreditCardBubbleController* CreateController(WebContents* contents) {
43     return new NewCreditCardBubbleController(contents);
44   }
45
46   WebContents* AppendToTabStrip() {
47     WebContents* web_contents = WebContents::Create(
48         content::WebContents::CreateParams(profile(), site_instance_.get()));
49     browser()->tab_strip_model()->AppendWebContents(
50         web_contents, /*foreground=*/true);
51     return web_contents;
52  }
53
54  private:
55   scoped_refptr<SiteInstance> site_instance_;
56 };
57
58 TEST_F(NewCreditCardBubbleCocoaUnitTest, CloseDeletes) {
59   base::mac::ScopedNSAutoreleasePool autorelease_pool;
60   NSWindow* window = browser()->window()->GetNativeWindow();
61   BrowserWindowController* bwc = [BrowserWindowController
62       browserWindowControllerForWindow:window];
63   [bwc showWindow:nil];
64
65   WebContents* web_contents = AppendToTabStrip();
66   NewCreditCardBubbleController* controller = CreateController(web_contents);
67   base::WeakPtr<NewCreditCardBubbleView> view =
68       NewCreditCardBubbleView::Create(controller);
69   autorelease_pool.Recycle();
70   ASSERT_TRUE(view.get());
71
72   // Now reach deep into |view| and pre-create the bubble controller...
73   NewCreditCardBubbleCocoa* cocoa_view =
74       static_cast<NewCreditCardBubbleCocoa*>(view.get());
75   cocoa_view->CreateCocoaController(window);
76
77   // ... so window animations can be disabled, guaranteeing prompt deallocation.
78   InfoBubbleWindow* bubble_window =
79       base::mac::ObjCCastStrict<InfoBubbleWindow>(
80           [cocoa_view->bubbleController_ window]);
81   [bubble_window setAllowedAnimations:info_bubble::kAnimateNone];
82
83   view->Show();
84   autorelease_pool.Recycle();
85   ASSERT_TRUE(view.get());
86
87   view->Hide();
88   autorelease_pool.Recycle();
89   ASSERT_FALSE(view.get());
90
91   // |controller| gets magically deleted via the bubble closing. And since it
92   // doesn't expose weak ptrs or callbacks, there's no way to verify it except
93   // hope for the memory bots.
94 }
95
96 }  // autofill