1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
|
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/tabs/pinned_tab_service.h"
#include "base/memory/raw_ptr.h"
#include "chrome/browser/profiles/keep_alive/profile_keep_alive_types.h"
#include "chrome/browser/profiles/keep_alive/scoped_profile_keep_alive.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_navigator_params.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/tabs/pinned_tab_codec.h"
#include "chrome/browser/ui/tabs/pinned_tab_service_factory.h"
#include "chrome/browser/ui/tabs/pinned_tab_test_utils.h"
#include "chrome/browser/ui/tabs/tab_enums.h"
#include "chrome/browser/ui/test/test_browser_closed_waiter.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/test_utils.h"
using PinnedTabServiceBrowserTest = InProcessBrowserTest;
// Makes sure pinned tabs are updated when tabstrip is empty.
// http://crbug.com/71939
IN_PROC_BROWSER_TEST_F(PinnedTabServiceBrowserTest, TabStripEmpty) {
Profile* profile = browser()->profile();
GURL url("https://www.google.com");
NavigateParams params(browser(), url, ui::PAGE_TRANSITION_TYPED);
ui_test_utils::NavigateToURL(¶ms);
TabStripModel* tab_strip_model = browser()->tab_strip_model();
tab_strip_model->SetTabPinned(0, true);
PinnedTabCodec::WritePinnedTabs(profile);
std::string result =
PinnedTabTestUtils::TabsToString(PinnedTabCodec::ReadPinnedTabs(profile));
EXPECT_EQ("https://www.google.com/:pinned", result);
// When tab strip is empty, browser window will be closed and PinnedTabService
// must update data on this event.
ScopedProfileKeepAlive profile_keep_alive(
profile, ProfileKeepAliveOrigin::kBrowserWindow);
TestBrowserClosedWaiter waiter(browser());
tab_strip_model->SetTabPinned(0, false);
int previous_tab_count = tab_strip_model->count();
tab_strip_model->CloseWebContentsAt(0, TabCloseTypes::CLOSE_NONE);
EXPECT_EQ(previous_tab_count - 1, tab_strip_model->count());
ASSERT_TRUE(waiter.WaitUntilClosed());
// Let's see it's cleared out properly.
result =
PinnedTabTestUtils::TabsToString(PinnedTabCodec::ReadPinnedTabs(profile));
EXPECT_TRUE(result.empty());
}
IN_PROC_BROWSER_TEST_F(PinnedTabServiceBrowserTest, CloseWindow) {
Profile* profile = browser()->profile();
EXPECT_TRUE(PinnedTabServiceFactory::GetForProfile(profile));
EXPECT_TRUE(profile->GetPrefs());
GURL url("https://www.google.com");
NavigateParams params(browser(), url, ui::PAGE_TRANSITION_TYPED);
ui_test_utils::NavigateToURL(¶ms);
TabStripModel* tab_strip_model = browser()->tab_strip_model();
tab_strip_model->SetTabPinned(0, true);
ScopedProfileKeepAlive profile_keep_alive(
profile, ProfileKeepAliveOrigin::kBrowserWindow);
TestBrowserClosedWaiter waiter(browser());
browser()->window()->Close();
ASSERT_TRUE(waiter.WaitUntilClosed());
std::string result =
PinnedTabTestUtils::TabsToString(PinnedTabCodec::ReadPinnedTabs(profile));
EXPECT_EQ("https://www.google.com/:pinned", result);
}
// Makes sure closing a popup triggers writing pinned tabs.
IN_PROC_BROWSER_TEST_F(PinnedTabServiceBrowserTest, Popup) {
Profile* profile = browser()->profile();
EXPECT_TRUE(PinnedTabServiceFactory::GetForProfile(profile));
EXPECT_TRUE(profile->GetPrefs());
GURL url("https://www.google.com");
NavigateParams params(browser(), url, ui::PAGE_TRANSITION_TYPED);
ui_test_utils::NavigateToURL(¶ms);
TabStripModel* tab_strip_model = browser()->tab_strip_model();
tab_strip_model->SetTabPinned(0, true);
// Create a popup browser.
Browser* popup_browser = Browser::Create(
Browser::CreateParams(Browser::TYPE_POPUP, browser()->profile(), true));
ASSERT_TRUE(popup_browser->is_type_popup());
// Close the browser. This should trigger saving the tabs. No need to destroy
// the browser (this happens automatically in the test destructor).
browser()->OnWindowClosing();
std::string result =
PinnedTabTestUtils::TabsToString(PinnedTabCodec::ReadPinnedTabs(profile));
EXPECT_EQ("https://www.google.com/:pinned", result);
// Close the popup browser. This shouldn't reset the saved state.
popup_browser->tab_strip_model()->CloseAllTabs();
// Check the state to make sure it hasn't changed.
result =
PinnedTabTestUtils::TabsToString(PinnedTabCodec::ReadPinnedTabs(profile));
EXPECT_EQ("https://www.google.com/:pinned", result);
}
|