// Copyright (c) 2009 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 #include "base/scoped_nsobject.h" #include "base/scoped_ptr.h" #import "chrome/browser/cocoa/cocoa_test_helper.h" #include "chrome/browser/cocoa/status_bubble_mac.h" #include "googleurl/src/gurl.h" #include "testing/gtest/include/gtest/gtest.h" #include "testing/platform_test.h" #import "third_party/GTM/AppKit/GTMTheme.h" @interface StatusBubbleMacTestWindowDelegate : NSObject ; @end @implementation StatusBubbleMacTestWindowDelegate - (GTMTheme *)gtm_themeForWindow:(NSWindow *)window { NSLog(@"gettheme"); return [[[GTMTheme alloc] init] autorelease]; } @end class StatusBubbleMacTest : public PlatformTest { public: StatusBubbleMacTest() { NSWindow* window = cocoa_helper_.window(); bubble_.reset(new StatusBubbleMac(window, nil)); EXPECT_TRUE(bubble_.get()); EXPECT_FALSE(bubble_->window_); // lazily creates window } bool IsVisible() { return [bubble_->window_ isVisible] ? true: false; } NSString* GetText() { return bubble_->status_text_; } NSString* GetURLText() { return bubble_->url_text_; } NSWindow* GetWindow() { return bubble_->window_; } NSWindow* GetParent() { return bubble_->parent_; } CocoaTestHelper cocoa_helper_; // Inits Cocoa, creates window, etc... scoped_ptr bubble_; }; TEST_F(StatusBubbleMacTest, Theme) { bubble_->SetStatus(L"Theme test"); // Creates the window [GetParent() setDelegate: [[[StatusBubbleMacTestWindowDelegate alloc] init] autorelease]]; EXPECT_TRUE([GetParent() gtm_theme] != nil); EXPECT_TRUE([[GetWindow() contentView] gtm_theme] != nil); } TEST_F(StatusBubbleMacTest, SetStatus) { bubble_->SetStatus(L""); bubble_->SetStatus(L"This is a test"); EXPECT_TRUE([GetText() isEqualToString:@"This is a test"]); EXPECT_TRUE(IsVisible()); // Set the status to the exact same thing again bubble_->SetStatus(L"This is a test"); EXPECT_TRUE([GetText() isEqualToString:@"This is a test"]); // Hide it bubble_->SetStatus(L""); EXPECT_FALSE(IsVisible()); EXPECT_FALSE(GetText()); } TEST_F(StatusBubbleMacTest, SetURL) { bubble_->SetURL(GURL(), L""); EXPECT_FALSE(IsVisible()); bubble_->SetURL(GURL("bad url"), L""); EXPECT_FALSE(IsVisible()); bubble_->SetURL(GURL("http://"), L""); EXPECT_TRUE(IsVisible()); EXPECT_TRUE([GetURLText() isEqualToString:@"http:"]); bubble_->SetURL(GURL("about:blank"), L""); EXPECT_TRUE(IsVisible()); EXPECT_TRUE([GetURLText() isEqualToString:@"about:blank"]); bubble_->SetURL(GURL("foopy://"), L""); EXPECT_TRUE(IsVisible()); EXPECT_TRUE([GetURLText() isEqualToString:@"foopy:"]); bubble_->SetURL(GURL("http://www.cnn.com"), L""); EXPECT_TRUE(IsVisible()); EXPECT_TRUE([GetURLText() isEqualToString:@"http://www.cnn.com/"]); } // Test hiding bubble that's already hidden. TEST_F(StatusBubbleMacTest, Hides) { bubble_->SetStatus(L"Showing"); EXPECT_TRUE(IsVisible()); bubble_->Hide(); EXPECT_FALSE(IsVisible()); bubble_->Hide(); EXPECT_FALSE(IsVisible()); } TEST_F(StatusBubbleMacTest, MouseMove) { // TODO(pinkerton): Not sure what to do here since it relies on // [NSEvent currentEvent] and the current mouse location. } TEST_F(StatusBubbleMacTest, Delete) { NSWindow* window = cocoa_helper_.window(); // Create and delete immediately. StatusBubbleMac* bubble = new StatusBubbleMac(window, nil); delete bubble; // Create then delete while visible. bubble = new StatusBubbleMac(window, nil); bubble->SetStatus(L"showing"); delete bubble; }