/*
 *    Copyright 2013 APPNEXUS INC
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.appnexus.opensdk;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertTrue;

@Config(emulateSdk=18)
@RunWith(RobolectricTestRunner.class)
public class AdRequestToAdRequesterTest extends BaseRoboTest implements AdRequester {
    boolean requesterFailed, requesterReceivedResponse, requesterReturnedOwner;

    @Override
    public void setup() {
        super.setup();
        requesterFailed = false;
        requesterReceivedResponse = false;
        requesterReturnedOwner = false;
    }

    @Override
    public void assertCallbacks(boolean success) {
        assertTrue(requesterReturnedOwner);
        assertEquals(success, requesterReceivedResponse);
        assertEquals(!success, requesterFailed);
    }

    @Test
    public void testRequestSucceeded() {
        // adRequest initialization goes here because getOwner is called in the constructor
        adRequest = new AdRequest(this);

        Robolectric.addPendingHttpResponse(200, TestResponses.banner());
        adRequest.execute();
        Robolectric.runBackgroundTasks();
        Robolectric.runUiThreadTasks();
        assertCallbacks(true);
    }

    @Test
    public void testRequestBlank() {
        adRequest = new AdRequest(this);

        // blanks are handled by requester
        Robolectric.addPendingHttpResponse(200, TestResponses.blank());
        adRequest.execute();
        Robolectric.runBackgroundTasks();
        Robolectric.runUiThreadTasks();
        assertCallbacks(true);
    }

    @Test
    public void testRequestStatusError() {
        adRequest = new AdRequest(this);

        Robolectric.addPendingHttpResponse(404, TestResponses.banner());
        adRequest.execute();
        Robolectric.runBackgroundTasks();
        Robolectric.runUiThreadTasks();
        assertCallbacks(false);
    }

    @Override
    public void failed(AdRequest request) {
        requesterFailed = true;
    }

    @Override
    public void onReceiveResponse(AdResponse response) {
        requesterReceivedResponse = true;
    }

    @Override
    public AdView getOwner() {
        requesterReturnedOwner = true;
        return bannerAdView;
    }

    long time;
    @Override
    public void markLatencyStart() {
        time = System.currentTimeMillis();
    }

    @Override
    public long getLatency(long now) {
        return System.currentTimeMillis()-time;
    }
}

@@ -832,16 +832,46 @@
 onfig;%0A%0A
+import java.util.LinkedList;%0A%0A
 import s
@@ -913,16 +913,16 @@
 Equals;%0A
-
 import s
@@ -1168,32 +1168,49 @@
 onse
-, requesterReturnedOwn
+;%0A    RequestParameters requestParamet
 er
+s
 ;%0A%0A 
@@ -1366,31 +1366,242 @@
 uest
-erReturnedOwner = false
+Parameters = new RequestParameters(activity);%0A        requestParameters.setPlacementID(%22%22);%0A        requestParameters.setAdWidth(320);%0A        requestParameters.setAdHeight(50);%0A        requestParameters.setMediaType(MediaType.BANNER)
 ;%0A  
@@ -1700,27 +1700,49 @@
 uesterRe
-turnedOwner
+ceivedResponse %7C%7C requesterFailed
 );%0A     
@@ -3149,128 +3149,8 @@
  %7D%0A%0A
-    @Override%0A    public AdView getOwner() %7B%0A        requesterReturnedOwner = true;%0A        return bannerAdView;%0A    %7D%0A%0A
     
@@ -3367,11 +3367,211 @@
 e;%0A    %7D
+%0A%0A    @Override%0A    public LinkedList%3CMediatedAd%3E getMediatedAds() %7B%0A        return null;%0A    %7D%0A%0A    @Override%0A    public RequestParameters getRequestParams() %7B%0A        return requestParameters;%0A    %7D
 %0A%7D%0A

update unit tests for refactor