Java Code Examples for android.os.ConditionVariable#block()

The following examples show how to use android.os.ConditionVariable#block() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WebViewAppTest.java    From unity-ads-android with Apache License 2.0 6 votes vote down vote up
@Test
public void testSendEventShouldFail () throws Exception {
	WebViewApp.setCurrentApp(null);
	final ConditionVariable cv = new ConditionVariable();

	Handler handler = new Handler(Looper.getMainLooper());
	handler.post(new Runnable() {
		@Override
		public void run() {
			WebViewApp.setCurrentApp(new WebViewApp());
			WebViewApp.getCurrentApp().setWebView(new MockWebView(InstrumentationRegistry.getContext()));
			WebViewApp.getCurrentApp().setWebAppInitialized(true);
			cv.open();
		}
	});

	boolean cvsuccess = cv.block(10000);
	assertTrue("ConditionVariable was not opened successfully", cvsuccess);
	boolean success = WebViewApp.getCurrentApp().sendEvent(MockEventCategory.TEST_CATEGORY_1, MockEvent.TEST_EVENT_1);
	assertFalse("sendEvent -method should've returned false", success);
	assertFalse("WebView invokeJavascript should've not been invoked but was (webviewapp is not loaded so no call should have occured)", ((MockWebView) WebViewApp.getCurrentApp().getWebView()).JS_INVOKED);
	assertNull("The invoked JavaScript string should be null (webviewapp is not loaded so no call should have occured)", ((MockWebView) WebViewApp.getCurrentApp().getWebView()).JS_CALL);
}
 
Example 2
Source File: WebViewAppTest.java    From unity-ads-android with Apache License 2.0 6 votes vote down vote up
@Test
public void testSetConfiguration () throws Exception {
	WebViewApp.setCurrentApp(null);
	final ConditionVariable cv = new ConditionVariable();

	Handler handler = new Handler(Looper.getMainLooper());
	handler.post(new Runnable() {
		@Override
		public void run() {
			WebViewApp.setCurrentApp(new WebViewApp());
			WebViewApp.getCurrentApp().setWebView(new MockWebView(InstrumentationRegistry.getContext()));
			WebViewApp.getCurrentApp().setWebAppLoaded(true);
			WebViewApp.getCurrentApp().setWebAppInitialized(true);
			cv.open();
		}
	});

	boolean success = cv.block(10000);
	assertTrue("ConditionVariable was not opened successfully", success);

	final Configuration conf = new Configuration(TestUtilities.getTestServerAddress());
	WebViewApp.getCurrentApp().setConfiguration(conf);

	assertNotNull("Current WebApp configuration should not be null", WebViewApp.getCurrentApp().getConfiguration());
	assertEquals("Local configuration and current WebApp configuration should be the same object", conf, WebViewApp.getCurrentApp().getConfiguration());
}
 
Example 3
Source File: WebViewAppTest.java    From unity-ads-android with Apache License 2.0 6 votes vote down vote up
@Test
public void testSetWebView () throws Exception {
	WebViewApp.setCurrentApp(null);
	final ConditionVariable cv = new ConditionVariable();

	Handler handler = new Handler(Looper.getMainLooper());
	handler.post(new Runnable() {
		@Override
		public void run() {
			WebViewApp.setCurrentApp(new WebViewApp());
			WebView webView = new WebView(InstrumentationRegistry.getContext());
			WebViewApp.getCurrentApp().setWebView(webView);
			assertEquals("Local and WebApps WebView should be the same object", webView, WebViewApp.getCurrentApp().getWebView());
			WebViewApp.getCurrentApp().setWebAppLoaded(true);
			WebViewApp.getCurrentApp().setWebAppInitialized(true);
			cv.open();
		}
	});

	boolean success = cv.block(10000);

	assertTrue("ConditionVariable was not opened successfully", success);
	assertNotNull("Current WebApps WebView should not be null because it was set", WebViewApp.getCurrentApp().getWebView());
}
 
Example 4
Source File: FacebookActivityTestCase.java    From FacebookImageShareIntent with MIT License 6 votes vote down vote up
protected void runOnBlockerThread(final Runnable runnable, boolean waitForCompletion) {
    Runnable runnableToPost = runnable;
    final ConditionVariable condition = waitForCompletion ? new ConditionVariable(!waitForCompletion) : null;

    if (waitForCompletion) {
        runnableToPost = new Runnable() {
            @Override
            public void run() {
                runnable.run();
                condition.open();
            }
        };
    }

    TestBlocker blocker = getTestBlocker();
    Handler handler = blocker.getHandler();
    handler.post(runnableToPost);

    if (waitForCompletion) {
        boolean success = condition.block(10000);
        assertTrue(success);
    }
}
 
Example 5
Source File: WebViewAppTest.java    From unity-ads-android with Apache License 2.0 6 votes vote down vote up
@Test
public void testInvokeMethodShouldSucceedMethodNull () throws Exception {
	WebViewApp.setCurrentApp(null);
	final ConditionVariable cv = new ConditionVariable();
	Handler handler = new Handler(Looper.getMainLooper());
	handler.post(new Runnable() {
		@Override
		public void run() {
			WebViewApp.setCurrentApp(new WebViewApp());
			WebViewApp.getCurrentApp().setWebView(new MockWebView(InstrumentationRegistry.getContext()));
			WebViewApp.getCurrentApp().setWebAppLoaded(true);
			WebViewApp.getCurrentApp().setWebAppInitialized(true);
			cv.open();
		}
	});

	boolean cvsuccess = cv.block(10000);
	assertTrue("ConditionVariable was not opened successfully", cvsuccess);
	Method m = null;
	boolean success = WebViewApp.getCurrentApp().invokeMethod("TestClass", "testMethod", m);
	assertTrue("invokeMethod -method should've returned true", success);
	assertTrue("WebView invokeJavascript should've succeeded but didn't", ((MockWebView) WebViewApp.getCurrentApp().getWebView()).JS_INVOKED);
	assertNotNull("The invoked JavaScript string should not be null.", ((MockWebView) WebViewApp.getCurrentApp().getWebView()).JS_CALL);
}
 
