OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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 "chrome/browser/sync/sessions/sync_sessions_web_contents_router.h" |
| 6 #include "base/memory/ptr_util.h" |
| 7 #include "chrome/browser/sync/sessions/sync_sessions_web_contents_router_factory
.h" |
| 8 #include "chrome/browser/ui/sync/tab_contents_synced_tab_delegate.h" |
| 9 #include "chrome/test/base/testing_profile.h" |
| 10 #include "content/public/test/test_browser_thread_bundle.h" |
| 11 #include "content/public/test/web_contents_tester.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 |
| 14 namespace sync_sessions { |
| 15 |
| 16 class StartSyncFlareMock { |
| 17 public: |
| 18 StartSyncFlareMock() {} |
| 19 ~StartSyncFlareMock() {} |
| 20 |
| 21 void StartSyncFlare(syncer::ModelType type) { was_run_ = true; } |
| 22 |
| 23 bool was_run() { return was_run_; } |
| 24 |
| 25 private: |
| 26 bool was_run_ = false; |
| 27 }; |
| 28 |
| 29 class SyncSessionsWebContentsRouterTest : public testing::Test { |
| 30 protected: |
| 31 ~SyncSessionsWebContentsRouterTest() override {} |
| 32 |
| 33 void SetUp() override { |
| 34 router_ = |
| 35 SyncSessionsWebContentsRouterFactory::GetInstance()->GetForProfile( |
| 36 &profile_); |
| 37 test_contents_.reset( |
| 38 content::WebContentsTester::CreateTestWebContents(&profile_, nullptr)); |
| 39 } |
| 40 |
| 41 SyncSessionsWebContentsRouter* router() { return router_; } |
| 42 content::WebContents* test_contents() { return test_contents_.get(); } |
| 43 |
| 44 private: |
| 45 content::TestBrowserThreadBundle thread_bundle_; |
| 46 TestingProfile profile_; |
| 47 SyncSessionsWebContentsRouter* router_; |
| 48 std::unique_ptr<content::WebContents> test_contents_; |
| 49 }; |
| 50 |
| 51 TEST_F(SyncSessionsWebContentsRouterTest, FlareNotRun) { |
| 52 StartSyncFlareMock mock; |
| 53 router()->InjectStartSyncFlare( |
| 54 base::Bind(&StartSyncFlareMock::StartSyncFlare, base::Unretained(&mock))); |
| 55 |
| 56 // There's no delegate for the tab, so the flare shouldn't run. |
| 57 router()->NotifyTabModified(test_contents(), false); |
| 58 EXPECT_FALSE(mock.was_run()); |
| 59 |
| 60 TabContentsSyncedTabDelegate::CreateForWebContents(test_contents()); |
| 61 |
| 62 // There's a delegate for the tab, but it's not a load completed event, so the |
| 63 // flare still shouldn't run. |
| 64 router()->NotifyTabModified(test_contents(), false); |
| 65 EXPECT_FALSE(mock.was_run()); |
| 66 } |
| 67 |
| 68 // Make sure we don't crash when there's not a flare. |
| 69 TEST_F(SyncSessionsWebContentsRouterTest, FlareNotSet) { |
| 70 TabContentsSyncedTabDelegate::CreateForWebContents(test_contents()); |
| 71 router()->NotifyTabModified(test_contents(), false); |
| 72 } |
| 73 |
| 74 // Disabled on android due to complexity of creating a full TabAndroid object |
| 75 // for a unit test. The logic being tested here isn't directly affected by |
| 76 // platform-specific peculiarities. |
| 77 #if !defined(OS_ANDROID) |
| 78 TEST_F(SyncSessionsWebContentsRouterTest, FlareRunsForLoadCompleted) { |
| 79 TabContentsSyncedTabDelegate::CreateForWebContents(test_contents()); |
| 80 |
| 81 StartSyncFlareMock mock; |
| 82 router()->InjectStartSyncFlare( |
| 83 base::Bind(&StartSyncFlareMock::StartSyncFlare, base::Unretained(&mock))); |
| 84 |
| 85 // There's a delegate for the tab, and it's a load completed event, so the |
| 86 // flare should run. |
| 87 router()->NotifyTabModified(test_contents(), true); |
| 88 EXPECT_TRUE(mock.was_run()); |
| 89 } |
| 90 #endif // !defined(OS_ANDROID) |
| 91 |
| 92 } // namespace sync_sessions |
OLD | NEW |