Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / xwalk / application / test / application_testapi.h
1 // Copyright (c) 2013 Intel Corporation. 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 XWALK_APPLICATION_TEST_APPLICATION_TESTAPI_H_
6 #define XWALK_APPLICATION_TEST_APPLICATION_TESTAPI_H_
7
8 #include <string>
9
10 #include "xwalk/extensions/browser/xwalk_extension_function_handler.h"
11 #include "xwalk/extensions/common/xwalk_extension.h"
12
13 using xwalk::extensions::XWalkExtension;
14 using xwalk::extensions::XWalkExtensionFunctionHandler;
15 using xwalk::extensions::XWalkExtensionFunctionInfo;
16 using xwalk::extensions::XWalkExtensionInstance;
17
18 namespace content {
19 class MessageLoopRunner;
20 }
21
22 class ApiTestExtensionInstance : public XWalkExtensionInstance {
23  public:
24   // Observer will be created in UI thread.
25   class Observer {
26    public:
27     virtual void OnTestNotificationReceived(
28         scoped_ptr<XWalkExtensionFunctionInfo> info,
29         const std::string& result_str) = 0;
30    protected:
31     virtual ~Observer() {}
32   };
33
34   explicit ApiTestExtensionInstance(Observer* observer = NULL);
35
36   virtual void HandleMessage(scoped_ptr<base::Value> msg) OVERRIDE;
37
38  private:
39   void OnNotifyPass(scoped_ptr<XWalkExtensionFunctionInfo> info);
40   void OnNotifyFail(scoped_ptr<XWalkExtensionFunctionInfo> info);
41   void OnNotifyTimeout(scoped_ptr<XWalkExtensionFunctionInfo> info);
42
43   Observer* observer_;
44   XWalkExtensionFunctionHandler handler_;
45 };
46
47 class ApiTestExtension : public XWalkExtension {
48  public:
49   ApiTestExtension();
50
51   virtual XWalkExtensionInstance* CreateInstance() OVERRIDE;
52
53   void SetObserver(ApiTestExtensionInstance::Observer* observer);
54
55  private:
56   ApiTestExtensionInstance::Observer* observer_;
57 };
58
59 class ApiTestRunner : public ApiTestExtensionInstance::Observer {
60  public:
61   enum Result {
62     NOT_SET = 0,
63     PASS,
64     FAILURE,
65     TIMEOUT,
66   };
67
68   ApiTestRunner();
69   virtual ~ApiTestRunner();
70
71   // Block wait until the test API is called. If the test API is already called,
72   // this will return immediately. Returns true if the waiting happened, returns
73   // false if the waiting does not happen.
74   bool WaitForTestNotification();
75
76   // Implement ApiTestExtensionInstance::Observer.
77   virtual void OnTestNotificationReceived(
78       scoped_ptr<XWalkExtensionFunctionInfo> info,
79       const std::string& result_str) OVERRIDE;
80
81   void PostResultToNotificationCallback();
82
83   Result GetTestsResult() const;
84
85  private:
86   // Reset current test result, then it can wait again.
87   void ResetResult();
88
89   scoped_ptr<XWalkExtensionFunctionInfo> notify_func_info_;
90   Result result_;
91   scoped_refptr<content::MessageLoopRunner> message_loop_runner_;
92 };
93
94 #endif  // XWALK_APPLICATION_TEST_APPLICATION_TESTAPI_H_