OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2017 The Chromium Authors. All rights reserved. | |
sky
2017/06/26 21:38:09
no (c) (see chromium style guide).
ducbui
2017/06/26 22:30:58
Done.
I got the (c) when I copied the license fro
| |
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 "chrome/browser/sessions/session_restore_observer.h" | |
6 | |
7 #include "chrome/browser/sessions/session_restore.h" | |
8 #include "chrome/browser/sessions/tab_loader.h" | |
9 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | |
10 #include "content/public/browser/web_contents.h" | |
11 #include "content/public/test/web_contents_tester.h" | |
12 | |
13 using content::WebContentsTester; | |
14 | |
15 namespace { | |
16 | |
17 const char kDefaultUrl[] = "https://www.google.com"; | |
18 | |
19 } // namespace | |
20 | |
21 class MockSessionRestoreObserver : public SessionRestoreObserver { | |
22 public: | |
23 MockSessionRestoreObserver() { SessionRestore::AddObserver(this); } | |
24 | |
25 ~MockSessionRestoreObserver() { SessionRestore::RemoveObserver(this); } | |
26 | |
27 enum class SessionRestoreEvent { | |
28 STARTED_LOADING_TABS, | |
29 FINISHED_LOADING_TABS | |
30 }; | |
31 | |
32 SessionRestoreEvent GetSessionRestoreEventAt(size_t index) const { | |
33 DCHECK(index < session_restore_events_.size()); | |
sky
2017/06/26 21:38:09
This is rather awkward. Why don't you just return
ducbui
2017/06/26 22:30:58
I am fine with the change. Your method looks clean
| |
34 return session_restore_events_[index]; | |
35 } | |
36 | |
37 // SessionRestoreObserver implementation: | |
38 void OnSessionRestoreStartedLoadingTabs() override { | |
39 session_restore_events_.emplace_back( | |
40 SessionRestoreEvent::STARTED_LOADING_TABS); | |
41 } | |
42 | |
43 void OnSessionRestoreFinishedLoadingTabs() override { | |
44 session_restore_events_.emplace_back( | |
45 SessionRestoreEvent::FINISHED_LOADING_TABS); | |
46 } | |
47 | |
48 private: | |
49 std::vector<SessionRestoreEvent> session_restore_events_; | |
50 | |
51 DISALLOW_COPY_AND_ASSIGN(MockSessionRestoreObserver); | |
52 }; | |
53 | |
54 class SessionRestoreObserverTest : public ChromeRenderViewHostTestHarness { | |
55 public: | |
56 using RestoredTab = SessionRestoreDelegate::RestoredTab; | |
57 | |
58 SessionRestoreObserverTest() {} | |
59 | |
60 // testing::Test: | |
61 void SetUp() override { | |
62 ChromeRenderViewHostTestHarness::SetUp(); | |
63 restored_tabs_.emplace_back(web_contents(), false, false, false); | |
64 } | |
65 | |
66 void TearDown() override { | |
67 ChromeRenderViewHostTestHarness::TearDown(); | |
68 restored_tabs_.clear(); | |
69 } | |
70 | |
71 protected: | |
72 void LoadWebContents(content::WebContents* contents) { | |
73 WebContentsTester::For(contents)->NavigateAndCommit(GURL(kDefaultUrl)); | |
74 WebContentsTester::For(contents)->TestSetIsLoading(false); | |
75 } | |
76 | |
77 MockSessionRestoreObserver mock_observer_; | |
78 std::vector<RestoredTab> restored_tabs_; | |
79 | |
80 private: | |
81 DISALLOW_COPY_AND_ASSIGN(SessionRestoreObserverTest); | |
82 }; | |
83 | |
84 TEST_F(SessionRestoreObserverTest, SingleSessionRestore) { | |
85 TabLoader::RestoreTabs(restored_tabs_, base::TimeTicks()); | |
86 EXPECT_EQ( | |
87 mock_observer_.GetSessionRestoreEventAt(0), | |
sky
2017/06/26 21:38:09
You should assert the size of the vector, otherwis
ducbui
2017/06/26 22:30:58
Done.
| |
88 MockSessionRestoreObserver::SessionRestoreEvent::STARTED_LOADING_TABS); | |
sky
2017/06/26 21:38:09
Generally we use the format "expected, actual", so
ducbui
2017/06/26 22:30:58
Done.
| |
89 | |
90 LoadWebContents(web_contents()); | |
91 EXPECT_EQ( | |
92 mock_observer_.GetSessionRestoreEventAt(1), | |
93 MockSessionRestoreObserver::SessionRestoreEvent::FINISHED_LOADING_TABS); | |
94 } | |
95 | |
96 TEST_F(SessionRestoreObserverTest, SequentialSessionRestore) { | |
97 const int number_of_session_restores = 3; | |
98 size_t event_index = 0; | |
99 for (int i = 0; i < number_of_session_restores; ++i) { | |
100 TabLoader::RestoreTabs(restored_tabs_, base::TimeTicks()); | |
101 EXPECT_EQ( | |
102 mock_observer_.GetSessionRestoreEventAt(event_index++), | |
103 MockSessionRestoreObserver::SessionRestoreEvent::STARTED_LOADING_TABS); | |
104 | |
105 LoadWebContents(web_contents()); | |
106 EXPECT_EQ( | |
107 mock_observer_.GetSessionRestoreEventAt(event_index++), | |
108 MockSessionRestoreObserver::SessionRestoreEvent::FINISHED_LOADING_TABS); | |
109 } | |
110 } | |
111 | |
112 TEST_F(SessionRestoreObserverTest, ConcurrentSessionRestore) { | |
113 std::vector<RestoredTab> another_restored_tabs; | |
114 std::unique_ptr<content::WebContents> test_contents( | |
115 WebContentsTester::CreateTestWebContents(browser_context(), nullptr)); | |
116 another_restored_tabs.emplace_back(test_contents.get(), false, false, false); | |
117 | |
118 TabLoader::RestoreTabs(restored_tabs_, base::TimeTicks()); | |
119 TabLoader::RestoreTabs(another_restored_tabs, base::TimeTicks()); | |
120 EXPECT_EQ( | |
121 mock_observer_.GetSessionRestoreEventAt(0), | |
122 MockSessionRestoreObserver::SessionRestoreEvent::STARTED_LOADING_TABS); | |
123 | |
124 LoadWebContents(web_contents()); | |
125 LoadWebContents(test_contents.get()); | |
126 | |
127 EXPECT_EQ( | |
128 mock_observer_.GetSessionRestoreEventAt(1), | |
129 MockSessionRestoreObserver::SessionRestoreEvent::FINISHED_LOADING_TABS); | |
130 } | |
OLD | NEW |