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 <string> 6 #include <vector> 7 8 #include "chrome/browser/ui/browser.h" 9 #include "chrome/browser/ui/browser_tabstrip.h" 10 #include "chrome/browser/ui/tabs/pinned_tab_codec.h" 11 #include "chrome/browser/ui/tabs/pinned_tab_service.h" 12 #include "chrome/browser/ui/tabs/pinned_tab_service_factory.h" 13 #include "chrome/browser/ui/tabs/pinned_tab_test_utils.h" 14 #include "chrome/browser/ui/tabs/tab_strip_model.h" 15 #include "chrome/test/base/browser_with_test_window_test.h" 16 #include "chrome/test/base/testing_profile.h" 17 #include "testing/gtest/include/gtest/gtest.h" 18 19 namespace { 20 21 BrowserContextKeyedService* BuildPinnedTabService( 22 content::BrowserContext* profile) { 23 return new PinnedTabService(static_cast<Profile*>(profile)); 24 } 25 26 PinnedTabService* BuildForProfile(Profile* profile) { 27 return static_cast<PinnedTabService*>( 28 PinnedTabServiceFactory::GetInstance()->SetTestingFactoryAndUse( 29 profile, BuildPinnedTabService)); 30 } 31 32 class PinnedTabServiceTest : public BrowserWithTestWindowTest { 33 public: 34 PinnedTabServiceTest() {} 35 36 protected: 37 virtual TestingProfile* CreateProfile() OVERRIDE { 38 TestingProfile* profile = BrowserWithTestWindowTest::CreateProfile(); 39 pinned_tab_service_ = BuildForProfile(profile); 40 return profile; 41 } 42 43 private: 44 PinnedTabService* pinned_tab_service_; 45 46 DISALLOW_COPY_AND_ASSIGN(PinnedTabServiceTest); 47 }; 48 49 // Makes sure closing a popup triggers writing pinned tabs. 50 TEST_F(PinnedTabServiceTest, Popup) { 51 GURL url("http://www.google.com"); 52 AddTab(browser(), url); 53 browser()->tab_strip_model()->SetTabPinned(0, true); 54 55 // Create a popup. 56 Browser::CreateParams params(Browser::TYPE_POPUP, profile(), 57 browser()->host_desktop_type()); 58 scoped_ptr<Browser> popup( 59 chrome::CreateBrowserWithTestWindowForParams(¶ms)); 60 61 // Close the browser. This should trigger saving the tabs. No need to destroy 62 // the browser (this happens automatically in the test destructor). 63 browser()->OnWindowClosing(); 64 65 std::string result = PinnedTabTestUtils::TabsToString( 66 PinnedTabCodec::ReadPinnedTabs(profile())); 67 EXPECT_EQ("http://www.google.com/::pinned:", result); 68 69 // Close the popup. This shouldn't reset the saved state. 70 popup->tab_strip_model()->CloseAllTabs(); 71 popup.reset(NULL); 72 73 // Check the state to make sure it hasn't changed. 74 result = PinnedTabTestUtils::TabsToString( 75 PinnedTabCodec::ReadPinnedTabs(profile())); 76 EXPECT_EQ("http://www.google.com/::pinned:", result); 77 } 78 79 } // namespace 80