This source file includes following definitions.
- SetUpCommandLine
- ExecuteCmdAndCheckReply
- AddNewFileInGallery
- GetBackgroundHostForTestExtension
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
#include "base/file_util.h"
#include "base/files/file_path.h"
#include "base/path_service.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "chrome/browser/extensions/extension_apitest.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/extension_test_message_listener.h"
#include "chrome/browser/media_galleries/media_galleries_test_util.h"
#include "chrome/common/chrome_paths.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_view_host.h"
#include "extensions/browser/extension_system.h"
#include "extensions/common/extension.h"
#include "extensions/common/switches.h"
namespace {
const char kTestExtensionId[] = "gceegfkgibmgpfopknlcgleimclbknie";
const char kTestExtensionPath[] = "media_galleries_private/gallerywatch";
#if !defined(OS_CHROMEOS)
const char kGetAllWatchedGalleryIdsCmd[] = "getAllWatchedGalleryIds()";
const char kGetMediaFileSystemsCmd[] = "getMediaFileSystems()";
const char kSetupWatchOnValidGalleriesCmd[] = "setupWatchOnValidGalleries()";
#if defined(OS_WIN)
const char kAddGalleryChangedListenerCmd[] = "addGalleryChangedListener()";
const char kRemoveAllGalleryWatchCmd[] = "removeAllGalleryWatch()";
const char kRemoveGalleryChangedListenerCmd[] =
"removeGalleryChangedListener()";
const char kRemoveGalleryWatchCmd[] = "removeGalleryWatch()";
const char kSetupWatchOnInvalidGalleryCmd[] = "setupWatchOnInvalidGallery()";
#endif
const char kAddGalleryWatchOK[] = "add_gallery_watch_ok";
const char kGetAllGalleryWatchOK[] = "get_all_gallery_watch_ok";
const char kGetMediaFileSystemsCallbackOK[] =
"get_media_file_systems_callback_ok";
const char kGetMediaFileSystemsOK[] = "get_media_file_systems_ok";
#if defined(OS_WIN)
const char kAddGalleryChangedListenerOK[] = "add_gallery_changed_listener_ok";
const char kRemoveAllGalleryWatchOK[] = "remove_all_gallery_watch_ok";
const char kRemoveGalleryChangedListenerOK[] =
"remove_gallery_changed_listener_ok";
const char kRemoveGalleryWatchOK[] = "remove_gallery_watch_ok";
#endif
const char kGetAllGalleryWatchResultA[] = "gallery_watchers_does_not_exists";
const char kAddGalleryWatchRequestFailed[] = "add_watch_request_failed";
#if defined(OS_WIN)
const char kAddGalleryWatchRequestSucceeded[] = "add_watch_request_succeeded";
const char kGalleryChangedEventReceived[] = "gallery_changed_event_received";
const char kGetAllGalleryWatchResultB[] =
"watchers_for_galleries_{1, 2, 3}_found";
#endif
#endif
}
class MediaGalleriesPrivateGalleryWatchApiTest : public ExtensionApiTest {
public:
MediaGalleriesPrivateGalleryWatchApiTest() {}
virtual ~MediaGalleriesPrivateGalleryWatchApiTest() {}
protected:
virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
ExtensionApiTest::SetUpCommandLine(command_line);
command_line->AppendSwitchASCII(
extensions::switches::kWhitelistedExtensionID,
kTestExtensionId);
}
void ExecuteCmdAndCheckReply(content::RenderViewHost* host,
const std::string& js_command,
const std::string& ok_message) {
ExtensionTestMessageListener listener(ok_message, false);
host->GetMainFrame()->ExecuteJavaScript(base::ASCIIToUTF16(js_command));
EXPECT_TRUE(listener.WaitUntilSatisfied());
}
bool AddNewFileInGallery(int gallery_directory_key) {
if ((gallery_directory_key != chrome::DIR_USER_MUSIC) &&
(gallery_directory_key != chrome::DIR_USER_PICTURES) &&
(gallery_directory_key != chrome::DIR_USER_VIDEOS))
return false;
base::FilePath gallery_dir;
if (!PathService::Get(gallery_directory_key, &gallery_dir))
return false;
base::FilePath gallery_file =
gallery_dir.Append(FILE_PATH_LITERAL("test1.txt"));
std::string content("new content");
int write_size = base::WriteFile(gallery_file, content.c_str(),
content.length());
return (write_size == static_cast<int>(content.length()));
}
content::RenderViewHost* GetBackgroundHostForTestExtension() {
const extensions::Extension* extension =
LoadExtension(test_data_dir_.AppendASCII(kTestExtensionPath));
if (!extension)
return NULL;
return extensions::ExtensionSystem::Get(browser()->profile())->
process_manager()->GetBackgroundHostForExtension(extension->id())->
render_view_host();
}
private:
DISALLOW_COPY_AND_ASSIGN(MediaGalleriesPrivateGalleryWatchApiTest);
};
#if defined(OS_WIN)
IN_PROC_BROWSER_TEST_F(MediaGalleriesPrivateGalleryWatchApiTest,
BasicGalleryWatch) {
EnsureMediaDirectoriesExists media_directories;
content::RenderViewHost* host = GetBackgroundHostForTestExtension();
ASSERT_TRUE(host);
ExtensionTestMessageListener get_media_systems_finished(
kGetMediaFileSystemsCallbackOK, false );
ExecuteCmdAndCheckReply(host, kGetMediaFileSystemsCmd,
kGetMediaFileSystemsOK);
EXPECT_TRUE(get_media_systems_finished.WaitUntilSatisfied());
ExtensionTestMessageListener add_gallery_watch_finished(
kAddGalleryWatchRequestSucceeded, false );
ExecuteCmdAndCheckReply(host, kSetupWatchOnValidGalleriesCmd,
kAddGalleryWatchOK);
EXPECT_TRUE(add_gallery_watch_finished.WaitUntilSatisfied());
ExecuteCmdAndCheckReply(host, kAddGalleryChangedListenerCmd,
kAddGalleryChangedListenerOK);
ExtensionTestMessageListener music_gallery_change_event_received(
kGalleryChangedEventReceived, false );
ASSERT_TRUE(AddNewFileInGallery(chrome::DIR_USER_MUSIC));
EXPECT_TRUE(music_gallery_change_event_received.WaitUntilSatisfied());
ExtensionTestMessageListener pictures_gallery_change_event_received(
kGalleryChangedEventReceived, false );
ASSERT_TRUE(AddNewFileInGallery(chrome::DIR_USER_PICTURES));
EXPECT_TRUE(pictures_gallery_change_event_received.WaitUntilSatisfied());
ExtensionTestMessageListener videos_gallery_change_event_received(
kGalleryChangedEventReceived, false );
ASSERT_TRUE(AddNewFileInGallery(chrome::DIR_USER_VIDEOS));
EXPECT_TRUE(videos_gallery_change_event_received.WaitUntilSatisfied());
ExecuteCmdAndCheckReply(host, kRemoveGalleryChangedListenerCmd,
kRemoveGalleryChangedListenerOK);
ExecuteCmdAndCheckReply(host, kRemoveGalleryWatchCmd, kRemoveGalleryWatchOK);
}
IN_PROC_BROWSER_TEST_F(MediaGalleriesPrivateGalleryWatchApiTest,
RemoveListenerAndModifyGallery) {
EnsureMediaDirectoriesExists media_directories;
content::RenderViewHost* host = GetBackgroundHostForTestExtension();
ASSERT_TRUE(host);
ExtensionTestMessageListener get_media_systems_finished(
kGetMediaFileSystemsCallbackOK, false );
ExecuteCmdAndCheckReply(host, kGetMediaFileSystemsCmd,
kGetMediaFileSystemsOK);
EXPECT_TRUE(get_media_systems_finished.WaitUntilSatisfied());
ExtensionTestMessageListener add_gallery_watch_finished(
kAddGalleryWatchRequestSucceeded, false );
ExecuteCmdAndCheckReply(host, kSetupWatchOnValidGalleriesCmd,
kAddGalleryWatchOK);
EXPECT_TRUE(add_gallery_watch_finished.WaitUntilSatisfied());
ExecuteCmdAndCheckReply(host, kAddGalleryChangedListenerCmd,
kAddGalleryChangedListenerOK);
ExtensionTestMessageListener music_gallery_change_event_received(
kGalleryChangedEventReceived, false );
ASSERT_TRUE(AddNewFileInGallery(chrome::DIR_USER_MUSIC));
EXPECT_TRUE(music_gallery_change_event_received.WaitUntilSatisfied());
ExecuteCmdAndCheckReply(host, kRemoveGalleryChangedListenerCmd,
kRemoveGalleryChangedListenerOK);
ASSERT_TRUE(AddNewFileInGallery(chrome::DIR_USER_MUSIC));
ExecuteCmdAndCheckReply(host, kRemoveGalleryWatchCmd, kRemoveGalleryWatchOK);
}
IN_PROC_BROWSER_TEST_F(MediaGalleriesPrivateGalleryWatchApiTest,
SetupGalleryWatchWithoutListeners) {
EnsureMediaDirectoriesExists media_directories;
content::RenderViewHost* host = GetBackgroundHostForTestExtension();
ASSERT_TRUE(host);
ExtensionTestMessageListener get_media_systems_finished(
kGetMediaFileSystemsCallbackOK, false );
ExecuteCmdAndCheckReply(host, kGetMediaFileSystemsCmd,
kGetMediaFileSystemsOK);
EXPECT_TRUE(get_media_systems_finished.WaitUntilSatisfied());
ExecuteCmdAndCheckReply(host, kSetupWatchOnValidGalleriesCmd,
kAddGalleryWatchOK);
ExtensionTestMessageListener music_gallery_change_event_received(
kGalleryChangedEventReceived, false );
ASSERT_TRUE(AddNewFileInGallery(chrome::DIR_USER_MUSIC));
ExecuteCmdAndCheckReply(host, kRemoveGalleryWatchCmd, kRemoveGalleryWatchOK);
}
IN_PROC_BROWSER_TEST_F(MediaGalleriesPrivateGalleryWatchApiTest,
SetupGalleryChangedListenerWithoutWatchers) {
EnsureMediaDirectoriesExists media_directories;
content::RenderViewHost* host = GetBackgroundHostForTestExtension();
ASSERT_TRUE(host);
ExtensionTestMessageListener get_media_systems_finished(
kGetMediaFileSystemsCallbackOK, false );
ExecuteCmdAndCheckReply(host, kGetMediaFileSystemsCmd,
kGetMediaFileSystemsOK);
EXPECT_TRUE(get_media_systems_finished.WaitUntilSatisfied());
ExecuteCmdAndCheckReply(host, kAddGalleryChangedListenerCmd,
kAddGalleryChangedListenerOK);
ExtensionTestMessageListener music_gallery_change_event_received(
kGalleryChangedEventReceived, false );
ASSERT_TRUE(AddNewFileInGallery(chrome::DIR_USER_MUSIC));
ExecuteCmdAndCheckReply(host, kRemoveGalleryChangedListenerCmd,
kRemoveGalleryChangedListenerOK);
}
IN_PROC_BROWSER_TEST_F(MediaGalleriesPrivateGalleryWatchApiTest,
SetupWatchOnInvalidGallery) {
content::RenderViewHost* host = GetBackgroundHostForTestExtension();
ASSERT_TRUE(host);
ExtensionTestMessageListener invalid_gallery_watch_request_finished(
kAddGalleryWatchRequestFailed, false );
ExecuteCmdAndCheckReply(host, kSetupWatchOnInvalidGalleryCmd,
kAddGalleryWatchOK);
EXPECT_TRUE(invalid_gallery_watch_request_finished.WaitUntilSatisfied());
}
IN_PROC_BROWSER_TEST_F(MediaGalleriesPrivateGalleryWatchApiTest,
GetAllGalleryWatch) {
EnsureMediaDirectoriesExists media_directories;
content::RenderViewHost* host = GetBackgroundHostForTestExtension();
ASSERT_TRUE(host);
ExtensionTestMessageListener get_media_systems_finished(
kGetMediaFileSystemsCallbackOK, false );
ExecuteCmdAndCheckReply(host, kGetMediaFileSystemsCmd,
kGetMediaFileSystemsOK);
EXPECT_TRUE(get_media_systems_finished.WaitUntilSatisfied());
ExtensionTestMessageListener initial_get_all_check_finished(
kGetAllGalleryWatchResultA, false );
ExecuteCmdAndCheckReply(host, kGetAllWatchedGalleryIdsCmd,
kGetAllGalleryWatchOK);
EXPECT_TRUE(initial_get_all_check_finished.WaitUntilSatisfied());
ExtensionTestMessageListener add_gallery_watch_finished(
kAddGalleryWatchRequestSucceeded, false );
ExecuteCmdAndCheckReply(host, kSetupWatchOnValidGalleriesCmd,
kAddGalleryWatchOK);
EXPECT_TRUE(add_gallery_watch_finished.WaitUntilSatisfied());
ExtensionTestMessageListener get_all_watched_galleries_finished(
kGetAllGalleryWatchResultB, false );
ExecuteCmdAndCheckReply(host, kGetAllWatchedGalleryIdsCmd,
kGetAllGalleryWatchOK);
EXPECT_TRUE(get_all_watched_galleries_finished.WaitUntilSatisfied());
ExecuteCmdAndCheckReply(host, kRemoveGalleryWatchCmd, kRemoveGalleryWatchOK);
ExtensionTestMessageListener final_get_all_check_finished(
kGetAllGalleryWatchResultA, false );
ExecuteCmdAndCheckReply(host, kGetAllWatchedGalleryIdsCmd,
kGetAllGalleryWatchOK);
EXPECT_TRUE(final_get_all_check_finished.WaitUntilSatisfied());
}
IN_PROC_BROWSER_TEST_F(MediaGalleriesPrivateGalleryWatchApiTest,
RemoveAllGalleryWatch) {
EnsureMediaDirectoriesExists media_directories;
content::RenderViewHost* host = GetBackgroundHostForTestExtension();
ASSERT_TRUE(host);
ExtensionTestMessageListener get_media_systems_finished(
kGetMediaFileSystemsCallbackOK, false );
ExecuteCmdAndCheckReply(host, kGetMediaFileSystemsCmd,
kGetMediaFileSystemsOK);
EXPECT_TRUE(get_media_systems_finished.WaitUntilSatisfied());
ExtensionTestMessageListener add_gallery_watch_finished(
kAddGalleryWatchRequestSucceeded, false );
ExecuteCmdAndCheckReply(host, kSetupWatchOnValidGalleriesCmd,
kAddGalleryWatchOK);
EXPECT_TRUE(add_gallery_watch_finished.WaitUntilSatisfied());
ExtensionTestMessageListener get_all_watched_galleries_finished(
kGetAllGalleryWatchResultB, false );
ExecuteCmdAndCheckReply(host, kGetAllWatchedGalleryIdsCmd,
kGetAllGalleryWatchOK);
EXPECT_TRUE(get_all_watched_galleries_finished.WaitUntilSatisfied());
ExecuteCmdAndCheckReply(host, kRemoveAllGalleryWatchCmd,
kRemoveAllGalleryWatchOK);
ExtensionTestMessageListener final_get_all_check_finished(
kGetAllGalleryWatchResultA, false );
ExecuteCmdAndCheckReply(host, kGetAllWatchedGalleryIdsCmd,
kGetAllGalleryWatchOK);
EXPECT_TRUE(final_get_all_check_finished.WaitUntilSatisfied());
}
#endif
#if !defined(OS_WIN) && !defined(OS_CHROMEOS)
IN_PROC_BROWSER_TEST_F(MediaGalleriesPrivateGalleryWatchApiTest,
SetupGalleryWatch) {
EnsureMediaDirectoriesExists media_directories;
content::RenderViewHost* host = GetBackgroundHostForTestExtension();
ASSERT_TRUE(host);
ExtensionTestMessageListener get_media_systems_finished(
kGetMediaFileSystemsCallbackOK, false );
ExecuteCmdAndCheckReply(host, kGetMediaFileSystemsCmd,
kGetMediaFileSystemsOK);
EXPECT_TRUE(get_media_systems_finished.WaitUntilSatisfied());
ExtensionTestMessageListener gallery_watch_request_finished(
kAddGalleryWatchRequestFailed, false );
ExecuteCmdAndCheckReply(host, kSetupWatchOnValidGalleriesCmd,
kAddGalleryWatchOK);
EXPECT_TRUE(gallery_watch_request_finished.WaitUntilSatisfied());
}
IN_PROC_BROWSER_TEST_F(MediaGalleriesPrivateGalleryWatchApiTest,
GetAllGalleryWatch) {
EnsureMediaDirectoriesExists media_directories;
content::RenderViewHost* host = GetBackgroundHostForTestExtension();
ASSERT_TRUE(host);
ExtensionTestMessageListener get_media_systems_finished(
kGetMediaFileSystemsCallbackOK, false );
ExecuteCmdAndCheckReply(host, kGetMediaFileSystemsCmd,
kGetMediaFileSystemsOK);
EXPECT_TRUE(get_media_systems_finished.WaitUntilSatisfied());
ExecuteCmdAndCheckReply(host, kSetupWatchOnValidGalleriesCmd,
kAddGalleryWatchOK);
ExtensionTestMessageListener get_all_gallery_watch_finished(
kGetAllGalleryWatchResultA, false );
ExecuteCmdAndCheckReply(host, kGetAllWatchedGalleryIdsCmd,
kGetAllGalleryWatchOK);
EXPECT_TRUE(get_all_gallery_watch_finished.WaitUntilSatisfied());
}
#endif