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