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 "content/public/test/test_browser_thread_bundle.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 |
| 19 namespace system_logs { |
| 20 |
| 21 using SupportedSource = SingleDebugDaemonLogSource::SupportedSource; |
| 22 |
| 23 class SingleDebugDaemonLogSourceTest : public ::testing::Test { |
| 24 public: |
| 25 SingleDebugDaemonLogSourceTest() |
| 26 : scoped_task_environment_( |
| 27 base::test::ScopedTaskEnvironment::MainThreadType::UI), |
| 28 fetch_callback_( |
| 29 base::Bind(&SingleDebugDaemonLogSourceTest::OnFetchComplete, |
| 30 base::Unretained(this))), |
| 31 num_callback_calls_(0) {} |
| 32 |
| 33 void SetUp() override { |
| 34 // Since no debug daemon will be available during a unit test, use |
| 35 // FakeDebugDaemonClient to provide dummy DebugDaemonClient functionality. |
| 36 chromeos::DBusThreadManager::GetSetterForTesting()->SetDebugDaemonClient( |
| 37 base::MakeUnique<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 // Creates the necessary browser threads. Defined after |
| 66 // |scoped_task_environment_| in order to use the MessageLoop it created. |
| 67 content::TestBrowserThreadBundle browser_thread_bundle_; |
| 68 |
| 69 // Pre-made callback object for passing OnFetchComplete() to an asynchronous |
| 70 // function. |
| 71 const SysLogsSourceCallback fetch_callback_; |
| 72 |
| 73 // Used to verify that OnFetchComplete was called the correct number of times. |
| 74 int num_callback_calls_; |
| 75 |
| 76 // Stores results from the log source. |
| 77 SystemLogsResponse response_; |
| 78 |
| 79 DISALLOW_COPY_AND_ASSIGN(SingleDebugDaemonLogSourceTest); |
| 80 }; |
| 81 |
| 82 TEST_F(SingleDebugDaemonLogSourceTest, SingleCall) { |
| 83 SingleDebugDaemonLogSource source(SupportedSource::kModetest); |
| 84 |
| 85 source.Fetch(fetch_callback()); |
| 86 base::RunLoop().RunUntilIdle(); |
| 87 |
| 88 EXPECT_EQ(1, num_callback_calls()); |
| 89 ASSERT_EQ(1U, response().size()); |
| 90 |
| 91 EXPECT_EQ("modetest", response().begin()->first); |
| 92 EXPECT_EQ("modetest: response from GetLog", response().begin()->second); |
| 93 } |
| 94 |
| 95 TEST_F(SingleDebugDaemonLogSourceTest, MultipleCalls) { |
| 96 SingleDebugDaemonLogSource source(SupportedSource::kLsusb); |
| 97 |
| 98 source.Fetch(fetch_callback()); |
| 99 base::RunLoop().RunUntilIdle(); |
| 100 |
| 101 EXPECT_EQ(1, num_callback_calls()); |
| 102 ASSERT_EQ(1U, response().size()); |
| 103 |
| 104 EXPECT_EQ("lsusb", response().begin()->first); |
| 105 EXPECT_EQ("lsusb: response from GetLog", response().begin()->second); |
| 106 |
| 107 ClearResponse(); |
| 108 |
| 109 source.Fetch(fetch_callback()); |
| 110 base::RunLoop().RunUntilIdle(); |
| 111 |
| 112 EXPECT_EQ(2, num_callback_calls()); |
| 113 ASSERT_EQ(1U, response().size()); |
| 114 |
| 115 EXPECT_EQ("lsusb", response().begin()->first); |
| 116 EXPECT_EQ("lsusb: response from GetLog", response().begin()->second); |
| 117 |
| 118 ClearResponse(); |
| 119 |
| 120 source.Fetch(fetch_callback()); |
| 121 base::RunLoop().RunUntilIdle(); |
| 122 |
| 123 EXPECT_EQ(3, num_callback_calls()); |
| 124 ASSERT_EQ(1U, response().size()); |
| 125 |
| 126 EXPECT_EQ("lsusb", response().begin()->first); |
| 127 EXPECT_EQ("lsusb: response from GetLog", response().begin()->second); |
| 128 } |
| 129 |
| 130 TEST_F(SingleDebugDaemonLogSourceTest, MultipleSources) { |
| 131 SingleDebugDaemonLogSource source1(SupportedSource::kModetest); |
| 132 source1.Fetch(fetch_callback()); |
| 133 base::RunLoop().RunUntilIdle(); |
| 134 |
| 135 EXPECT_EQ(1, num_callback_calls()); |
| 136 ASSERT_EQ(1U, response().size()); |
| 137 |
| 138 EXPECT_EQ("modetest", response().begin()->first); |
| 139 EXPECT_EQ("modetest: response from GetLog", response().begin()->second); |
| 140 |
| 141 ClearResponse(); |
| 142 |
| 143 SingleDebugDaemonLogSource source2(SupportedSource::kLsusb); |
| 144 source2.Fetch(fetch_callback()); |
| 145 base::RunLoop().RunUntilIdle(); |
| 146 |
| 147 EXPECT_EQ(2, num_callback_calls()); |
| 148 ASSERT_EQ(1U, response().size()); |
| 149 |
| 150 EXPECT_EQ("lsusb", response().begin()->first); |
| 151 EXPECT_EQ("lsusb: response from GetLog", response().begin()->second); |
| 152 } |
| 153 |
| 154 } // namespace system_logs |
OLD | NEW |