Chromium Code Reviews| 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) | |
|
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.
| |
| 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 LogSourceAccessManager::SetRateLimitingTimeoutForTesting(nullptr); | |
| 88 | |
| 89 ExtensionApiTest::TearDown(); | |
| 90 } | |
| 91 | |
| 92 protected: | |
| 93 SingleLogSourceFactory::CreateCallback create_callback_; | |
| 94 #endif // defined(OS_CHROMEOS) | |
| 16 }; | 95 }; |
| 17 | 96 |
| 18 IN_PROC_BROWSER_TEST_F(FeedbackApiTest, Basic) { | 97 IN_PROC_BROWSER_TEST_F(FeedbackApiTest, Basic) { |
| 19 EXPECT_TRUE(RunExtensionTest("feedback_private/basic")) << message_; | 98 EXPECT_TRUE(RunExtensionTest("feedback_private/basic")) << message_; |
| 20 } | 99 } |
| 21 | 100 |
| 101 #if defined(OS_CHROMEOS) | |
| 102 IN_PROC_BROWSER_TEST_F(FeedbackApiTest, ReadLogSource) { | |
| 103 const base::TimeDelta timeout(base::TimeDelta::FromMilliseconds(0)); | |
| 104 LogSourceAccessManager::SetRateLimitingTimeoutForTesting(&timeout); | |
| 105 | |
| 106 EXPECT_TRUE(RunExtensionTest("feedback_private/read_log_source")) << message_; | |
| 107 } | |
| 108 | |
| 109 IN_PROC_BROWSER_TEST_F(FeedbackApiTest, ReadLogSourceRateLimited) { | |
| 110 const base::TimeDelta timeout(base::TimeDelta::FromMilliseconds(2000)); | |
| 111 LogSourceAccessManager::SetRateLimitingTimeoutForTesting(&timeout); | |
| 112 | |
| 113 EXPECT_TRUE(RunExtensionTest("feedback_private/read_log_source_rate_limited")) | |
| 114 << message_; | |
| 115 } | |
| 116 #endif // defined(OS_CHROMEOS) | |
| 117 | |
| 22 } // namespace extensions | 118 } // namespace extensions |
| OLD | NEW |