Upstream version 7.35.136.0
[platform/framework/web/crosswalk.git] / src / apps / app_restore_service_browsertest.cc
1 // Copyright (c) 2013 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 #include "apps/app_restore_service.h"
6 #include "apps/app_restore_service_factory.h"
7 #include "apps/saved_files_service.h"
8 #include "chrome/browser/apps/app_browsertest_util.h"
9 #include "chrome/browser/chrome_notification_types.h"
10 #include "chrome/browser/extensions/api/file_system/file_system_api.h"
11 #include "chrome/browser/extensions/extension_test_message_listener.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "content/public/browser/notification_service.h"
14 #include "content/public/test/test_utils.h"
15 #include "extensions/browser/extension_prefs.h"
16 #include "extensions/common/extension.h"
17
18 using extensions::Extension;
19 using extensions::ExtensionPrefs;
20 using extensions::ExtensionSystem;
21 using extensions::FileSystemChooseEntryFunction;
22
23 // TODO(benwells): Move PlatformAppBrowserTest to apps namespace in apps
24 // component.
25 using extensions::PlatformAppBrowserTest;
26
27 namespace apps {
28
29 // Tests that a running app is recorded in the preferences as such.
30 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, RunningAppsAreRecorded) {
31   content::WindowedNotificationObserver extension_suspended(
32       chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED,
33       content::NotificationService::AllSources());
34
35   const Extension* extension = LoadExtension(
36       test_data_dir_.AppendASCII("platform_apps/restart_test"));
37   ASSERT_TRUE(extension);
38   ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(browser()->profile());
39
40   // App is running.
41   ASSERT_TRUE(extension_prefs->IsExtensionRunning(extension->id()));
42
43   // Wait for the extension to get suspended.
44   extension_suspended.Wait();
45
46   // App isn't running because it got suspended.
47   ASSERT_FALSE(extension_prefs->IsExtensionRunning(extension->id()));
48
49   // Pretend that the app is supposed to be running.
50   extension_prefs->SetExtensionRunning(extension->id(), true);
51
52   ExtensionTestMessageListener restart_listener("onRestarted", false);
53   apps::AppRestoreServiceFactory::GetForProfile(browser()->profile())->
54       HandleStartup(true);
55   restart_listener.WaitUntilSatisfied();
56 }
57
58 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, FileAccessIsSavedToPrefs) {
59   content::WindowedNotificationObserver extension_suspended(
60       chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED,
61       content::NotificationService::AllSources());
62
63   base::ScopedTempDir temp_directory;
64   ASSERT_TRUE(temp_directory.CreateUniqueTempDir());
65   base::FilePath temp_file;
66   ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_directory.path(),
67                                              &temp_file));
68
69   FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
70       &temp_file);
71   FileSystemChooseEntryFunction::RegisterTempExternalFileSystemForTest(
72       "temp", temp_directory.path());
73
74   ExtensionTestMessageListener file_written_listener("fileWritten", false);
75   ExtensionTestMessageListener access_ok_listener(
76       "restartedFileAccessOK", false);
77
78   const Extension* extension =
79       LoadAndLaunchPlatformApp("file_access_saved_to_prefs_test");
80   ASSERT_TRUE(extension);
81   file_written_listener.WaitUntilSatisfied();
82
83   SavedFilesService* saved_files_service = SavedFilesService::Get(profile());
84
85   std::vector<SavedFileEntry> file_entries =
86       saved_files_service->GetAllFileEntries(extension->id());
87   // One for the read-only file entry and one for the writable file entry.
88   ASSERT_EQ(2u, file_entries.size());
89
90   extension_suspended.Wait();
91   file_entries = saved_files_service->GetAllFileEntries(extension->id());
92   // File entries should be cleared when the extension is suspended.
93   ASSERT_TRUE(file_entries.empty());
94 }
95
96 // Flaky: crbug.com/269613
97 #if defined(OS_LINUX) || defined(OS_WIN)
98 #define MAYBE_FileAccessIsRestored DISABLED_FileAccessIsRestored
99 #else
100 #define MAYBE_FileAccessIsRestored FileAccessIsRestored
101 #endif
102
103 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, MAYBE_FileAccessIsRestored) {
104   content::WindowedNotificationObserver extension_suspended(
105       chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED,
106       content::NotificationService::AllSources());
107
108   base::ScopedTempDir temp_directory;
109   ASSERT_TRUE(temp_directory.CreateUniqueTempDir());
110   base::FilePath temp_file;
111   ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_directory.path(),
112                                              &temp_file));
113
114   FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
115       &temp_file);
116   FileSystemChooseEntryFunction::RegisterTempExternalFileSystemForTest(
117       "temp", temp_directory.path());
118
119   ExtensionTestMessageListener file_written_listener("fileWritten", false);
120   ExtensionTestMessageListener access_ok_listener(
121       "restartedFileAccessOK", false);
122
123   const Extension* extension =
124       LoadAndLaunchPlatformApp("file_access_restored_test");
125   ASSERT_TRUE(extension);
126   file_written_listener.WaitUntilSatisfied();
127
128   ExtensionPrefs* extension_prefs =
129       ExtensionPrefs::Get(browser()->profile());
130   SavedFilesService* saved_files_service = SavedFilesService::Get(profile());
131   std::vector<SavedFileEntry> file_entries =
132       saved_files_service->GetAllFileEntries(extension->id());
133   extension_suspended.Wait();
134
135   // Simulate a restart by populating the preferences as if the browser didn't
136   // get time to clean itself up.
137   extension_prefs->SetExtensionRunning(extension->id(), true);
138   for (std::vector<SavedFileEntry>::const_iterator it = file_entries.begin();
139        it != file_entries.end(); ++it) {
140     saved_files_service->RegisterFileEntry(
141         extension->id(), it->id, it->path, it->is_directory);
142   }
143
144   apps::AppRestoreServiceFactory::GetForProfile(browser()->profile())->
145       HandleStartup(true);
146
147   access_ok_listener.WaitUntilSatisfied();
148 }
149
150 }  // namespace apps