1 // Copyright 2019 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/send_tab_to_self/send_tab_to_self_client_service.h"
6 
7 #include <memory>
8 
9 #include "base/time/time.h"
10 #include "chrome/browser/send_tab_to_self/desktop_notification_handler.h"
11 #include "chrome/browser/send_tab_to_self/receiving_ui_handler.h"
12 #include "components/send_tab_to_self/test_send_tab_to_self_model.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "url/gurl.h"
15 
16 namespace send_tab_to_self {
17 
18 namespace {
19 
20 // A test ReceivingUiHandler that keeps track of the number of entries for which
21 // DisplayNewEntry was called.
22 class TestReceivingUiHandler : public ReceivingUiHandler {
23  public:
24   TestReceivingUiHandler() = default;
~TestReceivingUiHandler()25   ~TestReceivingUiHandler() override {}
26 
DisplayNewEntries(const std::vector<const SendTabToSelfEntry * > & new_entries)27   void DisplayNewEntries(
28       const std::vector<const SendTabToSelfEntry*>& new_entries) override {
29     number_displayed_entries_ = number_displayed_entries_ + new_entries.size();
30   }
31 
DismissEntries(const std::vector<std::string> & guids)32   void DismissEntries(const std::vector<std::string>& guids) override {}
33 
number_displayed_entries() const34   size_t number_displayed_entries() const { return number_displayed_entries_; }
35 
36  private:
37   size_t number_displayed_entries_ = 0;
38 };
39 
40 // A test SendTabToSelfClientService that exposes the TestReceivingUiHandler.
41 class TestSendTabToSelfClientService : public SendTabToSelfClientService {
42  public:
TestSendTabToSelfClientService(SendTabToSelfModel * model)43   explicit TestSendTabToSelfClientService(SendTabToSelfModel* model)
44       : SendTabToSelfClientService(nullptr, model) {}
45 
46   ~TestSendTabToSelfClientService() override = default;
47 
SetupHandlerRegistry(Profile * profile)48   void SetupHandlerRegistry(Profile* profile) override {}
49 
SetupTestHandler()50   TestReceivingUiHandler* SetupTestHandler() {
51     test_handlers_.clear();
52     std::unique_ptr<TestReceivingUiHandler> handler =
53         std::make_unique<TestReceivingUiHandler>();
54     TestReceivingUiHandler* handler_ptr = handler.get();
55     test_handlers_.push_back(std::move(handler));
56     return handler_ptr;
57   }
58 
59   // This copies the SendTabToSelfClientService implementation without a cast to
60   // DesktopNotificationHandler on desktop platforms. See notes in that file.
EntriesAddedRemotely(const std::vector<const SendTabToSelfEntry * > & new_entries)61   void EntriesAddedRemotely(
62       const std::vector<const SendTabToSelfEntry*>& new_entries) override {
63     for (const std::unique_ptr<ReceivingUiHandler>& handler : GetHandlers()) {
64       handler->DisplayNewEntries(new_entries);
65     }
66   }
67 
GetHandlers() const68   const std::vector<std::unique_ptr<ReceivingUiHandler>>& GetHandlers()
69       const override {
70     return test_handlers_;
71   }
72 
73  protected:
74   std::vector<std::unique_ptr<ReceivingUiHandler>> test_handlers_;
75 };
76 
77 // Tests that the UI handlers gets notified of each entries that were added
78 // remotely.
TEST(SendTabToSelfClientServiceTest,MultipleEntriesAdded)79 TEST(SendTabToSelfClientServiceTest, MultipleEntriesAdded) {
80   // Set up the test objects.
81   TestSendTabToSelfModel test_model_;
82   TestSendTabToSelfClientService client_service(&test_model_);
83   TestReceivingUiHandler* test_handler = client_service.SetupTestHandler();
84 
85   // Create 2 entries and simulated that they were both added remotely.
86   SendTabToSelfEntry entry1("a", GURL("http://www.example-a.com"), "a site",
87                             base::Time(), base::Time(), "device a", "device b");
88   SendTabToSelfEntry entry2("b", GURL("http://www.example-b.com"), "b site",
89                             base::Time(), base::Time(), "device b", "device a");
90   client_service.EntriesAddedRemotely({&entry1, &entry2});
91 
92   EXPECT_EQ(2u, test_handler->number_displayed_entries());
93 }
94 
95 }  // namespace
96 
97 }  // namespace send_tab_to_self
98