1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef EXTENSIONS_TEST_RESULT_CATCHER_H_
6 #define EXTENSIONS_TEST_RESULT_CATCHER_H_
7 
8 #include <string>
9 
10 #include "base/callback.h"
11 #include "base/compiler_specific.h"
12 #include "base/containers/circular_deque.h"
13 #include "content/public/browser/notification_observer.h"
14 #include "content/public/browser/notification_registrar.h"
15 
16 namespace content {
17 class BrowserContext;
18 }  // namespace content
19 
20 namespace extensions {
21 
22 // Helper class that observes tests failing or passing. Observation starts
23 // when the class is constructed. Get the next result by calling
24 // GetNextResult() and message() if GetNextResult() return false. If there
25 // are no results, this method will pump the UI message loop until one is
26 // received.
27 class ResultCatcher : public content::NotificationObserver {
28  public:
29   ResultCatcher();
30   ~ResultCatcher() override;
31 
32   // Pumps the UI loop until a notification is received that an API test
33   // succeeded or failed. Returns true if the test succeeded, false otherwise.
34   bool GetNextResult() WARN_UNUSED_RESULT;
35 
RestrictToBrowserContext(content::BrowserContext * context)36   void RestrictToBrowserContext(content::BrowserContext* context) {
37     browser_context_restriction_ = context;
38   }
39 
message()40   const std::string& message() { return message_; }
41 
42  private:
43   // content::NotificationObserver:
44   void Observe(int type,
45                const content::NotificationSource& source,
46                const content::NotificationDetails& details) override;
47 
48   content::NotificationRegistrar registrar_;
49 
50   // A sequential list of pass/fail notifications from the test extension(s).
51   base::circular_deque<bool> results_;
52 
53   // If it failed, what was the error message?
54   base::circular_deque<std::string> messages_;
55   std::string message_;
56 
57   // If non-NULL, we will listen to events from this BrowserContext only.
58   content::BrowserContext* browser_context_restriction_;
59 
60   // Only set if we're in a nested run loop waiting for results from
61   // the extension.
62   base::OnceClosure quit_closure_;
63 };
64 
65 }  // namespace extensions
66 
67 #endif  // EXTENSIONS_TEST_RESULT_CATCHER_H_
68