Derek Sollenberger | d7ebf27 | 2009-06-18 11:19:41 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2008, The Android Open Source Project |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * * Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * * Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY |
| 14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR |
| 17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #include "BackgroundPlugin.h" |
| 27 | #include "android_npapi.h" |
| 28 | |
| 29 | #include <stdio.h> |
| 30 | #include <sys/time.h> |
| 31 | #include <time.h> |
| 32 | #include <math.h> |
| 33 | #include <string.h> |
| 34 | |
| 35 | extern NPNetscapeFuncs* browser; |
| 36 | extern ANPBitmapInterfaceV0 gBitmapI; |
Derek Sollenberger | d7ebf27 | 2009-06-18 11:19:41 -0400 | [diff] [blame] | 37 | extern ANPCanvasInterfaceV0 gCanvasI; |
Derek Sollenberger | d049ec1 | 2009-08-07 11:26:44 -0400 | [diff] [blame] | 38 | extern ANPLogInterfaceV0 gLogI; |
Derek Sollenberger | d7ebf27 | 2009-06-18 11:19:41 -0400 | [diff] [blame] | 39 | extern ANPPaintInterfaceV0 gPaintI; |
Derek Sollenberger | d049ec1 | 2009-08-07 11:26:44 -0400 | [diff] [blame] | 40 | extern ANPSurfaceInterfaceV0 gSurfaceI; |
Derek Sollenberger | b4a2391 | 2009-11-09 15:38:58 -0500 | [diff] [blame^] | 41 | extern ANPSystemInterfaceV0 gSystemI; |
Derek Sollenberger | d7ebf27 | 2009-06-18 11:19:41 -0400 | [diff] [blame] | 42 | extern ANPTypefaceInterfaceV0 gTypefaceI; |
| 43 | |
Derek Sollenberger | d7ebf27 | 2009-06-18 11:19:41 -0400 | [diff] [blame] | 44 | #define ARRAY_COUNT(array) (sizeof(array) / sizeof(array[0])) |
| 45 | |
Derek Sollenberger | 0c763b6 | 2009-08-11 10:05:46 -0400 | [diff] [blame] | 46 | static uint32_t getMSecs() { |
| 47 | struct timeval tv; |
| 48 | gettimeofday(&tv, NULL); |
| 49 | return (uint32_t) (tv.tv_sec * 1000 + tv.tv_usec / 1000 ); // microseconds to milliseconds |
| 50 | } |
Derek Sollenberger | d7ebf27 | 2009-06-18 11:19:41 -0400 | [diff] [blame] | 51 | |
| 52 | /////////////////////////////////////////////////////////////////////////////// |
| 53 | |
Derek Sollenberger | 08581f1 | 2009-09-08 18:36:29 -0400 | [diff] [blame] | 54 | BackgroundPlugin::BackgroundPlugin(NPP inst) : SurfaceSubPlugin(inst) { |
Derek Sollenberger | d7ebf27 | 2009-06-18 11:19:41 -0400 | [diff] [blame] | 55 | |
Derek Sollenberger | d049ec1 | 2009-08-07 11:26:44 -0400 | [diff] [blame] | 56 | // initialize the drawing surface |
Derek Sollenberger | 08581f1 | 2009-09-08 18:36:29 -0400 | [diff] [blame] | 57 | m_surface = NULL; |
Derek Sollenberger | d7ebf27 | 2009-06-18 11:19:41 -0400 | [diff] [blame] | 58 | |
Derek Sollenberger | d049ec1 | 2009-08-07 11:26:44 -0400 | [diff] [blame] | 59 | //initialize bitmap transparency variables |
Derek Sollenberger | d7ebf27 | 2009-06-18 11:19:41 -0400 | [diff] [blame] | 60 | mFinishedStageOne = false; |
| 61 | mFinishedStageTwo = false; |
| 62 | mFinishedStageThree = false; |
| 63 | |
| 64 | // test basic plugin functionality |
| 65 | test_logging(); // android logging |
| 66 | test_timers(); // plugin timers |
| 67 | test_bitmaps(); // android bitmaps |
Derek Sollenberger | c356b9f | 2009-07-30 16:57:07 -0400 | [diff] [blame] | 68 | test_domAccess(); |
| 69 | test_javascript(); |
Derek Sollenberger | b4a2391 | 2009-11-09 15:38:58 -0500 | [diff] [blame^] | 70 | test_loadJavaClass(); |
Derek Sollenberger | d7ebf27 | 2009-06-18 11:19:41 -0400 | [diff] [blame] | 71 | } |
| 72 | |
Derek Sollenberger | b8947ee | 2009-10-05 14:40:18 -0400 | [diff] [blame] | 73 | BackgroundPlugin::~BackgroundPlugin() { |
| 74 | surfaceDestroyed(); |
| 75 | } |
Derek Sollenberger | d7ebf27 | 2009-06-18 11:19:41 -0400 | [diff] [blame] | 76 | |
Derek Sollenberger | 21f3991 | 2009-07-09 09:19:39 -0400 | [diff] [blame] | 77 | bool BackgroundPlugin::supportsDrawingModel(ANPDrawingModel model) { |
Derek Sollenberger | d049ec1 | 2009-08-07 11:26:44 -0400 | [diff] [blame] | 78 | return (model == kSurface_ANPDrawingModel); |
Derek Sollenberger | 21f3991 | 2009-07-09 09:19:39 -0400 | [diff] [blame] | 79 | } |
| 80 | |
Derek Sollenberger | 08581f1 | 2009-09-08 18:36:29 -0400 | [diff] [blame] | 81 | bool BackgroundPlugin::isFixedSurface() { |
| 82 | return false; |
| 83 | } |
| 84 | |
Derek Sollenberger | b8947ee | 2009-10-05 14:40:18 -0400 | [diff] [blame] | 85 | void BackgroundPlugin::surfaceCreated(jobject surface) { |
| 86 | m_surface = surface; |
Derek Sollenberger | 08581f1 | 2009-09-08 18:36:29 -0400 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | void BackgroundPlugin::surfaceChanged(int format, int width, int height) { |
| 90 | drawPlugin(width, height); |
| 91 | } |
| 92 | |
| 93 | void BackgroundPlugin::surfaceDestroyed() { |
| 94 | JNIEnv* env = NULL; |
Derek Sollenberger | b8947ee | 2009-10-05 14:40:18 -0400 | [diff] [blame] | 95 | if (m_surface && gVM->GetEnv((void**) &env, JNI_VERSION_1_4) == JNI_OK) { |
Derek Sollenberger | 08581f1 | 2009-09-08 18:36:29 -0400 | [diff] [blame] | 96 | env->DeleteGlobalRef(m_surface); |
| 97 | m_surface = NULL; |
| 98 | } |
| 99 | } |
| 100 | |
Derek Sollenberger | d049ec1 | 2009-08-07 11:26:44 -0400 | [diff] [blame] | 101 | void BackgroundPlugin::drawPlugin(int surfaceWidth, int surfaceHeight) { |
| 102 | |
| 103 | // get the plugin's dimensions according to the DOM |
| 104 | PluginObject *obj = (PluginObject*) inst()->pdata; |
| 105 | const int W = obj->window->width; |
| 106 | const int H = obj->window->height; |
| 107 | |
| 108 | // compute the current zoom level |
| 109 | const float zoomFactorW = static_cast<float>(surfaceWidth) / W; |
| 110 | const float zoomFactorH = static_cast<float>(surfaceHeight) / H; |
| 111 | |
| 112 | // check to make sure the zoom level is uniform |
| 113 | if (zoomFactorW + .01 < zoomFactorH && zoomFactorW - .01 > zoomFactorH) |
| 114 | gLogI.log(inst(), kError_ANPLogType, " ------ %p zoom is out of sync (%f,%f)", |
| 115 | inst(), zoomFactorW, zoomFactorH); |
| 116 | |
| 117 | // scale the variables based on the zoom level |
| 118 | const int fontSize = (int)(zoomFactorW * 16); |
| 119 | const int leftMargin = (int)(zoomFactorW * 10); |
| 120 | |
| 121 | // lock the surface |
| 122 | ANPBitmap bitmap; |
Derek Sollenberger | 08581f1 | 2009-09-08 18:36:29 -0400 | [diff] [blame] | 123 | JNIEnv* env = NULL; |
Derek Sollenberger | b8947ee | 2009-10-05 14:40:18 -0400 | [diff] [blame] | 124 | if (!m_surface || gVM->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK || |
Derek Sollenberger | 08581f1 | 2009-09-08 18:36:29 -0400 | [diff] [blame] | 125 | !gSurfaceI.lock(env, m_surface, &bitmap, NULL)) { |
Derek Sollenberger | d049ec1 | 2009-08-07 11:26:44 -0400 | [diff] [blame] | 126 | gLogI.log(inst(), kError_ANPLogType, " ------ %p unable to lock the plugin", inst()); |
| 127 | return; |
| 128 | } |
| 129 | |
| 130 | // create a canvas |
Derek Sollenberger | c0f2657 | 2009-07-16 11:38:02 -0400 | [diff] [blame] | 131 | ANPCanvas* canvas = gCanvasI.newCanvas(&bitmap); |
Derek Sollenberger | d7ebf27 | 2009-06-18 11:19:41 -0400 | [diff] [blame] | 132 | gCanvasI.drawColor(canvas, 0xFFFFFFFF); |
| 133 | |
Derek Sollenberger | d049ec1 | 2009-08-07 11:26:44 -0400 | [diff] [blame] | 134 | ANPPaint* paint = gPaintI.newPaint(); |
| 135 | gPaintI.setFlags(paint, gPaintI.getFlags(paint) | kAntiAlias_ANPPaintFlag); |
| 136 | gPaintI.setColor(paint, 0xFFFF0000); |
| 137 | gPaintI.setTextSize(paint, fontSize); |
Derek Sollenberger | d7ebf27 | 2009-06-18 11:19:41 -0400 | [diff] [blame] | 138 | |
Derek Sollenberger | d049ec1 | 2009-08-07 11:26:44 -0400 | [diff] [blame] | 139 | ANPTypeface* tf = gTypefaceI.createFromName("serif", kItalic_ANPTypefaceStyle); |
| 140 | gPaintI.setTypeface(paint, tf); |
| 141 | gTypefaceI.unref(tf); |
| 142 | |
| 143 | ANPFontMetrics fm; |
| 144 | gPaintI.getFontMetrics(paint, &fm); |
| 145 | |
| 146 | gPaintI.setColor(paint, 0xFF0000FF); |
Derek Sollenberger | d7ebf27 | 2009-06-18 11:19:41 -0400 | [diff] [blame] | 147 | const char c[] = "This is a background plugin."; |
Derek Sollenberger | d049ec1 | 2009-08-07 11:26:44 -0400 | [diff] [blame] | 148 | gCanvasI.drawText(canvas, c, sizeof(c)-1, leftMargin, -fm.fTop, paint); |
| 149 | |
| 150 | // clean up variables and unlock the surface |
| 151 | gPaintI.deletePaint(paint); |
| 152 | gCanvasI.deleteCanvas(canvas); |
Derek Sollenberger | 08581f1 | 2009-09-08 18:36:29 -0400 | [diff] [blame] | 153 | gSurfaceI.unlock(env, m_surface); |
Derek Sollenberger | d7ebf27 | 2009-06-18 11:19:41 -0400 | [diff] [blame] | 154 | } |
| 155 | |
Derek Sollenberger | d7ebf27 | 2009-06-18 11:19:41 -0400 | [diff] [blame] | 156 | int16 BackgroundPlugin::handleEvent(const ANPEvent* evt) { |
Derek Sollenberger | d7ebf27 | 2009-06-18 11:19:41 -0400 | [diff] [blame] | 157 | switch (evt->eventType) { |
| 158 | case kDraw_ANPEventType: |
Derek Sollenberger | d049ec1 | 2009-08-07 11:26:44 -0400 | [diff] [blame] | 159 | gLogI.log(inst(), kError_ANPLogType, " ------ %p the plugin did not request draw events", inst()); |
| 160 | break; |
Derek Sollenberger | d049ec1 | 2009-08-07 11:26:44 -0400 | [diff] [blame] | 161 | case kLifecycle_ANPEventType: |
| 162 | if (evt->data.lifecycle.action == kOnLoad_ANPLifecycleAction) { |
| 163 | gLogI.log(inst(), kDebug_ANPLogType, " ------ %p the plugin received an onLoad event", inst()); |
| 164 | return 1; |
Derek Sollenberger | d7ebf27 | 2009-06-18 11:19:41 -0400 | [diff] [blame] | 165 | } |
Derek Sollenberger | d049ec1 | 2009-08-07 11:26:44 -0400 | [diff] [blame] | 166 | break; |
Derek Sollenberger | d7ebf27 | 2009-06-18 11:19:41 -0400 | [diff] [blame] | 167 | case kTouch_ANPEventType: |
Derek Sollenberger | d049ec1 | 2009-08-07 11:26:44 -0400 | [diff] [blame] | 168 | gLogI.log(inst(), kError_ANPLogType, " ------ %p the plugin did not request touch events", inst()); |
| 169 | break; |
Derek Sollenberger | d7ebf27 | 2009-06-18 11:19:41 -0400 | [diff] [blame] | 170 | case kKey_ANPEventType: |
Derek Sollenberger | d049ec1 | 2009-08-07 11:26:44 -0400 | [diff] [blame] | 171 | gLogI.log(inst(), kError_ANPLogType, " ------ %p the plugin did not request key events", inst()); |
| 172 | break; |
Derek Sollenberger | d7ebf27 | 2009-06-18 11:19:41 -0400 | [diff] [blame] | 173 | default: |
| 174 | break; |
| 175 | } |
| 176 | return 0; // unknown or unhandled event |
| 177 | } |
| 178 | |
| 179 | /////////////////////////////////////////////////////////////////////////////// |
| 180 | // LOGGING TESTS |
| 181 | /////////////////////////////////////////////////////////////////////////////// |
| 182 | |
| 183 | |
| 184 | void BackgroundPlugin::test_logging() { |
| 185 | NPP instance = this->inst(); |
| 186 | |
| 187 | //LOG_ERROR(instance, " ------ %p Testing Log Error", instance); |
| 188 | gLogI.log(instance, kError_ANPLogType, " ------ %p Testing Log Error", instance); |
| 189 | gLogI.log(instance, kWarning_ANPLogType, " ------ %p Testing Log Warning", instance); |
| 190 | gLogI.log(instance, kDebug_ANPLogType, " ------ %p Testing Log Debug", instance); |
| 191 | } |
| 192 | |
| 193 | /////////////////////////////////////////////////////////////////////////////// |
| 194 | // TIMER TESTS |
| 195 | /////////////////////////////////////////////////////////////////////////////// |
| 196 | |
| 197 | #define TIMER_INTERVAL 50 |
| 198 | static void timer_oneshot(NPP instance, uint32 timerID); |
| 199 | static void timer_repeat(NPP instance, uint32 timerID); |
| 200 | static void timer_neverfires(NPP instance, uint32 timerID); |
| 201 | static void timer_latency(NPP instance, uint32 timerID); |
| 202 | |
| 203 | void BackgroundPlugin::test_timers() { |
| 204 | NPP instance = this->inst(); |
| 205 | |
| 206 | //Setup the testing counters |
| 207 | mTimerRepeatCount = 5; |
| 208 | mTimerLatencyCount = 5; |
| 209 | |
| 210 | // test for bogus timerID |
| 211 | browser->unscheduletimer(instance, 999999); |
| 212 | // test one-shot |
| 213 | browser->scheduletimer(instance, 100, false, timer_oneshot); |
| 214 | // test repeat |
| 215 | browser->scheduletimer(instance, 50, true, timer_repeat); |
| 216 | // test timer latency |
| 217 | browser->scheduletimer(instance, TIMER_INTERVAL, true, timer_latency); |
| 218 | mStartTime = mPrevTime = getMSecs(); |
| 219 | // test unschedule immediately |
| 220 | uint32 id = browser->scheduletimer(instance, 100, false, timer_neverfires); |
| 221 | browser->unscheduletimer(instance, id); |
| 222 | // test double unschedule (should be no-op) |
| 223 | browser->unscheduletimer(instance, id); |
| 224 | |
| 225 | } |
| 226 | |
| 227 | static void timer_oneshot(NPP instance, uint32 timerID) { |
| 228 | gLogI.log(instance, kDebug_ANPLogType, "-------- oneshot timer\n"); |
| 229 | } |
| 230 | |
| 231 | static void timer_repeat(NPP instance, uint32 timerID) { |
| 232 | BackgroundPlugin *obj = ((BackgroundPlugin*) ((PluginObject*) instance->pdata)->activePlugin); |
| 233 | |
| 234 | gLogI.log(instance, kDebug_ANPLogType, "-------- repeat timer %d\n", |
| 235 | obj->mTimerRepeatCount); |
| 236 | if (--obj->mTimerRepeatCount == 0) { |
| 237 | browser->unscheduletimer(instance, timerID); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | static void timer_neverfires(NPP instance, uint32 timerID) { |
| 242 | gLogI.log(instance, kError_ANPLogType, "-------- timer_neverfires!!!\n"); |
| 243 | } |
| 244 | |
| 245 | static void timer_latency(NPP instance, uint32 timerID) { |
| 246 | BackgroundPlugin *obj = ((BackgroundPlugin*) ((PluginObject*) instance->pdata)->activePlugin); |
| 247 | |
| 248 | obj->mTimerLatencyCurrentCount += 1; |
| 249 | |
| 250 | uint32_t now = getMSecs(); |
| 251 | uint32_t interval = now - obj->mPrevTime; |
| 252 | uint32_t dur = now - obj->mStartTime; |
| 253 | uint32_t expectedDur = obj->mTimerLatencyCurrentCount * TIMER_INTERVAL; |
| 254 | int32_t drift = dur - expectedDur; |
| 255 | int32_t avgDrift = drift / obj->mTimerLatencyCurrentCount; |
| 256 | |
| 257 | obj->mPrevTime = now; |
| 258 | |
| 259 | gLogI.log(instance, kDebug_ANPLogType, |
| 260 | "-------- latency test: [%3d] interval %d expected %d, total %d expected %d, drift %d avg %d\n", |
| 261 | obj->mTimerLatencyCurrentCount, interval, TIMER_INTERVAL, dur, |
| 262 | expectedDur, drift, avgDrift); |
| 263 | |
| 264 | if (--obj->mTimerLatencyCount == 0) { |
| 265 | browser->unscheduletimer(instance, timerID); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | /////////////////////////////////////////////////////////////////////////////// |
| 270 | // BITMAP TESTS |
| 271 | /////////////////////////////////////////////////////////////////////////////// |
| 272 | |
| 273 | static void test_formats(NPP instance); |
| 274 | |
| 275 | void BackgroundPlugin::test_bitmaps() { |
| 276 | test_formats(this->inst()); |
| 277 | } |
| 278 | |
| 279 | static void test_formats(NPP instance) { |
| 280 | |
| 281 | // TODO pull names from enum in npapi instead of hardcoding them |
| 282 | static const struct { |
| 283 | ANPBitmapFormat fFormat; |
| 284 | const char* fName; |
| 285 | } gRecs[] = { |
| 286 | { kUnknown_ANPBitmapFormat, "unknown" }, |
| 287 | { kRGBA_8888_ANPBitmapFormat, "8888" }, |
| 288 | { kRGB_565_ANPBitmapFormat, "565" }, |
| 289 | }; |
| 290 | |
| 291 | ANPPixelPacking packing; |
| 292 | for (size_t i = 0; i < ARRAY_COUNT(gRecs); i++) { |
| 293 | if (gBitmapI.getPixelPacking(gRecs[i].fFormat, &packing)) { |
| 294 | gLogI.log(instance, kDebug_ANPLogType, |
| 295 | "pixel format [%d] %s has packing ARGB [%d %d] [%d %d] [%d %d] [%d %d]\n", |
| 296 | gRecs[i].fFormat, gRecs[i].fName, |
| 297 | packing.AShift, packing.ABits, |
| 298 | packing.RShift, packing.RBits, |
| 299 | packing.GShift, packing.GBits, |
| 300 | packing.BShift, packing.BBits); |
| 301 | } else { |
| 302 | gLogI.log(instance, kDebug_ANPLogType, |
| 303 | "pixel format [%d] %s has no packing\n", |
| 304 | gRecs[i].fFormat, gRecs[i].fName); |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | void BackgroundPlugin::test_bitmap_transparency(const ANPEvent* evt) { |
| 310 | NPP instance = this->inst(); |
| 311 | |
| 312 | // check default & set transparent |
| 313 | if (!mFinishedStageOne) { |
| 314 | |
| 315 | gLogI.log(instance, kDebug_ANPLogType, "BEGIN: testing bitmap transparency"); |
| 316 | |
| 317 | //check to make sure it is not transparent |
| 318 | if (evt->data.draw.data.bitmap.format == kRGBA_8888_ANPBitmapFormat) { |
| 319 | gLogI.log(instance, kError_ANPLogType, "bitmap default format is transparent"); |
| 320 | } |
| 321 | |
| 322 | //make it transparent (any non-null value will set it to true) |
| 323 | bool value = true; |
| 324 | NPError err = browser->setvalue(instance, NPPVpluginTransparentBool, &value); |
| 325 | if (err != NPERR_NO_ERROR) { |
| 326 | gLogI.log(instance, kError_ANPLogType, "Error setting transparency."); |
| 327 | } |
| 328 | |
| 329 | mFinishedStageOne = true; |
| 330 | browser->invalidaterect(instance, NULL); |
| 331 | } |
| 332 | // check transparent & set opaque |
| 333 | else if (!mFinishedStageTwo) { |
| 334 | |
| 335 | //check to make sure it is transparent |
| 336 | if (evt->data.draw.data.bitmap.format != kRGBA_8888_ANPBitmapFormat) { |
| 337 | gLogI.log(instance, kError_ANPLogType, "bitmap did not change to transparent format"); |
| 338 | } |
| 339 | |
| 340 | //make it opaque |
| 341 | NPError err = browser->setvalue(instance, NPPVpluginTransparentBool, NULL); |
| 342 | if (err != NPERR_NO_ERROR) { |
| 343 | gLogI.log(instance, kError_ANPLogType, "Error setting transparency."); |
| 344 | } |
| 345 | |
| 346 | mFinishedStageTwo = true; |
| 347 | } |
| 348 | // check opaque |
| 349 | else if (!mFinishedStageThree) { |
| 350 | |
| 351 | //check to make sure it is not transparent |
| 352 | if (evt->data.draw.data.bitmap.format == kRGBA_8888_ANPBitmapFormat) { |
| 353 | gLogI.log(instance, kError_ANPLogType, "bitmap default format is transparent"); |
| 354 | } |
| 355 | |
| 356 | gLogI.log(instance, kDebug_ANPLogType, "END: testing bitmap transparency"); |
| 357 | |
| 358 | mFinishedStageThree = true; |
| 359 | } |
| 360 | } |
Derek Sollenberger | c356b9f | 2009-07-30 16:57:07 -0400 | [diff] [blame] | 361 | |
| 362 | /////////////////////////////////////////////////////////////////////////////// |
| 363 | // DOM TESTS |
| 364 | /////////////////////////////////////////////////////////////////////////////// |
| 365 | |
| 366 | void BackgroundPlugin::test_domAccess() { |
| 367 | NPP instance = this->inst(); |
| 368 | |
| 369 | gLogI.log(instance, kDebug_ANPLogType, " ------ %p Testing DOM Access", instance); |
| 370 | |
| 371 | // Get the plugin's DOM object |
| 372 | NPObject* windowObject = NULL; |
| 373 | browser->getvalue(instance, NPNVWindowNPObject, &windowObject); |
| 374 | |
| 375 | if (!windowObject) |
| 376 | gLogI.log(instance, kError_ANPLogType, " ------ %p Unable to retrieve DOM Window", instance); |
| 377 | |
| 378 | // Retrieve a property from the plugin's DOM object |
| 379 | NPIdentifier topIdentifier = browser->getstringidentifier("top"); |
| 380 | NPVariant topObjectVariant; |
| 381 | browser->getproperty(instance, windowObject, topIdentifier, &topObjectVariant); |
| 382 | |
| 383 | if (topObjectVariant.type != NPVariantType_Object) |
| 384 | gLogI.log(instance, kError_ANPLogType, " ------ %p Invalid Variant type for DOM Property: %d,%d", instance, topObjectVariant.type, NPVariantType_Object); |
| 385 | } |
| 386 | |
| 387 | |
| 388 | /////////////////////////////////////////////////////////////////////////////// |
| 389 | // JAVASCRIPT TESTS |
| 390 | /////////////////////////////////////////////////////////////////////////////// |
| 391 | |
| 392 | |
| 393 | void BackgroundPlugin::test_javascript() { |
| 394 | NPP instance = this->inst(); |
| 395 | |
| 396 | gLogI.log(instance, kDebug_ANPLogType, " ------ %p Testing JavaScript Access", instance); |
| 397 | |
| 398 | // Get the plugin's DOM object |
| 399 | NPObject* windowObject = NULL; |
| 400 | browser->getvalue(instance, NPNVWindowNPObject, &windowObject); |
| 401 | |
| 402 | if (!windowObject) |
| 403 | gLogI.log(instance, kError_ANPLogType, " ------ %p Unable to retrieve DOM Window", instance); |
| 404 | |
| 405 | // create a string (JS code) that is stored in memory allocated by the browser |
| 406 | const char* jsString = "1200 + 34"; |
| 407 | void* stringMem = browser->memalloc(strlen(jsString)); |
| 408 | memcpy(stringMem, jsString, strlen(jsString)); |
| 409 | |
| 410 | // execute the javascript in the plugin's DOM object |
| 411 | NPString script = { (char*)stringMem, strlen(jsString) }; |
| 412 | NPVariant scriptVariant; |
| 413 | if (!browser->evaluate(instance, windowObject, &script, &scriptVariant)) |
| 414 | gLogI.log(instance, kError_ANPLogType, " ------ %p Unable to eval the JS.", instance); |
| 415 | |
| 416 | if (scriptVariant.type == NPVariantType_Int32) { |
| 417 | if (scriptVariant.value.intValue != 1234) |
| 418 | gLogI.log(instance, kError_ANPLogType, " ------ %p Invalid Value for JS Return: %d,1234", instance, scriptVariant.value.intValue); |
| 419 | } else { |
| 420 | gLogI.log(instance, kError_ANPLogType, " ------ %p Invalid Variant type for JS Return: %d,%d", instance, scriptVariant.type, NPVariantType_Int32); |
| 421 | } |
| 422 | |
| 423 | // free the memory allocated within the browser |
| 424 | browser->memfree(stringMem); |
| 425 | } |
Derek Sollenberger | b4a2391 | 2009-11-09 15:38:58 -0500 | [diff] [blame^] | 426 | |
| 427 | /////////////////////////////////////////////////////////////////////////////// |
| 428 | // Load Java Classes Tests |
| 429 | /////////////////////////////////////////////////////////////////////////////// |
| 430 | |
| 431 | void BackgroundPlugin::test_loadJavaClass() { |
| 432 | |
| 433 | JNIEnv* env = NULL; |
| 434 | if (gVM->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) { |
| 435 | gLogI.log(inst(), kError_ANPLogType, " ---- LoadJavaTest: failed to get env"); |
| 436 | return; |
| 437 | } |
| 438 | |
| 439 | const char* className = "com.android.sampleplugin.BackgroundTest"; |
| 440 | jclass backgroundClass = gSystemI.loadJavaClass(inst(), className); |
| 441 | |
| 442 | if(!backgroundClass) { |
| 443 | gLogI.log(inst(), kError_ANPLogType, " ---- LoadJavaTest: failed to load class"); |
| 444 | return; |
| 445 | } |
| 446 | |
| 447 | jmethodID constructor = env->GetMethodID(backgroundClass, "<init>", "()V"); |
| 448 | jmethodID addMethod = env->GetMethodID(backgroundClass, "addInt", "(II)I"); |
| 449 | jobject backgroundObject = env->NewObject(backgroundClass, constructor); |
| 450 | |
| 451 | if(!backgroundObject) { |
| 452 | gLogI.log(inst(), kError_ANPLogType, " ---- LoadJavaTest: failed to construct object"); |
| 453 | return; |
| 454 | } |
| 455 | |
| 456 | jint result = env->CallIntMethod(backgroundObject, addMethod, 2, 2); |
| 457 | |
| 458 | if (result != 4) { |
| 459 | gLogI.log(inst(), kError_ANPLogType, " ---- LoadJavaTest: invalid result (%d != 4)", result); |
| 460 | } |
| 461 | } |