OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/api/feedback_private/feedback_private_api.h" | 5 #include "chrome/browser/extensions/api/feedback_private/feedback_private_api.h" |
6 | 6 |
7 #include "base/json/json_writer.h" | 7 #include "base/json/json_writer.h" |
8 #include "base/macros.h" | 8 #include "base/macros.h" |
9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
10 #include "base/test/simple_test_tick_clock.h" | 10 #include "base/test/simple_test_tick_clock.h" |
11 #include "base/values.h" | 11 #include "base/values.h" |
12 #include "chrome/browser/extensions/api/feedback_private/log_source_resource.h" | 12 #include "chrome/browser/extensions/api/feedback_private/log_source_resource.h" |
13 #include "chrome/browser/extensions/api/feedback_private/single_log_source_facto
ry.h" | 13 #include "chrome/browser/extensions/api/feedback_private/single_log_source_facto
ry.h" |
14 #include "chrome/browser/extensions/extension_api_unittest.h" | 14 #include "chrome/browser/extensions/extension_api_unittest.h" |
15 #include "extensions/browser/api_test_utils.h" | 15 #include "extensions/browser/api_test_utils.h" |
16 | 16 |
17 namespace extensions { | 17 namespace extensions { |
18 | 18 |
19 namespace { | 19 namespace { |
20 | 20 |
| 21 using api::feedback_private::LogSource; |
21 using api::feedback_private::ReadLogSourceResult; | 22 using api::feedback_private::ReadLogSourceResult; |
22 using api::feedback_private::ReadLogSourceParams; | 23 using api::feedback_private::ReadLogSourceParams; |
23 using base::TimeDelta; | 24 using base::TimeDelta; |
24 using system_logs::SingleLogSource; | |
25 using system_logs::SystemLogsResponse; | 25 using system_logs::SystemLogsResponse; |
26 using SupportedSource = system_logs::SingleLogSource::SupportedSource; | 26 using system_logs::SystemLogsSource; |
27 | 27 |
28 std::unique_ptr<KeyedService> ApiResourceManagerTestFactory( | 28 std::unique_ptr<KeyedService> ApiResourceManagerTestFactory( |
29 content::BrowserContext* context) { | 29 content::BrowserContext* context) { |
30 return base::MakeUnique<ApiResourceManager<LogSourceResource>>(context); | 30 return base::MakeUnique<ApiResourceManager<LogSourceResource>>(context); |
31 } | 31 } |
32 | 32 |
33 // Converts |params| to a string containing a JSON dictionary within an argument | 33 // Converts |params| to a string containing a JSON dictionary within an argument |
34 // list. | 34 // list. |
35 std::string ParamsToJSON(const ReadLogSourceParams& params) { | 35 std::string ParamsToJSON(const ReadLogSourceParams& params) { |
36 base::ListValue params_value; | 36 base::ListValue params_value; |
37 params_value.Append(params.ToValue()); | 37 params_value.Append(params.ToValue()); |
38 std::string params_json_string; | 38 std::string params_json_string; |
39 EXPECT_TRUE(base::JSONWriter::Write(params_value, ¶ms_json_string)); | 39 EXPECT_TRUE(base::JSONWriter::Write(params_value, ¶ms_json_string)); |
40 | 40 |
41 return params_json_string; | 41 return params_json_string; |
42 } | 42 } |
43 | 43 |
44 // A dummy SingleLogSource that does not require real system logs to be | 44 // A dummy SystemLogsSource that does not require real system logs to be |
45 // available during testing. | 45 // available during testing. |
46 class TestSingleLogSource : public SingleLogSource { | 46 class TestSingleLogSource : public SystemLogsSource { |
47 public: | 47 public: |
48 explicit TestSingleLogSource(SupportedSource type) | 48 explicit TestSingleLogSource(LogSource type) |
49 : SingleLogSource(type), call_count_(0) {} | 49 : SystemLogsSource(ToString(type)), call_count_(0) {} |
50 | 50 |
51 ~TestSingleLogSource() override = default; | 51 ~TestSingleLogSource() override = default; |
52 | 52 |
53 // Fetch() will return a single different string each time, in the following | 53 // Fetch() will return a single different string each time, in the following |
54 // sequence: "a", " bb", " ccc", until 25 spaces followed by 26 z's. Will | 54 // sequence: "a", " bb", " ccc", until 25 spaces followed by 26 z's. Will |
55 // never return an empty result. | 55 // never return an empty result. |
56 void Fetch(const system_logs::SysLogsSourceCallback& callback) override { | 56 void Fetch(const system_logs::SysLogsSourceCallback& callback) override { |
57 int count_modulus = call_count_ % kNumCharsToIterate; | 57 int count_modulus = call_count_ % kNumCharsToIterate; |
58 std::string result = | 58 std::string result = |
59 std::string(count_modulus, ' ') + | 59 std::string(count_modulus, ' ') + |
60 std::string(count_modulus + 1, kInitialChar + count_modulus); | 60 std::string(count_modulus + 1, kInitialChar + count_modulus); |
61 ASSERT_GT(result.size(), 0U); | 61 ASSERT_GT(result.size(), 0U); |
62 ++call_count_; | 62 ++call_count_; |
63 | 63 |
64 SystemLogsResponse* result_map = new SystemLogsResponse; | 64 SystemLogsResponse* result_map = new SystemLogsResponse; |
65 result_map->emplace("", result); | 65 result_map->emplace("", result); |
66 | 66 |
67 // Do not directly pass the result to the callback, because that's not how | 67 // Do not directly pass the result to the callback, because that's not how |
68 // log sources actually work. Instead, simulate the asynchronous operation | 68 // log sources actually work. Instead, simulate the asynchronous operation |
69 // of a SingleLogSource by invoking the callback separately. | 69 // of a SystemLogsSource by invoking the callback separately. |
70 base::ThreadTaskRunnerHandle::Get()->PostTask( | 70 base::ThreadTaskRunnerHandle::Get()->PostTask( |
71 FROM_HERE, base::BindOnce(callback, base::Owned(result_map))); | 71 FROM_HERE, base::BindOnce(callback, base::Owned(result_map))); |
72 } | 72 } |
73 | 73 |
74 // Instantiates a new instance of this class. Does not retain ownership. Used | 74 // Instantiates a new instance of this class. Does not retain ownership. Used |
75 // to create a Callback that can be used to override the default behavior of | 75 // to create a Callback that can be used to override the default behavior of |
76 // SingleLogSourceFactory. | 76 // SingleLogSourceFactory. |
77 static std::unique_ptr<SingleLogSource> Create(SupportedSource type) { | 77 static std::unique_ptr<SystemLogsSource> Create(LogSource type) { |
78 return base::MakeUnique<TestSingleLogSource>(type); | 78 return base::MakeUnique<TestSingleLogSource>(type); |
79 } | 79 } |
80 | 80 |
81 private: | 81 private: |
82 // Iterate over the whole lowercase alphabet, starting from 'a'. | 82 // Iterate over the whole lowercase alphabet, starting from 'a'. |
83 const int kNumCharsToIterate = 26; | 83 const int kNumCharsToIterate = 26; |
84 const char kInitialChar = 'a'; | 84 const char kInitialChar = 'a'; |
85 | 85 |
86 // Keep track of how many times Fetch() has been called, in order to determine | 86 // Keep track of how many times Fetch() has been called, in order to determine |
87 // its behavior each time. | 87 // its behavior each time. |
(...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
385 EXPECT_FALSE( | 385 EXPECT_FALSE( |
386 RunReadLogSourceFunction(params, &result_reader_id, &result_string)); | 386 RunReadLogSourceFunction(params, &result_reader_id, &result_string)); |
387 | 387 |
388 // Another read is finally allowed at t=310. | 388 // Another read is finally allowed at t=310. |
389 test_clock->Advance(TimeDelta::FromMilliseconds(1)); | 389 test_clock->Advance(TimeDelta::FromMilliseconds(1)); |
390 EXPECT_TRUE( | 390 EXPECT_TRUE( |
391 RunReadLogSourceFunction(params, &result_reader_id, &result_string)); | 391 RunReadLogSourceFunction(params, &result_reader_id, &result_string)); |
392 } | 392 } |
393 | 393 |
394 } // namespace extensions | 394 } // namespace extensions |
OLD | NEW |