summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/cocoa/autofill/new_credit_card_bubble_cocoa_unittest.mm
blob: a263acd835be319ccbc4bf140e0f9a8a923eb1cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/cocoa/autofill/new_credit_card_bubble_cocoa.h"

#import <Cocoa/Cocoa.h>
#include "base/compiler_specific.h"
#include "base/mac/foundation_util.h"
#include "base/mac/scoped_nsautorelease_pool.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "chrome/browser/ui/autofill/new_credit_card_bubble_controller.h"
#include "chrome/browser/ui/autofill/new_credit_card_bubble_view.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/cocoa/browser_window_controller.h"
#include "chrome/browser/ui/cocoa/cocoa_profile_test.h"
#import "chrome/browser/ui/cocoa/info_bubble_window.h"
#include "chrome/test/base/testing_profile.h"
#include "content/public/browser/site_instance.h"
#include "content/public/browser/web_contents.h"
#include "testing/gtest_mac.h"
using autofill::NewCreditCardBubbleView;
using autofill::NewCreditCardBubbleController;
using autofill::NewCreditCardBubbleCocoa;
using content::SiteInstance;
using content::WebContents;

namespace autofill {

class NewCreditCardBubbleCocoaUnitTest : public CocoaProfileTest {
 public:
  void SetUp() override {
    CocoaProfileTest::SetUp();
    ASSERT_TRUE(profile());

    site_instance_ = SiteInstance::Create(profile());
  }

  NewCreditCardBubbleController* CreateController(WebContents* contents) {
    return new NewCreditCardBubbleController(contents);
  }

  WebContents* AppendToTabStrip() {
    WebContents* web_contents = WebContents::Create(
        content::WebContents::CreateParams(profile(), site_instance_.get()));
    browser()->tab_strip_model()->AppendWebContents(
        web_contents, /*foreground=*/true);
    return web_contents;
 }

 private:
  scoped_refptr<SiteInstance> site_instance_;
};

TEST_F(NewCreditCardBubbleCocoaUnitTest, CloseDeletes) {
  base::mac::ScopedNSAutoreleasePool autorelease_pool;
  NSWindow* window = browser()->window()->GetNativeWindow();
  WebContents* web_contents = AppendToTabStrip();
  NewCreditCardBubbleController* controller = CreateController(web_contents);
  base::WeakPtr<NewCreditCardBubbleView> view =
      NewCreditCardBubbleView::Create(controller);
  autorelease_pool.Recycle();
  ASSERT_TRUE(view.get());

  // Now reach deep into |view| and pre-create the bubble controller...
  NewCreditCardBubbleCocoa* cocoa_view =
      static_cast<NewCreditCardBubbleCocoa*>(view.get());
  cocoa_view->CreateCocoaController(window);

  // ... so window animations can be disabled, guaranteeing prompt deallocation.
  InfoBubbleWindow* bubble_window = cocoa_view->GetInfoBubbleWindow();
  [bubble_window setAllowedAnimations:info_bubble::kAnimateNone];

  view->Show();
  autorelease_pool.Recycle();
  ASSERT_TRUE(view.get());

  view->Hide();
  autorelease_pool.Recycle();
  ASSERT_FALSE(view.get());

  // |controller| gets deleted via the bubble closing.
}

}  // autofill