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.
5 #include "content/public/test/test_utils.h"
6 #include "net/base/net_util.h"
7 #include "xwalk/application/browser/application.h"
8 #include "xwalk/application/browser/application_event_manager.h"
9 #include "xwalk/application/browser/application_system.h"
10 #include "xwalk/application/browser/application_service.h"
11 #include "xwalk/application/browser/event_observer.h"
12 #include "xwalk/application/common/event_names.h"
13 #include "xwalk/application/test/application_browsertest.h"
14 #include "xwalk/application/test/application_testapi.h"
15 #include "xwalk/runtime/browser/xwalk_runner.h"
17 using xwalk::application::Application;
18 using xwalk::application::ApplicationEventManager;
19 using xwalk::application::Event;
20 using xwalk::application::kOnJavaScriptEventAck;
24 const char kMockEvent[] = "onMockEvent";
26 scoped_refptr<Event> CreateMockEvent() {
27 scoped_ptr<base::ListValue> args(new base::ListValue());
28 args->AppendInteger(1234);
29 args->AppendBoolean(false);
30 return Event::CreateEvent(kMockEvent, args.Pass());
35 class ApplicationEventApiTest : public ApplicationBrowserTest {
37 class MockFinishObserver : public xwalk::application::EventObserver {
40 ApplicationEventManager* event_manager,
41 const std::string& app_id)
42 : EventObserver(event_manager),
43 has_been_notified_(false) {
44 event_manager_->AttachObserver(app_id, kOnJavaScriptEventAck, this);
47 virtual void Observe(const std::string& app_id,
48 scoped_refptr<Event> event) OVERRIDE {
49 ASSERT_EQ(event->name(), std::string(kOnJavaScriptEventAck));
50 std::string ack_event_name;
51 ASSERT_TRUE(event->args()->GetString(0, &ack_event_name));
52 EXPECT_EQ(ack_event_name, std::string(kMockEvent));
53 has_been_notified_ = true;
56 bool has_been_notified_;
60 IN_PROC_BROWSER_TEST_F(ApplicationEventApiTest, EventApiTest) {
61 xwalk::application::ApplicationSystem* system =
62 xwalk::XWalkRunner::GetInstance()->app_system();
63 Application* app = system->application_service()->Launch(
64 test_data_dir_.Append(FILE_PATH_LITERAL("eventapi")));
67 test_runner_->WaitForTestNotification();
68 EXPECT_EQ(test_runner_->GetTestsResult(), ApiTestRunner::PASS);
69 test_runner_->PostResultToNotificationCallback();
70 scoped_ptr<MockFinishObserver> event_finish_observer(
71 new MockFinishObserver(system->event_manager(), app->id()));
73 system->event_manager()->SendEvent(app->id(), CreateMockEvent());
74 test_runner_->WaitForTestNotification();
75 EXPECT_EQ(test_runner_->GetTestsResult(), ApiTestRunner::PASS);
76 EXPECT_TRUE(event_finish_observer->has_been_notified_);
78 content::RunAllPendingInMessageLoop();