Example 6
Source File: RequestTest.java    From unity-ads-android with Apache License 2.0 5 votes vote down vote up
@SdkSuppress(minSdkVersion = 16)
@Ignore
@Test
public void testValidGetWithInvalidHeader () throws Exception {
	final ConditionVariable cv = new ConditionVariable();
	MockWebViewApp webViewApp = new MockWebViewApp() {
		@Override
		public boolean sendEvent(Enum eventCategory, Enum eventId, Object... params) {
			EVENT_CATEGORY = eventCategory;
			EVENT_ID = eventId;
			EVENT_PARAMS = params;
			cv.open();

			return true;
		}
	};

	JSONArray headers = new JSONArray("[[\"C,o-n#n#e*c*t*io*n\", \"close\"]]");
	WebViewApp.setCurrentApp(webViewApp);
	Invocation invocation = new Invocation();
	WebViewCallback callback = new WebViewCallback("Test_Valid_Get_With_Invalid_Header", invocation.getId());
	Request.get("1", validUrl, headers, connectTimeout, readTimeout, callback);
	invocation.sendInvocationCallback();

	boolean success = cv.block(30000);

	assertTrue("ConditionVariable was not opened", success);
	assertEquals("Callback status was not OK, successfull callback was expected", CallbackStatus.OK, CALLBACK_STATUS);
	assertNull("Callback error was not NULL, successfull callback was expected", CALLBACK_ERROR);
	assertEquals("Callback params list length was expected to be 2 (callback id and request id in params)", 2, CALLBACK_PARAMS.length);
	assertEquals("Callback first param was expected to be Callback ID", callback.getCallbackId(), CALLBACK_ID);
	assertEquals("Request has changed for some reason", validUrl, EVENT_PARAMS[1]);
	assertEquals("Expected three (3) params in event parameters", 3, EVENT_PARAMS.length);
	assertEquals("Event Category was incorrect", WebViewEventCategory.REQUEST, EVENT_CATEGORY);
	assertEquals("Event ID was incorrect", WebRequestEvent.FAILED, EVENT_ID);
}
 
Example 7
Source File: SettingsTests.java    From FacebookImageShareIntent with MIT License 5 votes vote down vote up
@SmallTest @MediumTest @LargeTest
public void testSetExecutor() {
    final ConditionVariable condition = new ConditionVariable();

    final Runnable runnable = new Runnable() {
        @Override
        public void run() { }
    };

    final Executor executor = new Executor() {
        @Override
        public void execute(Runnable command) {
            assertEquals(runnable, command);
            command.run();

            condition.open();
        }
    };

    Executor original = Settings.getExecutor();
    try {
        Settings.setExecutor(executor);
        Settings.getExecutor().execute(runnable);

        boolean success = condition.block(5000);
        assertTrue(success);
    } finally {
        Settings.setExecutor(original);
    }
}
 
Example 8
Source File: RequestTest.java    From unity-ads-android with Apache License 2.0 5 votes vote down vote up
@Test
public void testValidPostWithNullHeaders () throws Exception {
	final ConditionVariable cv = new ConditionVariable();
	MockWebViewApp webViewApp = new MockWebViewApp() {
		@Override
		public boolean sendEvent(Enum eventCategory, Enum eventId, Object... params) {
			EVENT_CATEGORY = eventCategory;
			EVENT_ID = eventId;
			EVENT_PARAMS = params;
			cv.open();

			return true;
		}
	};

	WebViewApp.setCurrentApp(webViewApp);
	Invocation invocation = new Invocation();
	WebViewCallback callback = new WebViewCallback("Test_Valid_Post_No_Headers", invocation.getId());
	Request.post("1", validUrl, null, null, connectTimeout, readTimeout, callback);
	invocation.sendInvocationCallback();

	boolean success = cv.block(30000);

	assertTrue("ConditionVariable was not opened", success);
	assertEquals("Callback status was not OK, successfull callback was expected", CallbackStatus.OK, CALLBACK_STATUS);
	assertNull("Callback error was not NULL, successfull callback was expected", CALLBACK_ERROR);
	assertEquals("Callback params list length was expected to be 2 (callback id and request id in params)", 2, CALLBACK_PARAMS.length);
	assertEquals("Callback first param was expected to be Callback ID", callback.getCallbackId(), CALLBACK_ID);

	assertEquals("Request has changed for some reason", validUrl, EVENT_PARAMS[1]);
	assertEquals("Response code was not OK (200)", 200, EVENT_PARAMS[3]);
	assertEquals("Event ID was incorrect", WebRequestEvent.COMPLETE, EVENT_ID);
	assertEquals("Event Category was incorrect", WebViewEventCategory.REQUEST, EVENT_CATEGORY);
}
 
Example 9
Source File: InvocationTest.java    From unity-ads-android with Apache License 2.0 5 votes vote down vote up
@Test
public void testBatchInvocationOneInvalidMethod () {
	Invocation batch = new Invocation();

	final ConditionVariable cv = new ConditionVariable();
	Handler handler = new Handler(Looper.getMainLooper());
	handler.postDelayed(new Runnable() {
		@Override
		public void run() {
			WebViewApp.getCurrentApp().setWebView(new WebView(ClientProperties.getApplicationContext()) {
				@Override
				public void invokeJavascript(String data) {
					BatchInvocationTestApi.JAVASCRIPT_INVOKED = true;
				}
			});
			cv.open();
		}
	}, 100);

	boolean success = cv.block(30000);
	assertTrue("Condition Variable was not opened", success);

	batch.addInvocation("com.unity3d.ads.test.legacy.InvocationTest$BatchInvocationTestApi", "apiTestMethodNonExistent", new Object[]{"test1"}, new WebViewCallback("CALLBACK_01", batch.getId()));
	batch.addInvocation("com.unity3d.ads.test.legacy.InvocationTest$BatchInvocationTestApi", "apiTestMethod", new Object[]{"test2"}, new WebViewCallback("CALLBACK_02", batch.getId()));
	batch.addInvocation("com.unity3d.ads.test.legacy.InvocationTest$BatchInvocationTestApi", "apiTestMethod", new Object[]{"test3"}, new WebViewCallback("CALLBACK_03", batch.getId()));
	batch.addInvocation("com.unity3d.ads.test.legacy.InvocationTest$BatchInvocationTestApi", "apiTestMethod", new Object[]{"test4"}, new WebViewCallback("CALLBACK_04", batch.getId()));

	batch.nextInvocation();
	batch.nextInvocation();
	batch.nextInvocation();
	batch.nextInvocation();
	batch.sendInvocationCallback();

	assertTrue("Invocation should have happened", BatchInvocationTestApi.INVOKED);
	assertTrue("Javascript invocation should have happened", BatchInvocationTestApi.JAVASCRIPT_INVOKED);
	assertEquals("Successfull invocation count was different than expected", 3, BatchInvocationTestApi.INVOCATION_COUNT);
	assertEquals("Invocation response value should have been set", BatchInvocationTestApi.VALUE, "test4");
}
 
