Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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 "components/ntp_snippets/sessions/tab_delegate_sync_adapter.h" | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 9 #include "base/macros.h" | |
| 10 #include "components/sync/driver/fake_sync_service.h" | |
| 11 #include "components/sync_sessions/open_tabs_ui_delegate.h" | |
| 12 #include "testing/gmock/include/gmock/gmock.h" | |
| 13 #include "testing/gtest/include/gtest/gtest.h" | |
| 14 | |
| 15 using base::RefCountedMemory; | |
| 16 using sessions::SessionTab; | |
| 17 using sessions::SessionWindow; | |
| 18 using syncer::SyncServiceObserver; | |
| 19 using sync_sessions::SyncedSession; | |
| 20 using sync_sessions::OpenTabsUIDelegate; | |
| 21 using testing::Test; | |
| 22 | |
| 23 namespace ntp_snippets { | |
| 24 namespace { | |
| 25 | |
| 26 class TestSyncService : public syncer::FakeSyncService { | |
| 27 public: | |
| 28 TestSyncService() {} | |
| 29 OpenTabsUIDelegate* GetOpenTabsUIDelegate() override { return tabs_; } | |
| 30 OpenTabsUIDelegate* tabs_ = nullptr; | |
| 31 | |
| 32 private: | |
| 33 DISALLOW_COPY_AND_ASSIGN(TestSyncService); | |
| 34 }; | |
| 35 | |
| 36 class MockOpenTabsUIDelegate : public OpenTabsUIDelegate { | |
| 37 public: | |
| 38 MockOpenTabsUIDelegate() {} | |
| 39 MOCK_CONST_METHOD2(GetSyncedFaviconForPageURL, | |
| 40 bool(const std::string&, | |
| 41 scoped_refptr<RefCountedMemory>*)); | |
| 42 MOCK_METHOD1(GetAllForeignSessions, bool(std::vector<const SyncedSession*>*)); | |
| 43 MOCK_METHOD3(GetForeignTab, | |
| 44 bool(const std::string&, | |
| 45 SessionID::id_type, | |
| 46 const SessionTab**)); | |
| 47 MOCK_METHOD1(DeleteForeignSession, void(const std::string&)); | |
| 48 MOCK_METHOD2(GetForeignSession, | |
| 49 bool(const std::string&, std::vector<const SessionWindow*>*)); | |
| 50 MOCK_METHOD2(GetForeignSessionTabs, | |
| 51 bool(const std::string&, std::vector<const SessionTab*>*)); | |
| 52 MOCK_METHOD1(GetLocalSession, bool(const SyncedSession**)); | |
| 53 | |
| 54 private: | |
| 55 DISALLOW_COPY_AND_ASSIGN(MockOpenTabsUIDelegate); | |
| 56 }; | |
| 57 | |
| 58 class TabDelegateSyncAdapterTest : public Test { | |
| 59 public: | |
| 60 TabDelegateSyncAdapterTest() : adapter_(&service_) { | |
| 61 adapter_.SubscribeForForeignTabChange(base::Bind( | |
| 62 &TabDelegateSyncAdapterTest::OnChange, base::Unretained(this))); | |
| 63 } | |
| 64 | |
| 65 SyncServiceObserver* observer() { return &adapter_; } | |
| 66 | |
| 67 void SetHasOpenTabs(bool is_enabled) { | |
| 68 service_.tabs_ = is_enabled ? &tabs_ : nullptr; | |
| 69 } | |
| 70 | |
| 71 void OnChange() { ++callback_count_; } | |
| 72 | |
| 73 int CallbackCount() { return callback_count_; } | |
| 74 | |
| 75 private: | |
| 76 MockOpenTabsUIDelegate tabs_; | |
| 77 TestSyncService service_; | |
| 78 TabDelegateSyncAdapter adapter_; | |
| 79 int callback_count_ = 0; | |
| 80 | |
| 81 DISALLOW_COPY_AND_ASSIGN(TabDelegateSyncAdapterTest); | |
| 82 }; | |
| 83 | |
| 84 // CallbackCount should only trigger on transitions between having and not | |
| 85 // having open tabs. | |
| 86 TEST_F(TabDelegateSyncAdapterTest, CallbackCount) { | |
| 87 EXPECT_EQ(0, CallbackCount()); | |
|
Marc Treib
2016/12/19 11:47:53
nit: ASSERT_EQ since this is a precondition rather
skym
2016/12/19 20:31:43
Done.
| |
| 88 observer()->OnStateChanged(); | |
| 89 EXPECT_EQ(0, CallbackCount()); | |
| 90 | |
| 91 SetHasOpenTabs(true); | |
| 92 observer()->OnStateChanged(); | |
| 93 EXPECT_EQ(1, CallbackCount()); | |
| 94 observer()->OnStateChanged(); | |
| 95 EXPECT_EQ(1, CallbackCount()); | |
| 96 | |
| 97 SetHasOpenTabs(false); | |
| 98 observer()->OnStateChanged(); | |
| 99 EXPECT_EQ(2, CallbackCount()); | |
| 100 observer()->OnStateChanged(); | |
| 101 EXPECT_EQ(2, CallbackCount()); | |
| 102 | |
| 103 // OnSyncCycleCompleted should behave like OnStateChanged. | |
| 104 observer()->OnSyncCycleCompleted(); | |
| 105 EXPECT_EQ(2, CallbackCount()); | |
| 106 SetHasOpenTabs(true); | |
| 107 observer()->OnSyncCycleCompleted(); | |
| 108 EXPECT_EQ(3, CallbackCount()); | |
| 109 observer()->OnSyncCycleCompleted(); | |
| 110 EXPECT_EQ(3, CallbackCount()); | |
| 111 } | |
| 112 | |
| 113 // No callback should be invoked from OnSyncConfigurationCompleted. | |
| 114 TEST_F(TabDelegateSyncAdapterTest, OnSyncConfigurationCompleted) { | |
| 115 EXPECT_EQ(0, CallbackCount()); | |
| 116 | |
| 117 observer()->OnSyncConfigurationCompleted(); | |
| 118 EXPECT_EQ(0, CallbackCount()); | |
| 119 | |
| 120 SetHasOpenTabs(true); | |
| 121 observer()->OnSyncConfigurationCompleted(); | |
| 122 EXPECT_EQ(0, CallbackCount()); | |
| 123 } | |
| 124 | |
| 125 // OnForeignSessionUpdated should always trigger a callback. | |
| 126 TEST_F(TabDelegateSyncAdapterTest, OnForeignSessionUpdated) { | |
| 127 EXPECT_EQ(0, CallbackCount()); | |
| 128 observer()->OnForeignSessionUpdated(); | |
| 129 EXPECT_EQ(1, CallbackCount()); | |
| 130 observer()->OnForeignSessionUpdated(); | |
| 131 EXPECT_EQ(2, CallbackCount()); | |
| 132 | |
| 133 SetHasOpenTabs(true); | |
| 134 observer()->OnForeignSessionUpdated(); | |
| 135 EXPECT_EQ(3, CallbackCount()); | |
| 136 observer()->OnForeignSessionUpdated(); | |
| 137 EXPECT_EQ(4, CallbackCount()); | |
| 138 } | |
| 139 | |
| 140 // If OnForeignSessionUpdated is called before OnStateChanged, then calling | |
| 141 // OnStateChanged should not trigger a callback. | |
| 142 TEST_F(TabDelegateSyncAdapterTest, OnForeignSessionUpdatedUpdatesState) { | |
| 143 SetHasOpenTabs(true); | |
| 144 observer()->OnForeignSessionUpdated(); | |
| 145 EXPECT_EQ(1, CallbackCount()); | |
| 146 | |
| 147 observer()->OnStateChanged(); | |
| 148 EXPECT_EQ(1, CallbackCount()); | |
| 149 } | |
| 150 | |
| 151 } // namespace | |
| 152 } // namespace ntp_snippets | |
| OLD | NEW |