1 // Copyright (c) 2012 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 "base/memory/scoped_ptr.h"
6 #include "base/strings/utf_string_conversions.h"
7 #include "chrome/browser/notifications/balloon.h"
8 #include "chrome/browser/notifications/balloon_collection.h"
9 #include "chrome/browser/notifications/notification.h"
10 #include "chrome/browser/notifications/notification_object_proxy.h"
11 #include "chrome/browser/ui/browser.h"
12 #include "chrome/browser/ui/cocoa/cocoa_test_helper.h"
13 #include "chrome/browser/ui/cocoa/notifications/balloon_controller.h"
14 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
15 #include "chrome/test/base/test_browser_window.h"
16 #include "chrome/test/base/testing_profile.h"
18 // Subclass balloon controller and mock out the initialization of the RVH.
19 @interface TestBalloonController : BalloonController {
21 - (void)initializeHost;
24 @implementation TestBalloonController
25 - (void)initializeHost {}
30 // Use a dummy balloon collection for testing.
31 class MockBalloonCollection : public BalloonCollection {
32 virtual void Add(const Notification& notification,
33 Profile* profile) OVERRIDE {}
34 virtual const Notification* FindById(const std::string& id) const OVERRIDE {
37 virtual bool RemoveById(const std::string& id) OVERRIDE { return false; }
38 virtual bool RemoveBySourceOrigin(const GURL& origin) OVERRIDE {
41 virtual bool RemoveByProfile(Profile* profile) OVERRIDE { return false; }
42 virtual void RemoveAll() OVERRIDE {}
43 virtual bool HasSpace() const OVERRIDE { return true; }
44 virtual void ResizeBalloon(
46 const gfx::Size& size) OVERRIDE {
48 virtual void DisplayChanged() OVERRIDE {}
49 virtual void SetPositionPreference(PositionPreference preference) OVERRIDE {}
50 virtual void OnBalloonClosed(Balloon* source) OVERRIDE {};
51 virtual const Balloons& GetActiveBalloons() OVERRIDE {
59 class BalloonControllerTest : public ChromeRenderViewHostTestHarness {
60 virtual void SetUp() OVERRIDE {
61 ChromeRenderViewHostTestHarness::SetUp();
62 CocoaTest::BootstrapCocoa();
63 Browser::CreateParams native_params(profile(), chrome::GetActiveDesktop());
65 chrome::CreateBrowserWithTestWindowForParams(&native_params));
66 collection_.reset(new MockBalloonCollection());
69 virtual void TearDown() OVERRIDE {
72 ChromeRenderViewHostTestHarness::TearDown();
76 scoped_ptr<Browser> browser_;
77 scoped_ptr<BalloonCollection> collection_;
80 TEST_F(BalloonControllerTest, ShowAndCloseTest) {
81 Notification n(GURL("http://www.google.com"), GURL("http://www.google.com"),
82 base::ASCIIToUTF16("http://www.google.com"), base::string16(),
83 new NotificationObjectProxy(-1, -1, -1, false));
84 scoped_ptr<Balloon> balloon(
85 new Balloon(n, profile(), collection_.get()));
86 balloon->SetPosition(gfx::Point(1, 1), false);
87 balloon->set_content_size(gfx::Size(100, 100));
89 BalloonController* controller =
90 [[TestBalloonController alloc] initWithBalloon:balloon.get()];
92 [controller showWindow:nil];
93 [controller closeBalloon:YES];
96 TEST_F(BalloonControllerTest, SizesTest) {
97 Notification n(GURL("http://www.google.com"), GURL("http://www.google.com"),
98 base::ASCIIToUTF16("http://www.google.com"), base::string16(),
99 new NotificationObjectProxy(-1, -1, -1, false));
100 scoped_ptr<Balloon> balloon(
101 new Balloon(n, profile(), collection_.get()));
102 balloon->SetPosition(gfx::Point(1, 1), false);
103 balloon->set_content_size(gfx::Size(100, 100));
105 BalloonController* controller =
106 [[TestBalloonController alloc] initWithBalloon:balloon.get()];
108 [controller showWindow:nil];
110 EXPECT_TRUE([controller desiredTotalWidth] > 100);
111 EXPECT_TRUE([controller desiredTotalHeight] > 100);
113 [controller closeBalloon:YES];