Example 10
Source File: RequestTest.java    From unity-ads-android with Apache License 2.0 5 votes vote down vote up
@Test
public void testValidGetWithNullHeaders () throws Exception {
	final ConditionVariable cv = new ConditionVariable();
	MockWebViewApp webViewApp = new MockWebViewApp() {
		@Override
		public boolean sendEvent(Enum eventCategory, Enum eventId, Object... params) {
			EVENT_CATEGORY = eventCategory;
			EVENT_ID = eventId;
			EVENT_PARAMS = params;
			cv.open();

			return true;
		}
	};

	WebViewApp.setCurrentApp(webViewApp);
	Invocation invocation = new Invocation();
	WebViewCallback callback = new WebViewCallback("Test_Valid_Get_No_Headers", invocation.getId());
	Request.get("1", validUrl, null, connectTimeout, readTimeout, callback);
	invocation.sendInvocationCallback();
	boolean success = cv.block(30000);

	assertTrue("ConditionVariable was not opened", success);
	assertEquals("Callback status was not OK, successfull callback was expected", CallbackStatus.OK, CALLBACK_STATUS);
	assertNull("Callback error was not NULL, successfull callback was expected", CALLBACK_ERROR);
	assertEquals("Callback params list length was expected to be 2 (callback id and request id in params)", 2, CALLBACK_PARAMS.length);
	assertEquals("Callback first param was expected to be Callback ID", callback.getCallbackId(), CALLBACK_ID);
	assertEquals("Request has changed for some reason", validUrl, EVENT_PARAMS[1]);
	assertEquals("Response code was not OK (200)", 200, EVENT_PARAMS[3]);
	assertEquals("Event ID was incorrect", WebRequestEvent.COMPLETE, EVENT_ID);
	assertEquals("Event Category was incorrect", WebViewEventCategory.REQUEST, EVENT_CATEGORY);
}
 
