| 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/chromeos/system_logs/single_debug_daemon_log_source.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/macros.h" |
| 11 #include "base/memory/ptr_util.h" |
| 12 #include "base/run_loop.h" |
| 13 #include "base/test/scoped_task_environment.h" |
| 14 #include "chromeos/dbus/dbus_thread_manager.h" |
| 15 #include "chromeos/dbus/fake_debug_daemon_client.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 |
| 18 namespace system_logs { |
| 19 |
| 20 using SupportedSource = SingleDebugDaemonLogSource::SupportedSource; |
| 21 |
| 22 class SingleDebugDaemonLogSourceTest : public ::testing::Test { |
| 23 public: |
| 24 SingleDebugDaemonLogSourceTest() |
| 25 : scoped_task_environment_( |
| 26 base::test::ScopedTaskEnvironment::MainThreadType::UI), |
| 27 fetch_callback_( |
| 28 base::Bind(&SingleDebugDaemonLogSourceTest::OnFetchComplete, |
| 29 base::Unretained(this))), |
| 30 num_callback_calls_(0) {} |
| 31 |
| 32 void SetUp() override { |
| 33 // Since no debug daemon will be available during a unit test, use |
| 34 // FakeDebugDaemonClient to provide dummy DebugDaemonClient functionality. |
| 35 chromeos::DBusThreadManager::GetSetterForTesting()->SetDebugDaemonClient( |
| 36 std::unique_ptr<chromeos::DebugDaemonClient>( |
| 37 new chromeos::FakeDebugDaemonClient)); |
| 38 } |
| 39 |
| 40 void TearDown() override { |
| 41 chromeos::DBusThreadManager::GetSetterForTesting()->SetDebugDaemonClient( |
| 42 nullptr); |
| 43 } |
| 44 |
| 45 protected: |
| 46 const SysLogsSourceCallback& fetch_callback() const { |
| 47 return fetch_callback_; |
| 48 } |
| 49 |
| 50 int num_callback_calls() const { return num_callback_calls_; } |
| 51 |
| 52 const SystemLogsResponse& response() const { return response_; } |
| 53 |
| 54 void ClearResponse() { response_.clear(); } |
| 55 |
| 56 private: |
| 57 void OnFetchComplete(SystemLogsResponse* response) { |
| 58 ++num_callback_calls_; |
| 59 response_ = *response; |
| 60 } |
| 61 |
| 62 // For running scheduled tasks. |
| 63 base::test::ScopedTaskEnvironment scoped_task_environment_; |
| 64 |
| 65 // Pre-made callback object for passing OnFetchComplete() to an asynchronous |
| 66 // function. |
| 67 const SysLogsSourceCallback fetch_callback_; |
| 68 |
| 69 // Used to verify that OnFetchComplete was called the correct number of times. |
| 70 int num_callback_calls_; |
| 71 |
| 72 // Stores results from the log source. |
| 73 SystemLogsResponse response_; |
| 74 |
| 75 DISALLOW_COPY_AND_ASSIGN(SingleDebugDaemonLogSourceTest); |
| 76 }; |
| 77 |
| 78 TEST_F(SingleDebugDaemonLogSourceTest, SingleCall) { |
| 79 SingleDebugDaemonLogSource source(SupportedSource::kModetest); |
| 80 |
| 81 source.Fetch(fetch_callback()); |
| 82 base::RunLoop().RunUntilIdle(); |
| 83 |
| 84 EXPECT_EQ(1, num_callback_calls()); |
| 85 EXPECT_EQ(1U, response().size()); |
| 86 |
| 87 EXPECT_EQ("modetest", response().begin()->first); |
| 88 EXPECT_EQ("Response from individual log", response().begin()->second); |
| 89 } |
| 90 |
| 91 TEST_F(SingleDebugDaemonLogSourceTest, MultipleCalls) { |
| 92 SingleDebugDaemonLogSource source(SupportedSource::kLsusb); |
| 93 |
| 94 source.Fetch(fetch_callback()); |
| 95 base::RunLoop().RunUntilIdle(); |
| 96 |
| 97 EXPECT_EQ(1, num_callback_calls()); |
| 98 EXPECT_EQ(1U, response().size()); |
| 99 |
| 100 EXPECT_EQ("lsusb", response().begin()->first); |
| 101 EXPECT_EQ("Response from individual log", response().begin()->second); |
| 102 |
| 103 ClearResponse(); |
| 104 |
| 105 source.Fetch(fetch_callback()); |
| 106 base::RunLoop().RunUntilIdle(); |
| 107 |
| 108 EXPECT_EQ(2, num_callback_calls()); |
| 109 EXPECT_EQ(1U, response().size()); |
| 110 |
| 111 EXPECT_EQ("lsusb", response().begin()->first); |
| 112 EXPECT_EQ("Response from individual log", response().begin()->second); |
| 113 |
| 114 ClearResponse(); |
| 115 |
| 116 source.Fetch(fetch_callback()); |
| 117 base::RunLoop().RunUntilIdle(); |
| 118 |
| 119 EXPECT_EQ(3, num_callback_calls()); |
| 120 EXPECT_EQ(1U, response().size()); |
| 121 |
| 122 EXPECT_EQ("lsusb", response().begin()->first); |
| 123 EXPECT_EQ("Response from individual log", response().begin()->second); |
| 124 } |
| 125 |
| 126 TEST_F(SingleDebugDaemonLogSourceTest, MultipleSources) { |
| 127 SingleDebugDaemonLogSource source1(SupportedSource::kModetest); |
| 128 source1.Fetch(fetch_callback()); |
| 129 base::RunLoop().RunUntilIdle(); |
| 130 |
| 131 EXPECT_EQ(1, num_callback_calls()); |
| 132 EXPECT_EQ(1U, response().size()); |
| 133 |
| 134 EXPECT_EQ("modetest", response().begin()->first); |
| 135 EXPECT_EQ("Response from individual log", response().begin()->second); |
| 136 |
| 137 ClearResponse(); |
| 138 |
| 139 SingleDebugDaemonLogSource source2(SupportedSource::kLsusb); |
| 140 source2.Fetch(fetch_callback()); |
| 141 base::RunLoop().RunUntilIdle(); |
| 142 |
| 143 EXPECT_EQ(2, num_callback_calls()); |
| 144 EXPECT_EQ(1U, response().size()); |
| 145 |
| 146 EXPECT_EQ("lsusb", response().begin()->first); |
| 147 EXPECT_EQ("Response from individual log", response().begin()->second); |
| 148 } |
| 149 |
| 150 } // namespace system_logs |
| OLD | NEW |