Home | History | Annotate | Download | only in bookmarks
      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 "chrome/browser/ui/gtk/bookmarks/bookmark_bar_gtk.h"
      6 
      7 #include "base/task.h"
      8 #include "base/utf_string_conversions.h"
      9 #include "chrome/browser/bookmarks/bookmark_model.h"
     10 #include "chrome/browser/ui/browser.h"
     11 #include "chrome/browser/ui/gtk/tabstrip_origin_provider.h"
     12 #include "chrome/test/testing_profile.h"
     13 #include "content/browser/browser_thread.h"
     14 #include "testing/gtest/include/gtest/gtest.h"
     15 
     16 // Dummy implementation that's good enough for the tests; we don't test
     17 // rendering here so all we need is a non-NULL object.
     18 class EmptyTabstripOriginProvider : public TabstripOriginProvider {
     19  public:
     20   virtual gfx::Point GetTabStripOriginForWidget(GtkWidget* widget) {
     21     return gfx::Point(0, 0);
     22   }
     23 };
     24 
     25 class BookmarkBarGtkUnittest : public ::testing::Test {
     26  protected:
     27   BookmarkBarGtkUnittest()
     28       : ui_thread_(BrowserThread::UI, &message_loop_),
     29         file_thread_(BrowserThread::FILE, &message_loop_) {
     30   }
     31 
     32   virtual void SetUp() {
     33     profile_.reset(new TestingProfile());
     34     profile_->CreateBookmarkModel(true);
     35     profile_->BlockUntilBookmarkModelLoaded();
     36     browser_.reset(new Browser(Browser::TYPE_NORMAL, profile_.get()));
     37 
     38     origin_provider_.reset(new EmptyTabstripOriginProvider);
     39     bookmark_bar_.reset(new BookmarkBarGtk(NULL, profile_.get(), browser_.get(),
     40                                            origin_provider_.get()));
     41   }
     42 
     43   virtual void TearDown() {
     44     message_loop_.RunAllPending();
     45 
     46     bookmark_bar_.reset();
     47     origin_provider_.reset();
     48     browser_.reset();
     49     profile_.reset();
     50   }
     51 
     52   MessageLoopForUI message_loop_;
     53   BrowserThread ui_thread_;
     54   BrowserThread file_thread_;
     55 
     56   scoped_ptr<TestingProfile> profile_;
     57   scoped_ptr<Browser> browser_;
     58   scoped_ptr<TabstripOriginProvider> origin_provider_;
     59   scoped_ptr<BookmarkBarGtk> bookmark_bar_;
     60 };
     61 
     62 TEST_F(BookmarkBarGtkUnittest, DisplaysHelpMessageOnEmpty) {
     63   BookmarkModel* model = profile_->GetBookmarkModel();
     64   bookmark_bar_->Loaded(model);
     65 
     66   // There are no bookmarks in the model by default. Expect that the
     67   // |instructions_label| is shown.
     68   EXPECT_TRUE(bookmark_bar_->show_instructions_);
     69 }
     70 
     71 TEST_F(BookmarkBarGtkUnittest, HidesHelpMessageWithBookmark) {
     72   BookmarkModel* model = profile_->GetBookmarkModel();
     73 
     74   const BookmarkNode* parent = model->GetBookmarkBarNode();
     75   model->AddURL(parent, parent->child_count(),
     76                 ASCIIToUTF16("title"), GURL("http://one.com"));
     77 
     78   bookmark_bar_->Loaded(model);
     79   EXPECT_FALSE(bookmark_bar_->show_instructions_);
     80 }
     81 
     82 TEST_F(BookmarkBarGtkUnittest, BuildsButtons) {
     83   BookmarkModel* model = profile_->GetBookmarkModel();
     84 
     85   const BookmarkNode* parent = model->GetBookmarkBarNode();
     86   model->AddURL(parent, parent->child_count(),
     87                 ASCIIToUTF16("title"), GURL("http://one.com"));
     88   model->AddURL(parent, parent->child_count(),
     89                 ASCIIToUTF16("other"), GURL("http://two.com"));
     90 
     91   bookmark_bar_->Loaded(model);
     92 
     93   // We should expect two children to the bookmark bar's toolbar.
     94   GList* children = gtk_container_get_children(
     95       GTK_CONTAINER(bookmark_bar_->bookmark_toolbar_.get()));
     96   EXPECT_EQ(2U, g_list_length(children));
     97   g_list_free(children);
     98 }
     99