Home | History | Annotate | Download | only in cocoa
      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 #import <Cocoa/Cocoa.h>
      6 
      7 #include "base/memory/scoped_nsobject.h"
      8 #import "chrome/browser/ui/cocoa/hyperlink_button_cell.h"
      9 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h"
     10 #include "testing/gtest/include/gtest/gtest.h"
     11 #include "testing/platform_test.h"
     12 
     13 namespace {
     14 
     15 class HyperlinkButtonCellTest : public CocoaTest {
     16  public:
     17   HyperlinkButtonCellTest() {
     18     NSRect frame = NSMakeRect(0, 0, 50, 30);
     19     scoped_nsobject<NSButton> view([[NSButton alloc] initWithFrame:frame]);
     20     view_ = view.get();
     21     scoped_nsobject<HyperlinkButtonCell> cell(
     22         [[HyperlinkButtonCell alloc] initTextCell:@"Testing"]);
     23     cell_ = cell.get();
     24     [view_ setCell:cell_];
     25     [[test_window() contentView] addSubview:view_];
     26   }
     27 
     28   void TestCellCustomization(HyperlinkButtonCell* cell) {
     29     EXPECT_FALSE([cell isBordered]);
     30     EXPECT_EQ(NSNoCellMask, [cell_ highlightsBy]);
     31     EXPECT_TRUE([cell showsBorderOnlyWhileMouseInside]);
     32     EXPECT_TRUE([cell textColor]);
     33   }
     34 
     35   NSButton* view_;
     36   HyperlinkButtonCell* cell_;
     37 };
     38 
     39 TEST_VIEW(HyperlinkButtonCellTest, view_)
     40 
     41 // Tests the three designated intializers.
     42 TEST_F(HyperlinkButtonCellTest, Initializers) {
     43   TestCellCustomization(cell_);  // |-initTextFrame:|
     44   scoped_nsobject<HyperlinkButtonCell> cell([[HyperlinkButtonCell alloc] init]);
     45   TestCellCustomization(cell.get());
     46 
     47   // Need to create a dummy archiver to test |-initWithCoder:|.
     48   NSData* emptyData = [NSKeyedArchiver archivedDataWithRootObject:@""];
     49   NSCoder* coder =
     50     [[[NSKeyedUnarchiver alloc] initForReadingWithData:emptyData] autorelease];
     51   cell.reset([[HyperlinkButtonCell alloc] initWithCoder:coder]);
     52   TestCellCustomization(cell);
     53 }
     54 
     55 // Test set color.
     56 TEST_F(HyperlinkButtonCellTest, SetTextColor) {
     57   NSColor* textColor = [NSColor redColor];
     58   EXPECT_NE(textColor, [cell_ textColor]);
     59   [cell_ setTextColor:textColor];
     60   EXPECT_EQ(textColor, [cell_ textColor]);
     61 }
     62 
     63 // Test mouse events.
     64 // TODO(rsesek): See if we can synthesize mouse events to more accurately
     65 // test this.
     66 TEST_F(HyperlinkButtonCellTest, MouseHover) {
     67   [[NSCursor disappearingItemCursor] push];  // Set a known state.
     68   [cell_ mouseEntered:nil];
     69   EXPECT_EQ([NSCursor pointingHandCursor], [NSCursor currentCursor]);
     70   [cell_ mouseExited:nil];
     71   EXPECT_EQ([NSCursor disappearingItemCursor], [NSCursor currentCursor]);
     72   [NSCursor pop];
     73 }
     74 
     75 }  // namespace
     76