| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/file_util.h" | 5 #include "base/file_util.h" |
| 6 #include "base/path_service.h" | 6 #include "base/path_service.h" |
| 7 #include "base/scoped_ptr.h" | 7 #include "base/scoped_ptr.h" |
| 8 #include "base/scoped_vector.h" | 8 #include "base/scoped_vector.h" |
| 9 #include "base/stl_util-inl.h" | 9 #include "base/stl_util-inl.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 11 #include "base/time.h" | 11 #include "base/time.h" |
| 12 #include "chrome/browser/defaults.h" | 12 #include "chrome/browser/defaults.h" |
| 13 #include "chrome/browser/sessions/session_backend.h" | 13 #include "chrome/browser/sessions/session_backend.h" |
| 14 #include "chrome/browser/sessions/session_service.h" | 14 #include "chrome/browser/sessions/session_service.h" |
| 15 #include "chrome/browser/sessions/session_service_test_helper.h" | 15 #include "chrome/browser/sessions/session_service_test_helper.h" |
| 16 #include "chrome/browser/sessions/session_types.h" | 16 #include "chrome/browser/sessions/session_types.h" |
| 17 #include "chrome/browser/tab_contents/navigation_entry.h" | 17 #include "chrome/browser/tab_contents/navigation_entry.h" |
| 18 #include "chrome/common/chrome_paths.h" | 18 #include "chrome/common/chrome_paths.h" |
| 19 #include "chrome/common/notification_observer.h" |
| 20 #include "chrome/common/notification_registrar.h" |
| 21 #include "chrome/common/notification_service.h" |
| 22 #include "chrome/common/notification_type.h" |
| 19 #include "chrome/test/browser_with_test_window_test.h" | 23 #include "chrome/test/browser_with_test_window_test.h" |
| 20 #include "chrome/test/file_test_utils.h" | 24 #include "chrome/test/file_test_utils.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
| 22 | 26 |
| 23 class SessionServiceTest : public BrowserWithTestWindowTest { | 27 class SessionServiceTest : public BrowserWithTestWindowTest, |
| 28 public NotificationObserver { |
| 24 public: | 29 public: |
| 25 SessionServiceTest() : window_bounds(0, 1, 2, 3) {} | 30 SessionServiceTest() : window_bounds(0, 1, 2, 3), sync_save_count_(0){} |
| 26 | 31 |
| 27 protected: | 32 protected: |
| 28 virtual void SetUp() { | 33 virtual void SetUp() { |
| 29 BrowserWithTestWindowTest::SetUp(); | 34 BrowserWithTestWindowTest::SetUp(); |
| 30 std::string b = Int64ToString(base::Time::Now().ToInternalValue()); | 35 std::string b = Int64ToString(base::Time::Now().ToInternalValue()); |
| 31 | 36 |
| 32 PathService::Get(base::DIR_TEMP, &path_); | 37 PathService::Get(base::DIR_TEMP, &path_); |
| 33 path_ = path_.Append(FILE_PATH_LITERAL("SessionTestDirs")); | 38 path_ = path_.Append(FILE_PATH_LITERAL("SessionTestDirs")); |
| 34 file_util::CreateDirectory(path_); | 39 file_util::CreateDirectory(path_); |
| 35 path_deleter_.reset(new FileAutoDeleter(path_)); | 40 path_deleter_.reset(new FileAutoDeleter(path_)); |
| 36 path_ = path_.AppendASCII(b); | 41 path_ = path_.AppendASCII(b); |
| 37 | 42 |
| 38 SessionService* session_service = new SessionService(path_); | 43 SessionService* session_service = new SessionService(path_); |
| 39 helper_.set_service(session_service); | 44 helper_.set_service(session_service); |
| 40 | 45 |
| 41 service()->SetWindowType(window_id, Browser::TYPE_NORMAL); | 46 service()->SetWindowType(window_id, Browser::TYPE_NORMAL); |
| 42 service()->SetWindowBounds(window_id, window_bounds, false); | 47 service()->SetWindowBounds(window_id, window_bounds, false); |
| 43 } | 48 } |
| 44 | 49 |
| 50 // Upon notification, increment the sync_save_count variable |
| 51 void Observe(NotificationType type, |
| 52 const NotificationSource& source, |
| 53 const NotificationDetails& details) { |
| 54 ASSERT_EQ(type.value, NotificationType::SESSION_SERVICE_SAVED); |
| 55 sync_save_count_++; |
| 56 } |
| 57 |
| 45 virtual void TearDown() { | 58 virtual void TearDown() { |
| 46 helper_.set_service(NULL); | 59 helper_.set_service(NULL); |
| 47 path_deleter_.reset(); | 60 path_deleter_.reset(); |
| 48 } | 61 } |
| 49 | 62 |
| 50 void UpdateNavigation(const SessionID& window_id, | 63 void UpdateNavigation(const SessionID& window_id, |
| 51 const SessionID& tab_id, | 64 const SessionID& tab_id, |
| 52 const TabNavigation& navigation, | 65 const TabNavigation& navigation, |
| 53 int index, | 66 int index, |
| 54 bool select) { | 67 bool select) { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 73 helper_.set_service(session_service); | 86 helper_.set_service(session_service); |
| 74 helper_.ReadWindows(windows); | 87 helper_.ReadWindows(windows); |
| 75 } | 88 } |
| 76 | 89 |
| 77 // Configures the session service with one window with one tab and a single | 90 // Configures the session service with one window with one tab and a single |
| 78 // navigation. If |pinned_state| is true or |write_always| is true, the | 91 // navigation. If |pinned_state| is true or |write_always| is true, the |
| 79 // pinned state of the tab is updated. The session service is then recreated | 92 // pinned state of the tab is updated. The session service is then recreated |
| 80 // and the pinned state of the read back tab is returned. | 93 // and the pinned state of the read back tab is returned. |
| 81 bool CreateAndWriteSessionWithOneTab(bool pinned_state, bool write_always) { | 94 bool CreateAndWriteSessionWithOneTab(bool pinned_state, bool write_always) { |
| 82 SessionID tab_id; | 95 SessionID tab_id; |
| 83 | |
| 84 TabNavigation nav1(0, GURL("http://google.com"), | 96 TabNavigation nav1(0, GURL("http://google.com"), |
| 85 GURL("http://www.referrer.com"), | 97 GURL("http://www.referrer.com"), |
| 86 ASCIIToUTF16("abc"), "def", | 98 ASCIIToUTF16("abc"), "def", |
| 87 PageTransition::QUALIFIER_MASK); | 99 PageTransition::QUALIFIER_MASK); |
| 88 | 100 |
| 89 helper_.PrepareTabInWindow(window_id, tab_id, 0, true); | 101 helper_.PrepareTabInWindow(window_id, tab_id, 0, true); |
| 90 UpdateNavigation(window_id, tab_id, nav1, 0, true); | 102 UpdateNavigation(window_id, tab_id, nav1, 0, true); |
| 91 | 103 |
| 92 if (pinned_state || write_always) | 104 if (pinned_state || write_always) |
| 93 helper_.service()->SetPinnedState(window_id, tab_id, pinned_state); | 105 helper_.service()->SetPinnedState(window_id, tab_id, pinned_state); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 109 } | 121 } |
| 110 | 122 |
| 111 SessionService* service() { return helper_.service(); } | 123 SessionService* service() { return helper_.service(); } |
| 112 | 124 |
| 113 SessionBackend* backend() { return helper_.backend(); } | 125 SessionBackend* backend() { return helper_.backend(); } |
| 114 | 126 |
| 115 const gfx::Rect window_bounds; | 127 const gfx::Rect window_bounds; |
| 116 | 128 |
| 117 SessionID window_id; | 129 SessionID window_id; |
| 118 | 130 |
| 131 int sync_save_count_; |
| 132 |
| 119 // Path used in testing. | 133 // Path used in testing. |
| 120 FilePath path_; | 134 FilePath path_; |
| 121 scoped_ptr<FileAutoDeleter> path_deleter_; | 135 scoped_ptr<FileAutoDeleter> path_deleter_; |
| 122 | 136 |
| 123 SessionServiceTestHelper helper_; | 137 SessionServiceTestHelper helper_; |
| 124 }; | 138 }; |
| 125 | 139 |
| 126 TEST_F(SessionServiceTest, Basic) { | 140 TEST_F(SessionServiceTest, Basic) { |
| 127 SessionID tab_id; | 141 SessionID tab_id; |
| 128 ASSERT_NE(window_id.id(), tab_id.id()); | 142 ASSERT_NE(window_id.id(), tab_id.id()); |
| (...skipping 482 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 611 NavigateAndCommitActiveTab(GURL("http://foo/2")); | 625 NavigateAndCommitActiveTab(GURL("http://foo/2")); |
| 612 AddTab(browser(), GURL("http://bar/1")); | 626 AddTab(browser(), GURL("http://bar/1")); |
| 613 NavigateAndCommitActiveTab(GURL("http://bar/2")); | 627 NavigateAndCommitActiveTab(GURL("http://bar/2")); |
| 614 | 628 |
| 615 CancelableRequestConsumer consumer; | 629 CancelableRequestConsumer consumer; |
| 616 GetCurrentSessionCallbackHandler handler; | 630 GetCurrentSessionCallbackHandler handler; |
| 617 service()->GetCurrentSession(&consumer, | 631 service()->GetCurrentSession(&consumer, |
| 618 NewCallback(&handler, &GetCurrentSessionCallbackHandler::OnGotSession)); | 632 NewCallback(&handler, &GetCurrentSessionCallbackHandler::OnGotSession)); |
| 619 } | 633 } |
| 620 | 634 |
| 635 // Test that the notification for SESSION_SERVICE_SAVED is working properly. |
| 636 TEST_F(SessionServiceTest, SavedSessionNotification) { |
| 637 NotificationRegistrar registrar_; |
| 638 registrar_.Add(this, NotificationType::SESSION_SERVICE_SAVED, |
| 639 NotificationService::AllSources()); |
| 640 service()->Save(); |
| 641 EXPECT_EQ(sync_save_count_, 1); |
| 642 } |
| 643 |
| 621 // Makes sure a tab closed by a user gesture is not restored. | 644 // Makes sure a tab closed by a user gesture is not restored. |
| 622 TEST_F(SessionServiceTest, CloseTabUserGesture) { | 645 TEST_F(SessionServiceTest, CloseTabUserGesture) { |
| 623 SessionID tab_id; | 646 SessionID tab_id; |
| 624 ASSERT_NE(window_id.id(), tab_id.id()); | 647 ASSERT_NE(window_id.id(), tab_id.id()); |
| 625 | 648 |
| 626 TabNavigation nav1(0, GURL("http://google.com"), | 649 TabNavigation nav1(0, GURL("http://google.com"), |
| 627 GURL("http://www.referrer.com"), | 650 GURL("http://www.referrer.com"), |
| 628 ASCIIToUTF16("abc"), "def", | 651 ASCIIToUTF16("abc"), "def", |
| 629 PageTransition::QUALIFIER_MASK); | 652 PageTransition::QUALIFIER_MASK); |
| 630 | 653 |
| 631 helper_.PrepareTabInWindow(window_id, tab_id, 0, true); | 654 helper_.PrepareTabInWindow(window_id, tab_id, 0, true); |
| 632 UpdateNavigation(window_id, tab_id, nav1, 0, true); | 655 UpdateNavigation(window_id, tab_id, nav1, 0, true); |
| 633 service()->TabClosed(window_id, tab_id, true); | 656 service()->TabClosed(window_id, tab_id, true); |
| 634 | 657 |
| 635 ScopedVector<SessionWindow> windows; | 658 ScopedVector<SessionWindow> windows; |
| 636 ReadWindows(&(windows.get())); | 659 ReadWindows(&(windows.get())); |
| 637 | 660 |
| 638 ASSERT_TRUE(windows->empty()); | 661 ASSERT_TRUE(windows->empty()); |
| 639 } | 662 } |
| OLD | NEW |