new_credit_card_bubble_cocoa_unittest.mm revision f2477e01787aa58f445919b809d89e252beef54f
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"
25using autofill::NewCreditCardBubbleView;
26using autofill::NewCreditCardBubbleController;
27using autofill::NewCreditCardBubbleCocoa;
28using content::SiteInstance;
29using content::WebContents;
30
31namespace autofill {
32
33class 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
58TEST_F(NewCreditCardBubbleCocoaUnitTest, CloseDeletes) {
59  base::mac::ScopedNSAutoreleasePool autorelease_pool;
60  NSWindow* window = browser()->window()->GetNativeWindow();
61  WebContents* web_contents = AppendToTabStrip();
62  NewCreditCardBubbleController* controller = CreateController(web_contents);
63  base::WeakPtr<NewCreditCardBubbleView> view =
64      NewCreditCardBubbleView::Create(controller);
65  autorelease_pool.Recycle();
66  ASSERT_TRUE(view.get());
67
68  // Now reach deep into |view| and pre-create the bubble controller...
69  NewCreditCardBubbleCocoa* cocoa_view =
70      static_cast<NewCreditCardBubbleCocoa*>(view.get());
71  cocoa_view->CreateCocoaController(window);
72
73  // ... so window animations can be disabled, guaranteeing prompt deallocation.
74  InfoBubbleWindow* bubble_window =
75      base::mac::ObjCCastStrict<InfoBubbleWindow>(
76          [cocoa_view->bubbleController_ window]);
77  [bubble_window setAllowedAnimations:info_bubble::kAnimateNone];
78
79  view->Show();
80  autorelease_pool.Recycle();
81  ASSERT_TRUE(view.get());
82
83  view->Hide();
84  autorelease_pool.Recycle();
85  ASSERT_FALSE(view.get());
86
87  // |controller| gets deleted via the bubble closing.
88}
89
90}  // autofill
91