| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "chrome/browser/sessions/session_restore_stats_collector.h" | 5 #include "chrome/browser/sessions/session_restore_stats_collector.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/message_loop/message_loop.h" | |
| 12 #include "base/test/simple_test_tick_clock.h" | 11 #include "base/test/simple_test_tick_clock.h" |
| 13 #include "chrome/test/base/testing_profile.h" | 12 #include "chrome/test/base/testing_profile.h" |
| 14 #include "content/public/browser/notification_service.h" | 13 #include "content/public/browser/notification_service.h" |
| 15 #include "content/public/browser/notification_types.h" | 14 #include "content/public/browser/notification_types.h" |
| 16 #include "content/public/browser/render_widget_host.h" | 15 #include "content/public/browser/render_widget_host.h" |
| 17 #include "content/public/browser/render_widget_host_view.h" | 16 #include "content/public/browser/render_widget_host_view.h" |
| 18 #include "content/public/browser/web_contents.h" | 17 #include "content/public/browser/web_contents.h" |
| 19 #include "content/public/test/test_browser_thread.h" | 18 #include "content/public/test/test_browser_thread_bundle.h" |
| 20 #include "content/public/test/test_web_contents_factory.h" | 19 #include "content/public/test/test_web_contents_factory.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 22 | 21 |
| 23 namespace { | 22 namespace { |
| 24 | 23 |
| 25 using TabLoaderStats = SessionRestoreStatsCollector::TabLoaderStats; | 24 using TabLoaderStats = SessionRestoreStatsCollector::TabLoaderStats; |
| 26 using StatsReportingDelegate = | 25 using StatsReportingDelegate = |
| 27 SessionRestoreStatsCollector::StatsReportingDelegate; | 26 SessionRestoreStatsCollector::StatsReportingDelegate; |
| 28 | 27 |
| 29 // A mock StatsReportingDelegate. This is used by the unittests to validate the | 28 // A mock StatsReportingDelegate. This is used by the unittests to validate the |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 | 173 |
| 175 base::SimpleTestTickClock* test_tick_clock_; | 174 base::SimpleTestTickClock* test_tick_clock_; |
| 176 | 175 |
| 177 DISALLOW_COPY_AND_ASSIGN(TestSessionRestoreStatsCollector); | 176 DISALLOW_COPY_AND_ASSIGN(TestSessionRestoreStatsCollector); |
| 178 }; | 177 }; |
| 179 | 178 |
| 180 class SessionRestoreStatsCollectorTest : public testing::Test { | 179 class SessionRestoreStatsCollectorTest : public testing::Test { |
| 181 public: | 180 public: |
| 182 using RestoredTab = SessionRestoreDelegate::RestoredTab; | 181 using RestoredTab = SessionRestoreDelegate::RestoredTab; |
| 183 | 182 |
| 184 SessionRestoreStatsCollectorTest() | 183 SessionRestoreStatsCollectorTest() = default; |
| 185 : ui_thread_(content::BrowserThread::UI, &message_loop_) {} | |
| 186 | 184 |
| 187 void SetUp() override { | 185 void SetUp() override { |
| 188 test_web_contents_factory_.reset(new content::TestWebContentsFactory); | 186 test_web_contents_factory_.reset(new content::TestWebContentsFactory); |
| 189 | 187 |
| 190 // Ownership of the reporting delegate is passed to the | 188 // Ownership of the reporting delegate is passed to the |
| 191 // SessionRestoreStatsCollector, but a raw pointer is kept to it so it can | 189 // SessionRestoreStatsCollector, but a raw pointer is kept to it so it can |
| 192 // be queried by the test. | 190 // be queried by the test. |
| 193 passthrough_reporting_delegate_ = new PassthroughStatsReportingDelegate(); | 191 passthrough_reporting_delegate_ = new PassthroughStatsReportingDelegate(); |
| 194 | 192 |
| 195 // Ownership of this clock is passed to the SessionRestoreStatsCollector. | 193 // Ownership of this clock is passed to the SessionRestoreStatsCollector. |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 282 content::NotificationService::NoDetails()); | 280 content::NotificationService::NoDetails()); |
| 283 } | 281 } |
| 284 | 282 |
| 285 // Defers a tab. | 283 // Defers a tab. |
| 286 void DeferTab(size_t tab_index) { | 284 void DeferTab(size_t tab_index) { |
| 287 content::WebContents* contents = restored_tabs_[tab_index].contents(); | 285 content::WebContents* contents = restored_tabs_[tab_index].contents(); |
| 288 content::NavigationController* controller = &contents->GetController(); | 286 content::NavigationController* controller = &contents->GetController(); |
| 289 stats_collector_->DeferTab(controller); | 287 stats_collector_->DeferTab(controller); |
| 290 } | 288 } |
| 291 | 289 |
| 290 content::TestBrowserThreadBundle test_browser_thread_bundle_; |
| 291 // |test_browser_thread_bundle_| needs to still be alive when |
| 292 // |testing_profile_| is destroyed. |
| 293 TestingProfile testing_profile_; |
| 294 |
| 292 // Inputs to the stats collector. Reset prior to each test. | 295 // Inputs to the stats collector. Reset prior to each test. |
| 293 base::SimpleTestTickClock* test_tick_clock_; | 296 base::SimpleTestTickClock* test_tick_clock_; |
| 294 std::vector<RestoredTab> restored_tabs_; | 297 std::vector<RestoredTab> restored_tabs_; |
| 295 | 298 |
| 296 // Infrastructure needed for using the TestWebContentsFactory. These are | |
| 297 // initialized once by the fixture and reused across unittests. | |
| 298 base::MessageLoop message_loop_; | |
| 299 content::TestBrowserThread ui_thread_; | |
| 300 // |ui_thread_| needs to still be alive when |testing_profile_| is destroyed. | |
| 301 TestingProfile testing_profile_; | |
| 302 | |
| 303 // A new web contents factory is generated per test. This automatically cleans | 299 // A new web contents factory is generated per test. This automatically cleans |
| 304 // up any tabs created by previous tests. | 300 // up any tabs created by previous tests. |
| 305 std::unique_ptr<content::TestWebContentsFactory> test_web_contents_factory_; | 301 std::unique_ptr<content::TestWebContentsFactory> test_web_contents_factory_; |
| 306 | 302 |
| 307 // These are recreated for each test. The reporting delegate allows the test | 303 // These are recreated for each test. The reporting delegate allows the test |
| 308 // to observe the behaviour of the SessionRestoreStatsCollector under test. | 304 // to observe the behaviour of the SessionRestoreStatsCollector under test. |
| 309 PassthroughStatsReportingDelegate* passthrough_reporting_delegate_; | 305 PassthroughStatsReportingDelegate* passthrough_reporting_delegate_; |
| 310 TestSessionRestoreStatsCollector* stats_collector_; | 306 TestSessionRestoreStatsCollector* stats_collector_; |
| 311 | 307 |
| 312 private: | 308 private: |
| (...skipping 319 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 632 mock_reporting_delegate.ExpectReportTabLoaderStatsCalled(2, 1, 1, 0, 0, 0, 0, | 628 mock_reporting_delegate.ExpectReportTabLoaderStatsCalled(2, 1, 1, 0, 0, 0, 0, |
| 633 1); | 629 1); |
| 634 mock_reporting_delegate.EnsureNoUnexpectedCalls(); | 630 mock_reporting_delegate.EnsureNoUnexpectedCalls(); |
| 635 | 631 |
| 636 // Destroy the background tab. The collector should release itself. | 632 // Destroy the background tab. The collector should release itself. |
| 637 Tick(); // 3 ms. | 633 Tick(); // 3 ms. |
| 638 GenerateWebContentsDestroyed(1); | 634 GenerateWebContentsDestroyed(1); |
| 639 mock_reporting_delegate.ExpectReportStatsCollectorDeathCalled(); | 635 mock_reporting_delegate.ExpectReportStatsCollectorDeathCalled(); |
| 640 mock_reporting_delegate.EnsureNoUnexpectedCalls(); | 636 mock_reporting_delegate.EnsureNoUnexpectedCalls(); |
| 641 } | 637 } |
| OLD | NEW |