如何在Android模拟器上以编程方式设置虚拟场景图像

时间:2019-06-18 21:22:06

标签: android android-studio flutter

我已经为flutter项目编写了一些驱动测试,并且有条形码扫描器功能,可以使用android仿真器提供的virtual scene工具成功测试。

但是,对于不同的条形码,有很多情况需要测试。我想针对每种情况在虚拟场景上设置特定的条形码图像。有可能吗?

我发现此图片的值放在import re text= """ -text1-text2-text3 -text4- """ result = re.findall(r'-(.*?)-', text) print(result,end="") 变量的~/.android/avd/[emulatorName]/AVD.conf文件中。

enter image description here

virtualscene\posters

enter image description here

virtualscene\posters=@Variant(\0\0\0\b\0\0\0\x2\0\0\0\b\0w\0\x61\0l\0l\0\0\0\n\xff\xff\xff\xff\0\0\0\n\0t\0\x61\0\x62\0l\0\x65\0\0\0\n\xff\xff\xff\xff)

2 个答案:

答案 0 :(得分:0)

您可以将poster.png图像替换位于$ANDROID_SDK_HOME/emulator/resources/poster.png的默认(全局)图像, 也不能通过编辑文件$ANDROID_SDK_HOME/emulator/resources/Toren1BD.posters来更改默认指针。

答案 1 :(得分:0)

您可以将虚拟场景图像设置为指定路径。并在测试时操纵目标图像。 由于仪器测试正在您的(虚拟)设备上运行,因此它无法直接操作主机文件。可以做的(这是一个丑陋的破解)是在主机上启动服务器,可以从具有主机环回“ 10.0.2.2”地址的虚拟设备访问该服务器。 该服务器可以操纵目标文件。 如果有人有更好的解决方案,请分享!

示例服务器和客户端在这里。

服务器:

import java.io.*;
import java.net.*;
import java.nio.channels.FileChannel;

public class FileManipulatorServer {
    public static void main(String args[]) {
        int port = 6789;
        FileManipulatorServer server = new FileManipulatorServer( port );
        server.startServer();
    }

    // declare a server socket and a client socket for the server

    private ServerSocket fileManipulatorServer = null;
    private Socket clientSocket = null;
    private int port;

    public FileManipulatorServer(int port ) {
        this.port = port;
    }

    public void stopServer() {
        System.out.println( "Server cleaning up." );
        System.exit(0);
    }

    public void startServer() {
        // Try to open a server socket on the given port
        // Note that we can't choose a port less than 1024 if we are not
        // privileged users (root)

        try {
            fileManipulatorServer = new ServerSocket(port);
        }
        catch (IOException e) {
            System.out.println(e);
        }

        System.out.println( "Waiting for connections. Only one connection is allowed." );

        // Create a socket object from the ServerSocket to listen and accept connections.
        // Use FileManipulatorTask to process the connection.

        while ( true ) {
            try {
                clientSocket = fileManipulatorServer.accept();
                FileManipulatorTask task = new FileManipulatorTask(clientSocket, this);
                task.run();
            }
            catch (IOException e) {
                System.out.println(e);
            }
        }
    }
}

class FileManipulatorTask {
    private BufferedReader is;
    private PrintStream os;
    private Socket clientSocket;
    private FileManipulatorServer server;

    public FileManipulatorTask(Socket clientSocket, FileManipulatorServer server) {
        this.clientSocket = clientSocket;
        this.server = server;
        System.out.println( "Connection established with: " + clientSocket );
        try {
            is = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            os = new PrintStream(clientSocket.getOutputStream());
        } catch (IOException e) {
            System.out.println(e);
        }
    }

    public void run() {
        String line;
        try {
            boolean serverStop = false;

            line = is.readLine();
            System.out.println( "Received " + line );
            saveImageToPoster(line.trim());

            os.println("OK");
            os.flush();

            System.out.println( "Connection closed." );
            is.close();
            os.close();
            clientSocket.close();

            if ( serverStop ) server.stopServer();
        } catch (IOException e) {
            System.out.println(e);
        }
    }

    private void saveImageToPoster(String filename) {

        try {
            FileChannel src = new FileInputStream("C:\\fullpathtopostercandidates\\"+filename).getChannel();
            FileChannel dest = new FileOutputStream("C:\\fullpathtoconfiguredposter\\poster.jpg").getChannel();
            dest.transferFrom(src, 0, src.size());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

客户:

import java.io.*;
import java.net.*;

public class FileNameSenderClient {

    private String hostname = "10.0.2.2";
    private int port = 6789;

    public void sendFileName(String filename) {

        Socket clientSocket = null;
        DataOutputStream os = null;
        BufferedReader is = null;
        try {
            clientSocket = new Socket(hostname, port);
            os = new DataOutputStream(clientSocket.getOutputStream());
            is = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host: " + hostname);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to: " + hostname);
        }


        if (clientSocket == null || os == null || is == null) {
            System.err.println( "Something is wrong. One variable is null." );
            return;
        }

        try {
            System.out.println("Write to output stream");
            os.writeBytes( filename +"\n");
            os.flush();
            String responseLine = is.readLine();             
            System.out.println("Server returns: " + responseLine);
            os.close();
            is.close();
            clientSocket.close();
        } catch (UnknownHostException e) {
            System.err.println("Trying to connect to unknown host: " + e);
        } catch (IOException e) {
            System.err.println("IOException:  " + e);
        }
    }
}

像这样在您的测试中使用FileNameSenderClient。

@Test
public void testQRcodeReadingOK()
{
   FileNameSenderClient c = new FileNameSenderClient();
   c.sendFileName("QRCode.jpg");

   //your code that wants to use the image, like the this:

    onView(withId(R.id.load_qr_code)).perform(click());
}