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