/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * 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 androidx.media3.common.util;

import static android.graphics.Color.BLACK;
import static android.graphics.Color.RED;
import static android.graphics.Color.WHITE;
import static android.graphics.Color.argb;
import static android.graphics.Color.parseColor;
import static androidx.media3.common.util.ColorParser.parseTtmlColor;
import static com.google.common.truth.Truth.assertThat;

import android.graphics.Color;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;

/** Unit test for {@link ColorParser}. */
@RunWith(AndroidJUnit4.class)
public final class ColorParserTest {

  // Negative tests.

  @Test(expected = IllegalArgumentException.class)
  public void parseUnknownColor() {
    ColorParser.parseTtmlColor("colorOfAnElectron");
  }

  @Test(expected = IllegalArgumentException.class)
  public void parseNull() {
    ColorParser.parseTtmlColor(null);
  }

  @Test(expected = IllegalArgumentException.class)
  public void parseEmpty() {
    ColorParser.parseTtmlColor("");
  }

  @Test(expected = IllegalArgumentException.class)
  public void rgbColorParsingRgbValuesNegative() {
    ColorParser.parseTtmlColor("rgb(-4, 55, 209)");
  }

  // Positive tests.

  @Test
  public void hexCodeParsing() {
    assertThat(parseTtmlColor("#FFFFFF")).isEqualTo(WHITE);
    assertThat(parseTtmlColor("#FFFFFFFF")).isEqualTo(WHITE);
    assertThat(parseTtmlColor("#123456")).isEqualTo(parseColor("#FF123456"));
    // Hex colors in ColorParser are RGBA, where-as {@link Color#parseColor} takes ARGB.
    assertThat(parseTtmlColor("#FFFFFF00")).isEqualTo(parseColor("#00FFFFFF"));
    assertThat(parseTtmlColor("#12345678")).isEqualTo(parseColor("#78123456"));
  }

  @Test
  public void rgbColorParsing() {
    assertThat(parseTtmlColor("rgb(255,255,255)")).isEqualTo(WHITE);
    // Spaces are ignored.
    assertThat(parseTtmlColor("   rgb (      255, 255, 255)")).isEqualTo(WHITE);
  }

  @Test
  public void rgbColorParsingRgbValuesOutOfBounds() {
    int outOfBounds = ColorParser.parseTtmlColor("rgb(999, 999, 999)");
    int color = Color.rgb(999, 999, 999);
    // Behave like the framework does.
    assertThat(outOfBounds).isEqualTo(color);
  }

  @Test
  public void rgbaColorParsing() {
    assertThat(parseTtmlColor("rgba(255,255,255,255)")).isEqualTo(WHITE);
    assertThat(parseTtmlColor("rgba(255,255,255,255)")).isEqualTo(argb(255, 255, 255, 255));
    assertThat(parseTtmlColor("rgba(0, 0, 0, 255)")).isEqualTo(BLACK);
    assertThat(parseTtmlColor("rgba(0, 0, 255, 0)")).isEqualTo(argb(0, 0, 0, 255));
    assertThat(parseTtmlColor("rgba(255, 0, 0, 255)")).isEqualTo(RED);
    assertThat(parseTtmlColor("rgba(255, 0, 255, 0)")).isEqualTo(argb(0, 255, 0, 255));
    assertThat(parseTtmlColor("rgba(255, 0, 0, 205)")).isEqualTo(argb(205, 255, 0, 0));
  }
}

@@ -998,16 +998,61 @@
 ertThat;
+%0Aimport static org.junit.Assert.assertThrows;
 %0A%0Aimport
@@ -1330,91 +1330,108 @@
 Test
-(expected = IllegalArgumentException.class)%0A  public void parseUnknownColor() %7B%0A   
+%0A  public void parseUnknownColor() %7B%0A    assertThrows(%0A        IllegalArgumentException.class, () -%3E
  Col
@@ -1474,16 +1474,17 @@
 ectron%22)
+)
 ;%0A  %7D%0A%0A 
@@ -1493,83 +1493,91 @@
 Test
-(expected = IllegalArgumentException.class)%0A  public void parseNull() %7B%0A   
+%0A  public void parseNull() %7B%0A    assertThrows(IllegalArgumentException.class, () -%3E
  Col
@@ -1605,16 +1605,17 @@
 or(null)
+)
 ;%0A  %7D%0A%0A 
@@ -1624,84 +1624,92 @@
 Test
-(expected = IllegalArgumentException.class)%0A  public void parseEmpty() %7B%0A   
+%0A  public void parseEmpty() %7B%0A    assertThrows(IllegalArgumentException.class, () -%3E
  Col
@@ -1735,16 +1735,17 @@
 olor(%22%22)
+)
 ;%0A  %7D%0A%0A 
@@ -1754,51 +1754,8 @@
 Test
-(expected = IllegalArgumentException.class)
 %0A  p
@@ -1805,16 +1805,76 @@
 () %7B%0A   
+ assertThrows(%0A        IllegalArgumentException.class, () -%3E
  ColorPa
@@ -1908,24 +1908,25 @@
 , 55, 209)%22)
+)
 ;%0A  %7D%0A%0A  // 

Migrate from strongly discouraged `@Test(expected = ...)` to `assertThrows(...)`.