| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "base/macros.h" |
| 5 #include "base/message_loop/message_loop.h" | 6 #include "base/message_loop/message_loop.h" |
| 7 #include "base/task_scheduler/post_task.h" |
| 6 #include "build/build_config.h" | 8 #include "build/build_config.h" |
| 7 #include "chrome/browser/extensions/api/feedback_private/feedback_private_api.h" | 9 #include "chrome/browser/extensions/api/feedback_private/feedback_private_api.h" |
| 10 #include "chrome/browser/extensions/api/feedback_private/single_log_source_facto
ry.h" |
| 8 #include "chrome/browser/extensions/extension_apitest.h" | 11 #include "chrome/browser/extensions/extension_apitest.h" |
| 9 | 12 |
| 10 namespace extensions { | 13 namespace extensions { |
| 11 | 14 |
| 15 namespace { |
| 16 |
| 17 using system_logs::SingleLogSource; |
| 18 using system_logs::SystemLogsResponse; |
| 19 using SupportedSource = system_logs::SingleLogSource::SupportedSource; |
| 20 |
| 21 // A dummy SingleLogSource that does not require real system logs to be |
| 22 // available during testing. |
| 23 class TestSingleLogSource : public SingleLogSource { |
| 24 public: |
| 25 explicit TestSingleLogSource(SupportedSource type) |
| 26 : SingleLogSource(type), call_count_(0) {} |
| 27 |
| 28 ~TestSingleLogSource() override {} |
| 29 |
| 30 // Fetch() will return a single different string each time, in the following |
| 31 // sequence: "a", "bb", "ccc", until a string of 26 z's. Will never return an |
| 32 // empty result. |
| 33 void Fetch(const system_logs::SysLogsSourceCallback& callback) override { |
| 34 int count_modulus = call_count_ % kNumCharsToIterate; |
| 35 std::string result(count_modulus + 1, kInitialChar + count_modulus); |
| 36 CHECK_GT(result.size(), 0U); |
| 37 ++call_count_; |
| 38 |
| 39 SystemLogsResponse* result_map = new SystemLogsResponse; |
| 40 result_map->emplace("", result); |
| 41 |
| 42 // Do not directly pass the result to the callback, because that's not how |
| 43 // log sources actually work. Instead, simulate the asynchronous operation |
| 44 // of a SingleLogSource by invoking the callback separately. |
| 45 base::PostDelayedTaskWithTraits( |
| 46 FROM_HERE, base::TaskPriority::BACKGROUND, |
| 47 base::Bind(callback, base::Owned(result_map)), |
| 48 base::TimeDelta::FromMilliseconds(0)); |
| 49 } |
| 50 |
| 51 // Instantiates a new instance of this class. Does not retain ownership. Used |
| 52 // to create a Callback that can be used to override the default behavior of |
| 53 // SingleLogSourceFactory. |
| 54 static SingleLogSource* Create(SupportedSource type) { |
| 55 return new TestSingleLogSource(type); |
| 56 } |
| 57 |
| 58 private: |
| 59 // Iterate over the whole lowercase alphabet, starting from 'a'. |
| 60 const int kNumCharsToIterate = 26; |
| 61 const char kInitialChar = 'a'; |
| 62 |
| 63 // Keep track of how many times Fetch() has been called, in order to determine |
| 64 // its behavior each time. |
| 65 int call_count_; |
| 66 |
| 67 DISALLOW_COPY_AND_ASSIGN(TestSingleLogSource); |
| 68 }; |
| 69 |
| 70 } // namespace |
| 71 |
| 12 class FeedbackApiTest: public ExtensionApiTest { | 72 class FeedbackApiTest: public ExtensionApiTest { |
| 13 public: | 73 public: |
| 14 FeedbackApiTest() {} | 74 FeedbackApiTest() {} |
| 15 ~FeedbackApiTest() override {} | 75 ~FeedbackApiTest() override {} |
| 76 |
| 77 #if defined(OS_CHROMEOS) |
| 78 void SetUp() override { |
| 79 ExtensionApiTest::SetUp(); |
| 80 |
| 81 create_callback_ = base::Bind(&TestSingleLogSource::Create); |
| 82 SingleLogSourceFactory::InitForTesting(&create_callback_); |
| 83 } |
| 84 |
| 85 void TearDown() override { |
| 86 SingleLogSourceFactory::InitForTesting(nullptr); |
| 87 FeedbackPrivateReadLogSourceFunction::SetRateLimitingTimeoutForTesting( |
| 88 nullptr); |
| 89 |
| 90 ExtensionApiTest::TearDown(); |
| 91 } |
| 92 |
| 93 protected: |
| 94 SingleLogSourceFactory::CreateCallback create_callback_; |
| 95 #endif // defined(OS_CHROMEOS) |
| 16 }; | 96 }; |
| 17 | 97 |
| 18 IN_PROC_BROWSER_TEST_F(FeedbackApiTest, Basic) { | 98 IN_PROC_BROWSER_TEST_F(FeedbackApiTest, Basic) { |
| 19 EXPECT_TRUE(RunExtensionTest("feedback_private/basic")) << message_; | 99 EXPECT_TRUE(RunExtensionTest("feedback_private/basic")) << message_; |
| 20 } | 100 } |
| 21 | 101 |
| 102 #if defined(OS_CHROMEOS) |
| 103 IN_PROC_BROWSER_TEST_F(FeedbackApiTest, ReadLogSource) { |
| 104 const base::TimeDelta timeout(base::TimeDelta::FromMilliseconds(0)); |
| 105 FeedbackPrivateReadLogSourceFunction::SetRateLimitingTimeoutForTesting( |
| 106 &timeout); |
| 107 |
| 108 EXPECT_TRUE(RunExtensionTest("feedback_private/read_log_source")) << message_; |
| 109 } |
| 110 |
| 111 IN_PROC_BROWSER_TEST_F(FeedbackApiTest, ReadLogSourceRateLimited) { |
| 112 const base::TimeDelta timeout(base::TimeDelta::FromMilliseconds(2000)); |
| 113 FeedbackPrivateReadLogSourceFunction::SetRateLimitingTimeoutForTesting( |
| 114 &timeout); |
| 115 |
| 116 EXPECT_TRUE(RunExtensionTest("feedback_private/read_log_source_rate_limited")) |
| 117 << message_; |
| 118 } |
| 119 #endif // defined(OS_CHROMEOS) |
| 120 |
| 22 } // namespace extensions | 121 } // namespace extensions |
| OLD | NEW |