Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / ppapi / tests / test_flash_drm.cc
1 // Copyright (c) 2012 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 "ppapi/tests/test_flash_drm.h"
6
7 #if defined(PPAPI_OS_WIN)
8 #include <Windows.h>
9 #endif
10
11 #include "ppapi/c/pp_macros.h"
12 #include "ppapi/c/private/ppb_file_ref_private.h"
13 #include "ppapi/c/private/ppb_flash_drm.h"
14 #include "ppapi/cpp/instance.h"
15 #include "ppapi/cpp/module.h"
16 #include "ppapi/cpp/private/flash_device_id.h"
17 #include "ppapi/cpp/private/flash_drm.h"
18 #include "ppapi/cpp/var.h"
19 #include "ppapi/tests/testing_instance.h"
20
21 REGISTER_TEST_CASE(FlashDRM);
22
23 using pp::flash::DeviceID;
24 using pp::flash::DRM;
25 using pp::FileRef;
26 using pp::PassRef;
27 using pp::Var;
28
29 namespace {
30
31 const char kExepectedVoucherFilename[] = "plugin.vch";
32
33 // Check that the Hmonitor value is what it is expected to be for each platform.
34 bool CheckExpectedHmonitorValue(bool success, int64_t hmonitor) {
35 #if defined(PPAPI_OS_MACOSX)
36   // TODO(raymes): Verify the expected |hmonitor| value on Mac.
37   return success;
38 #elif defined(PPAPI_OS_WIN)
39   MONITORINFO info = { sizeof(info) };
40   return success &&
41       ::GetMonitorInfo(reinterpret_cast<HMONITOR>(hmonitor), &info) == TRUE;
42 #else
43   // Not implemented for other platforms, should return false.
44   return !success;
45 #endif
46 }
47
48 }  // namespace
49
50 TestFlashDRM::TestFlashDRM(TestingInstance* instance)
51     : TestCase(instance),
52       callback_factory_(this) {
53 }
54
55 void TestFlashDRM::RunTests(const std::string& filter) {
56   RUN_TEST(GetDeviceID, filter);
57   RUN_TEST(GetHmonitor, filter);
58   RUN_TEST(GetVoucherFile, filter);
59 }
60
61 std::string TestFlashDRM::TestGetDeviceID() {
62   // Test the old C++ wrapper.
63   // TODO(raymes): Remove this once Flash switches APIs.
64   {
65     DeviceID device_id(instance_);
66     TestCompletionCallbackWithOutput<Var> output_callback(
67         instance_->pp_instance());
68     int32_t rv = device_id.GetDeviceID(output_callback.GetCallback());
69     output_callback.WaitForResult(rv);
70     ASSERT_TRUE(output_callback.result() == PP_OK);
71     Var result = output_callback.output();
72     ASSERT_TRUE(result.is_string());
73     std::string id = result.AsString();
74     ASSERT_FALSE(id.empty());
75   }
76
77   {
78     DRM drm(instance_);
79     TestCompletionCallbackWithOutput<Var> output_callback(
80         instance_->pp_instance());
81     int32_t rv = drm.GetDeviceID(output_callback.GetCallback());
82     output_callback.WaitForResult(rv);
83     ASSERT_TRUE(output_callback.result() == PP_OK);
84     Var result = output_callback.output();
85     ASSERT_TRUE(result.is_string());
86     std::string id = result.AsString();
87     ASSERT_FALSE(id.empty());
88   }
89
90   PASS();
91 }
92
93 std::string TestFlashDRM::TestGetHmonitor() {
94   DRM drm(instance_);
95   int64_t hmonitor;
96   while (true) {
97     bool success = drm.GetHmonitor(&hmonitor);
98     if (CheckExpectedHmonitorValue(success, hmonitor))
99       break;
100     else
101       PlatformSleep(30);
102   }
103
104   PASS();
105 }
106
107 std::string TestFlashDRM::TestGetVoucherFile() {
108   DRM drm(instance_);
109   TestCompletionCallbackWithOutput<FileRef> output_callback(
110       instance_->pp_instance());
111   int32_t rv = drm.GetVoucherFile(output_callback.GetCallback());
112   output_callback.WaitForResult(rv);
113   ASSERT_EQ(PP_OK, output_callback.result());
114   FileRef result = output_callback.output();
115   ASSERT_EQ(PP_FILESYSTEMTYPE_EXTERNAL, result.GetFileSystemType());
116
117   // The PPB_FileRefPrivate interface doesn't have a C++ wrapper yet, so just
118   // use the C interface.
119   const PPB_FileRefPrivate* file_ref_private =
120       static_cast<const PPB_FileRefPrivate*>(
121           pp::Module::Get()->GetBrowserInterface(PPB_FILEREFPRIVATE_INTERFACE));
122   ASSERT_TRUE(file_ref_private);
123   Var path(PassRef(), file_ref_private->GetAbsolutePath(result.pp_resource()));
124   ASSERT_TRUE(path.is_string());
125   std::string path_string = path.AsString();
126   std::string expected_filename = std::string(kExepectedVoucherFilename);
127   ASSERT_EQ(expected_filename,
128             path_string.substr(path_string.size() - expected_filename.size()));
129
130   PASS();
131 }