Chromium Code Reviews| Index: chrome/browser/extensions/api/feedback_private/feedback_private_apitest.cc |
| diff --git a/chrome/browser/extensions/api/feedback_private/feedback_private_apitest.cc b/chrome/browser/extensions/api/feedback_private/feedback_private_apitest.cc |
| index de42a7ec285ea2d6665e4efe2a1d94c57cf50062..edc99e27a7be45f40f151e6c67fadb481e3018be 100644 |
| --- a/chrome/browser/extensions/api/feedback_private/feedback_private_apitest.cc |
| +++ b/chrome/browser/extensions/api/feedback_private/feedback_private_apitest.cc |
| @@ -2,21 +2,117 @@ |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| +#include "base/macros.h" |
| #include "base/message_loop/message_loop.h" |
| +#include "base/task_scheduler/post_task.h" |
| #include "build/build_config.h" |
| #include "chrome/browser/extensions/api/feedback_private/feedback_private_api.h" |
| +#include "chrome/browser/extensions/api/feedback_private/single_log_source_factory.h" |
| #include "chrome/browser/extensions/extension_apitest.h" |
| namespace extensions { |
| +namespace { |
| + |
| +using system_logs::SingleLogSource; |
| +using system_logs::SystemLogsResponse; |
| +using SupportedSource = system_logs::SingleLogSource::SupportedSource; |
| + |
| +// A dummy SingleLogSource that does not require real system logs to be |
| +// available during testing. |
| +class TestSingleLogSource : public SingleLogSource { |
| + public: |
| + explicit TestSingleLogSource(SupportedSource type) |
| + : SingleLogSource(type), call_count_(0) {} |
| + |
| + ~TestSingleLogSource() override {} |
| + |
| + // Fetch() will return a single different string each time, in the following |
| + // sequence: "a", "bb", "ccc", until a string of 26 z's. Will never return an |
| + // empty result. |
| + void Fetch(const system_logs::SysLogsSourceCallback& callback) override { |
| + int count_modulus = call_count_ % kNumCharsToIterate; |
| + std::string result(count_modulus + 1, kInitialChar + count_modulus); |
| + CHECK_GT(result.size(), 0U); |
| + ++call_count_; |
| + |
| + SystemLogsResponse* result_map = new SystemLogsResponse; |
| + result_map->emplace("", result); |
| + |
| + // Do not directly pass the result to the callback, because that's not how |
| + // log sources actually work. Instead, simulate the asynchronous operation |
| + // of a SingleLogSource by invoking the callback separately. |
| + base::PostDelayedTaskWithTraits( |
| + FROM_HERE, base::TaskPriority::BACKGROUND, |
| + base::Bind(callback, base::Owned(result_map)), |
| + base::TimeDelta::FromMilliseconds(0)); |
| + } |
| + |
| + // Instantiates a new instance of this class. Does not retain ownership. Used |
| + // to create a Callback that can be used to override the default behavior of |
| + // SingleLogSourceFactory. |
| + static SingleLogSource* Create(SupportedSource type) { |
| + return new TestSingleLogSource(type); |
| + } |
| + |
| + private: |
| + // Iterate over the whole lowercase alphabet, starting from 'a'. |
| + const int kNumCharsToIterate = 26; |
| + const char kInitialChar = 'a'; |
| + |
| + // Keep track of how many times Fetch() has been called, in order to determine |
| + // its behavior each time. |
| + int call_count_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(TestSingleLogSource); |
| +}; |
| + |
| +} // namespace |
| + |
| class FeedbackApiTest: public ExtensionApiTest { |
| public: |
| FeedbackApiTest() {} |
| ~FeedbackApiTest() override {} |
| + |
| +#if defined(OS_CHROMEOS) |
|
tbarzic
2017/05/30 19:35:35
can you move the Chrome OS specific test code into
Simon Que
2017/05/31 00:21:43
Done.
|
| + void SetUp() override { |
| + ExtensionApiTest::SetUp(); |
| + |
| + create_callback_ = base::Bind(&TestSingleLogSource::Create); |
| + SingleLogSourceFactory::InitForTesting(&create_callback_); |
| + } |
| + |
| + void TearDown() override { |
| + SingleLogSourceFactory::InitForTesting(nullptr); |
| + LogSourceAccessManager::SetRateLimitingTimeoutForTesting(nullptr); |
| + |
| + ExtensionApiTest::TearDown(); |
| + } |
| + |
| + protected: |
| + SingleLogSourceFactory::CreateCallback create_callback_; |
| +#endif // defined(OS_CHROMEOS) |
| }; |
| IN_PROC_BROWSER_TEST_F(FeedbackApiTest, Basic) { |
| EXPECT_TRUE(RunExtensionTest("feedback_private/basic")) << message_; |
| } |
| +#if defined(OS_CHROMEOS) |
| +IN_PROC_BROWSER_TEST_F(FeedbackApiTest, ReadLogSource) { |
| + const base::TimeDelta timeout(base::TimeDelta::FromMilliseconds(0)); |
| + LogSourceAccessManager::SetRateLimitingTimeoutForTesting(&timeout); |
| + |
| + EXPECT_TRUE(RunExtensionTest("feedback_private/read_log_source")) << message_; |
| +} |
| + |
| +IN_PROC_BROWSER_TEST_F(FeedbackApiTest, ReadLogSourceRateLimited) { |
| + const base::TimeDelta timeout(base::TimeDelta::FromMilliseconds(2000)); |
| + LogSourceAccessManager::SetRateLimitingTimeoutForTesting(&timeout); |
| + |
| + EXPECT_TRUE(RunExtensionTest("feedback_private/read_log_source_rate_limited")) |
| + << message_; |
| +} |
| +#endif // defined(OS_CHROMEOS) |
| + |
| } // namespace extensions |