1 // Copyright (c) 2011 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 "base/memory/scoped_nsobject.h" 6 #include "base/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/ui/cocoa/browser_test_helper.h" 11 #include "chrome/browser/ui/cocoa/cocoa_test_helper.h" 12 #include "chrome/browser/ui/cocoa/notifications/balloon_controller.h" 13 #include "chrome/test/testing_profile.h" 14 #include "content/browser/renderer_host/test_render_view_host.h" 15 #import "third_party/ocmock/OCMock/OCMock.h" 16 17 // Subclass balloon controller and mock out the initialization of the RVH. 18 @interface TestBalloonController : BalloonController { 19 } 20 - (void)initializeHost; 21 @end 22 23 @implementation TestBalloonController 24 - (void)initializeHost {} 25 @end 26 27 namespace { 28 29 // Use a dummy balloon collection for testing. 30 class MockBalloonCollection : public BalloonCollection { 31 virtual void Add(const Notification& notification, 32 Profile* profile) {} 33 virtual bool RemoveById(const std::string& id) { return false; } 34 virtual bool RemoveBySourceOrigin(const GURL& origin) { return false; } 35 virtual void RemoveAll() {} 36 virtual bool HasSpace() const { return true; } 37 virtual void ResizeBalloon(Balloon* balloon, const gfx::Size& size) {} 38 virtual void DisplayChanged() {} 39 virtual void SetPositionPreference(PositionPreference preference) {} 40 virtual void OnBalloonClosed(Balloon* source) {}; 41 virtual const Balloons& GetActiveBalloons() { 42 NOTREACHED(); 43 return balloons_; 44 } 45 private: 46 Balloons balloons_; 47 }; 48 49 class BalloonControllerTest : public RenderViewHostTestHarness { 50 public: 51 BalloonControllerTest() : 52 ui_thread_(BrowserThread::UI, MessageLoop::current()), 53 io_thread_(BrowserThread::IO, MessageLoop::current()) { 54 } 55 56 virtual void SetUp() { 57 RenderViewHostTestHarness::SetUp(); 58 CocoaTest::BootstrapCocoa(); 59 profile_.reset(new TestingProfile()); 60 profile_->CreateRequestContext(); 61 browser_.reset(new Browser(Browser::TYPE_NORMAL, profile_.get())); 62 collection_.reset(new MockBalloonCollection()); 63 } 64 65 virtual void TearDown() { 66 MessageLoop::current()->RunAllPending(); 67 RenderViewHostTestHarness::TearDown(); 68 } 69 70 protected: 71 BrowserThread ui_thread_; 72 BrowserThread io_thread_; 73 scoped_ptr<TestingProfile> profile_; 74 scoped_ptr<Browser> browser_; 75 scoped_ptr<BalloonCollection> collection_; 76 }; 77 78 TEST_F(BalloonControllerTest, ShowAndCloseTest) { 79 Notification n(GURL("http://www.google.com"), GURL("http://www.google.com"), 80 ASCIIToUTF16("http://www.google.com"), string16(), 81 new NotificationObjectProxy(-1, -1, -1, false)); 82 scoped_ptr<Balloon> balloon( 83 new Balloon(n, profile_.get(), collection_.get())); 84 balloon->SetPosition(gfx::Point(1, 1), false); 85 balloon->set_content_size(gfx::Size(100, 100)); 86 87 BalloonController* controller = 88 [[TestBalloonController alloc] initWithBalloon:balloon.get()]; 89 90 [controller showWindow:nil]; 91 [controller closeBalloon:YES]; 92 } 93 94 TEST_F(BalloonControllerTest, SizesTest) { 95 Notification n(GURL("http://www.google.com"), GURL("http://www.google.com"), 96 ASCIIToUTF16("http://www.google.com"), string16(), 97 new NotificationObjectProxy(-1, -1, -1, false)); 98 scoped_ptr<Balloon> balloon( 99 new Balloon(n, profile_.get(), collection_.get())); 100 balloon->SetPosition(gfx::Point(1, 1), false); 101 balloon->set_content_size(gfx::Size(100, 100)); 102 103 BalloonController* controller = 104 [[TestBalloonController alloc] initWithBalloon:balloon.get()]; 105 106 [controller showWindow:nil]; 107 108 EXPECT_TRUE([controller desiredTotalWidth] > 100); 109 EXPECT_TRUE([controller desiredTotalHeight] > 100); 110 111 [controller closeBalloon:YES]; 112 } 113 114 } 115