Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(333)

Side by Side Diff: chrome/browser/extensions/api/feedback_private/feedback_private_apitest.cc

Issue 2840103002: Add new API function: feedbackPrivate.readLogSource (Closed)
Patch Set: Add comments to SingleLogSourceFactory; Add new histogram enum Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/message_loop/message_loop.h" 5 #include "base/message_loop/message_loop.h"
6 #include "base/task_scheduler/post_task.h"
6 #include "build/build_config.h" 7 #include "build/build_config.h"
7 #include "chrome/browser/extensions/api/feedback_private/feedback_private_api.h" 8 #include "chrome/browser/extensions/api/feedback_private/feedback_private_api.h"
9 #include "chrome/browser/extensions/api/feedback_private/single_log_source_facto ry.h"
8 #include "chrome/browser/extensions/extension_apitest.h" 10 #include "chrome/browser/extensions/extension_apitest.h"
9 11
10 namespace extensions { 12 namespace extensions {
11 13
14 namespace {
15
16 using system_logs::SingleLogSource;
17 using system_logs::SystemLogsResponse;
18 using SupportedSource = system_logs::SingleLogSource::SupportedSource;
19
20 // A dummy SingleLogSource that does not require real system logs to be
21 // available during testing.
22 class TestSingleLogSource : public SingleLogSource {
23 public:
24 explicit TestSingleLogSource(SupportedSource type)
25 : SingleLogSource(type), call_count_(0) {}
26
27 ~TestSingleLogSource() override {}
28
29 // Fetch() will return a single different string each time, in the following
30 // sequence: "a", "bb", "ccc", until a string of 26 z's. Will never return an
31 // empty result.
32 void Fetch(const system_logs::SysLogsSourceCallback& callback) override {
33 int count_modulus = call_count_ % kNumCharsToIterate;
34 std::string result(count_modulus + 1, kInitialChar + count_modulus);
35 CHECK_GT(result.size(), 0U);
36 ++call_count_;
37
38 SystemLogsResponse* result_map = new SystemLogsResponse;
39 result_map->emplace("", result);
40
41 // Do not directly pass the result to the callback, because that's not how
42 // log sources actually work. Instead, simulate the asynchronous operation
43 // of a SingleLogSource by invoking the callback after a delay.
44 base::PostDelayedTaskWithTraits(
45 FROM_HERE, base::TaskPriority::BACKGROUND,
46 base::Bind(callback, base::Owned(result_map)),
47 base::TimeDelta::FromMilliseconds(100));
tbarzic 2017/05/22 23:02:55 I think you should use 0 delay here, that should b
Simon Que 2017/05/23 20:01:19 Done.
48 }
49
50 // Instantiates a new instance of this class. Does not retain ownership. Used
51 // to create a Callback that can be used to override the default behavior of
52 // SingleLogSourceFactory.
53 static SingleLogSource* Create(SupportedSource type) {
54 return new TestSingleLogSource(type);
55 }
56
57 private:
58 // Iterate over the whole lowercase alphabet, starting from 'a'.
59 const int kNumCharsToIterate = 26;
60 const char kInitialChar = 'a';
61
62 // Keep track of how many times Fetch() has been called, in order to determine
63 // its behavior each time.
64 int call_count_;
tbarzic 2017/05/22 23:02:55 add DISALLOW_COPY_AND_ASSIGN
Simon Que 2017/05/23 20:01:19 Done.
65 };
66
67 } // namespace
68
12 class FeedbackApiTest: public ExtensionApiTest { 69 class FeedbackApiTest: public ExtensionApiTest {
13 public: 70 public:
14 FeedbackApiTest() {} 71 FeedbackApiTest() {}
15 ~FeedbackApiTest() override {} 72 ~FeedbackApiTest() override {}
16 }; 73 };
17 74
18 IN_PROC_BROWSER_TEST_F(FeedbackApiTest, Basic) { 75 IN_PROC_BROWSER_TEST_F(FeedbackApiTest, Basic) {
19 EXPECT_TRUE(RunExtensionTest("feedback_private/basic")) << message_; 76 EXPECT_TRUE(RunExtensionTest("feedback_private/basic")) << message_;
20 } 77 }
21 78
79 #if defined(OS_CHROMEOS)
80 IN_PROC_BROWSER_TEST_F(FeedbackApiTest, ReadLogSource) {
81 const base::TimeDelta timeout(base::TimeDelta::FromMilliseconds(0));
82 FeedbackPrivateReadLogSourceFunction::SetRateLimitingTimeoutForTesting(
83 &timeout);
84 SingleLogSourceFactory::CreateCallback callback =
85 base::Bind(&TestSingleLogSource::Create);
86 SingleLogSourceFactory::InitForTesting(&callback);
87
88 EXPECT_TRUE(RunExtensionTest("feedback_private/read_log_source")) << message_;
89
90 SingleLogSourceFactory::InitForTesting(nullptr);
91 FeedbackPrivateReadLogSourceFunction::SetRateLimitingTimeoutForTesting(
92 nullptr);
93 }
94
95 IN_PROC_BROWSER_TEST_F(FeedbackApiTest, ReadLogSourceRateLimited) {
96 const base::TimeDelta timeout(base::TimeDelta::FromMilliseconds(2000));
tbarzic 2017/05/22 23:02:55 we should not hold up tests for 2 seconds :/
Simon Que 2017/05/23 20:01:19 Agreed, but the alarm API used by the test app doe
97 FeedbackPrivateReadLogSourceFunction::SetRateLimitingTimeoutForTesting(
98 &timeout);
99 SingleLogSourceFactory::CreateCallback callback =
100 base::Bind(&TestSingleLogSource::Create);
tbarzic 2017/05/22 23:02:55 can you do this in FeedbackApiTest::SetUp?
Simon Que 2017/05/23 20:01:19 Done. Not sure if I'm doing it right though.
101 SingleLogSourceFactory::InitForTesting(&callback);
102
103 EXPECT_TRUE(RunExtensionTest("feedback_private/read_log_source_rate_limited"))
104 << message_;
105
106 SingleLogSourceFactory::InitForTesting(nullptr);
107 FeedbackPrivateReadLogSourceFunction::SetRateLimitingTimeoutForTesting(
108 nullptr);
109 }
110 #endif // defined(OS_CHROMEOS)
111
22 } // namespace extensions 112 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698