Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / xwalk / application / test / application_testapi.cc
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 #include "xwalk/application/test/application_testapi.h"
6
7 #include "content/public/browser/browser_thread.h"
8 #include "content/public/test/test_utils.h"
9 #include "grit/xwalk_application_resources.h"
10 #include "ui/base/resource/resource_bundle.h"
11
12 using content::BrowserThread;
13
14 namespace {
15 const char kTestApiName[] = "xwalk.app.test";
16
17 const char kTestNotifyPass[] = "notifyPass";
18 const char kTestNotifyFail[] = "notifyFail";
19 const char kTestNotifyTimeout[] = "notifyTimeout";
20
21 }  // namespace
22
23 ApiTestExtension::ApiTestExtension() {
24   set_name(kTestApiName);
25   set_javascript_api(ResourceBundle::GetSharedInstance().GetRawDataResource(
26       IDR_XWALK_APPLICATION_TEST_API).as_string());
27 }
28
29 xwalk::extensions::XWalkExtensionInstance* ApiTestExtension::CreateInstance() {
30   return new ApiTestExtensionInstance(observer_);
31 }
32
33 void ApiTestExtension::SetObserver(
34     ApiTestExtensionInstance::Observer* observer) {
35   observer_ = observer;
36 }
37
38 ApiTestExtensionInstance::ApiTestExtensionInstance(
39     ApiTestExtensionInstance::Observer* observer)
40   : observer_(observer),
41     handler_(this) {
42   handler_.Register(
43       kTestNotifyPass,
44       base::Bind(&ApiTestExtensionInstance::OnNotifyPass,
45                  base::Unretained(this)));
46   handler_.Register(
47       kTestNotifyFail,
48       base::Bind(&ApiTestExtensionInstance::OnNotifyFail,
49                  base::Unretained(this)));
50   handler_.Register(
51       kTestNotifyTimeout,
52       base::Bind(&ApiTestExtensionInstance::OnNotifyTimeout,
53                  base::Unretained(this)));
54 }
55
56 void ApiTestExtensionInstance::HandleMessage(scoped_ptr<base::Value> msg) {
57   handler_.HandleMessage(msg.Pass());
58 }
59
60 void ApiTestExtensionInstance::OnNotifyPass(
61     scoped_ptr<XWalkExtensionFunctionInfo> info) {
62   CHECK(observer_);
63   observer_->OnTestNotificationReceived(info.Pass(), kTestNotifyPass);
64 }
65
66 void ApiTestExtensionInstance::OnNotifyFail(
67     scoped_ptr<XWalkExtensionFunctionInfo> info) {
68   CHECK(observer_);
69   observer_->OnTestNotificationReceived(info.Pass(), kTestNotifyFail);
70 }
71
72 void ApiTestExtensionInstance::OnNotifyTimeout(
73     scoped_ptr<XWalkExtensionFunctionInfo> info) {
74   CHECK(observer_);
75   observer_->OnTestNotificationReceived(info.Pass(), kTestNotifyTimeout);
76 }
77
78 ApiTestRunner::ApiTestRunner()
79   : result_(NOT_SET) {
80 }
81
82 ApiTestRunner::~ApiTestRunner() {
83 }
84
85 bool ApiTestRunner::WaitForTestNotification() {
86   if (result_ != NOT_SET || message_loop_runner_)
87     return false;
88
89   message_loop_runner_ = new content::MessageLoopRunner;
90   message_loop_runner_->Run();
91   message_loop_runner_ = NULL;
92   return true;
93 }
94
95 void ApiTestRunner::OnTestNotificationReceived(
96     scoped_ptr<XWalkExtensionFunctionInfo> info,
97     const std::string& result_str) {
98   notify_func_info_.reset(info.release());
99   Result result = NOT_SET;
100   if (result_str == kTestNotifyPass)
101     result = PASS;
102   else if (result_str == kTestNotifyFail)
103     result = FAILURE;
104   else if (result_str == kTestNotifyTimeout)
105     result = TIMEOUT;
106   else
107     NOTREACHED();
108
109   CHECK(result != NOT_SET);
110   result_ = result;
111   // It may be notified before wait really happens.
112   if (!message_loop_runner_)
113     return;
114
115   message_loop_runner_->Quit();
116 }
117
118 void ApiTestRunner::PostResultToNotificationCallback() {
119   ResetResult();
120   notify_func_info_->PostResult(
121       scoped_ptr<base::ListValue>(new base::ListValue));
122 }
123
124 ApiTestRunner::Result ApiTestRunner::GetTestsResult() const {
125   return result_;
126 }
127
128 void ApiTestRunner::ResetResult() {
129   CHECK(!message_loop_runner_);
130   result_ = NOT_SET;
131 }