Example 11
Source File: VideoViewTest.java    From unity-ads-android with Apache License 2.0 4 votes vote down vote up
@Test
@RequiresDevice
@SdkSuppress(minSdkVersion = 17)
public void testDisableInfoListener () throws Exception {
	final Activity activity = waitForActivityStart(null);

	WebViewApp.setCurrentApp(new MockWebViewApp() {
		@Override
		public boolean sendEvent(Enum eventCategory, Enum eventId, Object... params) {
			super.sendEvent(eventCategory, eventId, params);

			if ("ON_FOCUS_GAINED".equals(eventId.name()) || "ON_FOCUS_LOST".equals(eventId.name())) {
				return true;
			}

			EVENT_CATEGORIES.add(eventCategory);
			EVENTS.add(eventId);
			EVENT_PARAMS = params;
			EVENT_COUNT++;

			if (eventCategory.equals(WebViewEventCategory.VIDEOPLAYER) && eventId.equals(VideoPlayerEvent.PREPARED)) {
				VIDEOPLAYER_VIEW.play();
			}
			if (eventCategory.equals(WebViewEventCategory.VIDEOPLAYER) && eventId.equals(VideoPlayerEvent.INFO)) {
				INFO_EVENTS = new ArrayList<>();
				INFO_EVENTS.add((Integer) params[1]);
			}
			if (eventCategory.equals(WebViewEventCategory.VIDEOPLAYER) && eventId.equals(VideoPlayerEvent.COMPLETED)) {
				CONDITION_VARIABLE.open();
			}

			return true;
		}
	});

	final String validUrl = TestUtilities.getTestServerAddress() + "/blue_test_trailer.mp4";

	final Handler handler = new Handler(Looper.getMainLooper());
	final ConditionVariable viewAddCV = new ConditionVariable();
	handler.post(new Runnable() {
		@Override
		public void run() {
			((MockWebViewApp)WebViewApp.getCurrentApp()).VIDEOPLAYER_VIEW = new VideoPlayerView(getInstrumentation().getTargetContext());
			activity.addContentView(((MockWebViewApp)WebViewApp.getCurrentApp()).VIDEOPLAYER_VIEW, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
			viewAddCV.open();
		}
	});
	boolean success = viewAddCV.block(3000);
	assertTrue("ConditionVariable did not open in view add", success);

	final MockWebViewApp mockWebViewApp = (MockWebViewApp)WebViewApp.getCurrentApp();
	ConditionVariable cv = new ConditionVariable();
	mockWebViewApp.CONDITION_VARIABLE = cv;
	((MockWebViewApp)WebViewApp.getCurrentApp()).VIDEOPLAYER_VIEW.setInfoListenerEnabled(true);
	((MockWebViewApp)WebViewApp.getCurrentApp()).VIDEOPLAYER_VIEW.setInfoListenerEnabled(false);
	((MockWebViewApp)WebViewApp.getCurrentApp()).VIDEOPLAYER_VIEW.prepare(validUrl, 1f, 0);
	success = cv.block(30000);

	assertTrue("Condition Variable was not opened: COMPLETED or PREPARE ERROR event was not received", success);
	assertEquals("Event category should be videoplayer category", WebViewEventCategory.VIDEOPLAYER, mockWebViewApp.EVENT_CATEGORIES.get(0));
	assertEquals("Event ID should be completed", VideoPlayerEvent.COMPLETED, mockWebViewApp.EVENTS.get(mockWebViewApp.EVENTS.size() - 1));
	assertEquals("The video url and the url received from the completed event should be the same", validUrl, mockWebViewApp.EVENT_PARAMS[0]);
	assertNull("No info-events shoul've been received", mockWebViewApp.INFO_EVENTS);
	assertTrue("Didn't get activity finish", waitForActivityFinish(activity));
}
 
Example 12
Source File: VideoViewTest.java    From unity-ads-android with Apache License 2.0 4 votes vote down vote up
@Test
@RequiresDevice
@SdkSuppress(minSdkVersion = 17)
public void testInfoListener () throws Exception {
	final Activity activity = waitForActivityStart(null);

	WebViewApp.setCurrentApp(new MockWebViewApp() {
		@Override
		public boolean sendEvent(Enum eventCategory, Enum eventId, Object... params) {
			super.sendEvent(eventCategory, eventId, params);

			if ("ON_FOCUS_GAINED".equals(eventId.name()) || "ON_FOCUS_LOST".equals(eventId.name())) {
				return true;
			}

			EVENT_CATEGORIES.add(eventCategory);
			EVENTS.add(eventId);
			EVENT_PARAMS = params;
			EVENT_COUNT++;

			if (eventCategory.equals(WebViewEventCategory.VIDEOPLAYER) && eventId.equals(VideoPlayerEvent.PREPARED)) {
				VIDEOPLAYER_VIEW.play();
			}
			if (eventCategory.equals(WebViewEventCategory.VIDEOPLAYER) && eventId.equals(VideoPlayerEvent.INFO)) {
				INFO_EVENTS = new ArrayList<>();
				INFO_EVENTS.add((Integer) params[1]);
			}
			if (eventCategory.equals(WebViewEventCategory.VIDEOPLAYER) && eventId.equals(VideoPlayerEvent.COMPLETED)) {
				CONDITION_VARIABLE.open();
			}

			return true;
		}
	});

	final String validUrl = TestUtilities.getTestServerAddress() + "/blue_test_trailer.mp4";

	final Handler handler = new Handler(Looper.getMainLooper());
	final ConditionVariable viewAddCV = new ConditionVariable();
	handler.post(new Runnable() {
		@Override
		public void run() {
			((MockWebViewApp)WebViewApp.getCurrentApp()).VIDEOPLAYER_VIEW = new VideoPlayerView(getInstrumentation().getTargetContext());
			activity.addContentView(((MockWebViewApp)WebViewApp.getCurrentApp()).VIDEOPLAYER_VIEW, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
			viewAddCV.open();
		}
	});
	boolean success = viewAddCV.block(3000);
	assertTrue("ConditionVariable did not open in view add", success);

	final MockWebViewApp mockWebViewApp = (MockWebViewApp)WebViewApp.getCurrentApp();
	ConditionVariable cv = new ConditionVariable();
	mockWebViewApp.CONDITION_VARIABLE = cv;
	((MockWebViewApp)WebViewApp.getCurrentApp()).VIDEOPLAYER_VIEW.prepare(validUrl, 1f, 0);
	success = cv.block(30000);

	assertTrue("Condition Variable was not opened: COMPLETED or PREPARE ERROR event was not received", success);
	assertEquals("Event category should be videoplayer category", WebViewEventCategory.VIDEOPLAYER, mockWebViewApp.EVENT_CATEGORIES.get(0));
	assertEquals("Event ID should be completed", VideoPlayerEvent.COMPLETED, mockWebViewApp.EVENTS.get(mockWebViewApp.EVENTS.size() - 1));
	assertEquals("The video url and the url received from the completed event should be the same", validUrl, mockWebViewApp.EVENT_PARAMS[0]);
	assertNotNull("Info events should not be NULL", mockWebViewApp.INFO_EVENTS);
	assertTrue("There should be at least one INFO event received", mockWebViewApp.INFO_EVENTS.size() > 0);
	assertTrue("Didn't get activity finish", waitForActivityFinish(activity));
}
 
Example 13
Source File: WebPlayerViewTest.java    From unity-ads-android with Apache License 2.0 4 votes vote down vote up
@Test
public void testSettings () throws Exception {
	final Activity activity = waitForActivityStart(null);
	assertNotNull("Started activity should not be null!", activity);

	WebViewApp.setCurrentApp(new MockWebViewApp() {
		public boolean sendEvent(Enum eventCategory, Enum eventId, Object... params) {
			super.sendEvent(eventCategory, eventId, params);

			EVENT_CATEGORIES.add(eventCategory);
			EVENTS.add(eventId);
			EVENT_PARAMS = params;
			EVENT_COUNT++;

			DeviceLog.debug("GOT EVENT: " + eventId.name() + ": " + CONDITION_VARIABLE);

			if (eventId.name().equals("PAGE_FINISHED") && CONDITION_VARIABLE != null) {
				CONDITION_VARIABLE.open();
			}

			return true;
		}
	});

	final Handler handler = new Handler(Looper.getMainLooper());
	final ConditionVariable viewAddCV = new ConditionVariable();

	final JSONObject webSettings = new JSONObject();
	webSettings.put("setSaveFormData", new JSONArray("[true]"));
	webSettings.put("setSavePassword", new JSONArray("[true]"));
	webSettings.put("setLayoutAlgorithm", new JSONArray("[{\"value\": \"NORMAL\", \"type\": \"Enum\", \"className\": \"android.webkit.WebSettings$LayoutAlgorithm\"}]"));

	final JSONObject webPlayerSettings = new JSONObject();
	webPlayerSettings.put("setHorizontalScrollBarEnabled", new JSONArray("[false]"));
	webPlayerSettings.put("setVerticalScrollBarEnabled", new JSONArray("[false]"));
	webPlayerSettings.put("setInitialScale", new JSONArray("[1]"));

	handler.post(new Runnable() {
		@Override
		public void run() {
			_webPlayerView = new WebPlayerView(activity, "webplayer", webSettings, webPlayerSettings);
			activity.addContentView(_webPlayerView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
			viewAddCV.open();
		}
	});
	boolean success = viewAddCV.block(3000);
	assertTrue("ConditionVariable did not open in view add", success);

	assertNotNull("WebPlayer should not be null", _webPlayerView);
	assertNull("There shouldn't be errored settings", _webPlayerView.getErroredSettings());

	boolean didFinish = waitForActivityFinish(activity);
	assertTrue("Activity should have finished", didFinish);
}
 
Example 14
Source File: AdUnitActivityTest.java    From unity-ads-android with Apache License 2.0 4 votes vote down vote up
@Test
public void testIntentSetWithViewsAndOrientationRemoveViews () {
	final String[] views = new String[]{VIDEO_PLAYER_VIEW, WEB_VIEW};
	final String[] removeViewsWebView = new String[]{WEB_VIEW};
	final String[] removeViewsVideoPlayer = new String[]{VIDEO_PLAYER_VIEW};
	Intent intent = new Intent();
	intent.putExtra(AdUnitActivity.EXTRA_VIEWS, views);
	intent.putExtra(AdUnitActivity.EXTRA_ORIENTATION, 6);

	ArrayList<Enum> allEvents = new ArrayList<>();
	ArrayList<Enum> allEventCategories = new ArrayList<>();
	int totalEventCount = 0;

	final Activity activity = waitForActivityStart(intent);
	MockWebViewApp webViewApp = (MockWebViewApp)WebViewApp.getCurrentApp();
	allEvents.addAll(webViewApp.EVENTS);
	allEventCategories.addAll(webViewApp.EVENT_CATEGORIES);
	totalEventCount += webViewApp.EVENT_COUNT;

	ConditionVariable cv = new ConditionVariable();
	cv.block(300);

	assertEquals("View list first value not same than expected", views[0], AdUnit.getAdUnitActivity().getViews()[0]);
	assertEquals("View list second value not same than expected", views[1], AdUnit.getAdUnitActivity().getViews()[1]);

	final ConditionVariable cvSetViews = new ConditionVariable();
	Handler handler = new Handler(Looper.getMainLooper());
	handler.post(new Runnable() {
		@Override
		public void run() {
			((AdUnitActivity)activity).setViews(removeViewsWebView);

			assertEquals("View list first value not same than expected", removeViewsWebView[0], AdUnit.getAdUnitActivity().getViews()[0]);

			((AdUnitActivity)activity).setViews(views);

			assertEquals("View list first value not same than expected after setting them back", views[0], AdUnit.getAdUnitActivity().getViews()[0]);
			assertEquals("View list second value not same than expected after setting them back", views[1], AdUnit.getAdUnitActivity().getViews()[1]);

			((AdUnitActivity)activity).setViews(removeViewsVideoPlayer);
			cvSetViews.open();
		}
	});
	boolean success = cvSetViews.block(2000);

	assertTrue("Weird ConditionVariable problem", success);
	assertEquals("View list first value not same than expected", removeViewsVideoPlayer[0], AdUnit.getAdUnitActivity().getViews()[0]);
	assertEquals("Current orientation was not the same that was given in intent", 6, AdUnit.getAdUnitActivity().getRequestedOrientation());
	assertTrue("Didn't get activity finish", waitForActivityFinish(activity));

	webViewApp = (MockWebViewApp)WebViewApp.getCurrentApp();
	allEvents.addAll(webViewApp.EVENTS);
	allEventCategories.addAll(webViewApp.EVENT_CATEGORIES);
	totalEventCount += webViewApp.EVENT_COUNT;

	assertTrue("Counted events should be at least 6. AdUnitActivity should have sent all 6 lifecycle events before finishing: " + printEvents(allEvents) + ", COUNT: " + allEvents.size(), totalEventCount >= 6);
	assertTrue("Counted event categories should be at least 6. AdUnitActivity should have sent all 6 lifecycle events before finishing: " + printEvents(allEvents) + ", COUNT: " + allEvents.size(), allEventCategories.size() >= 6);
	assertTrue("Counted actual event types be at least 6. AdUnitActivity should have sent all 6 lifecycle events before finishing: " + printEvents(allEvents) + ", COUNT: " + allEvents.size(), allEvents.size() >= 6);

	for (int idx = 0; idx < allEventCategories.size(); idx++) {
		assertEquals("Expected event categories to be WebViewEventCategory.ADUNIT", allEventCategories.get(idx), WebViewEventCategory.ADUNIT);
	}

	assertEquals("Expected first event from the AdUnitActivity to be ON_CREATE: " + printEvents(allEvents), AdUnitEvent.ON_CREATE, allEvents.get(0));
	assertEquals("Expected second event from the AdUnitActivity to be ON_START: " + printEvents(allEvents), AdUnitEvent.ON_START, allEvents.get(1));
	assertEquals("Expected third event from the AdUnitActivity to be ON_RESUME: " + printEvents(allEvents), AdUnitEvent.ON_RESUME, allEvents.get(2));
	assertEquals("Expected third last event from the AdUnitActivity to be ON_PAUSE: " + printEvents(allEvents), AdUnitEvent.ON_PAUSE, allEvents.get(allEvents.size() - 3));
	assertEquals("Expected second last event from the AdUnitActivity to be ON_STOP: " + printEvents(allEvents), AdUnitEvent.ON_STOP, allEvents.get(allEvents.size() - 2));
	assertEquals("Expected last event from the AdUnitActivity to be ON_DESTROY: " + printEvents(allEvents), AdUnitEvent.ON_DESTROY, allEvents.get(allEvents.size() - 1));
}
 
Example 15
Source File: AdUnitActivityTest.java    From unity-ads-android with Apache License 2.0 4 votes vote down vote up
@Test
public void testIntentSetWithViewsSetOrientation () {
	final String[] views = new String[]{VIDEO_PLAYER_VIEW, WEB_VIEW};
	Intent intent = new Intent();
	intent.putExtra(AdUnitActivity.EXTRA_VIEWS, views);
	intent.putExtra(AdUnitActivity.EXTRA_ORIENTATION, 6);

	ArrayList<Enum> allEvents = new ArrayList<>();
	ArrayList<Enum> allEventCategories = new ArrayList<>();
	int totalEventCount = 0;

	final Activity activity = waitForActivityStart(intent);
	MockWebViewApp webViewApp = (MockWebViewApp)WebViewApp.getCurrentApp();
	allEvents.addAll(webViewApp.EVENTS);
	allEventCategories.addAll(webViewApp.EVENT_CATEGORIES);
	totalEventCount += webViewApp.EVENT_COUNT;

	ConditionVariable cv = new ConditionVariable();
	cv.block(300);

	assertEquals("View list first value not same than expected", views[0], AdUnit.getAdUnitActivity().getViews()[0]);
	assertEquals("View list second value not same than expected", views[1], AdUnit.getAdUnitActivity().getViews()[1]);
	assertEquals("Current orientation was not the same that was given in intent", 6, AdUnit.getAdUnitActivity().getRequestedOrientation());

	final ConditionVariable cvSetViews = new ConditionVariable();
	Handler handler = new Handler(Looper.getMainLooper());
	handler.post(new Runnable() {
		@Override
		public void run() {
			((AdUnitActivity)activity).setOrientation(1);
			cvSetViews.open();
		}
	});
	boolean success = cvSetViews.block(2000);

	assertTrue("Weird ConditionVariable problem", success);
	assertEquals("Requested orientation should be same as given", activity.getRequestedOrientation(), 1);
	assertTrue("Didn't get activity finish", waitForActivityFinish(activity));

	webViewApp = (MockWebViewApp)WebViewApp.getCurrentApp();
	allEvents.addAll(webViewApp.EVENTS);
	allEventCategories.addAll(webViewApp.EVENT_CATEGORIES);
	totalEventCount += webViewApp.EVENT_COUNT;

	assertTrue("Counted events should be at least 6. AdUnitActivity should have sent all 6 lifecycle events before finishing: " + printEvents(allEvents) + ", COUNT: " + allEvents.size(), totalEventCount >= 6);
	assertTrue("Counted event categories should be at least 6. AdUnitActivity should have sent all 6 lifecycle events before finishing: " + printEvents(allEvents) + ", COUNT: " + allEvents.size(), allEventCategories.size() >= 6);
	assertTrue("Counted actual event types be at least 6. AdUnitActivity should have sent all 6 lifecycle events before finishing: " + printEvents(allEvents) + ", COUNT: " + allEvents.size(), allEvents.size() >= 6);

	for (int idx = 0; idx < allEventCategories.size(); idx++) {
		assertEquals("Expected event categories to be WebViewEventCategory.ADUNIT", allEventCategories.get(idx), WebViewEventCategory.ADUNIT);
	}

	assertEquals("Expected first event from the AdUnitActivity to be ON_CREATE: " + printEvents(allEvents), AdUnitEvent.ON_CREATE, allEvents.get(0));
	assertEquals("Expected second event from the AdUnitActivity to be ON_START: " + printEvents(allEvents), AdUnitEvent.ON_START, allEvents.get(1));
	assertEquals("Expected third event from the AdUnitActivity to be ON_RESUME: " + printEvents(allEvents), AdUnitEvent.ON_RESUME, allEvents.get(2));
	assertEquals("Expected third last event from the AdUnitActivity to be ON_PAUSE: " + printEvents(allEvents), AdUnitEvent.ON_PAUSE, allEvents.get(allEvents.size() - 3));
	assertEquals("Expected second last event from the AdUnitActivity to be ON_STOP: " + printEvents(allEvents), AdUnitEvent.ON_STOP, allEvents.get(allEvents.size() - 2));
	assertEquals("Expected last event from the AdUnitActivity to be ON_DESTROY: " + printEvents(allEvents), AdUnitEvent.ON_DESTROY, allEvents.get(allEvents.size() - 1));
}
 
Example 16
Source File: AdUnitActivityTest.java    From unity-ads-android with Apache License 2.0 4 votes vote down vote up
@Test
public void testIntentSetWithViewsAndOrientation () {
	final String[] views = new String[]{VIDEO_PLAYER_VIEW, WEB_VIEW};
	Intent intent = new Intent();
	intent.putExtra(AdUnitActivity.EXTRA_VIEWS, views);
	intent.putExtra(AdUnitActivity.EXTRA_ORIENTATION, 6);

	ArrayList<Enum> allEvents = new ArrayList<>();
	ArrayList<Enum> allEventCategories = new ArrayList<>();
	int totalEventCount = 0;

	final Activity activity = waitForActivityStart(intent);
	MockWebViewApp webViewApp = (MockWebViewApp)WebViewApp.getCurrentApp();
	allEvents.addAll(webViewApp.EVENTS);
	allEventCategories.addAll(webViewApp.EVENT_CATEGORIES);
	totalEventCount += webViewApp.EVENT_COUNT;

	ConditionVariable cv = new ConditionVariable();
	cv.block(300);

	assertEquals("View list first value not same than expected: ", views[0], AdUnit.getAdUnitActivity().getViews()[0]);
	assertEquals("View list second value not same than expected", views[1], AdUnit.getAdUnitActivity().getViews()[1]);
	assertEquals("Current orientation was not the same that was given in intent", 6, AdUnit.getAdUnitActivity().getRequestedOrientation());
	assertTrue("Didn't get activity finish", waitForActivityFinish(activity));

	webViewApp = (MockWebViewApp)WebViewApp.getCurrentApp();
	allEvents.addAll(webViewApp.EVENTS);
	allEventCategories.addAll(webViewApp.EVENT_CATEGORIES);
	totalEventCount += webViewApp.EVENT_COUNT;

	assertTrue("Counted events should be at least 6. AdUnitActivity should have sent all 6 lifecycle events before finishing: " + printEvents(allEvents) + ", COUNT: " + allEvents.size(), totalEventCount >= 6);
	assertTrue("Counted event categories should be at least 6. AdUnitActivity should have sent all 6 lifecycle events before finishing: " + printEvents(allEvents) + ", COUNT: " + allEvents.size(), allEventCategories.size() >= 6);
	assertTrue("Counted actual event types be at least 6. AdUnitActivity should have sent all 6 lifecycle events before finishing: " + printEvents(allEvents) + ", COUNT: " + allEvents.size(), allEvents.size() >= 6);

	for (int idx = 0; idx < allEventCategories.size(); idx++) {
		assertEquals("Expected event categories to be WebViewEventCategory.ADUNIT", allEventCategories.get(idx), WebViewEventCategory.ADUNIT);
	}

	assertEquals("Expected first event from the AdUnitActivity to be ON_CREATE: " + printEvents(allEvents), AdUnitEvent.ON_CREATE, allEvents.get(0));
	assertEquals("Expected second event from the AdUnitActivity to be ON_START: " + printEvents(allEvents), AdUnitEvent.ON_START, allEvents.get(1));
	assertEquals("Expected third event from the AdUnitActivity to be ON_RESUME: " + printEvents(allEvents), AdUnitEvent.ON_RESUME, allEvents.get(2));
	assertEquals("Expected third last event from the AdUnitActivity to be ON_PAUSE: " + printEvents(allEvents), AdUnitEvent.ON_PAUSE, allEvents.get(allEvents.size() - 3));
	assertEquals("Expected second last event from the AdUnitActivity to be ON_STOP: " + printEvents(allEvents), AdUnitEvent.ON_STOP, allEvents.get(allEvents.size() - 2));
	assertEquals("Expected last event from the AdUnitActivity to be ON_DESTROY: " + printEvents(allEvents), AdUnitEvent.ON_DESTROY, allEvents.get(allEvents.size() - 1));
}
 
Example 17
Source File: RequestTest.java    From unity-ads-android with Apache License 2.0 4 votes vote down vote up
@Test
public void testValidPostWithHeader () throws Exception {
	final ConditionVariable cv = new ConditionVariable();
	MockWebViewApp webViewApp = new MockWebViewApp() {
		@Override
		public boolean sendEvent(Enum eventCategory, Enum eventId, Object... params) {
			EVENT_CATEGORY = eventCategory;
			EVENT_ID = eventId;
			EVENT_PARAMS = params;
			cv.open();

			return true;
		}
	};

	JSONArray headers = new JSONArray("[[\"test-header\", \"test\"]]");
	WebViewApp.setCurrentApp(webViewApp);
	Invocation invocation = new Invocation();
	WebViewCallback callback = new WebViewCallback("Test_Valid_Get_With_Header", invocation.getId());
	Request.post("1", validUrl, null, headers, connectTimeout, readTimeout, callback);
	invocation.sendInvocationCallback();

	boolean success = cv.block(30000);
	assertTrue("ConditionVariable was not opened", success);

	JSONArray receivedHeaders = (JSONArray)EVENT_PARAMS[4];
	boolean testHeaderExists = false;
	for (int i = 0; i < receivedHeaders.length(); i++) {
		if (((JSONArray)receivedHeaders.get(i)).get(0).equals("test-header") && ((JSONArray)receivedHeaders.get(i)).get(1).equals("test")) {
			testHeaderExists = true;
		}
	}

	assertTrue("There should be headers in the connection result", receivedHeaders.length() > 0);
	assertEquals("Callback status was not OK, successfull callback was expected", CallbackStatus.OK, CALLBACK_STATUS);
	assertNull("Callback error was not NULL, successfull callback was expected", CALLBACK_ERROR);
	assertEquals("Callback params list length was expected to be 2 (callback id and request id in params)", 2, CALLBACK_PARAMS.length);
	assertEquals("Callback first param was expected to be Callback ID", callback.getCallbackId(), CALLBACK_ID);
	assertTrue("Header \"Test-Header\" not found or its content was not \"test\"", testHeaderExists);
	assertEquals("Request has changed for some reason", validUrl, EVENT_PARAMS[1]);
	assertEquals("Response code was not OK (200)", 200, EVENT_PARAMS[3]);
	assertEquals("Event ID was incorrect", WebRequestEvent.COMPLETE, EVENT_ID);
	assertEquals("Event Category was incorrect", WebViewEventCategory.REQUEST, EVENT_CATEGORY);
}
 
Example 18
Source File: BroadcastTest.java    From unity-ads-android with Apache License 2.0 4 votes vote down vote up
@Test
public void testTwoReceivers() throws JSONException {
	final String jsonAction = "com.unity3d.ads.ACTION_JSON";
	final String jsonReceiverName = "jsonReceiver";
	final String dataSchemeAction = "com.unity3d.ads.ACTION_DATA_SCHEME";
	final String dataSchemeReceiverName = "dataSchemeReceiver";

	final String testDataScheme = "test";
	final String testDataHost = "example.net";
	final String testDataPath = "/path";

	final String testStringKey = "testString";
	final String testBoolKey = "testBoolean";
	final String testLongKey = "testLong";
	final String testStringValue = "example";
	final boolean testBoolValue = true;
	final long testLongValue = (long)1234;

	final ConditionVariable jsonCv = new ConditionVariable();
	final ConditionVariable dataSchemeCv = new ConditionVariable();

	MockWebViewApp webapp = new MockWebViewApp() {
		@Override
		public void broadcastEvent(BroadcastEvent eventId, String name, String action, String data, JSONObject extras) {
			assertEquals("Broadcast test two receivers: wrong event id", eventId, BroadcastEvent.ACTION);
			assertTrue("Broadcast test two receivers: receiver name does not match json or data scheme receiver names", name.equals(jsonReceiverName) || name.equals(dataSchemeReceiverName));

			if(name.equals(jsonReceiverName)) {
				assertEquals("Broadcast test two receivers: action does not match", jsonAction, action);
				assertEquals("Broadcast test two receivers: there should be no data in event", "", data);
				assertEquals("Broadcast test two receivers: broadcast was sent with three values in extra bundle", 3, extras.length());
				try {
					assertEquals("Broadcast test two receivers: problem with string in bundle extras", testStringValue, extras.getString(testStringKey));
					assertEquals("Broadcast test two receivers: problem with boolean in bundle extras", testBoolValue, extras.getBoolean(testBoolKey));
					assertEquals("Broadcast test two receivers: problem with long in bundle extras", testLongValue, extras.getLong(testLongKey));
				} catch(JSONException e) {
					fail("Broadcast test two receivers: JSONException: " + e.getMessage());
				}

				jsonCv.open();
			} else {
				assertEquals("Broadcast test two receivers: action does not match", dataSchemeAction, action);
				assertEquals("Broadcast test two receivers: there should be no bundle extras in this event", 0, extras.length());

				Uri testUri = Uri.parse(data);
				assertEquals("Broadcast test two receivers: data scheme does not match", testDataScheme, testUri.getScheme());
				assertEquals("Broadcast test two receivers: data host does not match", testDataHost, testUri.getHost());
				assertEquals("Broadcast test two receivers: data path does not match", testDataPath, testUri.getPath());

				dataSchemeCv.open();
			}
		}
	};
	WebViewApp.setCurrentApp(webapp);
	WebViewApp.getCurrentApp().setWebAppLoaded(true);
	SdkProperties.setInitialized(true);

	BroadcastMonitor.addBroadcastListener(jsonReceiverName, null, new String[]{jsonAction});
	BroadcastMonitor.addBroadcastListener(dataSchemeReceiverName, testDataScheme, new String[]{dataSchemeAction});

	Intent jsonIntent = new Intent();
	jsonIntent.setAction(jsonAction);
	jsonIntent.putExtra(testStringKey, testStringValue);
	jsonIntent.putExtra(testBoolKey, testBoolValue);
	jsonIntent.putExtra(testLongKey, testLongValue);
	ClientProperties.getApplicationContext().sendBroadcast(jsonIntent);

	boolean success = jsonCv.block(30000);
	assertTrue("Broadcast test two receivers: ", success);

	Intent dataIntent = new Intent();
	dataIntent.setAction(dataSchemeAction);
	dataIntent.setData(Uri.parse(testDataScheme + "://" + testDataHost + testDataPath));
	ClientProperties.getApplicationContext().sendBroadcast(dataIntent);

	boolean success2 = dataSchemeCv.block(30000);
	assertTrue("Broadcast test two receivers: ", success2);
}
 
Example 19
Source File: VideoViewTest.java    From unity-ads-android with Apache License 2.0 4 votes vote down vote up
@Test
@RequiresDevice
public void testSetProgressInterval () throws Exception {
	final Activity activity = waitForActivityStart(null);

	WebViewApp.setCurrentApp(new MockWebViewApp() {
		@Override
		public boolean sendEvent(Enum eventCategory, Enum eventId, Object... params) {
			super.sendEvent(eventCategory, eventId, params);

			if ("ON_FOCUS_GAINED".equals(eventId.name()) || "ON_FOCUS_LOST".equals(eventId.name())) {
				return true;
			}

			EVENT_CATEGORIES.add(eventCategory);
			EVENTS.add(eventId);
			EVENT_PARAMS = params;
			EVENT_COUNT++;

			if (eventId.equals(VideoPlayerEvent.PROGRESS)) {
				EVENT_POSITIONS.add(System.currentTimeMillis());
			}

			if (eventCategory.equals(WebViewEventCategory.VIDEOPLAYER) && eventId.equals(VideoPlayerEvent.PREPARED)) {
				VIDEOPLAYER_VIEW.play();
			}
			else if (!EVENT_TRIGGERED && eventCategory.equals(WebViewEventCategory.VIDEOPLAYER) && eventId.equals(VideoPlayerEvent.PROGRESS)) {
				if (EVENT_COUNT > 12) {
					EVENT_TRIGGERED = true;
					VIDEOPLAYER_VIEW.pause();
					CONDITION_VARIABLE.open();
				}
			}

			return true;
		}
	});

	final String validUrl = TestUtilities.getTestServerAddress() + "/blue_test_trailer.mp4";

	final Handler handler = new Handler(Looper.getMainLooper());
	final ConditionVariable viewAddCV = new ConditionVariable();
	handler.post(new Runnable() {
		@Override
		public void run() {
			((MockWebViewApp)WebViewApp.getCurrentApp()).VIDEOPLAYER_VIEW = new VideoPlayerView(getInstrumentation().getTargetContext());
			activity.addContentView(((MockWebViewApp)WebViewApp.getCurrentApp()).VIDEOPLAYER_VIEW, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
			viewAddCV.open();
		}
	});
	boolean success = viewAddCV.block(3000);
	assertTrue("ConditionVariable did not open in view add", success);

	final MockWebViewApp mockWebViewApp = (MockWebViewApp)WebViewApp.getCurrentApp();
	ConditionVariable cv = new ConditionVariable();
	mockWebViewApp.CONDITION_VARIABLE = cv;
	((MockWebViewApp)WebViewApp.getCurrentApp()).VIDEOPLAYER_VIEW.setProgressEventInterval(300);
	((MockWebViewApp)WebViewApp.getCurrentApp()).VIDEOPLAYER_VIEW.prepare(validUrl, 1f, 0);
	success = cv.block(30000);

	assertTrue("Condition Variable was not opened: VIDEO PROGRESS or PREPARE ERROR event was not received", success);

	int failedIntervals = 0;

	for (int idx = 0; idx < EVENT_POSITIONS.size(); idx++) {
		if (idx + 1 < EVENT_POSITIONS.size()) {
			long interval = Math.abs(300 - (EVENT_POSITIONS.get(idx + 1) - EVENT_POSITIONS.get(idx)));
			DeviceLog.debug("Interval is: " + interval);

			if (interval > 80) {
				failedIntervals++;
			}

			assertFalse("Too many intervals failed to arrive in 80ms threshold (" + failedIntervals + ")", failedIntervals > 3);
		}
	}

	assertFalse("Videoplayer shouldn't be in isPlaying state", mockWebViewApp.VIDEOPLAYER_VIEW.isPlaying());
	assertEquals("getProgressInterval should return the same value as what was set", 300, mockWebViewApp.VIDEOPLAYER_VIEW.getProgressEventInterval());
	assertTrue("Didn't get activity finish", waitForActivityFinish(activity));
}
 
Example 20
Source File: VideoViewTest.java    From unity-ads-android with Apache License 2.0 4 votes vote down vote up
@Test
@RequiresDevice
public void testPreparePlaySeekToPause () throws Exception {
	final Activity activity = waitForActivityStart(null);

	WebViewApp.setCurrentApp(new MockWebViewApp() {
		private boolean allowEvents = true;
		@Override
		public boolean sendEvent(Enum eventCategory, Enum eventId, Object... params) {
			super.sendEvent(eventCategory, eventId, params);

			if ("ON_FOCUS_GAINED".equals(eventId.name()) || "ON_FOCUS_LOST".equals(eventId.name())) {
				return true;
			}

			EVENT_CATEGORIES.add(eventCategory);
			EVENTS.add(eventId);
			EVENT_PARAMS = params;
			EVENT_COUNT++;

			if (eventCategory.equals(WebViewEventCategory.VIDEOPLAYER) && eventId.equals(VideoPlayerEvent.PREPARED)) {
				VIDEOPLAYER_VIEW.play();
			}
			if (!EVENT_TRIGGERED && eventCategory.equals(WebViewEventCategory.VIDEOPLAYER) && eventId.equals(VideoPlayerEvent.PROGRESS)) {
				EVENT_TRIGGERED = true;
				VIDEOPLAYER_VIEW.seekTo(4080);
				VIDEOPLAYER_VIEW.pause();
				CONDITION_VARIABLE.open();
				allowEvents = false;
			}

			return true;
		}
	});

	final String validUrl = TestUtilities.getTestServerAddress() + "/blue_test_trailer.mp4";
	final Handler handler = new Handler(Looper.getMainLooper());
	final ConditionVariable viewAddCV = new ConditionVariable();
	handler.post(new Runnable() {
		@Override
		public void run() {
			((MockWebViewApp)WebViewApp.getCurrentApp()).VIDEOPLAYER_VIEW = new VideoPlayerView(getInstrumentation().getTargetContext());
			activity.addContentView(((MockWebViewApp)WebViewApp.getCurrentApp()).VIDEOPLAYER_VIEW, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
			viewAddCV.open();
		}
	});
	boolean success = viewAddCV.block(3000);
	assertTrue("ConditionVariable did not open in view add", success);

	final MockWebViewApp mockWebViewApp = (MockWebViewApp)WebViewApp.getCurrentApp();
	ConditionVariable cv = new ConditionVariable();
	mockWebViewApp.CONDITION_VARIABLE = cv;
	DeviceLog.debug("URL_GIVEN: " + validUrl);
	((MockWebViewApp)WebViewApp.getCurrentApp()).VIDEOPLAYER_VIEW.prepare(validUrl, 1f, 0);
	success = cv.block(30000);

	assertTrue("Condition Variable was not opened: PROGRESS or PREPARE ERROR event was not received", success);
	assertTrue("Current position should be over 300ms", mockWebViewApp.VIDEOPLAYER_VIEW.getCurrentPosition() > 300);
	int diff = Math.abs(4080 - mockWebViewApp.VIDEOPLAYER_VIEW.getCurrentPosition());
	DeviceLog.debug("Difference: " + diff);
	assertTrue("Difference between expected position and actual position should be less than 300ms", diff < 300);
	assertTrue("Didn't get activity finish", waitForActivityFinish(activity));
}