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