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 #import "base/memory/scoped_nsobject.h"
      8 #import "chrome/browser/cocoa/keystone_glue.h"
      9 #import "chrome/browser/ui/cocoa/about_window_controller.h"
     10 #include "chrome/browser/ui/cocoa/browser_test_helper.h"
     11 #include "chrome/browser/ui/cocoa/cocoa_test_helper.h"
     12 #include "testing/gtest/include/gtest/gtest.h"
     13 #import "testing/gtest_mac.h"
     14 #include "testing/platform_test.h"
     15 
     16 namespace {
     17 
     18 void PostAutoupdateStatusNotification(AutoupdateStatus status,
     19                                              NSString* version) {
     20   NSNumber* statusNumber = [NSNumber numberWithInt:status];
     21   NSMutableDictionary* dictionary =
     22       [NSMutableDictionary dictionaryWithObjects:&statusNumber
     23                                          forKeys:&kAutoupdateStatusStatus
     24                                            count:1];
     25   if (version) {
     26     [dictionary setObject:version forKey:kAutoupdateStatusVersion];
     27   }
     28 
     29   NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
     30   [center postNotificationName:kAutoupdateStatusNotification
     31                         object:nil
     32                       userInfo:dictionary];
     33 }
     34 
     35 class AboutWindowControllerTest : public CocoaTest {
     36  public:
     37   virtual void SetUp() {
     38     CocoaTest::SetUp();
     39     about_window_controller_ =
     40         [[AboutWindowController alloc] initWithProfile:nil];
     41     EXPECT_TRUE([about_window_controller_ window]);
     42   }
     43 
     44   virtual void TearDown() {
     45     [about_window_controller_ close];
     46     CocoaTest::TearDown();
     47   }
     48 
     49   AboutWindowController* about_window_controller_;
     50 };
     51 
     52 TEST_F(AboutWindowControllerTest, TestCopyright) {
     53   NSString* text = [[AboutWindowController legalTextBlock] string];
     54 
     55   // Make sure we have the word "Copyright" in it, which is present in all
     56   // locales.
     57   NSRange range = [text rangeOfString:@"Copyright"];
     58   EXPECT_NE(NSNotFound, range.location);
     59 }
     60 
     61 TEST_F(AboutWindowControllerTest, RemovesLinkAnchors) {
     62   NSString* text = [[AboutWindowController legalTextBlock] string];
     63 
     64   // Make sure that we removed the "BEGIN_LINK" and "END_LINK" anchors.
     65   NSRange range = [text rangeOfString:@"BEGIN_LINK"];
     66   EXPECT_EQ(NSNotFound, range.location);
     67 
     68   range = [text rangeOfString:@"END_LINK"];
     69   EXPECT_EQ(NSNotFound, range.location);
     70 }
     71 
     72 TEST_F(AboutWindowControllerTest, AwakeNibSetsString) {
     73   NSAttributedString* legal_text = [AboutWindowController legalTextBlock];
     74   NSAttributedString* text_storage =
     75       [[about_window_controller_ legalText] textStorage];
     76 
     77   EXPECT_TRUE([legal_text isEqualToAttributedString:text_storage]);
     78 }
     79 
     80 TEST_F(AboutWindowControllerTest, TestButton) {
     81   NSButton* button = [about_window_controller_ updateButton];
     82   ASSERT_TRUE(button);
     83 
     84   // Not enabled until we know if updates are available.
     85   ASSERT_FALSE([button isEnabled]);
     86   PostAutoupdateStatusNotification(kAutoupdateAvailable, nil);
     87   ASSERT_TRUE([button isEnabled]);
     88 
     89   // Make sure the button is hooked up
     90   ASSERT_EQ([button target], about_window_controller_);
     91   ASSERT_EQ([button action], @selector(updateNow:));
     92 }
     93 
     94 // Doesn't confirm correctness, but does confirm something happens.
     95 TEST_F(AboutWindowControllerTest, TestCallbacks) {
     96   NSString *lastText = [[about_window_controller_ updateText]
     97                         stringValue];
     98   PostAutoupdateStatusNotification(kAutoupdateCurrent, @"foo");
     99   ASSERT_NSNE(lastText, [[about_window_controller_ updateText] stringValue]);
    100 
    101   lastText = [[about_window_controller_ updateText] stringValue];
    102   PostAutoupdateStatusNotification(kAutoupdateCurrent, @"foo");
    103   ASSERT_NSEQ(lastText, [[about_window_controller_ updateText] stringValue]);
    104 
    105   lastText = [[about_window_controller_ updateText] stringValue];
    106   PostAutoupdateStatusNotification(kAutoupdateCurrent, @"bar");
    107   ASSERT_NSNE(lastText, [[about_window_controller_ updateText] stringValue]);
    108 
    109   lastText = [[about_window_controller_ updateText] stringValue];
    110   PostAutoupdateStatusNotification(kAutoupdateAvailable, nil);
    111   ASSERT_NSNE(lastText, [[about_window_controller_ updateText] stringValue]);
    112 
    113   lastText = [[about_window_controller_ updateText] stringValue];
    114   PostAutoupdateStatusNotification(kAutoupdateCheckFailed, nil);
    115   ASSERT_NSNE(lastText, [[about_window_controller_ updateText] stringValue]);
    116 
    117 #if 0
    118   // TODO(mark): The kAutoupdateInstalled portion of the test is disabled
    119   // because it leaks restart dialogs.  If the About box is revised to use
    120   // a button within the box to advise a restart instead of popping dialogs,
    121   // these tests should be enabled.
    122 
    123   lastText = [[about_window_controller_ updateText] stringValue];
    124   PostAutoupdateStatusNotification(kAutoupdateInstalled, @"ver");
    125   ASSERT_NSNE(lastText, [[about_window_controller_ updateText] stringValue]);
    126 
    127   lastText = [[about_window_controller_ updateText] stringValue];
    128   PostAutoupdateStatusNotification(kAutoupdateInstalled, nil);
    129   ASSERT_NSNE(lastText, [[about_window_controller_ updateText] stringValue]);
    130 #endif
    131 
    132   lastText = [[about_window_controller_ updateText] stringValue];
    133   PostAutoupdateStatusNotification(kAutoupdateInstallFailed, nil);
    134   ASSERT_NSNE(lastText, [[about_window_controller_ updateText] stringValue]);
    135 }
    136 
    137 }  // namespace
    138