39307edf9f150b62aa10d525a256db20a7139323
[platform/framework/web/crosswalk.git] / src / xwalk / application / test / application_event_test.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 "base/file_util.h"
6 #include "base/path_service.h"
7 #include "content/public/test/browser_test_utils.h"
8 #include "content/public/test/test_utils.h"
9 #include "net/base/net_util.h"
10 #include "url/gurl.h"
11 #include "xwalk/application/test/application_browsertest.h"
12 #include "xwalk/application/test/application_testapi.h"
13 #include "xwalk/runtime/browser/runtime.h"
14 #include "xwalk/runtime/browser/runtime_context.h"
15 #include "xwalk/runtime/common/xwalk_paths.h"
16 #include "xwalk/runtime/common/xwalk_switches.h"
17
18 using xwalk::application::Application;
19
20 class ApplicationEventTest : public ApplicationBrowserTest {
21  public:
22   virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
23     ApplicationBrowserTest::SetUpCommandLine(command_line);
24     GURL url = net::FilePathToFileURL(test_data_dir_.Append(
25           FILE_PATH_LITERAL("event")));
26     command_line->AppendArg(url.spec());
27   }
28 };
29
30 class ApplicationOnInstalledEventTest : public ApplicationBrowserTest {
31  public:
32   ApplicationOnInstalledEventTest() {
33     PathService::Get(xwalk::DIR_TEST_DATA, &app_data_path_);
34     app_data_path_ = app_data_path_
35         .Append(FILE_PATH_LITERAL("appdata_storage"));
36   }
37
38   virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
39     DeleteApplicationDataPath();
40
41     ApplicationBrowserTest::SetUpCommandLine(command_line);
42     command_line->AppendSwitchPath(switches::kXWalkDataPath, app_data_path_);
43     command_line->AppendSwitch(switches::kInstall);
44
45     GURL url = net::FilePathToFileURL(test_data_dir_.Append(
46           FILE_PATH_LITERAL("event")));
47     command_line->AppendArg(url.spec());
48   }
49
50   bool DeleteApplicationDataPath() {
51     base::FilePath resources(app_data_path_);
52     if (base::DirectoryExists(resources) &&
53         !base::DeleteFile(resources, true))
54       return false;
55     return true;
56   }
57
58  private:
59   base::FilePath app_data_path_;
60 };
61
62 IN_PROC_BROWSER_TEST_F(ApplicationEventTest, OnLaunchAndSuspendEventTest) {
63   // Wait for 'OnLaunch' event notification.
64   test_runner_->WaitForTestNotification();
65   EXPECT_EQ(ApiTestRunner::PASS, test_runner_->GetTestsResult());
66
67   EXPECT_EQ(application_sevice()->active_applications().size(), 1);
68   Application* app =
69       application_sevice()->active_applications()[0];
70   test_runner_->PostResultToNotificationCallback();
71   app->Terminate();
72   // Wait for 'OnSuspend' event notification.
73   test_runner_->WaitForTestNotification();
74   EXPECT_EQ(test_runner_->GetTestsResult(), ApiTestRunner::PASS);
75 }
76
77 IN_PROC_BROWSER_TEST_F(ApplicationOnInstalledEventTest, OnInstalledEventTest) {
78   test_runner_->WaitForTestNotification();
79   EXPECT_EQ(ApiTestRunner::PASS, test_runner_->GetTestsResult());
80
81   DeleteApplicationDataPath();
82 }