Home | History | Annotate | Download | only in cocoa
      1 // Copyright 2013 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 "base/mac/scoped_nsobject.h"
      6 #include "base/strings/utf_string_conversions.h"
      7 #import "testing/gtest_mac.h"
      8 #include "ui/app_list/app_list_view_delegate.h"
      9 #import "ui/app_list/cocoa/app_list_view_controller.h"
     10 #import "ui/app_list/cocoa/app_list_window_controller.h"
     11 #include "ui/app_list/search_box_model.h"
     12 #include "ui/app_list/test/app_list_test_view_delegate.h"
     13 #import "ui/gfx/test/ui_cocoa_test_helper.h"
     14 
     15 namespace {
     16 
     17 class AppListWindowControllerTest : public ui::CocoaTest {
     18  public:
     19   AppListWindowControllerTest();
     20 
     21  protected:
     22   virtual void TearDown() OVERRIDE;
     23 
     24   base::scoped_nsobject<AppListWindowController> controller_;
     25 
     26   app_list::test::AppListTestViewDelegate* delegate() {
     27     return static_cast<app_list::test::AppListTestViewDelegate*>(
     28         [[controller_ appListViewController] delegate]);
     29   }
     30 
     31  private:
     32   DISALLOW_COPY_AND_ASSIGN(AppListWindowControllerTest);
     33 };
     34 
     35 AppListWindowControllerTest::AppListWindowControllerTest() {
     36   Init();
     37   scoped_ptr<app_list::AppListViewDelegate> delegate(
     38       new app_list::test::AppListTestViewDelegate);
     39   controller_.reset([[AppListWindowController alloc] init]);
     40   [[controller_ appListViewController] setDelegate:delegate.Pass()];
     41 }
     42 
     43 void AppListWindowControllerTest::TearDown() {
     44   EXPECT_TRUE(controller_.get());
     45   [[controller_ window] close];
     46   [[controller_ appListViewController]
     47      setDelegate:scoped_ptr<app_list::AppListViewDelegate>()];
     48   controller_.reset();
     49   ui::CocoaTest::TearDown();
     50 }
     51 
     52 }  // namespace
     53 
     54 // Test showing, hiding and closing the app list window.
     55 TEST_F(AppListWindowControllerTest, ShowHideCloseRelease) {
     56   EXPECT_TRUE([controller_ window]);
     57   [[controller_ window] makeKeyAndOrderFront:nil];
     58   EXPECT_TRUE([[controller_ window] isVisible]);
     59   EXPECT_TRUE([[[controller_ window] contentView] superview]);
     60   [[controller_ window] close];  // Hide.
     61   EXPECT_FALSE([[controller_ window] isVisible]);
     62   [[controller_ window] makeKeyAndOrderFront:nil];
     63 }
     64 
     65 // Test that the key bound to cancel (usually Escape) asks the controller to
     66 // dismiss the window.
     67 TEST_F(AppListWindowControllerTest, DismissWithEscape) {
     68   [[controller_ window] makeKeyAndOrderFront:nil];
     69   EXPECT_EQ(0, delegate()->dismiss_count());
     70   [[controller_ window] cancelOperation:controller_];
     71   EXPECT_EQ(1, delegate()->dismiss_count());
     72 }
     73 
     74 // Test that search results are cleared when the window closes, not when a
     75 // search result is selected. If cleared upon selection, the animation showing
     76 // the results sliding away is seen as the window closes, which looks weird.
     77 TEST_F(AppListWindowControllerTest, CloseClearsSearch) {
     78   [[controller_ window] makeKeyAndOrderFront:nil];
     79   app_list::SearchBoxModel* model = delegate()->GetModel()->search_box();
     80   AppListViewController* view_controller = [controller_ appListViewController];
     81 
     82   EXPECT_FALSE([view_controller showingSearchResults]);
     83 
     84   const base::string16 search_text(base::ASCIIToUTF16("test"));
     85   model->SetText(search_text);
     86   EXPECT_TRUE([view_controller showingSearchResults]);
     87 
     88   EXPECT_EQ(0, delegate()->open_search_result_count());
     89   [view_controller openResult:nil];
     90   EXPECT_EQ(1, delegate()->open_search_result_count());
     91 
     92   EXPECT_TRUE([view_controller showingSearchResults]);
     93   [[controller_ window] close];  // Hide.
     94   EXPECT_FALSE([view_controller showingSearchResults]);
     95 }
     96