File size: 3,226 Bytes
2311079
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package com.tacticmaster;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.MotionEvent;

import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.rules.ActivityScenarioRule;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import com.tacticmaster.board.ChessboardView;
import com.tacticmaster.puzzle.Puzzle;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(AndroidJUnit4.class)
public class ChessboardViewTest {

    @Rule
    public ActivityScenarioRule<MainActivity> activityScenarioRule = new ActivityScenarioRule<>(MainActivity.class);

    private Context context;
    private ChessboardView chessboardView;
    private Puzzle puzzle;

    @Before
    public void setUp() {
        context = ApplicationProvider.getApplicationContext();
        puzzle = new Puzzle("1", "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1", "e2e4 e7e5", 1049, 80, 85, 208, "opening", "url", "tags");

        activityScenarioRule.getScenario().onActivity(activity -> {
            chessboardView = new ChessboardView(context, null);
            chessboardView.setPuzzle(puzzle);
            activity.setContentView(chessboardView);
        });
    }

    @Test
    public void testInitialization() {
        assertNotNull(chessboardView);
        assertNotNull(chessboardView.getPuzzle());
        assertNotNull(chessboardView.getChessboard());
    }

    @Test
    public void testPieceBitmaps() {
        Bitmap whiteKing = BitmapFactory.decodeResource(context.getResources(), R.drawable.wk);
        Bitmap blackKing = BitmapFactory.decodeResource(context.getResources(), R.drawable.bk);
        assertNotNull(whiteKing);
        assertNotNull(blackKing);
    }

    @Test
    public void testOnTouchEvent() {
        activityScenarioRule.getScenario().onActivity(activity -> {
            assertEquals(-1, chessboardView.getSelectedCol());
            assertEquals(-1, chessboardView.getSelectedRow());
            MotionEvent event = MotionEvent.obtain(100, 100, MotionEvent.ACTION_DOWN, 900, 935, 0);
            boolean result = chessboardView.onTouchEvent(event);
            assertTrue(result);
            assertEquals(6, chessboardView.getSelectedCol());
            assertEquals(6, chessboardView.getSelectedRow());
        });
    }

    @Test
    public void testPerformClick() {
        activityScenarioRule.getScenario().onActivity(activity -> {
            boolean result = chessboardView.performClick();
            assertFalse(result);
        });
    }

    @Test
    public void testSetPuzzle() {
        Puzzle newPuzzle = new Puzzle("2", "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1", "e2e4 e7e5", 1049, 80, 85, 208, "opening", "url", "tags");
        activityScenarioRule.getScenario().onActivity(activity -> {
            chessboardView.setPuzzle(newPuzzle);
            assertEquals(newPuzzle, chessboardView.getPuzzle());
        });
    }
}